All files noExpressionStatementRule.js

92.73% Statements 51/55
77.78% Branches 28/36
86.67% Functions 13/15
96.08% Lines 49/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77  1x 1x     1x 2x 2x 2x     1x 1x 1x 1x     3x 2x 2x 2x 2x     3x   1x 1x   3x   1x 3x 3x   1x   1x 1x 1x 1x   3x 3x 3x   1x 45x 6x 6x 6x 6x 6x 3x     45x   1x 6x 1x   5x 5x 2x       2x 1x     2x   1x    
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var Lint = require("tslint");
var OPTION_IGNORE_PREFIX = "ignore-prefix";
function parseOptions(options) {
    var ignorePrefix;
    for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
        var o = options_1[_i];
        Eif (typeof o === "object" && o[OPTION_IGNORE_PREFIX] !== null) {
            ignorePrefix = o[OPTION_IGNORE_PREFIX];
            break;
        }
    }
    return { ignorePrefix: ignorePrefix };
}
var Rule = (function (_super) {
    __extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Rule.prototype.apply = function (sourceFile) {
        var noExpressionStatementWalker = new NoExpressionStatementWalker(sourceFile, this.getOptions());
        return this.applyWithWalker(noExpressionStatementWalker);
    };
    return Rule;
}(Lint.Rules.AbstractRule));
Rule.FAILURE_STRING = "Using expressions to cause side-effects not allowed.";
exports.Rule = Rule;
var NoExpressionStatementWalker = (function (_super) {
    __extends(NoExpressionStatementWalker, _super);
    function NoExpressionStatementWalker(sourceFile, options) {
        var _this = _super.call(this, sourceFile, options) || this;
        Object.assign(_this, parseOptions(options.ruleArguments));
        return _this;
    }
    NoExpressionStatementWalker.prototype.visitNode = function (node) {
        if (node && node.kind === ts.SyntaxKind.ExpressionStatement) {
            var children = node.getChildren();
            var text = node.getText(this.getSourceFile());
            var isYield = children.every(function (n) { return n.kind === ts.SyntaxKind.YieldExpression; });
            var isIgnored = this.isIgnored(text);
            if (!isYield && !isIgnored) {
                this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
            }
        }
        _super.prototype.visitNode.call(this, node);
    };
    NoExpressionStatementWalker.prototype.isIgnored = function (text) {
        if (!this.ignorePrefix) {
            return false;
        }
        if (Array.isArray(this.ignorePrefix)) {
            if (this.ignorePrefix.find(function (pfx) { return text.indexOf(pfx) === 0; })) {
                return true;
            }
        }
        else {
            if (text.indexOf(this.ignorePrefix) === 0) {
                return true;
            }
        }
        return false;
    };
    return NoExpressionStatementWalker;
}(Lint.RuleWalker));