自定义tslint规则来捕获“ === undefined”不起作用

时间:2018-07-24 03:48:52

标签: typescript tslint

我已将this tslint rule复制到我的项目中并使其正常工作,因此tslint会选择任何=== null并使其出错。

除了undefined,我现在想做同样的事情。我已经实现了与null相同的功能,但更改了支票以查找ts.SyntaxKind.UndefinedKeyWord而不是ts.SyntaxKind.NullKeyword,由于某种原因,它在=== undefined上没有得到使用。

为什么它与正在工作的null相同?

noTripleEqualsNullRule.ts

import * as ts from "typescript";
import * as Lint from "tslint";

const OPTION_NO_NULL_KEYWORD = "no-null-keyword";

export class Rule extends Lint.Rules.AbstractRule {
    public static EQ_FAILURE_STRING = "Did you mean == null instead?";
    public static NEQ_FAILURE_STRING = "Did you mean != null instead?";

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        const noTripleEqualsNullWalker = new NoTripleEqualsNullWalker(sourceFile, this.getOptions());
        return this.applyWithWalker(noTripleEqualsNullWalker);
    }
}

class NoTripleEqualsNullWalker extends Lint.RuleWalker {
    public visitBinaryExpression(node: ts.BinaryExpression) {
        if (this.isExpressionAllowed(node)) {
            const position = node.getChildAt(1).getStart();
            const expressionWidth = node.right.getFullWidth() + 3;
            this.handleBinaryComparison(position, expressionWidth, node.operatorToken.kind, node.right.kind);
        }

        super.visitBinaryExpression(node);
    }

    private handleBinaryComparison(position: number, expressionWidth: number, operator: ts.SyntaxKind, right: ts.SyntaxKind) {
        switch (operator) {
            case ts.SyntaxKind.EqualsEqualsEqualsToken:
                if (right === ts.SyntaxKind.NullKeyword) {
                    this.addFailure(this.createFailure(position, expressionWidth, Rule.EQ_FAILURE_STRING));
                }
                break;
            case ts.SyntaxKind.ExclamationEqualsEqualsToken:
                if (right === ts.SyntaxKind.NullKeyword) {
                    this.addFailure(this.createFailure(position, expressionWidth, Rule.NEQ_FAILURE_STRING));
                }
                break;
            default:
                break;
        }
    }

    private isExpressionAllowed(node: ts.BinaryExpression) {
        const nullKeyword = ts.SyntaxKind.NullKeyword;

        return !this.hasOption(OPTION_NO_NULL_KEYWORD)
            && (node.left.kind ===  nullKeyword || node.right.kind === nullKeyword);
    }
}

noTripleEqualsUndefinedRule.ts

import * as ts from "typescript";
import * as Lint from "tslint";

const OPTION_NO_NULL_KEYWORD = "no-null-keyword";

export class Rule extends Lint.Rules.AbstractRule {
    public static EQ_FAILURE_STRING = "Did you mean == null instead?";
    public static NEQ_FAILURE_STRING = "Did you mean != null instead?";

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        const noTripleEqualsNullWalker = new NoTripleEqualsNullWalker(sourceFile, this.getOptions());
        return this.applyWithWalker(noTripleEqualsNullWalker);
    }
}

class NoTripleEqualsNullWalker extends Lint.RuleWalker {
    public visitBinaryExpression(node: ts.BinaryExpression) {
        if (this.isExpressionAllowed(node)) {
            const position = node.getChildAt(1).getStart();
            const expressionWidth = node.right.getFullWidth() + 3;
            this.handleBinaryComparison(position, expressionWidth, node.operatorToken.kind, node.right.kind);
        }

        super.visitBinaryExpression(node);
    }

    private handleBinaryComparison(position: number, expressionWidth: number, operator: ts.SyntaxKind, right: ts.SyntaxKind) {
        switch (operator) {
            case ts.SyntaxKind.EqualsEqualsEqualsToken:
                if (right === ts.SyntaxKind.UndefinedKeyword) {
                    this.addFailure(this.createFailure(position, expressionWidth, Rule.EQ_FAILURE_STRING));
                }
                break;
            case ts.SyntaxKind.ExclamationEqualsEqualsToken:
                if (right === ts.SyntaxKind.UndefinedKeyword) {
                    this.addFailure(this.createFailure(position, expressionWidth, Rule.NEQ_FAILURE_STRING));
                }
                break;
            default:
                break;
        }
    }

    private isExpressionAllowed(node: ts.BinaryExpression) {
        const nullKeyword = ts.SyntaxKind.UndefinedKeyword;

        return !this.hasOption(OPTION_NO_NULL_KEYWORD)
            && (node.left.kind ===  nullKeyword || node.right.kind === nullKeyword);
    }
}

1 个答案:

答案 0 :(得分:1)

短毛绒认为undefined的类型是SyntaxKind.Identifier而不是SyntaxKind.UndefinedKeyword。这种种类很有意义,因为可以重新定义undefined,但仍然很不幸。

如果确定未在代码库中的任何地方重新定义未定义,则可以改为检查node.getText() === "undefined"