简体   繁体   English

TypeScript 编译器不遵循 ESLint 规则

[英]TypeScript compiler not following ESLint rules

I'm having an issue where the JavaScript produced by the TypeScript compiler doesn't follow the ESLint rules set for the project.我遇到了一个问题,即由 TypeScript 编译器生成的 JavaScript 不遵循为项目设置的 ESLint 规则。

Here is the TypeScript configuration (tsconfig.json):这是 TypeScript 配置(tsconfig.json):

{
    "compilerOptions": {
        "lib": ["es2015"],
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true,
        "strict": true,
        "target": "es2015"
    },
    "include": [
        "src"
    ]
}

Here is the ESLint configuration (.eslintrc.json):这是 ESLint 配置(.eslintrc.json):

{
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "standard",
        "plugin:@typescript-eslint/recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 11
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
    }
}

I'm using VS Code and when editing TypeScript the errors from ESLint are shown.我正在使用 VS Code,在编辑 TypeScript 时,会显示来自 ESLint 的错误。 However, the problems I am having are: The compiler will allow code which doesn't conform to the ESLint rules to pass through, and the JavaScript code which is produced doesn't conform to the rules either.但是,我遇到的问题是:编译器将允许不符合 ESLint 规则的代码通过,并且生成的 JavaScript 代码也不符合规则。 For example, it will terminate each statement with a semicolon, when it should not.例如,它会在不应该的时候用分号终止每条语句。

I'm new to TypeScript and I'm pretty sure it's something basic I'm doing wrong, but haven't been able to find this info (so far) in any documentation.我是 TypeScript 的新手,我很确定这是我做错的基本问题,但是(到目前为止)在任何文档中都找不到此信息。

Your guidance is appreciated.感谢您的指导。

ESLint is not in the business of type-checking your typescript code. ESLint 不负责对您的 typescript 代码进行类型检查。 You should rely on TypeScript for checking for type errors like tslint (Configure it accordingly).您应该依靠 TypeScript 来检查 tslint 之类的类型错误(相应地进行配置)。 Example tslint.json示例 tslint.json

{
"rulesDirectory": ["node_modules/codelyzer"],
"rules": {
    "arrow-return-shorthand": true,
    "callable-types": true,
    .....
 }

ESLint is not involved with the bundle build pipeline. ESLint不涉及捆绑构建管道。 you may however run eslint before the bundle build in order to fail the whole build in case there are linting issues.但是,您可以在捆绑构建之前运行 eslint 以使整个构建失败,以防出现 linting 问题。

If you want to make any changes to the code generated it should be done with webpack plugins / typescript.如果您想对生成的代码进行任何更改,应使用 webpack 插件 / typescript 来完成。

So for example, if you want to remove semicolons, this probably should be done with your minifier/compressor.因此,例如,如果您想删除分号,这可能应该使用您的压缩器/压缩器来完成。 such as uglifyjs : https://github.com/mishoo/UglifyJS#output-options比如uglifyjshttps://github.com/mishoo/UglifyJS#output-options

all you are missing is a property call project on the parserOptions of your eslint file.您所缺少的只是 eslint 文件的parserOptions上的属性调用project

....
“parserOptions”:{
  .....
  “project”: [“./tsconfig.json”]
.....

you can algo add more things to both files as well您也可以向这两个文件添加更多内容

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

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