.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / banRule.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 var tslib_1 = require("tslib");
4 /**
5  * @license
6  * Copyright 2013 Palantir Technologies, Inc.
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
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);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
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 : "");
31     };
32     Rule.prototype.apply = function (sourceFile) {
33         return this.applyWithWalker(new BanFunctionWalker(sourceFile, this.ruleName, parseOptions(this.ruleArguments)));
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "ban",
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            "]))),
40         options: {
41             type: "list",
42             listType: {
43                 anyOf: [
44                     {
45                         type: "string",
46                     },
47                     {
48                         type: "array",
49                         items: { type: "string" },
50                         minLength: 1,
51                         maxLength: 3,
52                     },
53                     {
54                         type: "object",
55                         properties: {
56                             name: {
57                                 anyOf: [
58                                     { type: "string" },
59                                     { type: "array", items: { type: "string" }, minLength: 1 },
60                                 ],
61                             },
62                             message: { type: "string" },
63                         },
64                         required: ["name"],
65                     },
66                 ],
67             },
68         },
69         optionExamples: [
70             [
71                 true,
72                 "eval",
73                 { name: "$", message: "please don't" },
74                 ["describe", "only"],
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." },
79             ],
80         ],
81         type: "functionality",
82         typescriptOnly: false,
83     };
84     return Rule;
85 }(Lint.Rules.AbstractRule));
86 exports.Rule = Rule;
87 function parseOptions(args) {
88     var functions = [];
89     var methods = [];
90     for (var _i = 0, args_1 = args; _i < args_1.length; _i++) {
91         var arg = args_1[_i];
92         if (typeof arg === "string") {
93             functions.push({ name: arg });
94         }
95         else if (Array.isArray(arg)) {
96             switch (arg.length) {
97                 case 0:
98                     break;
99                 case 1:
100                     functions.push({ name: arg[0] });
101                     break;
102                 default:
103                     methods.push({ object: [arg[0]], name: arg[1], message: arg[2] });
104             }
105         }
106         else if (!Array.isArray(arg.name)) {
107             functions.push(arg);
108         }
109         else {
110             switch (arg.name.length) {
111                 case 0:
112                     break;
113                 case 1:
114                     functions.push({ name: arg.name[0], message: arg.message });
115                     break;
116                 default:
117                     methods.push({
118                         message: arg.message,
119                         name: arg.name[arg.name.length - 1],
120                         object: arg.name.slice(0, -1),
121                     });
122             }
123         }
124     }
125     return { functions: functions, methods: methods };
126 }
127 var BanFunctionWalker = /** @class */ (function (_super) {
128     tslib_1.__extends(BanFunctionWalker, _super);
129     function BanFunctionWalker() {
130         return _super !== null && _super.apply(this, arguments) || this;
131     }
132     BanFunctionWalker.prototype.walk = function (sourceFile) {
133         var _this = this;
134         var cb = function (node) {
135             if (tsutils_1.isCallExpression(node)) {
136                 if (tsutils_1.isIdentifier(node.expression)) {
137                     _this.checkFunctionBan(node.expression);
138                 }
139                 else if (tsutils_1.isPropertyAccessExpression(node.expression)) {
140                     _this.checkForObjectMethodBan(node.expression);
141                 }
142             }
143             return ts.forEachChild(node, cb);
144         };
145         return ts.forEachChild(sourceFile, cb);
146     };
147     BanFunctionWalker.prototype.checkForObjectMethodBan = function (expression) {
148         outer: for (var _i = 0, _a = this.options.methods; _i < _a.length; _i++) {
149             var ban = _a[_i];
150             if (expression.name.text !== ban.name) {
151                 continue;
152             }
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]) {
156                     continue outer;
157                 }
158                 current = current.expression;
159             }
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));
163                 break;
164             }
165         }
166     };
167     BanFunctionWalker.prototype.checkFunctionBan = function (name) {
168         var text = name.text;
169         for (var _i = 0, _a = this.options.functions; _i < _a.length; _i++) {
170             var ban = _a[_i];
171             if (ban.name === text) {
172                 this.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(text, ban.message));
173                 break;
174             }
175         }
176     };
177     return BanFunctionWalker;
178 }(Lint.AbstractWalker));
179 var templateObject_1;