Sublime Forum

How do you run `eslint --fix` on save?

#1

I can’t seem to find out how to do this. I’m using LSP and some linting rules and that’s all fine but when I want eslint --fix to be run when I save, how do I achieve this?

0 Likes

#2

You can try https://github.com/twolfson/sublime-hooks

Or you could write your own plugin by implementing an event listener on_post_save:

import sublime, sublime_plugin, subprocess

class RunMycmdCommand( sublime_plugin.EventListener ):
    def on_post_save( self, view ):
        if "source.js" in view.scope_name(0):
            cmd = ["eslint","--fix",view.file_name()]
            print(cmd)
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
            p.wait()
            if p.stderr is not None :
                for l in p.stderr.readlines():
                    l = l.decode("utf-8").strip()
                    if l:
                        print(l)
            if p.stdout is not None :
                for l in p.stdout.readlines():
                    l = l.decode("utf-8").strip()
                    if l:
                        print(l)

Might need to be adapted (like run asynchronously to avoid blocking the UI) but it should be a start

1 Like

#3

Oo cheers mate I’ll be giving that a shot later at some stage.

When I have some spare time I’ll experiment with this, seems like a good chance to look into the Sublime Text API and make some useful things – apart from the obviously findable things did you have any good Sublime Text API resources?

0 Likes

#4

https://www.sublimetext.com/docs/3/api_reference.html

0 Likes