簡體   English   中英

如何禁用有關某些未使用參數的警告,但保留“@typescript-eslint/no-unused-vars”規則

[英]How to disable warn about some unused params, but keep "@typescript-eslint/no-unused-vars" rule

我想禁用未使用的參數警告,但保留“未使用的變量”警告。

對於此代碼:

const Query = objectType({
  name: 'Query',
  definition(t) {
    t.field('test', {
      type: 'Test',
      resolve: (root, args, ctx) => {
        const x = 1

        return { id: 1, time: new Date().toString() }
      },
    })
  },
})

我收到警告:

  26:17  warning  'root' is defined but never used        @typescript-eslint/no-unused-vars
  26:23  warning  'args' is defined but never used        @typescript-eslint/no-unused-vars
  26:29  warning  'ctx' is defined but never used         @typescript-eslint/no-unused-vars
  27:15  warning  'x' is assigned a value but never used  @typescript-eslint/no-unused-vars

eslint 配置:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: { ecmaVersion: 2020, ecmaFeatures: { jsx: true } },
  env: {
    browser: true,
    node: true,
  },
  extends: ['plugin:react-hooks/recommended', 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react/recommended'],
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    '@typescript-eslint/no-empty-function': 'off',
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'react/prop-types': 'off',
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['off'],
  },
  ignorePatterns: ['**/generated/*'],
}

我試圖以某種方式禁用它,但發現只有這個選項可以禁用所有內容:

'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': ['off'],

我發現的唯一方法是在規則選項中使用忽略模式argsIgnorePattern 如果您的變量未使用,只需添加下划線_ctx並且 eslint 將忽略它,但no-unused-vars規則仍然適用於其他值。

 '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],

您可以使用正則表達式隨意更改此模式^_

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM