如何在 WebStorm/PHPStorm 中配置 ESLint 自动修复保存?
How to configure ESLint auto fix on save in WebStorm/PHPStorm?
我正在尝试在 WebStorm 中创建一个自定义文件观察器,它将在保存时自动修复 ESLint 错误。在 Settings > Tools > File Watchers
中,我使用以下设置创建了一个新的文件观察器:
- 文件类型:
Any
- 范围:
All places
- 节目:
/home/user/Projects/todo-app/eslint-autofix.sh
- 参数:空白
- 要刷新的输出路径:空白
- 其他选项 > 工作目录:
/home/user/Projects/todo-app
eslint-autofix.sh:
#!/usr/bin/env bash
./node_modules/.bin/eslint --fix
然后ESLint报错,按Ctrl + S保存。弹出以下错误:
/home/user/Projects/todo-app/eslint-autofix.sh
/usr/bin/env: ‘node’: No such file or directory
如何解决这个错误?
根据this文章,设置应该如下:
- 文件类型:任何(或JavaScript)
- 范围:项目文件
- 程序:$ProjectFileDir$/node_modules/.bin/eslint
- 参数:--fix $FilePath$
- 要刷新的输出路径:$FileDir$
只是为了扩展 jstice4all 和 gotjosh 的解决方案:
我能够将 FileWatcher 用于某些项目的 ESLint,但它无法与插件一起使用 extends: '@react-native-community'
@react-native-community/eslint-config#overrides[2]:
Environment key "jest/globals" is unknown
原来 @react-native-community 插件 需要来自项目文件夹本身 运行 才能加载环境变量,而文件watcher 从 node_module/eslint 路径运行。为了让它工作,我必须添加以下配置:
- 工作目录:$ProjectFileDir$
Screenshot Config
在 WebStorm 2020.1.1
上,有一个名为 Run eslint --fix on save
的复选框。
另见:
我正在尝试在 WebStorm 中创建一个自定义文件观察器,它将在保存时自动修复 ESLint 错误。在 Settings > Tools > File Watchers
中,我使用以下设置创建了一个新的文件观察器:
- 文件类型:
Any
- 范围:
All places
- 节目:
/home/user/Projects/todo-app/eslint-autofix.sh
- 参数:空白
- 要刷新的输出路径:空白
- 其他选项 > 工作目录:
/home/user/Projects/todo-app
eslint-autofix.sh:
#!/usr/bin/env bash
./node_modules/.bin/eslint --fix
然后ESLint报错,按Ctrl + S保存。弹出以下错误:
/home/user/Projects/todo-app/eslint-autofix.sh
/usr/bin/env: ‘node’: No such file or directory
如何解决这个错误?
根据this文章,设置应该如下:
- 文件类型:任何(或JavaScript)
- 范围:项目文件
- 程序:$ProjectFileDir$/node_modules/.bin/eslint
- 参数:--fix $FilePath$
- 要刷新的输出路径:$FileDir$
只是为了扩展 jstice4all 和 gotjosh 的解决方案:
我能够将 FileWatcher 用于某些项目的 ESLint,但它无法与插件一起使用 extends: '@react-native-community'
@react-native-community/eslint-config#overrides[2]:
Environment key "jest/globals" is unknown
原来 @react-native-community 插件 需要来自项目文件夹本身 运行 才能加载环境变量,而文件watcher 从 node_module/eslint 路径运行。为了让它工作,我必须添加以下配置:
- 工作目录:$ProjectFileDir$
Screenshot Config
在 WebStorm 2020.1.1
上,有一个名为 Run eslint --fix on save
的复选框。
另见: