2 Object.defineProperty(exports, "__esModule", { value: true });
3 var tslib_1 = require("tslib");
6 * Copyright 2013 Palantir Technologies, Inc.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 /* tslint:enable:object-literal-sort-keys */
29 Rule.FAILURE_STRING_FACTORY = function (expression, messageAddition) {
30 return "Calls to '" + expression + "' are not allowed." + (messageAddition !== undefined ? " " + messageAddition : "");
32 Rule.prototype.apply = function (sourceFile) {
33 return this.applyWithWalker(new BanFunctionWalker(sourceFile, this.ruleName, parseOptions(this.ruleArguments)));
35 /* tslint:disable:object-literal-sort-keys */
38 description: "Bans the use of specific functions or global methods.",
39 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n A list of banned functions or methods in the following format:\n\n * banning functions:\n * just the name of the function: `\"functionName\"`\n * the name of the function in an array with one element: `[\"functionName\"]`\n * an object in the following format: `{\"name\": \"functionName\", \"message\": \"optional explanation message\"}`\n * banning methods:\n * an array with the object name, method name and optional message: `[\"objectName\", \"methodName\", \"optional message\"]`\n * an object in the following format: `{\"name\": [\"objectName\", \"methodName\"], \"message\": \"optional message\"}`\n * you can also ban deeply nested methods: `{\"name\": [\"foo\", \"bar\", \"baz\"]}` bans `foo.bar.baz()`\n * the first element can contain a wildcard (`*`) that matches everything. `{\"name\": [\"*\", \"forEach\"]}` bans `[].forEach(...)`, `$(...).forEach(...)`, `arr.forEach(...)`, etc.\n "], ["\n A list of banned functions or methods in the following format:\n\n * banning functions:\n * just the name of the function: \\`\"functionName\"\\`\n * the name of the function in an array with one element: \\`[\"functionName\"]\\`\n * an object in the following format: \\`{\"name\": \"functionName\", \"message\": \"optional explanation message\"}\\`\n * banning methods:\n * an array with the object name, method name and optional message: \\`[\"objectName\", \"methodName\", \"optional message\"]\\`\n * an object in the following format: \\`{\"name\": [\"objectName\", \"methodName\"], \"message\": \"optional message\"}\\`\n * you can also ban deeply nested methods: \\`{\"name\": [\"foo\", \"bar\", \"baz\"]}\\` bans \\`foo.bar.baz()\\`\n * the first element can contain a wildcard (\\`*\\`) that matches everything. \\`{\"name\": [\"*\", \"forEach\"]}\\` bans\\\n \\`[].forEach(...)\\`, \\`$(...).forEach(...)\\`, \\`arr.forEach(...)\\`, etc.\n "]))),
49 items: { type: "string" },
59 { type: "array", items: { type: "string" }, minLength: 1 },
62 message: { type: "string" },
73 { name: "$", message: "please don't" },
75 { name: ["it", "only"], message: "don't focus tests" },
76 { name: ["chai", "assert", "equal"], message: "Use 'strictEqual' instead." },
77 { name: ["*", "forEach"], message: "Use a regular for loop instead." },
78 { name: ["*", "_id", "toString"], message: "Use 'toHexString' instead." },
81 type: "functionality",
82 typescriptOnly: false,
85 }(Lint.Rules.AbstractRule));
87 function parseOptions(args) {
90 for (var _i = 0, args_1 = args; _i < args_1.length; _i++) {
92 if (typeof arg === "string") {
93 functions.push({ name: arg });
95 else if (Array.isArray(arg)) {
100 functions.push({ name: arg[0] });
103 methods.push({ object: [arg[0]], name: arg[1], message: arg[2] });
106 else if (!Array.isArray(arg.name)) {
110 switch (arg.name.length) {
114 functions.push({ name: arg.name[0], message: arg.message });
118 message: arg.message,
119 name: arg.name[arg.name.length - 1],
120 object: arg.name.slice(0, -1),
125 return { functions: functions, methods: methods };
127 var BanFunctionWalker = /** @class */ (function (_super) {
128 tslib_1.__extends(BanFunctionWalker, _super);
129 function BanFunctionWalker() {
130 return _super !== null && _super.apply(this, arguments) || this;
132 BanFunctionWalker.prototype.walk = function (sourceFile) {
134 var cb = function (node) {
135 if (tsutils_1.isCallExpression(node)) {
136 if (tsutils_1.isIdentifier(node.expression)) {
137 _this.checkFunctionBan(node.expression);
139 else if (tsutils_1.isPropertyAccessExpression(node.expression)) {
140 _this.checkForObjectMethodBan(node.expression);
143 return ts.forEachChild(node, cb);
145 return ts.forEachChild(sourceFile, cb);
147 BanFunctionWalker.prototype.checkForObjectMethodBan = function (expression) {
148 outer: for (var _i = 0, _a = this.options.methods; _i < _a.length; _i++) {
150 if (expression.name.text !== ban.name) {
153 var current = expression.expression;
154 for (var i = ban.object.length - 1; i > 0; --i) {
155 if (!tsutils_1.isPropertyAccessExpression(current) || current.name.text !== ban.object[i]) {
158 current = current.expression;
160 if (ban.object[0] === "*" ||
161 (tsutils_1.isIdentifier(current) && current.text === ban.object[0])) {
162 this.addFailureAtNode(expression, Rule.FAILURE_STRING_FACTORY(ban.object.join(".") + "." + ban.name, ban.message));
167 BanFunctionWalker.prototype.checkFunctionBan = function (name) {
168 var text = name.text;
169 for (var _i = 0, _a = this.options.functions; _i < _a.length; _i++) {
171 if (ban.name === text) {
172 this.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(text, ban.message));
177 return BanFunctionWalker;
178 }(Lint.AbstractWalker));
179 var templateObject_1;