ESLint-Formatter icon indicating copy to clipboard operation
ESLint-Formatter copied to clipboard

Format on pre-save

Open bostrom opened this issue 7 years ago • 3 comments

Would it be possible to configure the formatter to format the file before it's saved (but after pressing Cmd-s) instead of after? Now it seems that the file is saved to disk, then the formatter formats the file and saves it again, resulting in two saves.

This is problematic in cases where saved files are watched for changes, as the first (unformatted) save triggers actions (babel-compiles etc), and the second (formatted) save goes by unnoticed since the action is already in progress.

bostrom avatar Feb 15 '17 09:02 bostrom

The eslint format command can't read from stdin, it can only format files on disk.

For this to be possible this plugin would need to write the file to some temp location, run eslint on that file with the configuration of the original file's directory, and then replace the buffer with the contents of the temp file before saving.

I would guess that the impact to perf would not be great and the logic would make this fairly complicated. I'd be willing to accept a PR though, especially if it had tests. :)

TheSavior avatar Feb 15 '17 19:02 TheSavior

The eslint format command can't read from stdin, it can only format files on disk.

This is now supported through the --fix-dry-run flag, shipped in eslint v4.9.0 https://github.com/eslint/eslint/pull/9073

CLI example: echo "startServer();;" | eslint --stdin --fix-dry-run --format json --stdin-filename lib/server2.js

cristianl avatar Nov 18 '17 10:11 cristianl

I added support for a very similar feature of eslint_d in #77; it should be pretty easy to update this to also support eslint itself.

skeggse avatar May 19 '20 21:05 skeggse