vim-autoformat / vim-autoformat

Provide easy code formatting in Vim by integrating existing code formatters.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

eslint_local formatter and eslint-plugin-node shebang issue

gautaz opened this issue · comments

Hello,

Currently autoformat needs to use a temporary file when using eslint to avoid modifying the project filesytem and still being able to get the fixed content back in the current buffer.
When using the eslint-plugin-node eslint plugin with the shebang rule, it ends up with autoformat removing the shebang line from files where it should be kept.
This is because eslint operates on a temporary file which obviously has not the same path as the original file present on the project's filesystem, thus the shebang rule from the eslint-plugin-node eslint plugin cannot assert the right fix.

I am trying to find a way to work this around and the only solution that comes to mind for the moment would be:

  1. Copying the file to lint to a temporary file
  2. Operating eslint on the original file and reflecting the modifications in the vim buffer
  3. Copying back the temporary file to the original file

I have no idea if this might work, any advice is welcome.
Would you accept a PR for this? (I am not fluent in vimscript but I can try)

Thanks for autoformat anyway :-).

If you look at the current code for eslint integration you can see it's already horrific. Imagine what any additional magic will do to it :)

I think it would be a lot wiser to patch ESLint to be able to print the results to stdout, like all other sane formatters do.

@Chiel92 Agreed, I'll look into it, is there an already opened issue on eslint related to that?

I'm not sure, there could be

Looking deeper in eslint documentation and what have been done for ALE, it seems that the favored solution provided by eslint for this need is to use --stdin --fix-dry-run --format=json in combination with --stdin-filename.

@Chiel92 Does this seem more acceptable?
It avoids completely the need for a temporary file but adds some boilerplate to parse the JSON output...

That sounds acceptable