【问题标题】:ESLint CLI with --rule option带有 --rule 选项的 ESLint CLI
【发布时间】:2020-11-29 00:10:56
【问题描述】:

我在使用带有 --rule 选项的 ESLint CLI 时遇到问题。

# This is what I tried
eslint --rule "{no-console: error}" --fix-dry-run . 

导致以下错误:

选项“规则”的值无效 - 预期类型 Object,接收值:{no-console:.

使用--rule 选项的正确方法是什么?我在本地安装了 ESLint 并使用 npx 运行它。

  • Node.js 版本 14.15.0
  • ESLint 版本 7.14.0
  • 操作系统 Windows 10

.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/vue3-recommended',
    'prettier',
    'prettier/vue'
  ],
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 12,
    sourceType: 'module'
  },
  plugins: ['@typescript-eslint'],
  rules: {
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-explicit-any': 'off'
  }
};

【问题讨论】:

    标签: command-line-interface eslint


    【解决方案1】:

    这似乎是npx 和 Windows 的错误。在this issue 中解释删除--rule 中的空格可以解决问题:

    npx eslint --rule "no-console:error" --fix-dry-run .
    

    【讨论】:

      【解决方案2】:

      您可以在.eslintrc.js 规则中添加"no-console": ["error"]

      module.exports = {
        ...
        rules: {
          '@typescript-eslint/explicit-module-boundary-types': 'off',
          '@typescript-eslint/no-explicit-any': 'off',
          'no-console': ['error']
        }
      };
      

      然后运行

      eslint --fix-dry-run ./
      

      eslint --rule "no-console: ['error']" --fix-dry-run .
      

      https://eslint.org/docs/user-guide/command-line-interface#-rule

      【讨论】:

      • 我只想在从命令行运行时应用no-console 规则。但是,做--rule "no-console: ['error']" 似乎对我不起作用。获取No files matching the pattern "['error']" were found
      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多