问题
When I save a file, I would like to run eslint, and have it fix any issues that eslint can fix.
I know this can be accomplished using the command line by running applying the --fix
argument.
I also know that Intellij-IDEA has integration with Eslint directly; however, Intellij-IDEA uses stdin for it's integration which means you can't pass --fix
as an argument.
回答1:
With a few quick steps you can setup a file watcher that will run eslint --fix on the files you save.
Step by step:
- Install the File Watcher plugin
- Navigate to
Preferences > Tools > File Watchers
and create a new File Watcher - Choose
File type: JavaScript
- Optionally, apply a scope for the watcher. For example, I chose
$ProjectFileDir$/apps/web/src/
and included all of it's contents recursively. - Choose the program to run. With node, npm, and eslint installed point to the eslint bin. In my project, the path was
$ProjectFileDir$/apps/web/node_modules/eslint/bin/eslint.js
- Apply the following arguments to run eslint with the fix option on the file that was saved
--fix $FileName$
- Specify the working directory as
$FileDir$
.
Name, and save the File Watcher. Then, edit a JavaScript file in the directory you scoped and watch many of your errors and warnings go away! Thanks Eslint!
Note: If you find Intellij-IDEA asking if you want to load file changes without you saving know (which gets annoying) it's because the IDE is saving in the background. You can uncheck Immediate file synchronization
to have a better editing experience.
回答2:
You better use the original third-party ESLint plugin, it does support quick fixes. See project README for details.
回答3:
in my case is my project was created by vue-cli
add this line to your package.json
"lintfix": "eslint --fix --ext .js,.vue src test/unit test/e2e/specs",
here are 3 ways you can do
- Click on the green arrow on the left side
- Click on run button as the image below
- In terminal run:
npm run lintfix
that all.
回答4:
You can use a Save Actions Plugin
Installation
IDE (recommended) Install it from your IDE (Intellij IDEA, PyCharm, etc.):
"File > Settings > Plugins > Browse repositories... > Search 'Save Actions' > Category 'Code tools'"
JetBrains plugin repository All versions of the plugin are available from the JetBrains plugin repository. You can download the
jar and add it to your IDE (you won't get updates thought):
"File > Settings > Plugins > Install plugin from disk..."
One of the options is "Reformat file" -> Enable / disable formatting (configured in "File > Settings > Code Style"). See "Reformat only changed code" for more options
来源:https://stackoverflow.com/questions/38883634/how-can-i-run-eslint-fix-on-my-javascript-in-intellij-idea-webstorm-other-je