ng lint 返回假错误 Angular

ng lint returning false errors Angular 8

当我 运行 ng lint 时,我无处不在,我得到了所有这些虚假错误

Forbidden 'var' keyword, use 'let' or 'const' instead

但我知道我没有在这些文件中的任何地方使用 var,当我 运行 ng lint --fix 它取代了 exp 中的 [=] function 中的 21=] 和 funexport function 变成 letport letction

我不知道这是怎么开始的,也不知道为什么会这样

这是我的 tslint 文件

{
    "rulesDirectory": ["node_modules/codelyzer"],
    "rules": {
        "arrow-return-shorthand": true,
        "callable-types": true,
        "class-name": true,
        "comment-format": [true],
        "curly": true,
        "eofline": true,
        "forin": true,
        "import-blacklist": [true],
        "import-spacing": true,
        "interface-over-type-literal": true,
        "label-position": true,
        "max-line-length": [false],
        "member-access": false,
        "member-ordering": [
            true,
            {
                "order": ["static-field", "instance-field", "static-method", "instance-method"]
            }
        ],
        "no-arg": true,
        "no-bitwise": true,
        "no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
        "no-construct": true,
        "no-debugger": true,
        "no-duplicate-super": true,
        "no-empty": false,
        "no-empty-interface": true,
        "no-eval": false,
        "no-inferrable-types": [true, "ignore-params"],
        "no-misused-new": true,
        "no-non-null-assertion": true,
        "no-shadowed-variable": false,
        "no-string-literal": false,
        "no-string-throw": true,
        "no-switch-case-fall-through": true,
        "no-trailing-whitespace": true,
        "no-unnecessary-initializer": true,
        "no-unused-expression": false,
        "no-var-keyword": true,
        "object-literal-sort-keys": false,
        "one-line": [true, "check-open-brace", "check-catch", "check-else", "check-whitespace"],
        "prefer-const": false,
        "quotemark": [true, "single"],
        "radix": false,
        "semicolon": [true, "always"],
        "triple-equals": [true, "allow-null-check"],
        "typedef-whitespace": [
            true,
            {
                "call-signature": "nospace",
                "index-signature": "nospace",
                "parameter": "nospace",
                "property-declaration": "nospace",
                "variable-declaration": "nospace"
            }
        ],
        "unified-signatures": true,
        "variable-name": false,
        "whitespace": [true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type"],
        "directive-selector": [false, "attribute", "app", "camelCase"],
        "component-selector": [false, "element", "app", "kebab-case"],
        "no-input-rename": true,
        "no-output-rename": true,
        "use-life-cycle-interface": true,
        "use-pipe-transform-interface": true,
        "component-class-suffix": true,
        "directive-class-suffix": true,
        "no-access-missing-member": false
    }
}

在我的 package.json

"typescript": "3.4.5",
"codelyzer": "^5.1.0",
"tslint": "5.17.0",

我似乎也收到了很多警告,例如

The 'no-arg' rule threw an error in '/angular/src/shared/helpers/FormattedStringValueExtracter.ts':

这是那个文件

class ExtractionResult {
    public IsMatch: boolean;
    public Matches: any[];

    constructor(isMatch: boolean) {
        this.IsMatch = isMatch;
        this.Matches = [];
    }
}

enum FormatStringTokenType {
    ConstantText,
    DynamicValue
}

class FormatStringToken {
    public Text: string;

    public Type: FormatStringTokenType;

    constructor(text: string, type: FormatStringTokenType) {
        this.Text = text;
        this.Type = type;
    }
}

class FormatStringTokenizer {
    Tokenize(format: string, includeBracketsForDynamicValues: boolean = false): FormatStringToken[] {
        const tokens: FormatStringToken[] = [];

        let currentText = '';
        let inDynamicValue = false;

        for (let i = 0; i < format.length; i++) {
            const c = format[i];
            switch (c) {
                case '{':
                    if (inDynamicValue) {
                        throw new Error(
                            'Incorrect syntax at char ' + i + '! format string can not contain nested dynamic value expression!'
                        );
                    }

                    inDynamicValue = true;

                    if (currentText.length > 0) {
                        tokens.push(new FormatStringToken(currentText, FormatStringTokenType.ConstantText));
                        currentText = '';
                    }

                    break;
                case '}':
                    if (!inDynamicValue) {
                        throw new Error(
                            'Incorrect syntax at char ' + i + '! These is no opening brackets for the closing bracket }.'
                        );
                    }

                    inDynamicValue = false;

                    if (currentText.length <= 0) {
                        throw new Error('Incorrect syntax at char ' + i + '! Brackets does not containt any chars.');
                    }

                    let dynamicValue = currentText;
                    if (includeBracketsForDynamicValues) {
                        dynamicValue = '{' + dynamicValue + '}';
                    }

                    tokens.push(new FormatStringToken(dynamicValue, FormatStringTokenType.DynamicValue));
                    currentText = '';

                    break;
                default:
                    currentText += c;
                    break;
            }
        }

        if (inDynamicValue) {
            throw new Error('There is no closing } char for an opened { char.');
        }

        if (currentText.length > 0) {
            tokens.push(new FormatStringToken(currentText, FormatStringTokenType.ConstantText));
        }

        return tokens;
    }
}

export class FormattedStringValueExtracter {
    Extract(str: string, format: string): ExtractionResult {
        if (str === format) {
            return new ExtractionResult(true);
        }

        const formatTokens = new FormatStringTokenizer().Tokenize(format);
        if (!formatTokens) {
            return new ExtractionResult(str === '');
        }

        const result = new ExtractionResult(true);

        for (let i = 0; i < formatTokens.length; i++) {
            const currentToken = formatTokens[i];
            const previousToken = i > 0 ? formatTokens[i - 1] : null;

            if (currentToken.Type === FormatStringTokenType.ConstantText) {
                if (i === 0) {
                    if (str.indexOf(currentToken.Text) !== 0) {
                        result.IsMatch = false;
                        return result;
                    }

                    str = str.substr(currentToken.Text.length, str.length - currentToken.Text.length);
                } else {
                    const matchIndex = str.indexOf(currentToken.Text);
                    if (matchIndex < 0) {
                        result.IsMatch = false;
                        return result;
                    }

                    result.Matches.push({ name: previousToken.Text, value: str.substr(0, matchIndex) });
                    str = str.substring(0, matchIndex + currentToken.Text.length);
                }
            }
        }

        const lastToken = formatTokens[formatTokens.length - 1];
        if (lastToken.Type === FormatStringTokenType.DynamicValue) {
            result.Matches.push({ name: lastToken.Text, value: str });
        }

        return result;
    }

    IsMatch(str: string, format: string): string[] {
        const result = new FormattedStringValueExtracter().Extract(str, format);
        if (!result.IsMatch) {
            return [];
        }

        const values = [];
        for (let i = 0; i < result.Matches.length; i++) {
            values.push(result.Matches[i].value);
        }

        return values;
    }
}

当我查看它说有问题的任何文件时,没有任何警告或错误...而当我实际上 var 在一个文件中时,它不再给我任何警告。 . 在 VSCODE

的问题面板中

如有任何帮助,我们将不胜感激!!真的想不通这个!

编辑

我也看到类似

的错误
TypeError: Cannot read property 'pos' of undefined
    at cb (/node_modules/tslint/lib/rules/oneLineRule.js

好像 tslint 有问题???

编辑

这是我的angular版本

Angular CLI: 8.1.2
Node: 10.16.0OS: linux x64
Angular: 8.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server 
... router, service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.6
@angular-devkit/build-angular     0.800.6
@angular-devkit/build-optimizer   0.800.6
@angular-devkit/build-webpack     0.800.6
@angular-devkit/core              8.1.2
@angular-devkit/schematics        7.3.9
@angular/cdk                      7.3.7
@angular/http                     7.2.15
@angular/pwa                      0.12.4
@ngtools/webpack                  8.0.6
@schematics/angular               7.2.4
@schematics/update                0.801.2
rxjs                              6.5.2
typescript                        3.4.5

我使用最新版本的 CLI 和框架 (v8.2.0) 启动了一个新的本地项目,然后按原样添加了您的文件。 运行 npm run lint 只给我一个 linting 错误

ERROR: /<...>/file.ts:145:9 - Expected a 'for-of' loop instead of a 'for' loop with this simple iteration

所以也许您遇到的问题可以通过更新 cli/framework 版本来解决。如果没有,那么也许检查你的 angular.json 文件以确保你的项目正在使用你期望的 ts 配置,这是我的默认安装

"lint": {
  "builder": "@angular-devkit/build-angular:tslint",
  "options": {
    "tsConfig": [
      "tsconfig.app.json",
      "tsconfig.spec.json",
      "e2e/tsconfig.json"
    ],
    "exclude": [
      "**/node_modules/**"
    ]
  }
}