1 | "use strict";
|
2 | var __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 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
|
16 | var 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 NoControlRegexRuleWalker(sourceFile, this.getOptions()));
|
23 | };
|
24 | Rule.metadata = {
|
25 | ruleName: 'no-control-regex',
|
26 | type: 'maintainability',
|
27 | description: 'Do not use control characters in regular expressions',
|
28 | options: null,
|
29 | optionsDescription: '',
|
30 | typescriptOnly: true,
|
31 | issueClass: 'Non-SDL',
|
32 | issueType: 'Warning',
|
33 | severity: 'Important',
|
34 | level: 'Opportunity for Excellence',
|
35 | group: 'Correctness'
|
36 | };
|
37 | Rule.FAILURE_STRING = 'Unexpected control character in regular expression';
|
38 | return Rule;
|
39 | }(Lint.Rules.AbstractRule));
|
40 | exports.Rule = Rule;
|
41 | var NoControlRegexRuleWalker = (function (_super) {
|
42 | __extends(NoControlRegexRuleWalker, _super);
|
43 | function NoControlRegexRuleWalker() {
|
44 | return _super !== null && _super.apply(this, arguments) || this;
|
45 | }
|
46 | NoControlRegexRuleWalker.prototype.visitNewExpression = function (node) {
|
47 | this.validateCall(node);
|
48 | _super.prototype.visitNewExpression.call(this, node);
|
49 | };
|
50 | NoControlRegexRuleWalker.prototype.visitCallExpression = function (node) {
|
51 | this.validateCall(node);
|
52 | _super.prototype.visitCallExpression.call(this, node);
|
53 | };
|
54 | NoControlRegexRuleWalker.prototype.visitRegularExpressionLiteral = function (node) {
|
55 | if (/(\\x[0-1][0-9a-f])/.test(node.getText())) {
|
56 | this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
|
57 | }
|
58 | _super.prototype.visitRegularExpressionLiteral.call(this, node);
|
59 | };
|
60 | NoControlRegexRuleWalker.prototype.validateCall = function (expression) {
|
61 | if (expression.expression.getText() === 'RegExp') {
|
62 | if (expression.arguments.length > 0) {
|
63 | var arg1 = expression.arguments[0];
|
64 | if (arg1.kind === ts.SyntaxKind.StringLiteral) {
|
65 | var regexpText = arg1.text;
|
66 | if (/[\x00-\x1f]/.test(regexpText)) {
|
67 | this.addFailureAt(arg1.getStart(), arg1.getWidth(), Rule.FAILURE_STRING);
|
68 | }
|
69 | }
|
70 | }
|
71 | }
|
72 | };
|
73 | return NoControlRegexRuleWalker;
|
74 | }(ErrorTolerantWalker_1.ErrorTolerantWalker));
|
75 |
|
\ | No newline at end of file |