Cannot read properties of undefined (reading 'getTokens') Occurred while linting /app/layout.tsx:16 Rule: "@typescript-eslint/no-empty-function"

huangapple go评论92阅读模式
英文:

Cannot read properties of undefined (reading 'getTokens') Occurred while linting /app/layout.tsx:16 Rule: "@typescript-eslint/no-empty-function"

问题

I don't understand why the function that returns JSX is being linted with the rule @typescript-eslint/no-empty-function when running the next lint script.

The code at line 16 of the layout.tsx file is as follows:

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

As you can see, the function is not empty, and it does not violate the @typescript-eslint/no-empty-function rule. However, the following error message occurs:

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.

I also considered the possibility of a misconfigured eslintrc.js file, but I couldn't figure out the cause.

const path = require('path');
module.exports = {
  // ... (rest of your ESLint configuration)
};

I don't understand why "next lint" is linting the function that returns JSX with the @typescript-eslint/no-empty-function rule. You can reproduce this error in the repository available here.

When running "next lint" in the root/packages/clothes_gpt directory, you should see the error.

Expected Behavior: When running the "next lint" script, functions that return JSX should not trigger the @typescript-eslint/no-empty-function rule.

Current Behavior: Functions that return JSX are triggering the @typescript-eslint/no-empty-function rule.

英文:

I don't understand why the function that returns JSX is being linted with the rule &quot;@typescript-eslint/no-empty-function&quot; when running the "next lint" script.

The code at line 16 of the layout.tsx file is as follows:

export default function RootLayout({
  children
}: {
  children: React.ReactNode;
}) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body className={inter.variable}&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}

As you can see, the function is not empty, and it does not violate the &quot;@typescript-eslint/no-empty-function&quot; rule. However, the following error message occurs:

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

I also considered the possibility of a misconfigured eslintrc.js file, but I couldn't figure out the cause.

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

I don't understand why "next lint" is linting the function that returns JSX with the &quot;@typescript-eslint/no-empty-function&quot; rule. You can reproduce this error in the repository available here

When running "next lint" in the root/packages/clothes_gpt directory, you should see the error.

Expected Behavior: When running the "next lint" script, functions that return JSX should not trigger the "@typescript-eslint/no-empty-function" rule.

Current Behavior: Functions that return JSX are triggering the "@typescript-eslint/no-empty-function" rule.

答案1

得分: 5

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

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

typescript-eslint/recommended

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

这个规则是导致持续进行@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"
      }
   }
]

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

英文:

I discovered the cause of my issue and resolved it.

The problem originated from a specific section in the eslintrc.js file, particularly within the plugin:@typescript-eslint/recommended extension plugin.

typescript-eslint/recommended

In line 14 of the eslint plugin @typescript-eslint/recommended, the rule &#39;@typescript-eslint/no-empty-function&#39;: &#39;error&#39; was set.

This rule was the reason behind the continuous linting of @typescript-eslint/no-empty-function. To overcome this, there are a couple of workarounds. One option is to avoid using the @typescript-eslint/recommended plugin altogether. Alternatively, you can override the rule by adding the @typescript-eslint/no-empty-function rule and setting it to &quot;off&quot;.

Here's an example configuration snippet demonstrating the override:

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

Regarding the question of why @typescript-eslint/no-empty-function returns JSX, it's best to refer to the specific rule file for detailed information. You can find it in no-empty-function source code

答案2

得分: 4

我想强调一下@krehwell的评论。在我的情况下,这是因为我有一个使用不同版本eslint的monorepo设置。我正在将它们全部重构成一个共享包。

将eslint的版本对齐解决了问题。

英文:

I want to amplify a comment from @krehwell. In my case this was occurring for me because I had a monorepo setup with different versions of eslint. I was in the process of refactoring it all into a shared package.

Aligning the version of eslint solved the problem.

答案3

得分: 0

我遇到了同样的问题,通过在monorepo项目中同意eslint版本来解决了它。

英文:

I had the same problem and I solved it by agreeing to the eslint version in the monorepo project

huangapple
  • 本文由 发表于 2023年6月12日 21:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定