Is it possible to run eslint automatically when I save either a .js or .jsx file in Sublime Text?
I'm using the ESLint sublime package right now but I have to manually run it each time using Cmd + Option + e.
Thanks!
Is it possible to run eslint automatically when I save either a .js or .jsx file in Sublime Text?
I'm using the ESLint sublime package right now but I have to manually run it each time using Cmd + Option + e.
Thanks!
Copyright © 2021 Jogjafile Inc.
Yes this can be done with a simple event driven plugin like the one I've written below.
The plugin has been tested but not with the
eslintcommand since I did not want to install that package. Clearly any command could be run instead ofeslintin the plugin'srun_command("eslint")line. If the desired command takes args then they can be specified like this:run_command("command", {"arg_1": val, "arg_2": val}).The
on_post_save_async(self, view)method (in my plugin below) will be called after theview, i.e. the active buffer, has been saved - note that this includes auto-saves.on_post_save_async()runs in a separate thread and does not block the application. You could alter the plugin to use a similar method depending of whether you wanteslintcalled before or after the file save has taken place and whether the method should block the application or be run in its own non-blocking thread. These are the 4 alternatives:on_pre_save(self, view): Called just before a view is saved. It blocks the application until the method returns.on_pre_save_async(self, view): Called just before a view is saved. Runs in a separate thread, and does not block the application.on_post_save(self, view): Called after a view has been saved. It blocks the application until the method returns.on_post_save_async(self, view): Called after a view has been saved. Runs in a separate thread, and does not block the application. [Currently used in the plugin below.]EventListenerdocumentation is located here - there are on load methods too.Save the plugin below somewhere in in your Sublime Text packages hierarchy with a
.pyextension. e.g.~/.config/sublime-text-3/Packages/User/AutoRunESLintOnSave.pyand it should work straight away.Instead of using file extensions to trigger running the
eslintcommand you could use the buffer's syntax, the code is even more concise.