【问题标题】:Why fix flag not works in nx lint?为什么修复标志在 nx lint 中不起作用?
【发布时间】:2021-09-14 10:27:59
【问题描述】:

我在我的 nx 项目 (.eslintrc.json) 中为 sort-imports 添加规则:

"rules": {        
          "sort-imports": ["error", {
              "ignoreCase": false,
              "ignoreDeclarationSort": false,
              "ignoreMemberSort": false,
              "memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
              "allowSeparatedGroups": false
          }],
        "@nrwl/nx/enforce-module-boundaries": [ ...

当我运行 --fix 标志时,文件中没有任何变化。

我得到的输出是:

➜  nx-app git:(master) ✗ yarn nx run nx-api:lint --fix
yarn run v1.22.10
$ nx run nx-api:lint --fix
> nx run nx-api:lint --fix
Linting "nx-api"...
/Users/.../nx-app/apps/nx-api/src/main.ts
  2:1  error  Imports should be sorted alphabetically  sort-imports
✖ 1 problem (1 error, 0 warnings)
Lint errors found in the listed files.

———————————————————————————————————————————————

>  NX   ERROR  Running target "nx-api:lint" failed

文件内容为:

import { writeFileSync } from 'fs';
import path from 'path';

console.log({ path });

console.log({ writeFileSync });
console.log('Hello World!');

那么为什么 eslint 不修复文件呢?文档中说:

The --fix option on the command line automatically fixes some problems reported by this rule

【问题讨论】:

    标签: javascript eslint nrwl-nx


    【解决方案1】:

    要使用此规则,您必须安装插件。

    插件是“eslint-plugin-sort-imports”,但不再存在。

    您可以使用:eslint-plugin-sort-imports-es6-autofix

    npm i --save-dev eslint-plugin-sort-imports-es6-autofix
    

    在 .eslintrc.json 中:

      "plugins": ["@nrwl/nx", "sort-imports-es6-autofix"],
      "rules": {
            "sort-imports-es6-autofix/sort-imports-es6": [2, {
              "ignoreCase": false,
              "ignoreMemberSort": false,
              "memberSyntaxSortOrder": ["none", "all", "multiple", "single"]
            }],
            "@nrwl/nx/enforce-module-boundaries": [
             ....
    

    现在使用--fix 运行,它应该可以工作了。

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 2015-01-13
      • 2012-02-07
      • 2015-11-08
      • 1970-01-01
      • 2021-06-02
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多