简体   繁体   English

ESLint:为一条规则运行 ESLint 和一条大规则 object

[英]ESLint: Running ESLint for one rule with a big rule object

I am trying to run ESLint in my React project for only to check one specific (eg: max-len ) rule like this:我试图在我的 React 项目中运行 ESLint 以仅检查一个特定的(例如: max-len )规则,如下所示:

eslint . --ext .js,.jsx,.ts,.tsx --rule "{'max-len': ['error', { code: 120, ignoreComments: true, ignoreTrailingComments: true, ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true, }]}"

But results are also showing errors for other type of errors.但结果也显示其他类型错误的错误。

Here is my .eslintrc.js file:这是我的.eslintrc.js文件:

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'react-app',
    'airbnb',
    'plugin:react/recommended',
    'plugin:import/errors',
    'plugin:import/warnings',
    'plugin:import/typescript',
    'plugin:jsx-a11y/recommended',
  ],
  plugins: [
    'react',
    'jsx-a11y',
    '@typescript-eslint',
  ],
  // parser: 'babel-eslint',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2017,
    sourceType: 'module',
  },
  settings: {
    'import/resolver': {
      node: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    },
  },
  rules: {
    'max-len': ['error', {
      code: 120,
      ignoreComments: true,
      ignoreTrailingComments: true,
      ignoreUrls: true,
      ignoreStrings: true,
      ignoreTemplateLiterals: true,
    }],
  },
};

I have also tried to run with ignoring file .eslintrc.js using --no-eslintrc like this:我还尝试使用--no-eslintrc忽略文件.eslintrc.js来运行,如下所示:

eslint . --ext .js,.jsx,.ts,.tsx --no-eslintrc --rule ....same-rule-above

This time I only get one max-len error and the rest are errors for parsing the import keyword:这次我只得到一个 max-len 错误,rest 是解析 import 关键字的错误:

/Users/olcayertas/10n/src/util/helpers.ts
  1:1  error  Parsing error: The keyword 'import' is reserved

/Users/olcayertas/10n/src/util/routes.ts
  1:1  error  Parsing error: The keyword 'export' is reserved

/Users/olcayertas/10n/src/util/ui-helpers.ts
  1:1  error  Parsing error: The keyword 'import' is reserved

✖ 196 problems (196 errors, 0 warnings)

How can I run ESLint for just one rule with a rule object?我怎样才能只为规则 object 的一条规则运行 ESLint?

I find the problem and here is the answer.我找到了问题,这就是答案。 Because our project is using TypeScript , we need the configuration on the eslintrc.js file, so using --no-eslintrc doesn't work.因为我们的项目使用的是TypeScript ,所以我们需要在eslintrc.js文件上进行配置,所以使用--no-eslintrc不起作用。 Instead we had disabled the extended rules (not the plugins):相反,我们禁用了扩展规则(不是插件):

module.exports = {
  ...
  extends: [
    'plugin:import/errors',
    'plugin:import/warnings',
    'plugin:import/typescript',
    //'plugin:react/recommended',
    //'arbnb',
  ],
  rules: {
    'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
    'import/prefer-default-export': 'off',
    'import/no-unresolved': ['error', { ignore: [ '.svg' ] } ],
    'import/extensions': [
      'error',
      'ignorePackages',
      {
        js: 'never',
        jsx: 'never',
        ts: 'never',
        tsx: 'never',
      },
    ],
  },
}

Since I want to see the errors for imports, I have left plugins and rules enabled for the imports.由于我想查看导入错误,因此我为导入启用了插件和规则。 After disabling the extended rules I run this command:禁用扩展规则后,我运行以下命令:

eslint . --ext .js,.jsx,.ts,.tsx

And it works perfect.它工作完美。 This way we can add rules step by step.这样我们可以逐步添加规则。

If you want to use your.eslintrc file to keep your configuration (parser, plugin settings, etc), you can use eslint-nibble with the --rule=max-len flag.如果你想使用你的 .eslintrc 文件来保留你的配置(解析器、插件设置等),你可以使用带有--rule=max-len标志的eslint- nibble。 This will respect your normal configuration, but only show you errors from that rule, and give the option to fix them, if the rule is auto-fixable.这将尊重您的正常配置,但仅向您显示该规则的错误,并提供修复它们的选项,如果该规则是可自动修复的。

Disclaimer: I'm the creator of eslint-nibble.免责声明:我是 eslint-nibble 的创建者。

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

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