代码之家  ›  专栏  ›  技术社区  ›  BeniaminoBaggins

捕获“==未定义”的自定义tslint规则不起作用

  •  1
  • BeniaminoBaggins  · 技术社区  · 7 年前

    我抄了 this tslint rule 在我的项目中工作,所以现在tslint === null 使它成为一个错误。

    我现在想做同样的事,但是为了 undefined . 我已经实现了和 null 但换了张支票 ts.SyntaxKind.UndefinedKeyWord 而不是 ts.SyntaxKind.NullKeyword 但不知什么原因 === undefined .

    如果它和 无效的 一个在工作?

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

    不可抗力

    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 回复  |  直到 7 年前
        1
  •  1
  •   vroomfondel Judy Morgan Loomis    7 年前

    皮棉匠认为 undefined SyntaxKind.Identifier 而不是 SyntaxKind.UndefinedKeyword . 这个 有点 有道理,因为未定义可以重新定义,但仍然是不幸的。

    如果您确定在代码库中的任何地方都没有重新定义undefined,您可以检查 node.getText() === "undefined" 相反。

    推荐文章