简体   繁体   English

如何在 VSCode 中的同一项目中的 .ts 文件和 .js 文件上运行 typescript-eslint

[英]How to run typescript-eslint on .ts files and eslint on .js files in the same project in VSCode

I have a mixed codebase with javascript and typescript files.我有一个包含 javascript 和 typescript 文件的混合代码库。 I'd like to have eslint run on .js files and typescript-eslint run on .ts files in VSCode.我想让 eslint 在 .js 文件上运行,而 typescript-eslint 在 VSCode 中的 .ts 文件上运行。

I've managed to configure my .eslintrc.json file to get typescript-eslint to run on .ts files.我已经成功配置了我的 .eslintrc.json 文件来让 typescript-eslint 在 .ts 文件上运行。 The problem is that it also ends up running typescript-eslint on .js files, instead of plain old eslint.问题是它最终也会在 .js 文件上运行 typescript-eslint,而不是普通的旧 eslint。

This seems like it should be simple and fairly common, but I haven't been able to find a solution despite searching all over the internet.这看起来应该很简单而且相当普遍,但尽管在互联网上搜索,我仍然无法找到解决方案。

You need to override the configuration to use separate parsers for js and ts files.您需要覆盖配置以对 js 和 ts文件使用单独的解析器 you can configure .eslintrc.js as below你可以配置 .eslintrc.js 如下

module.exports = {
    root: true,    
    extends: [
      'eslint:recommended'
    ],
    "overrides": [
      {
        "files": ["**/*.ts", "**/*.tsx"],
        "env": { "browser": true, "es6": true, "node": true },
        "extends": [
          "eslint:recommended",
          "plugin:@typescript-eslint/eslint-recommended",
          "plugin:@typescript-eslint/recommended"
        ],
        "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" },
        "parser": "@typescript-eslint/parser",
        "parserOptions": {
          "ecmaFeatures": { "jsx": true },
          "ecmaVersion": 2018,
          "sourceType": "module",
          "project": "./tsconfig.json"
        },
        "plugins": ["@typescript-eslint"],
        "rules": {
          "indent": ["error", 2, { "SwitchCase": 1 }],
          "linebreak-style": ["error", "unix"],
          "quotes": ["error", "single"],
          "comma-dangle": ["error", "always-multiline"],
          "@typescript-eslint/no-explicit-any": 0
        }
      }
    ]
  };

Sample Project is here示例项目在这里

Use the overrides prop to have typescript-eslint 's parser and related TS configuration only on .ts/.tsx files.使用overrides typescript-eslint仅在.ts/.tsx文件上具有typescript-eslint的解析器和相关的 TS 配置。 For example (React, TypeScript, ES2021):例如(React、TypeScript、ES2021):

module.exports = {
  root: true,
  extends: ['eslint:recommended'],
  env: {
    browser: true,
    es2021: true,
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  overrides: [
    {
      files: ['**/*.ts', '**/*.tsx'],
      plugins: [
        '@typescript-eslint',
      ],
      extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
      parser: '@typescript-eslint/parser',
      parserOptions: {
        project: ['./tsconfig.json'],
      },
    },
  ],
};

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

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