简体   繁体   English

为什么 eslint 考虑 JSX 或一些反应 @types undefined,因为将 typescript-eslint/parser 升级到版本 4.0.0

[英]Why eslint consider JSX or some react @types undefined, since upgrade typescript-eslint/parser to version 4.0.0

The context is pretty big project built with ReactJs, based on eslint rules, with this eslint configuration上下文是使用 ReactJs 构建的非常大的项目,基于 eslint 规则,具有此 eslint 配置

const DONT_WARN_CI = process.env.NODE_ENV === 'production' ? 0 : 1

module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:jsx-a11y/recommended',
    'plugin:react/recommended',
    'prettier',
    'prettier/@typescript-eslint'
  ],
  plugins: [
    'react',
    'html',
    'json',
    'prettier',
    'import',
    'jsx-a11y',
    'jest',
    '@typescript-eslint',
    'cypress'
  ],
  settings: {
    'html/indent': '0',
    es6: true,
    react: {
      version: '16.5'
    },
    propWrapperFunctions: ['forbidExtraProps'],
    'import/resolver': {
      node: {
        extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
      },
      alias: {
        extensions: ['.js', '.jsx', '.json']
      }
    }
  },
  env: {
    browser: true,
    node: true,
    es6: true,
    jest: true,
    'cypress/globals': true
  },
  globals: {
    React: true,
    google: true,
    mount: true,
    mountWithRouter: true,
    shallow: true,
    shallowWithRouter: true,
    context: true,
    expect: true,
    jsdom: true
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'es2020',
    ecmaFeatures: {
      globalReturn: true,
      jsx: true
    },
    lib: ['ES2020']
  },
  rules: {
    'arrow-parens': ['error', 'as-needed'],
    'comma-dangle': ['error', 'never'],
    eqeqeq: ['error', 'smart'],
    'import/first': 0,
    'import/named': 'error',
    'import/no-deprecated': process.env.NODE_ENV === 'production' ? 0 : 1,
    'import/no-unresolved': ['error', { commonjs: true }],
    'jsx-a11y/alt-text': DONT_WARN_CI,
    'jsx-a11y/anchor-has-content': DONT_WARN_CI,
    'jsx-a11y/anchor-is-valid': DONT_WARN_CI,
    'jsx-a11y/click-events-have-key-events': DONT_WARN_CI,
    'jsx-a11y/heading-has-content': DONT_WARN_CI,
    'jsx-a11y/iframe-has-title': DONT_WARN_CI,
    'jsx-a11y/label-has-associated-control': [
      'error',
      {
        controlComponents: ['select']
      }
    ],
    'jsx-a11y/label-has-for': [
      'error',
      {
        required: {
          some: ['nesting', 'id']
        }
      }
    ],
    'jsx-a11y/media-has-caption': DONT_WARN_CI,
    'jsx-a11y/mouse-events-have-key-events': DONT_WARN_CI,
    'jsx-a11y/no-autofocus': DONT_WARN_CI,
    'jsx-a11y/no-onchange': 0,
    'jsx-a11y/no-noninteractive-element-interactions': DONT_WARN_CI,
    'jsx-a11y/no-static-element-interactions': DONT_WARN_CI,
    'jsx-a11y/no-noninteractive-tabindex': DONT_WARN_CI,
    'jsx-a11y/tabindex-no-positive': DONT_WARN_CI,
    'no-console': 'warn',
    'no-debugger': 'warn',
    'no-mixed-operators': 0,
    'no-redeclare': 'off',
    'no-restricted-globals': [
      'error',
      'addEventListener',
      'blur',
      'close',
      'closed',
      'confirm',
      'defaultStatus',
      'defaultstatus',
      'event',
      'external',
      'find',
      'focus',
      'frameElement',
      'frames',
      'history',
      'innerHeight',
      'innerWidth',
      'length',
      'localStorage',
      'location',
      'locationbar',
      'menubar',
      'moveBy',
      'moveTo',
      'name',
      'onblur',
      'onerror',
      'onfocus',
      'onload',
      'onresize',
      'onunload',
      'open',
      'opener',
      'opera',
      'outerHeight',
      'outerWidth',
      'pageXOffset',
      'pageYOffset',
      'parent',
      'print',
      'removeEventListener',
      'resizeBy',
      'resizeTo',
      'screen',
      'screenLeft',
      'screenTop',
      'screenX',
      'screenY',
      'scroll',
      'scrollbars',
      'scrollBy',
      'scrollTo',
      'scrollX',
      'scrollY',
      'self',
      'status',
      'statusbar',
      'stop',
      'toolbar',
      'top'
    ],
    'no-restricted-modules': ['error', 'chai'],
    'no-unused-vars': [
      'error',
      {
        varsIgnorePattern: '^_',
        argsIgnorePattern: '^_'
      }
    ],
    'no-var': 'error',
    'one-var': ['error', { initialized: 'never' }],
    'prefer-const': [
      'error',
      {
        destructuring: 'any'
      }
    ],
    'prettier/prettier': 'error',
    'react/jsx-curly-brace-presence': [
      'error',
      { children: 'ignore', props: 'never' }
    ],
    'react/jsx-no-bind': [
      'error',
      {
        allowArrowFunctions: true
      }
    ],
    'react/jsx-no-literals': 1,
    'react/jsx-no-target-blank': DONT_WARN_CI,
    'react/jsx-no-undef': ['error', { allowGlobals: true }],
    'react/no-deprecated': DONT_WARN_CI,
    'react/prop-types': 0,
    'require-await': 'error',
    'space-before-function-paren': 0
  },
  overrides: [
    {
      files: ['**/*.ts', '**/*.tsx'],
      rules: {
        'no-unused-vars': 'off',
        'import/no-unresolved': 'off'
      }
    }
  ]
}

Since the upgrade of the library "@typescript-eslint/parser": "^4.0.0" from "@typescript-eslint/parser": "^3.10.1" this following command ...由于库"@typescript-eslint/parser": "^4.0.0""@typescript-eslint/parser": "^3.10.1"升级后,以下命令...

eslint --fix --ext .js,.jsx,.json,.ts,.tsx . && stylelint --fix '**/*.scss'

... brings these following errors ...带来以下这些错误

  9:45  error  'ScrollBehavior' is not defined                       no-undef
  224:12  error  'KeyboardEventInit' is not defined                  no-undef
  53:5   error  'JSX' is not defined                                 no-undef

I know I could fix them adding to the prop globals also the keys JSX: true or KeyboardEventInit: true but it is not the way I want to go.我知道我可以将它们添加到道具globals中,也可以将键JSX: trueKeyboardEventInit: true修复,但这不是我想要的方式。 Any ideas of what is going on here?关于这里发生了什么的任何想法? Where is the configuration error?配置错误在哪里? Thanks a lot非常感谢

I had the same issue when trying to declare variables as of type JSX.Element in typescript.尝试在打字稿中将变量声明为JSX.Element类型时,我遇到了同样的问题。 I added "JSX":"readonly" to globals in .eslintrc.json and the problem was gone.我在.eslintrc.json "JSX":"readonly"添加到globals中,问题就消失了。 In your case it would be:在您的情况下,它将是:

globals: {
    React: true,
    google: true,
    mount: true,
    mountWithRouter: true,
    shallow: true,
    shallowWithRouter: true,
    context: true,
    expect: true,
    jsdom: true,
    JSX: true,
},

From the following link, I got that you actually use several options after JSX .从以下链接中,我了解到您实际上在JSX之后使用了几个选项。 You could use true , false , writable or readonly (but not off ).您可以使用truefalsewritablereadonly (但不能使用off )。

https://eslint.org/docs/user-guide/configuring https://eslint.org/docs/user-guide/configuring

Official answer is here and it says indeed to add them to globals or to disable the no-undef rule because TypeScript already has already its own checks:官方答案在这里,它确实说要将它们添加到globals或禁用no-undef规则,因为 TypeScript 已经有自己的检查:

I get errors from the no-undef rule about global variables not being defined, even though there are no TypeScript errors我从关于未定义全局变量的no-undef规则中得到错误,即使没有 TypeScript 错误

The no-undef lint rule does not use TypeScript to determine the global variables that exist - instead, it relies upon ESLint's configuration. no-undef lint 规则不使用 TypeScript 来确定存在的全局变量 - 相反,它依赖于 ESLint 的配置。

We strongly recommend that you do not use the no-undef lint rule on TypeScript projects.我们强烈建议您不要在 TypeScript 项目中使用no-undef lint 规则。 The checks it provides are already provided by TypeScript without the need for configuration - TypeScript just does this significantly better.它提供的检查已经由 TypeScript 提供,无需配置——TypeScript 只是在这方面做得更好。

As of our v4.0.0 release, this also applies to types.从我们的 v4.0.0 版本开始,这也适用于类型。 If you use global types from a 3rd party package (ie anything from an @types package), then you will have to configure ESLint appropriately to define these global types.如果您使用来自第 3 方包的全局类型(即来自@types包的任何内容),那么您必须适当地配置 ESLint 以定义这些全局类型。 For example;例如; the JSX namespace from @types/react is a global 3rd party type that you must define in your ESLint config. @types/react中的JSX命名空间是你必须在 ESLint 配置中定义的全局第 3 方类型。

Note, that for a mixed project including JavaScript and TypeScript, the no-undef rule (like any role) can be turned off for TypeScript files alone by adding an overrides section to .eslintrc.json:请注意,对于包含 JavaScript 和 TypeScript 的混合项目,可以通过向 .eslintrc.json 添加overrides部分来单独为 TypeScript 文件关闭no-undef规则(与任何角色一样):

 "overrides": [ { "files": ["*.ts"], "rules": { "no-undef": "off" } } ]

If you choose to leave on the ESLint no-undef lint rule, you can manually define the set of allowed globals in your ESLint config , and/or you can use one of the pre-defined environment ( env ) configurations .如果您选择保留 ESLint no-undef lint 规则,您可以在 ESLint 配置中手动定义允许的globals,和/或您可以使用预定义的环境 ( env ) 配置之一。

From the typescript-eslint troubleshooting guide :来自typescript-eslint 故障排除指南

We strongly recommend that you do not use the no-undef lint rule on TypeScript projects.我们强烈建议您不要在 TypeScript 项目中使用 no-undef lint 规则。 The checks it provides are already provided by TypeScript without the need for configuration - TypeScript just does this significantly better.它提供的检查已经由 TypeScript 提供,无需配置——TypeScript 只是在这方面做得更好。

In your .eslintrc.js , turn the rule off for TypeScript files using overrides :在您的.eslintrc.js中,使用overrides关闭 TypeScript 文件的规则:

module.exports = {
  root: true,
  extends: '@react-native-community',
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  rules: {
    'no-shadow': 'off',
    '@typescript-eslint/no-shadow': ['error'],
  },
  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      rules: {
        'no-undef': 'off',
      },
    },
  ],
};

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

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