簡體   English   中英

創建 react app eslint 更漂亮的配置

[英]create react app eslint prettier configuration

我收到這些錯誤消息: 錯誤

但我在 vscode 上沒有收到任何錯誤

這是我的 elint 配置

const path = require('path');

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: [
    'airbnb',
    'prettier',
    'prettier/react',
    'plugin:react/recommended',
    'plugin:prettier/recommended',
    'plugin:import/errors',
    'plugin:import/warnings',
  ],
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', 'import', 'prettier'],
  rules: {
    'prettier/prettier': 'error',
    'react/jsx-uses-react': 'off',
    'react/react-in-jsx-scope': 'off',
    'import/named': 'off',
    'import/no-self-import': 'off',
    'import/no-extraneous-dependencies': 'off',
    'react/jsx-props-no-spreading': 'off',
    'import/prefer-default-export': 'off',
    'no-param-reassign': [
      'error',
      { props: true, ignorePropertyModificationsFor: ['draft'] },
    ],
  },
  settings: {
    'import/resolver': {
      alias: {
        map: [['@', path.join(path.resolve(__dirname, './src'))]],
        extensions: ['.js', '.jsx', '.json'],
      },
      node: {
        extensions: ['.js', '.jsx'],
      },
    },
  },
};

我的配置有什么問題?

這是我收到錯誤的組件: 零件

如您所見,沒有錯誤。

注意: eslint 和 prettier 擴展已啟用並成功運行。 在這些錯誤之前,我得到了錯誤並且 vscode 顯示錯誤,我確信 vscode eslint 可以工作。

怎么了?

我認為這些錯誤是由 create-react-app 附帶的webpack 配置引起的。 webpack 進程在您的項目中調用npm start時在幕后啟動,對您在自定義 eslint 配置文件中指定的別名一無所知。 您的自定義 eslint-config-file 僅由您的編輯器使用(或更具體地由您啟用的 eslint-extension 使用),我相信沒有平滑的方法來更改它(無需創建您自己的 webpack 配置)。

另請參閱https://create-react-app.dev/docs/setting-up-your-editor/#displaying-lint-output-in-the-editor ,它描述了我上面寫的內容。

請注意,即使您自定義 ESLint 配置,這些更改也只會影響編輯器集成。 它們不會影響終端和瀏覽器內 lint output。 這是因為 Create React App 有意提供了一組最小的規則來發現常見錯誤。

暫無
暫無

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

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