UNPKG

2.96 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var Rule = (function (_super) {
17 __extends(Rule, _super);
18 function Rule() {
19 return _super !== null && _super.apply(this, arguments) || this;
20 }
21 Rule.prototype.apply = function (sourceFile) {
22 return this.applyWithWalker(new NoInvalidRegexpRuleWalker(sourceFile, this.getOptions()));
23 };
24 Rule.metadata = {
25 ruleName: 'no-invalid-regexp',
26 type: 'maintainability',
27 description: 'Do not use invalid regular expression strings in the RegExp constructor.',
28 options: null,
29 optionsDescription: '',
30 typescriptOnly: true,
31 issueClass: 'Non-SDL',
32 issueType: 'Error',
33 severity: 'Critical',
34 level: 'Opportunity for Excellence',
35 group: 'Correctness'
36 };
37 return Rule;
38}(Lint.Rules.AbstractRule));
39exports.Rule = Rule;
40var NoInvalidRegexpRuleWalker = (function (_super) {
41 __extends(NoInvalidRegexpRuleWalker, _super);
42 function NoInvalidRegexpRuleWalker() {
43 return _super !== null && _super.apply(this, arguments) || this;
44 }
45 NoInvalidRegexpRuleWalker.prototype.visitNewExpression = function (node) {
46 this.validateCall(node);
47 _super.prototype.visitNewExpression.call(this, node);
48 };
49 NoInvalidRegexpRuleWalker.prototype.visitCallExpression = function (node) {
50 this.validateCall(node);
51 _super.prototype.visitCallExpression.call(this, node);
52 };
53 NoInvalidRegexpRuleWalker.prototype.validateCall = function (expression) {
54 if (expression.expression.getText() === 'RegExp') {
55 if (expression.arguments.length > 0) {
56 var arg1 = expression.arguments[0];
57 if (arg1.kind === ts.SyntaxKind.StringLiteral) {
58 var regexpText = arg1.text;
59 try {
60 new RegExp(regexpText);
61 }
62 catch (e) {
63 this.addFailureAt(arg1.getStart(), arg1.getWidth(), e.message);
64 }
65 }
66 }
67 }
68 };
69 return NoInvalidRegexpRuleWalker;
70}(ErrorTolerantWalker_1.ErrorTolerantWalker));
71//# sourceMappingURL=noInvalidRegexpRule.js.map
\No newline at end of file