简体   繁体   English

如何在 Intellij-IDEA、Webstorm 和其他 JetBrains IDE 中对我的 JavaScript 运行 eslint --fix?

[英]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:一步一步:

  • Install the File Watcher plugin安装文件观察器插件
  • Navigate to Preferences > Tools > File Watchers and create a new File Watcher导航到Preferences > Tools > File Watchers文件观察器并创建一个新的文件观察器
  • Choose File type: JavaScript选择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.例如,我选择了$ProjectFileDir$/apps/web/src/并递归地包含它的所有内容。
  • Choose the program to run.选择要运行的程序。 With node, npm, and eslint installed point to the eslint bin.安装 node、npm 和 eslint 后,指向 eslint bin。 In my project, the path was $ProjectFileDir$/apps/web/node_modules/eslint/bin/eslint.js在我的项目中,路径是$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$应用以下参数以在保存的文件上使用 fix 选项运行 eslint --fix $FileName$
  • Specify the working directory as $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 的原生支持:

Old (Deprecated):旧(已弃用):

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


New (Native support)新(本机支持)

在此处输入图片说明

1. "Automatic ESLint configuration" 1.《自动ESLint配置》

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 each package.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 配置的项目中的每个路径。

2. Manual ESLint configuration 2. 手动配置 ESLint

You can also set the Node interpreter + ESLint package + .eslintrc config manually你也可以手动设置 Node 解释器 + ESLint 包 + .eslintrc配置


Apply ESLint Code Style Rules应用 ESLint 代码样式规则

You can auto-import some code style rules from your ESLint config:您可以从 ESLint 配置中自动导入一些代码样式规则:

  1. Inside your .eslintrc config file context menu.eslintrc配置文件上下文菜单中

  2. Select " Apply ESLint Code Style Rules "选择“应用 ESLint 代码样式规则

    在此处输入图片说明

  3. Check output in " Event Log " panel检查“事件日志”面板中的输出


Configure Inspections配置检查

  1. From "Warnings" tooltip select "Configure Inspections..."从“警告”工具提示中选择“配置检查...”

    在此处输入图片说明

  2. Activate "ESLint" under "Code quality tools"在“代码质量工具”下激活“ESLint”

    在此处输入图片说明

  3. 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 个问题。


View Inspection Results查看检验结果

  1. "Code" > "Run Inspection by Name..." > type "eslint" “代码”>“按名称运行检查...”> 输入“eslint”

  2. You can "auto fix" problems, or fix the entire file (if rule can be fixed automatically)您可以“自动修复”问题,或修复整个文件(如果规则可以自动修复)

  3. Fixed problems are highlighted突出显示已修复的问题

    在此处输入图片说明


Run/Debug Configuration [optional]运行/调试配置 [可选]

Alternative, you can set up a NPM script run configuration:或者,您可以设置 NPM 脚本运行配置:

  1. After ESLint installation, add a script section to package.json ESLint 安装后,在package.json添加一个脚本部分

    "scripts": { "eslint": "eslint" }
  2. In "npm" panel, you should see the "eslint" script from your package.json .在“npm”面板中,您应该会看到package.json的“eslint”脚本。

  3. You can edit its settings to eg run ESLint for current file only:您可以编辑其设置,例如仅对当前文件运行 ESLint:

    在此处输入图片说明

  4. 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 种方法可以做到

  1. Click on the green arrow on the left side点击左侧的绿色箭头在此处输入图片说明
  2. Click on run button as the image below点击运行按钮,如下图在此处输入图片说明
  3. In terminal run: npm run lintfix在终端运行: npm run lintfix

that all.这一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在鼠标悬停时禁用 JetBrains IDE(IntelliJ IDEA、PyCharm、WebStorm)中的自动显示提示 - How to disable auto show hints in JetBrains IDEs (IntelliJ IDEA, PyCharm, WebStorm) on mouse over 我可以强制Intellij-IDEA运行被忽略的测试吗? - Can I force Intellij-IDEA to run an ignored test? IntelliJ IDEA 和其他 Jetbrains IDE 中的“省电模式”是什么? - What is "Power Save Mode" in IntelliJ IDEA and other Jetbrains IDEs? 如何跳转到将在Intellij-IDEA中运行的方法? - How do I jump to the method that will be run in Intellij-IDEA? 我怎样才能最好地调试 intelliJ-idea 中的 solr? - How can I best debug solr in intelliJ-idea? 为什么 JetBrains IDE(IntelliJ IDEA、WebStorm 等)找不到模块 node_modules/standardx/lib/options? - Why JetBrains IDEs (IntelliJ IDEA, WebStorm, etc) cannot find module node_modules/standardx/lib/options? 如何在WebStorm / IntelliJ IDEA(JetBrains)中永久添加范围 - How to add a scope permanently in WebStorm/IntelliJ IDEA (JetBrains) 如何使用 JetBrains Runtime 运行 IntelliJ IDEA - how to run IntelliJ IDEA with JetBrains Runtime 如何使用其他构建工具和脚本与intelliJ-idea完美配合? - How do I use other build tools and scripts playing nicely with intelliJ-idea? 在Intellij-IDEA中执行svn提交之前,如何运行命令? - How do I run a command before I do an svn commit in Intellij-IDEA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM