UNPKG

2.61 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-octal-literal',
29 type: 'maintainability',
30 description: 'Do not use octal literals or escaped octal sequences',
31 options: null,
32 optionsDescription: '',
33 typescriptOnly: true,
34 issueClass: 'SDL',
35 issueType: 'Error',
36 severity: 'Critical',
37 level: 'Mandatory',
38 group: 'Security'
39 };
40 Rule.FAILURE_STRING = 'Octal literals should not be used: ';
41 return Rule;
42}(Lint.Rules.AbstractRule));
43exports.Rule = Rule;
44function walk(ctx) {
45 function cb(node) {
46 if (tsutils.isStringLiteral(node) || node.kind === ts.SyntaxKind.FirstTemplateToken) {
47 failOnOctalString(node);
48 }
49 return ts.forEachChild(node, cb);
50 }
51 return ts.forEachChild(ctx.sourceFile, cb);
52 function failOnOctalString(node) {
53 var match = /("|'|`)[^\\]*(\\+-?[0-7]{1,3}(?![0-9]))(?:.|\n|\t|\u2028|\u2029)*(?:\1)/g.exec(node.getText());
54 if (match) {
55 var octalValue = match[2];
56 var backslashCount = octalValue.lastIndexOf('\\') + 1;
57 if (backslashCount % 2 === 1) {
58 octalValue = octalValue.substr(backslashCount - 1);
59 var startOfMatch = node.getStart() + node.getText().indexOf(octalValue);
60 var width = octalValue.length;
61 ctx.addFailureAt(startOfMatch, width, Rule.FAILURE_STRING + octalValue);
62 }
63 }
64 }
65}
66//# sourceMappingURL=noOctalLiteralRule.js.map
\No newline at end of file