elicwhite / ESLint-Formatter

Sublime Text 3 Plugin to Autoformat with Eslint
MIT License
139 stars 20 forks source link

Format on pre-save #34

Open bostrom opened 7 years ago

bostrom commented 7 years ago

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.

elicwhite commented 7 years ago

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. :)

cristianl commented 7 years ago

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

skeggse commented 4 years ago

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.