无法读取未定义的属性(读取“getTokens”)在 linting /app/layout.tsx:16 规则时发生:“@typescript-eslint/no-empty-function”

Blu*_*e A 22 javascript reactjs eslint next.js typescript-eslint

"@typescript-eslint/no-empty-function"我不明白为什么在运行“下一个 lint”脚本时返回 JSX 的函数会被规则检查。

layout.tsx文件第16行代码如下:

export default function RootLayout({
  children
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className={inter.variable}>{children}</body>
    </html>
  );
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该函数不为空,并且没有违反"@typescript-eslint/no-empty-function"规则。但是,出现以下错误消息:

Cannot read properties of undefined (reading 'getTokens')
Occurred while linting /Users/najaewan/Desktop/f-lab/clothesgpt/packages/clothes_gpt/app/layout.tsx:16
Rule: "@typescript-eslint/no-empty-function"
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Run Code Online (Sandbox Code Playgroud)

我也考虑过文件配置错误的可能性eslintrc.js,但我无法找出原因。

const path = require('path');
module.exports = {
  root: true,
  parserOptions: {
    project: ['./packages/*/tsconfig.json', 'typescript-eslint/parser'],
    ecmaVersion: 2018,
    sourceType: 'module'
  },
  extends: [
    'next/core-web-vitals',
    'plugin:@next/next/recommended',
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
    'prettier',
    'plugin:@typescript-eslint/recommended-requiring-type-checking'
  ],
  plugins: ['@typescript-eslint', 'react', 'react-hooks', 'prettier', 'jest'],
  parser: '@typescript-eslint/parser',
  rules: {
    '@typescript-eslint/no-floating-promises': 'error',
    '@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
    '@typescript-eslint/no-empty-function': 0,
    '@typescript-eslint/explicit-function-return-type': 0,
    '@typescript-eslint/no-var-requires': 0,
    '@typescript-eslint/interface-name-prefix': 0,
    '@typescript-eslint/no-empty-interface': 'error',
    '@typescript-eslint/no-use-before-define': 0,
    '@typescript-eslint/no-non-null-assertion': 'error',
    '@typescript-eslint/explicit-module-boundary-types': 0,
    '@typescript-eslint/no-explicit-any': 0,
    'import/no-unresolved': 'error',
    'react/display-name': 0,
    'react/self-closing-comp': 'error',
    'react/react-in-jsx-scope': 'off',
    'react/jsx-filename-extension': [
      'error',
      { extensions: ['.js', '.jsx', '.ts', '.tsx'] }
    ],
    'react/no-unescaped-entities': 0,
    'react/prop-types': 0,
    'no-undef': 0,
    'no-constant-condition': 1,
    'prettier/prettier': [
      'error',
      {
        bracketSpacing: true,
        printWidth: 80,
        singleQuote: true,
        trailingComma: 'none',
        tabWidth: 2,
        useTabs: false,
        bracketSameLine: false,
        semi: true
      }
    ]
  },
  overrides: [
    {
      files: ['**/*.ts?(x)'],
      parser: '@typescript-eslint/parser',
      extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking'
      ],
      parserOptions: {
        project: ['tsconfig.json', './packages/**/tsconfig.json'],
        ecmaFeatures: {
          jsx: true
        },
        ecmaVersion: 2018,
        sourceType: 'module'
      }
    },
    {
      files: [
        'packages/clothes_gpt/**/*.ts?(x)',
        'packages/clothes_gpt/**/*.js?(x)'
      ],
      rules: {
        '@next/next/no-img-element': 'off'
      },
      settings: {
        'import/parsers': {
          '@typescript-eslint/parser': ['.ts', '.tsx']
        },
        'import/resolver': {
          typescript: {
            project: path.resolve(
              `${__dirname}/packages/clothes_gpt/tsconfig.json`
            )
          }
        }
      }
    }
  ],
  settings: {
    react: {
      pragma: 'React',
      version: 'detect'
    }
  },
  env: {
    browser: true,
    es6: true,
    jest: true
  }
};

Run Code Online (Sandbox Code Playgroud)

我不明白为什么“下一个 lint”会对使用规则返回 JSX 的函数进行 linting "@typescript-eslint/no-empty-function"您可以在此处可用的存储库中重现此错误

在 root/packages/clothes_gpt 目录中运行“next lint”时,您应该会看到错误。

预期行为:运行“next lint”脚本时,返回 JSX 的函数不应触发“@typescript-eslint/no-empty-function”规则。

当前行为:返回 JSX 的函数正在触发“@typescript-eslint/no-empty-function”规则。

Blu*_*e A 9

我发现了问题的原因并解决了它。

该问题源于 eslintrc.js 文件中的特定部分,特别是在插件中:@typescript-eslint/recommended 扩展插件。

typescript-eslint/推荐

'@typescript-eslint/no-empty-function': 'error'在 eslint 插件 @typescript-eslint/recommended 的第 14 行,设置了规则。

这条规则是 . 不断出现 linting 的原因@typescript-eslint/no-empty-function。为了克服这个问题,有一些解决方法。一种选择是完全避免使用该@typescript-eslint/recommended插件。或者,您可以通过添加规则@typescript-eslint/no-empty-function并将其设置为 来覆盖该规则"off"

这是演示覆盖的示例配置片段:

"overrides": [
   "extends": [
      "plugin:@typescript-eslint/recommended",
      "plugin:@typescript-eslint/recommended-requiring-type-checking"
    ],
    "rules":{
      "@typescript-eslint/no-empty-function": "off"
    }
]

Run Code Online (Sandbox Code Playgroud)

关于为什么返回JSX的问题@typescript-eslint/no-empty-function,最好参考具体的规则文件来了解详细信息。您可以在非空函数源代码中找到它


Luk*_*ick 8

我想放大@krehwell 的评论。就我而言,这种情况发生在我身上,因为我有一个包含不同版本 eslint 的 monorepo 设置。我正在将其全部重构为一个共享包。

调整eslint的版本解决了这个问题。