TypeScript编译器未遵循ESLint规则

时间:2020-05-25 05:17:01

标签: javascript typescript eslint

我遇到一个问题,即TypeScript编译器生成的JavaScript不遵循为项目设置的ESLint规则。

这是TypeScript配置(tsconfig.json):

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

这是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": {
    }
}

我正在使用VS Code,并且在编辑TypeScript时会显示ESLint中的错误。但是,我遇到的问题是:编译器将允许不符合ESLint规则的代码通过,并且生成的JavaScript代码也不符合规则。例如,它将在不应该使用分号终止每个语句。

我是TypeScript的新手,我很确定这是我做错的基本事情,但是到目前为止(在任何文档中)都找不到该信息。

我们感谢您的指导。

3 个答案:

答案 0 :(得分:0)

ESLint不能进行打字稿代码的类型检查。您应该依靠TypeScript来检查类型错误,例如tslint(进行相应配置)。示例tslint.json

    for row in dfSpecificColumn:
        allTest = row.split("|")
        allTest.pop(0) #remove the 4| in the beginning of each line
        count = 0
        columnName = ''
        dict = OrderedDict()
        # for each test and value, insert into dictonary and for evrey line in csv add it to dataframe
        for text in allTest:
            if count % 2 == 1:
                dict[columnName] = text
            else:
                columnName = text
            count = count + 1
        dfOutputWithTestThatFailed = dfOutputWithTestThatFailed.append(dict, ignore_index=True)
    return dfOutputWithTestThatFailed

答案 1 :(得分:0)

ESLint与捆绑包构建管道无关。但是,您可以在捆绑软件构建之前运行eslint,以防整个更新过程中出现掉毛问题。

如果要对生成的代码进行任何更改,则应使用webpack插件/打字稿来完成。

因此,例如,如果要删除分号,则可能应使用缩小器/压缩器来完成。例如uglifyjshttps://github.com/mishoo/UglifyJS#output-options

答案 2 :(得分:0)

您所缺少的只是对eslint文件project进行的属性调用parserOptions

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

您也可以在两个文件中添加更多内容

相关问题