Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the code using --fix #104

Closed
hyperknot opened this issue Nov 17, 2015 · 32 comments
Closed

Fix the code using --fix #104

hyperknot opened this issue Nov 17, 2015 · 32 comments

Comments

@hyperknot
Copy link

eslint provides a really nice --fix option, which is perfectly usable for fixing smaller things, like indentation, styling issues, etc.

Please make a command which runs --fix on the actual code, so a quick eslint style fixing would be possible in Sublime.

@roadhump
Copy link
Collaborator

SublimeLinter does not support anything except linting and there is no easy way to add this formatting command separately to this plugin. I guess we should wait when such possiblity will land in SublimeLinter, probably create an issue, or create separate plugin, like https://github.com/TheSavior/SublimeJSCSFormatter/blob/master/JSCS-Formatter.py for jscs.

@hyperknot
Copy link
Author

I'll open an issue in linter, to let them know about the need for the feature.

Probably a separate plugin would be the most reliable way of implementing it, but it'd be a pity to not use all the config management which is already implemented in linter and in this plugin.

@darkred
Copy link

darkred commented Dec 5, 2015

For reference: what the --fix option currently (v1.10.3) offers (1, 2) is that:

When you run ESLint on the command line with the --fix flag, it will automatically fix any problems that it can (not all problems are fixable at once) and will not report the problems that were fixed.

and

This option instructs ESLint to try to fix as many issues as possible. The fixes are made to the actual files themselves and only the remaining unfixed issues are output.

i.e. running it like this:
eslint --fix myscript.js
it will replace the file with one having as many issues as possible fixed (i.e. it wont create a 2nd file),
and it doesn't report in its output which errors it just fixed
but only which unfixed issues remain, like this:

$ eslint --fix myscript.js

C:\Users\Kostas\Desktop\New folder\myscript.user.js
   26:18  error    Multiline support is limited to browsers supporting ES5 only                                   no-multi-str
   48:1   error    "GM_addStyle" is not defined                                                                   no-undef
   48:1   error    A function with a name starting with an uppercase letter should only be used as a constructor  new-cap
   73:1   warning  "time" was used before it was defined                                                          no-use-before-define
   75:1   error    String prototype is read only, properties should not be added                                  no-extend-native
   96:5   error    Identifier 'base_url' is not in camel case                                                     camelcase

Also, Atom's `linter-eslint` plugin since [version 5.2.0](https://github.com/AtomLinter/linter-eslint/releases) offers "`linter-eslint: Fix file`" command _(via Command Palette)_. It works on the currently opened file, provided that you have just saved it (i.e. it has no unsaved changes).

@softwarespot
Copy link

I am going to create a small sublime text plugin (first one by the way). So watch this space! =)

@joerideg
Copy link

I'll be watching you @softwarespot :D

@mummybot
Copy link

How is the plugin going? :)

TBH I am surprised at this as the Sublime Linter documentation, at least in plain english, implies that it passes the argument to the binary:

This setting specifies extra arguments to pass to an external binary. This is useful when a linter binary supports an option that is not part of the linter’s settings.
http://www.sublimelinter.com/en/latest/linter_settings.html#args

Yet I haven't been able to get --fix or --globals to work. What is the point of the args in SublimeLinter if not to allow arguments to passed to the run time?

The example provided in the SublimeLinter documentation even implies cmd syntax:

{
    "args": "--foo=bar --bar=7 --no-baz"
}

{
    "args": [
        "--foo=bar",
        "--bar=7",
        "--no-baz"
    ]
}

The following does not lint on my Sublime 3.0, removing the "--fix" and it does.

{
    "args": "--fix"
}

And yet the following works fine

eslint --fix myJsFile.js

Does not compute!

@bdefore
Copy link

bdefore commented Jan 26, 2016

Would also love to see --fix support! 👍

@joerideg
Copy link

Somebody who knows pyton (not me :( ) could fork https://github.com/TheSavior/SublimeJSCSFormatter/ and probably make an eslint version. The api of jscs and eslint are pretty much the same. @softwarespot

@roadhump
Copy link
Collaborator

@mummybot Plugin uses stdin as input (--stdin option of eslint). It does not work with --fix, as I know.

@karolyi
Copy link

karolyi commented Feb 18, 2016

+1

2 similar comments
@jorgenbs
Copy link

jorgenbs commented Mar 8, 2016

+1

@gblazex
Copy link

gblazex commented Mar 17, 2016

+1

@glenjamin
Copy link
Contributor

If it is possible in SumblimeLinter to tell the difference between an as-you-type lint and a has-hit-save lint, a good approach might be to use the real file instead of --stdin when the file has been saved. And then --fix could be passed in that scenario.

@softwarespot
Copy link

This was released 6 days ago >> https://github.com/TheSavior/ESLint-Formatter. It's JSCS Formatter renamed

@RichardForrester
Copy link

I just created a new build system and made shortcut:

{ "shell_cmd": "$project_path/node_modules/eslint/bin/eslint.js --fix $file" }

I don't know if that's the best solution, but works for me.

@Overload119
Copy link

Overload119 commented Jun 1, 2016

@RichardForrester Can you give instructions on how to install this? Where should I put that line?

@joeybaker
Copy link

@RichardForrester A slightly better option might be: $project_path/node_modules/.bin/eslint

@RichardForrester
Copy link

@Overload119 There isn't anything to install. Just go through the menu: Tools > Build System > New Build System

@joeybaker is correct, a slightly better path is as he suggests.

{ "shell_cmd": "$project_path/node_modules/.bin/eslint --fix $file" }

So basically you just save that as a build and then you can super + b to fix your current file.

@glebmachine
Copy link

+1 to supports:

{
"linters": {
            "eslint": {
                "args": [
                    "--fix"
                ]
            }
        }
}

@sheepsteak
Copy link

For anybody that isn't using projects like me and just opens Sublime with a directory using subl . or similar then $project_path won't have a value and you can use $folder instead:

{  "shell_cmd": "$folder/node_modules/.bin/eslint --fix $file"  }

💥

@michaelworm
Copy link

michaelworm commented Aug 26, 2016

For me being on windows and having local eslint installed via npm you can also use

{ "shell_cmd": "%appdata%/npm/eslint --fix $file" }

@kzap
Copy link

kzap commented Sep 12, 2016

guys this plugin is available, its for formatting using eslint
https://github.com/TheSavior/ESLint-Formatter

Use that instead of asking for this feature.

@gaoqiankun
Copy link

i find a way solve this problem, and i create a pull request, it works fine on load/save lint mode.
@roadhump could you merge the pull request

@sebastienbarre
Copy link

@RichardForrester I created a new build system, Sublime asked me to save it, I did -- but running "Build" still says "No Build System".

@kzap: ESLint-Formatter is too smart for its own good. Instead of assuming that eslint is in the path, like most eslint-related plugins do, it wants to know where node is, eslint, etc. My node path changes since I use nvm.

@npetruzzelli
Copy link

As far as I can see, the problem is here:
https://github.com/roadhump/SublimeLinter-eslint/blob/3967ca67bc98d4e6df7ae2aca61add27443c6aaa/linter.py#L26

From ESLint's documentation
http://eslint.org/docs/user-guide/command-line-interface#--fix

--fix

This option instructs ESLint to try to fix as many issues as possible. The fixes are made to the actual files themselves and only the remaining unfixed issues are output. Not all problems are fixable using this option, and the option does not work in these situations:

  1. This option throws an error when code is piped to ESLint.
  2. This option has no effect on code that uses processors.

So --fix will work just fine, as long as the linter is working on the original file. Not stdin and not a temporary file. From SublimeLinter's documentation:

I was able to come up with the following proof of concept by modifying linter.py:

-     cmd = ('eslint', '--format', 'compact', '--stdin', '--stdin-filename', '@')
+     cmd = ('eslint', '--format', 'compact', '@')
+     tempfile_suffix = '-'

I am a JavaScript developer. I don't know alot of Python and I have no idea what the '@' argument is supposed to do (so I left it in there).

Now, whenever I save a file (see Lint Modes), it is linted and fixed thanks to the following .sublimelinterrc

{
  "linters": {
    "eslint": {
      "@disable": false,
      "args": [
        "--fix"
      ],
      "excludes": []
    }
  }
}

If I remove --fix from args, then the errors are highlighted normally without modifying the file.

It would be great if this change could be made after someone is able to evaluate the impact it would have of stability and performance. Unfortunately that is currently outside of what I am capable of. Hopefully this will give someone a head start in creating a pull request for SublimeLinter-eslint.

Related: SublimeLinter/SublimeLinter#375

@npetruzzelli
Copy link

npetruzzelli commented Jul 3, 2017

My workaround does have a problem. It hits the filesystem twice. First when you initially save the file, and again when --fix has modified and resaved the file.

This is less than ideal, including that it would require debouncing of any file watcher, including editor plugins, build systems, or other pieces of software that may be outside a developer's control.

@npetruzzelli
Copy link

In the end, my attempt to work around the problem was limited by my knowledge of Python and limitations in both Prettier and ESLint. It ended up being more reasonable for me to change my workflow than attempting to get a single package to do it all.

Long story short, I was able to get SublimeLinter-eslint and ESLint-Formatter to meet most of my needs.

The following may be worth keeping an eye on:

@Nehon
Copy link

Nehon commented Sep 13, 2017

Hello @npetruzzelli I have the same setting now, but it seems the linter is executed before the formatter with the format_on_save option, so the file is formatted correctly, but the linter still reports the issues.
Did you manage to get around that?

@npetruzzelli
Copy link

I'm afraid I haven't run into any issues like that. Keep in mind that the ESLint-Formatter can only work on a saved file. It won't work if you have an open document with unsaved changes.

I typically lint as a background process or on file save. Then I will use the keyboard shortcut when I want to format. I think it is ctrl+shift+h.

Not all problems can be autofixed, so sometimes you will have to manually resolve ESLint errors.

Unfortunately, I don't know of any way to control the order in which Formatter and Linter execute or to get one to wait for the other.

@braver
Copy link
Member

braver commented Jan 9, 2018

We've decided (at least for the foreseeable future) that such functionality is out of scope for SL. More info: SublimeLinter/SublimeLinter#375

@braver braver closed this as completed Jan 9, 2018
@kaste
Copy link
Contributor

kaste commented May 29, 2018

Proof of Concept PR is #254

@59naga
Copy link

59naga commented Jun 28, 2018

{  "shell_cmd": "npx eslint --fix $file"  }

@sheepsteak's idea nice, but feel slowly.

[Finished in 4.5s]

Can improve speed with eslint_d.

yarn global add eslint_d
{  "shell_cmd": "eslint_d --fix $file --no-ignore"  } // use current .eslintrc
[Finished in 1.0s]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests