123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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: `[\"functionName\", \"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: \\`[\"functionName\", \"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. ],
  79. ],
  80. type: "functionality",
  81. typescriptOnly: false,
  82. };
  83. return Rule;
  84. }(Lint.Rules.AbstractRule));
  85. exports.Rule = Rule;
  86. function parseOptions(args) {
  87. var functions = [];
  88. var methods = [];
  89. for (var _i = 0, args_1 = args; _i < args_1.length; _i++) {
  90. var arg = args_1[_i];
  91. if (typeof arg === "string") {
  92. functions.push({ name: arg });
  93. }
  94. else if (Array.isArray(arg)) {
  95. switch (arg.length) {
  96. case 0:
  97. break;
  98. case 1:
  99. functions.push({ name: arg[0] });
  100. break;
  101. default:
  102. methods.push({ object: [arg[0]], name: arg[1], message: arg[2] });
  103. }
  104. }
  105. else if (!Array.isArray(arg.name)) {
  106. functions.push(arg);
  107. }
  108. else {
  109. switch (arg.name.length) {
  110. case 0:
  111. break;
  112. case 1:
  113. functions.push({ name: arg.name[0], message: arg.message });
  114. break;
  115. default:
  116. methods.push({ name: arg.name[arg.name.length - 1], object: arg.name.slice(0, -1), message: arg.message });
  117. }
  118. }
  119. }
  120. return { functions: functions, methods: methods };
  121. }
  122. var BanFunctionWalker = /** @class */ (function (_super) {
  123. tslib_1.__extends(BanFunctionWalker, _super);
  124. function BanFunctionWalker() {
  125. return _super !== null && _super.apply(this, arguments) || this;
  126. }
  127. BanFunctionWalker.prototype.walk = function (sourceFile) {
  128. var _this = this;
  129. var cb = function (node) {
  130. if (tsutils_1.isCallExpression(node)) {
  131. if (tsutils_1.isIdentifier(node.expression)) {
  132. _this.checkFunctionBan(node.expression);
  133. }
  134. else if (tsutils_1.isPropertyAccessExpression(node.expression)) {
  135. _this.checkForObjectMethodBan(node.expression);
  136. }
  137. }
  138. return ts.forEachChild(node, cb);
  139. };
  140. return ts.forEachChild(sourceFile, cb);
  141. };
  142. BanFunctionWalker.prototype.checkForObjectMethodBan = function (expression) {
  143. for (var _i = 0, _a = this.options.methods; _i < _a.length; _i++) {
  144. var ban = _a[_i];
  145. if (expression.name.text !== ban.name) {
  146. continue;
  147. }
  148. var current = expression.expression;
  149. for (var i = ban.object.length - 1; i > 0; --i) {
  150. if (!tsutils_1.isPropertyAccessExpression(current) || current.name.text !== ban.object[i]) {
  151. continue;
  152. }
  153. current = current.expression;
  154. }
  155. if (ban.object[0] === "*" ||
  156. tsutils_1.isIdentifier(current) && current.text === ban.object[0]) {
  157. this.addFailureAtNode(expression, Rule.FAILURE_STRING_FACTORY(ban.object.join(".") + "." + ban.name, ban.message));
  158. break;
  159. }
  160. }
  161. };
  162. BanFunctionWalker.prototype.checkFunctionBan = function (name) {
  163. var text = name.text;
  164. for (var _i = 0, _a = this.options.functions; _i < _a.length; _i++) {
  165. var ban = _a[_i];
  166. if (ban.name === text) {
  167. this.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(text, ban.message));
  168. break;
  169. }
  170. }
  171. };
  172. return BanFunctionWalker;
  173. }(Lint.AbstractWalker));
  174. var templateObject_1;