UNPKG

8.49 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tslint')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'tslint'], factory) :
4 (factory((global.tslintBanSnippets = {}),global.Lint));
5}(this, (function (exports,Lint) { 'use strict';
6
7 /*! *****************************************************************************
8 Copyright (c) Microsoft Corporation. All rights reserved.
9 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
10 this file except in compliance with the License. You may obtain a copy of the
11 License at http://www.apache.org/licenses/LICENSE-2.0
12
13 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 MERCHANTABLITY OR NON-INFRINGEMENT.
17
18 See the Apache Version 2.0 License for specific language governing permissions
19 and limitations under the License.
20 ***************************************************************************** */
21 /* global Reflect, Promise */
22
23 var extendStatics = function(d, b) {
24 extendStatics = Object.setPrototypeOf ||
25 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
26 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
27 return extendStatics(d, b);
28 };
29
30 function __extends(d, b) {
31 extendStatics(d, b);
32 function __() { this.constructor = d; }
33 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34 }
35
36 var BAN_SNIPPETS_RULE_ID = "tsl-ban-snippets";
37
38 var SNIPPETS_PROP = "snippets";
39 var REGEX_SNIPPETS_PROP = "regexSnippets";
40 var ConfigFactory;
41 (function (ConfigFactory) {
42 function createForBanSnippetsRule(options) {
43 var config = createFromArguments(options, BAN_SNIPPETS_RULE_ID);
44 validate(config, "banned", BAN_SNIPPETS_RULE_ID);
45 config.banned.forEach(function (b) { return validateSnippetsList(b, BAN_SNIPPETS_RULE_ID); });
46 return config;
47 }
48 ConfigFactory.createForBanSnippetsRule = createForBanSnippetsRule;
49 function validate(config, prop, ruleId) {
50 if (!hasProp(config, prop)) {
51 throw new Error("invalid config for rule " + ruleId + " - " + prop + " is missing");
52 }
53 }
54 function hasProp(config, prop) {
55 return config[prop] !== undefined;
56 }
57 function validateSnippetsList(config, ruleId) {
58 // either snippets OR regexSnippets is required
59 var hasSnippets = hasProp(config, SNIPPETS_PROP);
60 var hasRegexSnippets = hasProp(config, REGEX_SNIPPETS_PROP);
61 var isValid = hasSnippets || hasRegexSnippets;
62 var hasBoth = hasSnippets && hasRegexSnippets;
63 if (!isValid || hasBoth) {
64 throw new Error("invalid config for rule " + ruleId + " - either " + SNIPPETS_PROP + " or " + REGEX_SNIPPETS_PROP + " must be set");
65 }
66 }
67 function createFromArguments(options, ruleId) {
68 var args = options.ruleArguments;
69 if (args.length !== 1) {
70 throw new Error("tslint rule is misconfigured (" + ruleId + ") - options.ruleArguments length is " + args.length);
71 }
72 var config = args[0];
73 return config;
74 }
75 })(ConfigFactory || (ConfigFactory = {}));
76
77 var GeneralRuleUtils;
78 (function (GeneralRuleUtils) {
79 function buildFailureString(message, ruleId) {
80 // include the rule ID, to make it easier to disable
81 return message + " (" + ruleId + ")";
82 }
83 GeneralRuleUtils.buildFailureString = buildFailureString;
84 function isFileInPaths(filePath, paths) {
85 return paths.some(function (path) { return filePath.indexOf(path) >= 0; });
86 }
87 GeneralRuleUtils.isFileInPaths = isFileInPaths;
88 })(GeneralRuleUtils || (GeneralRuleUtils = {}));
89
90 var Rule = /** @class */ (function (_super) {
91 __extends(Rule, _super);
92 function Rule() {
93 return _super !== null && _super.apply(this, arguments) || this;
94 }
95 Rule.prototype.apply = function (sourceFile) {
96 var config = ConfigFactory.createForBanSnippetsRule(this.getOptions());
97 var walker = new StatementsWalker(sourceFile, this.getOptions(), config);
98 this.applyWithWalker(walker);
99 return walker.getFailures();
100 };
101 return Rule;
102 }(Lint.Rules.AbstractRule));
103 var StatementsWalker = /** @class */ (function (_super) {
104 __extends(StatementsWalker, _super);
105 function StatementsWalker(sourceFile, options, config) {
106 var _this = _super.call(this, sourceFile, options) || this;
107 _this.config = config;
108 return _this;
109 }
110 StatementsWalker.prototype.visitReturnStatement = function (node) {
111 this.visitSomeNode(node);
112 };
113 StatementsWalker.prototype.visitBinaryExpression = function (node) {
114 this.visitSomeNode(node);
115 _super.prototype.visitBinaryExpression.call(this, node);
116 };
117 StatementsWalker.prototype.visitCallExpression = function (node) {
118 this.visitSomeNode(node);
119 _super.prototype.visitCallExpression.call(this, node);
120 };
121 StatementsWalker.prototype.visitDebuggerStatement = function (node) {
122 this.visitSomeNode(node);
123 _super.prototype.visitDebuggerStatement.call(this, node);
124 };
125 StatementsWalker.prototype.visitSomeNode = function (node) {
126 var _this = this;
127 var text = node.getText();
128 var relevantBanned = this.getRelevantBanned();
129 relevantBanned.forEach(function (banned) {
130 if (banned.snippets) {
131 _this.checkBannedSnippet(banned.snippets, text, node, banned.message);
132 }
133 else if (banned.regexSnippets) {
134 _this.checkRegexBannedSnippet(banned.regexSnippets, text, node, banned.message);
135 }
136 else {
137 throw new Error("Invalid config? No snippets and no regex-snippets");
138 }
139 });
140 };
141 StatementsWalker.prototype.checkBannedSnippet = function (snippets, text, node, message) {
142 var bannedCodeFound = snippets.filter(function (bannedSnippet) { return text.indexOf(bannedSnippet) >= 0; });
143 if (bannedCodeFound.length > 0) {
144 this.failRule(node, bannedCodeFound, message);
145 }
146 };
147 StatementsWalker.prototype.checkRegexBannedSnippet = function (regexSnippets, text, node, message) {
148 var bannedCodeFound = regexSnippets.filter(function (regexSnippet) {
149 var regex = new RegExp(regexSnippet);
150 return regex.test(text);
151 });
152 if (bannedCodeFound.length > 0) {
153 this.failRule(node, bannedCodeFound, message);
154 }
155 };
156 StatementsWalker.prototype.failRule = function (node, bannedCodeFound, message) {
157 var failureNode = node.getFirstToken() || node;
158 this.addFailureAtNode(failureNode, GeneralRuleUtils.buildFailureString(message || "Do not use banned code '" + bannedCodeFound.join("' or '") + "'.", BAN_SNIPPETS_RULE_ID));
159 };
160 StatementsWalker.prototype.getRelevantBanned = function () {
161 var sourceFilePath = this.getSourceFile().fileName;
162 return this.config.banned.filter(function (b) {
163 return (!b.includePaths ||
164 GeneralRuleUtils.isFileInPaths(sourceFilePath, b.includePaths)) &&
165 (!b.excludePaths || !GeneralRuleUtils.isFileInPaths(sourceFilePath, b.excludePaths));
166 });
167 };
168 return StatementsWalker;
169 }(Lint.RuleWalker));
170
171 exports.Rule = Rule;
172
173 Object.defineProperty(exports, '__esModule', { value: true });
174
175})));
176//# sourceMappingURL=tslint-ban-snippets.umd.js.map