[英]How can I run eslint --fix on my JavaScript in Intellij-IDEA, Webstorm, other JetBrains IDEs?
When I save a file, I would like to run eslint, and have it fix any issues that eslint can fix.当我保存文件时,我想运行 eslint,并让它修复 eslint 可以修复的任何问题。
I know this can be accomplished using the command line by running applying the --fix
argument.我知道这可以通过运行应用
--fix
参数使用命令行来完成。
I also know that Intellij-IDEA has integration with Eslint directly;我也知道 Intellij-IDEA 直接与 Eslint 集成; however, Intellij-IDEA uses stdin for it's integration which means you can't pass
--fix
as an argument.但是,Intellij-IDEA 使用 stdin 进行集成,这意味着您不能将
--fix
作为参数传递。
With a few quick steps you can setup a file watcher that will run eslint --fix on the files you save.通过几个快速步骤,您可以设置一个文件观察器,该观察器将对您保存的文件运行 eslint --fix。
Step by step:一步一步:
Preferences > Tools > File Watchers
and create a new File WatcherPreferences > Tools > File Watchers
文件观察器并创建一个新的文件观察器File type: JavaScript
File type: JavaScript
$ProjectFileDir$/apps/web/src/
and included all of it's contents recursively.$ProjectFileDir$/apps/web/src/
并递归地包含它的所有内容。$ProjectFileDir$/apps/web/node_modules/eslint/bin/eslint.js
$ProjectFileDir$/apps/web/node_modules/eslint/bin/eslint.js
--fix $FileName$
--fix $FileName$
$FileDir$
.$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!
然后,在您限定的目录中编辑一个 JavaScript 文件,并观察您的许多错误和警告消失! 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.注意:如果您发现 Intellij-IDEA 询问您是否要在不保存的情况下加载文件更改(这很烦人),那是因为 IDE 正在后台保存。 You can uncheck
Immediate file synchronization
to have a better editing experience.您可以取消选中
Immediate file synchronization
以获得更好的编辑体验。
Things have changed.事情发生了变化。 You won't need external plugins.
您不需要外部插件。 There is now native support for ESlint:
现在有对 ESlint 的原生支持:
ESlint Plugin: https://plugins.jetbrains.com/plugin/7494-eslint/ ESlint 插件: https ://plugins.jetbrains.com/plugin/7494-eslint/
As of Intellij 14, a plugin based on this one was bundled into the IDE release by Jetbrains.
从 Intellij 14 开始,Jetbrains 将基于此插件的插件捆绑到 IDE 版本中。 What's the diffrence?
有什么不同? This plugin supports Intellij 13 and other versions, --fix option, quick fixes and other minor differences.
此插件支持 Intellij 13 和其他版本,--fix 选项,快速修复和其他细微差别。 Please make sure you are referring to this one before opening an issue.
在打开问题之前,请确保您指的是这个。
source: https://github.com/idok/eslint-plugin
来源: https : //github.com/idok/eslint-plugin
The "Automatic ESLint configuration" option works fine for me, too. “自动 ESLint 配置”选项也适用于我。 The IDE scans for
package.json
files with eslint
dependencies and run it. IDE 会扫描具有
eslint
依赖项的package.json
文件并运行它。 (You could use different ESLint versions in one project). (您可以在一个项目中使用不同的 ESLint 版本)。
ESLint config is auto detected from the nearest .eslintrc.x
(eg .json
) config file, or read from the package.json
从最近被自动检测ESLint配置
.eslintrc.x
(例如.json
)配置文件,或者从读package.json
By default, PhpStorm uses the ESLint package from the project
node_modules
folder and the.eslintrc.*
configuration file from the folder where the current file is stored.默认情况下,PhpStorm 使用项目
node_modules
文件夹中的 ESLint 包和当前文件所在文件夹中的.eslintrc.*
配置文件。 If no.eslintrc.*
is found in the current file folder, PhpStorm will look for one in its parent folders up to the project root.如果在当前文件夹中没有找到
.eslintrc.*
,PhpStorm 将在其父文件夹中查找直到项目根目录。
If you have several
package.json
files with ESLint listed as a dependency, PhpStorm starts a separate process for eachpackage.json
and processes everything below it.如果您有多个
package.json
文件,其中 ESLint 被列为依赖项,PhpStorm 会为每个package.json
启动一个单独的进程并处理它下面的所有内容。 This lets you apply a specific ESLint version or a specific set of plugins to each path in a monorepo or a project with multiple ESLint configurations.这使您可以将特定的 ESLint 版本或一组特定的插件应用于 monorepo 或具有多个 ESLint 配置的项目中的每个路径。
You can also set the Node interpreter + ESLint package + .eslintrc
config manually你也可以手动设置 Node 解释器 + ESLint 包 +
.eslintrc
配置
You can auto-import some code style rules from your ESLint config:您可以从 ESLint 配置中自动导入一些代码样式规则:
Inside your .eslintrc
config file context menu在
.eslintrc
配置文件上下文菜单中
Select " Apply ESLint Code Style Rules "选择“应用 ESLint 代码样式规则”
Check output in " Event Log " panel检查“事件日志”面板中的输出
From "Warnings" tooltip select "Configure Inspections..."从“警告”工具提示中选择“配置检查...”
Activate "ESLint" under "Code quality tools"在“代码质量工具”下激活“ESLint”
The "Problems" panel now lists ESLint issues AND (default) IDE problems “问题”面板现在列出 ESLint 问题和(默认)IDE 问题
You might want to disable IDEs default rules by and replace them with adequate ESLint rules.您可能希望禁用 IDE 的默认规则,并用适当的 ESLint 规则替换它们。 Otherwise you might see 2 problems for the same issue in the list.
否则,您可能会在列表中看到针对同一问题的 2 个问题。
"Code" > "Run Inspection by Name..." > type "eslint" “代码”>“按名称运行检查...”> 输入“eslint”
You can "auto fix" problems, or fix the entire file (if rule can be fixed automatically)您可以“自动修复”问题,或修复整个文件(如果规则可以自动修复)
Fixed problems are highlighted突出显示已修复的问题
Alternative, you can set up a NPM script run configuration:或者,您可以设置 NPM 脚本运行配置:
After ESLint installation, add a script section to package.json
ESLint 安装后,在
package.json
添加一个脚本部分
"scripts": { "eslint": "eslint" }
In "npm" panel, you should see the "eslint" script from your package.json
.在“npm”面板中,您应该会看到
package.json
的“eslint”脚本。
You can edit its settings to eg run ESLint for current file only:您可以编辑其设置,例如仅对当前文件运行 ESLint:
Now you can run the NPM script:现在您可以运行 NPM 脚本:
Source: https://www.jetbrains.com/help/phpstorm/2020.2/eslint.html?utm_campaign=PS&utm_medium=link&utm_source=product&utm_content=2020.2#ws_js_eslint_activate来源: https : //www.jetbrains.com/help/phpstorm/2020.2/eslint.html?utm_campaign=PS&utm_medium=link&utm_source=product&utm_content=2020.2#ws_js_eslint_activate
You better use the original third-party ESLint plugin , it does support quick fixes.您最好使用原始的第三方ESLint 插件,它确实支持快速修复。 See project README for details.
有关详细信息,请参阅项目自述文件。
You can use a Save Actions Plugin您可以使用保存操作插件
Installation
安装
IDE (recommended) Install it from your IDE (Intellij IDEA, PyCharm, etc.):
IDE(推荐)从您的 IDE(Intellij IDEA、PyCharm 等)安装它:
"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.
JetBrains 插件存储库 JetBrains 插件存储库提供了所有版本的插件。 You can download the
你可以下载
jar and add it to your IDE (you won't get updates thought):
jar 并将其添加到您的 IDE(您不会想到更新):
"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
有关更多选项,请参阅“仅重新格式化更改的代码”
in my case is my project was created by vue-cli就我而言,我的项目是由 vue-cli 创建的
add this line to your package.json将此行添加到您的 package.json
"lintfix": "eslint --fix --ext .js,.vue src test/unit test/e2e/specs",
here are 3 ways you can do这里有 3 种方法可以做到
npm run lintfix
npm run lintfix
that all.这一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.