如何在VSCode中的同一项目中对.ts文件和.js文件运行typescript-eslint [英] How to run typescript-eslint on .ts files and eslint on .js files in the same project in VSCode

查看:641
本文介绍了如何在VSCode中的同一项目中对.ts文件和.js文件运行typescript-eslint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含javascript和打字稿文件的混合代码库。我想让eslint在.js文件上运行,而typescript-eslint在.ts文件上在VSCode中运行。

I have a mixed codebase with javascript and typescript files. I'd like to have eslint run on .js files and typescript-eslint run on .ts files in VSCode.

我设法配置了.eslintrc.json文件以获取要在.ts文件上运行的typescript-eslint。问题在于它也最终会在.js文件上运行typescript-eslint,而不是普通的老式eslint。

I've managed to configure my .eslintrc.json file to get typescript-eslint to run on .ts files. The problem is that it also ends up running typescript-eslint on .js files, instead of plain old eslint.

这似乎应该很简单并且相当普遍,但是尽管在整个Internet上进行搜索,但我仍然找不到解决方案。

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.

推荐答案

您需要将配置覆盖为 为js和ts文件使用单独的解析器 。您可以按以下方式配置.eslintrc.js

You need to override the configuration to use separate parsers for js and ts files. you can configure .eslintrc.js as below

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
        }
      }
    ]
  };

示例项目为此处

这篇关于如何在VSCode中的同一项目中对.ts文件和.js文件运行typescript-eslint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆