Why fix flag not works in nx lint?

Viewed 1078

I add rule for sort-imports in my nx project (.eslintrc.json):

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

And when I run this the --fix flag, nothing change in the file.

the output I get is:

➜  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

The file content is:

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

console.log({ path });

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

So why eslint doesn't fix the file? it's say in the docs:

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

To use this rule you should have to install the plugin.

The plugin is "eslint-plugin-sort-imports", but no longer exists.

Instead you can use: eslint-plugin-sort-imports-es6-autofix

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

And in .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": [
         ....

Now run with --fix and it should work.

Related