ESLint с ошибкой TypeScript и Firebase — no-undef

#typescript #firebase #eslint

# #typescript #firebase #eslint

Вопрос:

Я использую eslint в своем проекте with typescript-eslint , чтобы иметь возможность вставлять код TypeScript.
У меня проблема с импортом, предоставленным в firebase-admin отношении no-undef правила.

Окружающая среда:

 "firebase-admin": "9.4.1"
"eslint": "7.14.0"
"@typescript-eslint/eslint-plugin": "4.8.2"
"@typescript-eslint/parser": "4.8.2"
"typescript": "4.1.2"
 

Правило:

 no-undef:
 - error
 - typeof: true
 

Все правила (.eslintrc):

 root: true
env:
  es6: true
  node: true
  jest/globals: true
extends:
  - 'eslint:recommended'
  - google
  - 'plugin:@typescript-eslint/eslint-recommended'
  - 'plugin:@typescript-eslint/recommended'
  - 'plugin:@typescript-eslint/recommended-requiring-type-checking'
  - 'plugin:import/errors'
  - 'plugin:import/warnings'
  - 'plugin:import/typescript'
  - 'plugin:jest/style'
  - 'plugin:jest/all'
  - 'plugin:jsdoc/recommended'
  - 'plugin:prettier/recommended'
globals:
  Atomics: readonly
  SharedArrayBuffer: readonly
parserOptions:
  ecmaVersion: 2018
  sourceType: module
  project: ./tsconfig.json
parser: '@typescript-eslint/parser'
plugins:
  - '@typescript-eslint'
  - typescript-sort-keys
  - jest
  - prettier
  - progress
  - jsdoc
settings:
  import/extensions:
    - .js
    - .ts
  import/parsers:
    '@typescript-eslint/parser':
      - .ts
  import/cache:
    lifetime: Infinity
rules:
  progress/activate: 1
  jest/no-done-callback: error
  jest/no-disabled-tests: off
  '@typescript-eslint/member-ordering':
    - error
    - default:
        - signature
        - public-static-field
        - protected-static-field
        - private-static-field
        - public-static-method
        - protected-static-method
        - private-static-method
        - public-abstract-field
        - protected-abstract-field
        - private-abstract-field
        - public-instance-field
        - protected-instance-field
        - private-instance-field
        - public-constructor
        - protected-constructor
        - private-constructor
        - public-instance-method
        - protected-instance-method
        - private-instance-method
        - public-abstract-method
        - protected-abstract-method
        - private-abstract-method
  jest/lowercase-name:
    - error
    - ignore:
        - describe
  jest/no-hooks:
    - error
    - allow:
        - beforeAll
        - beforeEach
        - afterAll
        - afterEach
  typescript-sort-keys/interface:
    - error
    - asc
    - caseSensitive: true
      natural: true
  typescript-sort-keys/string-enum:
    - error
    - asc
    - caseSensitive: true
      natural: true
  quotes: off
  no-else-return:
    - error
    - allowElseIf: true
  sort-keys:
    - error
    - asc
    - caseSensitive: true
      natural: true
      minKeys: 2
  '@typescript-eslint/quotes':
    - error
    - backtick
  '@typescript-eslint/no-var-requires': off
  '@typescript-eslint/explicit-function-return-type': off
  '@typescript-eslint/no-unused-vars':
    - error
    - argsIgnorePattern: ^_
  import/no-namespace: off
  import/first: error
  import/exports-last: error
  import/no-duplicates: error
  import/no-commonjs: error
  import/no-dynamic-require: error
  import/order:
    - error
    - groups:
      - index
      - sibling
      - parent
      - internal
      - external
      - builtin
      - object
      newlines-between: never
      alphabetize:
        order: asc
        caseInsensitive: false
  indent: off
  no-duplicate-imports: error
  sort-imports: off
  new-cap:
    - error
    - capIsNewExceptions:
      - StoreConfig
  '@typescript-eslint/naming-convention':
    - error
    - selector: default
      format:
        - camelCase
    - selector: variable
      format:
        - camelCase
        - UPPER_CASE
    - selector: variable
      types:
        - boolean
      format:
        - PascalCase
        - UPPER_CASE
      prefix:
        - is
        - has
        - should
        - contains
        - as
        - to
    - selector: parameter
      format:
        - camelCase
      leadingUnderscore: allow
    - selector: parameter
      types:
        - boolean
      format:
        - PascalCase
      leadingUnderscore: allow
      prefix:
        - is
        - has
        - should
        - contains
        - as
        - to
    - selector: function
      format:
        - camelCase
    - selector: memberLike
      modifiers:
        - private
      format:
        - camelCase
      leadingUnderscore: require
    - selector: memberLike
      modifiers:
        - protected
      format:
        - camelCase
      leadingUnderscore: require
    - selector: typeLike
      format:
        - PascalCase
    - selector: typeParameter
      format:
        - PascalCase
      prefix:
        - T
    - selector: interface
      format:
        - PascalCase
      prefix:
        - I
    - selector: typeAlias
      format:
        - PascalCase
      prefix:
        - I
    - selector: enumMember
      format:
        - UPPER_CASE
    - selector: enum
      format:
        - PascalCase
      suffix:
        - Enum
  '@typescript-eslint/no-explicit-any':
    - error
    - fixToUnknown: false
      ignoreRestArgs: false
  object-curly-spacing:
    - error
    - always
  linebreak-style: off
  computed-property-spacing:
    - error
    - never
  array-bracket-spacing: off
  prefer-rest-params: off
  require-jsdoc: off
  max-len: off
  comma-dangle: off
  object-shorthand:
    - error
    - always
    - avoidExplicitReturnArrows: true
  arrow-body-style:
    - error
    - as-needed
    - requireReturnForObjectLiteral: true
  no-dupe-else-if: error
  no-unmodified-loop-condition: error
  no-eval: error
  no-extra-label: error
  no-await-in-loop: error
  prefer-destructuring:
    - error
    - VariableDeclarator:
        array: true
        object: true
      AssignmentExpression:
        array: true
        object: true
    - enforceForRenamedProperties: false
  '@typescript-eslint/unbound-method': off
  '@typescript-eslint/no-unsafe-call': off
  '@typescript-eslint/no-unsafe-member-access': off
  '@typescript-eslint/no-unsafe-return': off
  '@typescript-eslint/no-unsafe-assignment': off
  '@typescript-eslint/prefer-enum-initializers': error
  '@typescript-eslint/prefer-literal-enum-member': off
  '@typescript-eslint/unified-signatures': error
  '@typescript-eslint/ban-ts-comment': error
  no-undef-init: error
  no-magic-numbers:
    - error
    - enforceConst: true
      detectObjects: false
  prettier/prettier:
    - error
    - printWidth: 120
      useTabs: false
      semi: true
      singleQuote: true
      quoteProps: consistent
      trailingComma: es5
      bracketSpacing: true
      arrowParens: always
      endOfLine: lf
      tabWidth: 2
  import/no-unresolved: error
  prefer-template: error
  no-useless-concat: error
  no-constant-condition:
    - error
    - checkLoops: true
  prefer-promise-reject-errors:
    - error
    - allowEmptyReject: false
  no-case-declarations: error
  no-irregular-whitespace:
    - error
    - skipStrings: false
      skipComments: false
      skipRegExps: false
      skipTemplates: false
  '@typescript-eslint/adjacent-overload-signatures': error
  '@typescript-eslint/no-misused-promises':
    - error
    - checksConditionals: true
      checksVoidReturn: true
  no-invalid-this: off
  '@typescript-eslint/no-invalid-this': error
  prefer-const:
    - error
    - destructuring: any
      ignoreReadBeforeAssign: false
  camelcase: off
  valid-jsdoc: off
  'jsdoc/require-jsdoc': warn # @todo change it to error but we should fix all current jsdoc errors
  curly:
    - error
    - all
  '@typescript-eslint/prefer-regexp-exec': error
  '@typescript-eslint/await-thenable': error
  '@typescript-eslint/prefer-readonly': error
  '@typescript-eslint/triple-slash-reference':
    - error
    - path: never
      types: never
      lib: never
  '@typescript-eslint/prefer-readonly-parameter-types': # @todo change it to error once the nested object issue was resolved (https://github.com/typescript-eslint/typescript-eslint/issues/2823)
    - warn
    - ignoreInferredTypes: true
      checkParameterProperties: false
  '@typescript-eslint/no-empty-interface':
    - error
    - allowSingleExtends: false
  '@typescript-eslint/restrict-template-expressions':
    - error
    - allowNumber: false
      allowBoolean: false
      allowAny: false
      allowNullish: false
  '@typescript-eslint/ban-types': error
  '@typescript-eslint/restrict-plus-operands':
    - error
    - checkCompoundAssignments: true
  '@typescript-eslint/no-floating-promises':
    - error
    - ignoreVoid: true
  no-undef:
    - warn # @todo change it to error 
    - typeof: true
  no-useless-escape: error
  no-prototype-builtins: error
  require-await: off
  '@typescript-eslint/require-await': error
  no-empty-function: off
  no-var: error
  spaced-comment:
    - error
    - always
    - markers:
        - /
  '@typescript-eslint/no-empty-function':
    - error
    - allow:
        - decoratedFunctions
overrides:
  - files:
      - '*.js'
    rules:
      '@typescript-eslint/explicit-module-boundary-types': off
      import/no-commonjs: off
  - files:
      - '*.ts'
    rules:
      '@typescript-eslint/explicit-function-return-type':
        - error
        - allowExpressions: false
          allowTypedFunctionExpressions: false
          allowHigherOrderFunctions: false
      '@typescript-eslint/no-var-requires': error
  - files:
      - '*.spec.ts'
    rules:
      no-magic-numbers: off
      '@typescript-eslint/ban-types':
        - error
        - types:
            Readonly:
              message: 'Sonia rule - This is useless inside the tests'
          extendDefaults: true
      '@typescript-eslint/naming-convention':
        - error
        - selector: default
          format:
            - camelCase
        - selector: variable
          format:
            - camelCase
            - UPPER_CASE
        - selector: variable
          types:
            - boolean
          format:
            - camelCase
            - UPPER_CASE
        - selector: parameter
          format:
            - camelCase
          leadingUnderscore: allow
        - selector: parameter
          types:
            - boolean
          format:
            - camelCase
          leadingUnderscore: allow
        - selector: function
          format:
            - camelCase
        - selector: memberLike
          modifiers:
            - private
          format:
            - camelCase
          leadingUnderscore: require
        - selector: memberLike
          modifiers:
            - protected
          format:
            - camelCase
          leadingUnderscore: require
        - selector: typeLike
          format:
            - PascalCase
        - selector: typeParameter
          format:
            - PascalCase
          prefix:
            - T
        - selector: interface
          format:
            - PascalCase
          prefix:
            - I
        - selector: typeAlias
          format:
            - PascalCase
          prefix:
            - I
        - selector: enumMember
          format:
            - UPPER_CASE
        - selector: enum
          format:
            - PascalCase
          suffix:
            - Enum
      '@typescript-eslint/no-explicit-any': off
      '@typescript-eslint/ban-ts-comment': off
 

Сбой импорта только на основе no-undef правила:

 import admin from 'firebase-admin';
import WriteResult = admin.firestore.WriteResu<
 

Ошибка:

 ESLint: 'firestore' is not defined.(no-undef)
 

Тем не менее, мой проект работает, и это действительно для TypeScript.

Основываясь на документации TypeScript ESLint, использование globals или env может быть лучшим обходным решением.
За исключением того, что я не нашел никакой связанной информации о существующей конфигурации, поэтому я застрял.

Есть ли у вас какое-либо решение? Спасибо.

Комментарии:

1. можете ли вы показать .eslintrc ?

2. @Anatoly конечно, добавил.

3. У меня также есть "settings": { "import/resolver": { "typescript": {} } }

4. @Anatoly ты имеешь в виду это? npmjs.com/package/eslint-import-resolver-typescript

5. Да, у меня есть эти пакеты в package.json ` «eslint-import-resolver-typescript»: «2.0.0», «eslint-plugin-import»: «2.22.0», `

Ответ №1:

Я полагаю, что вы уже ссылались на рекомендуемый ответ: отключение no-undef правила. Чтобы процитировать страницу документации, на которую вы ссылались:

Мы настоятельно рекомендуем вам не использовать no-undef правило lint в проектах TypeScript. Проверки, которые он предоставляет, уже предоставлены TypeScript без необходимости настройки — TypeScript просто делает это значительно лучше.

И если в вашем проекте используется сочетание TypeScript и JavaScript, лучше отключить его только для файлов TypeScript:

 overrides:
  - files: 
    - "*.ts"
    rules:
      'no-undef': off
 

Однако, если вы все еще предпочитаете сохранить правило включенным и хотите отключить ошибку для нескольких конкретных глобальных переменных ( firestore в вашем случае), вы можете следовать руководству по настройке ESLint для указания глобальных переменных, в котором перечислены 2 метода:

  1. Комментарии к конфигурации: добавить /* global firestore */ в верхней части вашего файла
  2. Файл конфигурации: добавьте следующее в свой .eslintrc :
 globals:
  firestore: readonly
 

Комментарии:

1. Спасибо, я только что увидел ваш комментарий, и это помогло мне решить мою проблему 😉

Ответ №2:

Это помогло мне в моем проекте React-Typescript

Добавьте это в свой файл .eslintrc.json

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

если вы используете .eslitrc.js сделай это

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