繁体   English   中英

为 ESLint 和 Airbnb typescript 设置配置 parserOptions 或解析器 package

[英]Configure parserOptions or parser package for ESLint and Airbnb typescript setup

在我的Gatsby (React) 项目中,我使用的是typescript 我需要设置 ESLint 并且必须使用eslint-config-airbnb-typescript

我也在使用 Prettier: prettier (与 Gatsby 一起提供),并且我安装了以下软件包:

npm install eslint-config-prettier eslint-plugin-prettier --save-dev

如果我查看eslint-config-airbnb-typescript的文档,我看不到任何关于@typescript-eslint/parser package 的信息?

他们展示了一个示例,例如:

module.exports = {
  extends: ['airbnb-typescript'],
  parserOptions: {
    project: './tsconfig.json',
  }
};

正确的方法是什么? 在他们的示例中,您不必使用@typescript-eslint/parser package?

我的 ESLint 配置.eslintrc.js

module.exports = {
  globals: {
    __PATH_PREFIX__: true,
  },
  env: {
    browser: true,
    es6: true,
  },
  plugins: ['react', '@typescript-eslint', 'prettier'],
  extends: [
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },
  rules: {
    'prettier/prettier': [
      'error',
      {
        endOfLine: 'auto',
      },
    ],
  },
};

我的tsconfig.js

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "esnext",
    "jsx": "preserve",
    "lib": ["dom", "es2015", "es2017"],
    "strict": true,
    "noEmit": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "baseUrl": "src"
  },
  "include": ["src/**/*"]
}

这是我在eslint-config-auto中设置的方式,它会根据您的 package.json 配置自动配置 eslint。

overrides: [
  {
    files: ['**.ts', '**.tsx'],
    parser: '@typescript-eslint/parser',
    parserOptions: {
      project: './tsconfig.json',
    },
  },
],

暂无
暂无

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

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