简体   繁体   English

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

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

I'm trying to create a rule to have a one empty line between internal and external imports.我正在尝试创建一个规则,在内部和外部导入之间有一个空行。

.eslintrc.json: .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"
    }
  ]
  }
}

I have the following error:我有以下错误:

Compiled with problems:X

ERROR

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

Install this dependency: eslint-import-resolver-typescript安装这个依赖: 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

And add import to your plugins:并将import添加到您的插件中:

 "plugins": ["import"],

eslint-import-resolver-typescript eslint-import-resolver-typescript

I modified an existing project with a similar eslint config to use your rule.我修改了一个具有类似 eslint 配置的现有项目以使用您的规则。 It is working nicely.它运行良好。 I have a suspicion your issue is that you don't have an extends property that includes the import rules.我怀疑您的问题是您没有包含导入规则的extends属性。 Here's my eslint config:这是我的 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',
            },
        ],
    },
};

When I run the linter, I get the expected error you want:当我运行 linter 时,我得到了你想要的预期错误:

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

I would try playing with your extends property.我会尝试使用您的extends属性。 I have some stuff in there you may not need at all, and I dont have the jsx stuff for this particular project, but hopefully this will get you started.我有一些你可能根本不需要的东西,我没有这个特定项目的 jsx 东西,但希望这会让你开始。

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

相关问题 eslintrc 中的 ESLint 配置无效:- 意外的顶级属性“import/no-extraneous-dependencies” - ESLint configuration in .eslintrc is invalid: - Unexpected top-level property “import/no-extraneous-dependencies” 意外的顶级异常(反应原生) - UNEXPECTED TOP-LEVEL EXCEPTION (react-native) eslint:分析错误:导入期间出现意外令牌 - eslint: Parsing error: Unexpected token during import Fullcalendar 错误:请在尝试导入插件之前导入顶级 fullcalendar 库(Create-React-App &amp; Craco.config.js) - Fullcalendar Error: Please import the top-level fullcalendar lib before attempting to import a plugin (Create-React-App & Craco.config.js) 是否可以重新加载顶级React Native组件以刷新所有子组件? - Possible to reload top-level React Native component in order to refresh all children components? 意外令牌=导入/未命名为默认eslint错误 - Unexpected token = import/no-named-as-default eslint error 将数据传递到顶级React组件 - Passing data to top-level React component 如何在打字稿中支持顶级等待? - How to support top-level awaits in typescript? 在顶级反应组件上调用方法 - calling method on top-level react component eslint 解析错误:意外标记 = - eslint Parsing error: Unexpected token =
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM