UNPKG

3.44 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || function (d, b) {
2 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3 function __() { this.constructor = d; }
4 __.prototype = b.prototype;
5 d.prototype = new __();
6};
7var ErrorTolerantWalker = require('./utils/ErrorTolerantWalker');
8var SyntaxKind = require('./utils/SyntaxKind');
9var Rule = (function (_super) {
10 __extends(Rule, _super);
11 function Rule() {
12 _super.apply(this, arguments);
13 }
14 Rule.prototype.apply = function (sourceFile) {
15 return this.applyWithWalker(new NoConstantConditionRuleWalker(sourceFile, this.getOptions()));
16 };
17 Rule.FAILURE_STRING = 'Found constant conditional: ';
18 return Rule;
19})(Lint.Rules.AbstractRule);
20exports.Rule = Rule;
21var NoConstantConditionRuleWalker = (function (_super) {
22 __extends(NoConstantConditionRuleWalker, _super);
23 function NoConstantConditionRuleWalker() {
24 _super.apply(this, arguments);
25 }
26 NoConstantConditionRuleWalker.prototype.isConstant = function (node) {
27 return node.kind === SyntaxKind.current().FalseKeyword
28 || node.kind === SyntaxKind.current().TrueKeyword
29 || node.kind === SyntaxKind.current().NumericLiteral;
30 };
31 NoConstantConditionRuleWalker.prototype.visitIfStatement = function (node) {
32 if (this.isConstant(node.expression)) {
33 var message = Rule.FAILURE_STRING + 'if (' + node.expression.getText() + ')';
34 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
35 }
36 _super.prototype.visitIfStatement.call(this, node);
37 };
38 NoConstantConditionRuleWalker.prototype.visitConditionalExpression = function (node) {
39 if (this.isConstant(node.condition)) {
40 var message = Rule.FAILURE_STRING + node.condition.getText() + ' ?';
41 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
42 }
43 _super.prototype.visitConditionalExpression.call(this, node);
44 };
45 NoConstantConditionRuleWalker.prototype.visitWhileStatement = function (node) {
46 if (this.isConstant(node.expression)) {
47 var message = Rule.FAILURE_STRING + 'while (' + node.expression.getText() + ')';
48 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
49 }
50 _super.prototype.visitWhileStatement.call(this, node);
51 };
52 NoConstantConditionRuleWalker.prototype.visitDoStatement = function (node) {
53 if (this.isConstant(node.expression)) {
54 var message = Rule.FAILURE_STRING + 'while (' + node.expression.getText() + ')';
55 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
56 }
57 _super.prototype.visitDoStatement.call(this, node);
58 };
59 NoConstantConditionRuleWalker.prototype.visitForStatement = function (node) {
60 if (node.condition != null) {
61 if (this.isConstant(node.condition)) {
62 var message = Rule.FAILURE_STRING + ';' + node.condition.getText() + ';';
63 this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
64 }
65 }
66 _super.prototype.visitForStatement.call(this, node);
67 };
68 return NoConstantConditionRuleWalker;
69})(ErrorTolerantWalker);
70//# sourceMappingURL=noConstantConditionRule.js.map
\No newline at end of file