UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var Rule = (function (_super) {
20 __extends(Rule, _super);
21 function Rule() {
22 return _super !== null && _super.apply(this, arguments) || this;
23 }
24 Rule.prototype.apply = function (sourceFile) {
25 return this.applyWithFunction(sourceFile, walk);
26 };
27 Rule.metadata = {
28 ruleName: 'no-control-regex',
29 type: 'maintainability',
30 description: 'Do not use control characters in regular expressions',
31 options: null,
32 optionsDescription: '',
33 typescriptOnly: true,
34 issueClass: 'Non-SDL',
35 issueType: 'Warning',
36 severity: 'Important',
37 level: 'Opportunity for Excellence',
38 group: 'Correctness'
39 };
40 Rule.FAILURE_STRING = 'Unexpected control character in regular expression';
41 return Rule;
42}(Lint.Rules.AbstractRule));
43exports.Rule = Rule;
44function walk(ctx) {
45 function cb(node) {
46 if (tsutils.isNewExpression(node) || tsutils.isCallExpression(node)) {
47 validateCall(node);
48 }
49 if (tsutils.isRegularExpressionLiteral(node)) {
50 if (/(\\x[0-1][0-9a-f])/.test(node.getText())) {
51 ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
52 }
53 }
54 return ts.forEachChild(node, cb);
55 }
56 return ts.forEachChild(ctx.sourceFile, cb);
57 function validateCall(expression) {
58 if (expression.expression.getText() === 'RegExp' && expression.arguments && expression.arguments.length > 0) {
59 var arg = expression.arguments[0];
60 if (tsutils.isStringLiteral(arg)) {
61 var regexpText = arg.text;
62 if (/[\x00-\x1f]/.test(regexpText)) {
63 ctx.addFailureAt(arg.getStart(), arg.getWidth(), Rule.FAILURE_STRING);
64 }
65 }
66 }
67 }
68}
69//# sourceMappingURL=noControlRegexRule.js.map
\No newline at end of file