繁体   English   中英

ESLINT 意外的顶级属性“导入/订购”错误

[英]ESLINT Unexpected top-level property "import/order" error

我正在尝试创建一个规则,在内部和外部导入之间有一个空行。

.eslintrc.json:

{
  "parser": "@typescript-eslint/parser",
  "env": {
      "es6": true,
      "node": true,
      "jest/globals": true,
      "browser": true
  },
  "parserOptions": {
      "sourceType": "module",
      "ecmaVersion": 2019,
      "project": "tsconfig.json",
      "ecmaFeatures": {
          "jsx": true
      }
  },
  "rules": {
    "import/order": [
    "error",
    {
      "groups": [["builtin", "external"]],
      "newlines-between": "always"
    }
  ]
  }
}

我有以下错误:

Compiled with problems:X

ERROR

ESLint configuration in .eslintrc.json is invalid:
    - Unexpected top-level property "import/order".

安装这个依赖: eslint-import-resolver-typescript

# npm
npm i -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript

# yarn
yarn add -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript

并将import添加到您的插件中:

 "plugins": ["import"],

eslint-import-resolver-typescript

我修改了一个具有类似 eslint 配置的现有项目以使用您的规则。 它运行良好。 我怀疑您的问题是您没有包含导入规则的extends属性。 这是我的 eslint 配置:

module.exports = {
    root: true,
    env: {
        node: true,
    },
    parser: '@typescript-eslint/parser',
    plugins: ['@typescript-eslint'],
    extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/eslint-recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:import/warnings', // <--- note this inclusion here
    ],
    rules: {
        'import/order': [
            'error',
            {
                groups: [['builtin', 'external']],
                'newlines-between': 'always',
            },
        ],
    },
};

当我运行 linter 时,我得到了你想要的预期错误:

13:1  error  There should be at least one empty line between import groups  import/order

我会尝试使用您的extends属性。 我有一些你可能根本不需要的东西,我没有这个特定项目的 jsx 东西,但希望这会让你开始。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM