noVarRequiresRule.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2014 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var ts = require("typescript");
  21. var Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. Rule.prototype.apply = function (sourceFile) {
  28. var requiresWalker = new NoVarRequiresWalker(sourceFile, this.getOptions());
  29. return this.applyWithWalker(requiresWalker);
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-var-requires",
  34. description: "Disallows the use of require statements except in import statements.",
  35. descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n In other words, the use of forms such as `var module = require(\"module\")` are banned.\n Instead use ES6 style imports or `import foo = require('foo')` imports."], ["\n In other words, the use of forms such as \\`var module = require(\"module\")\\` are banned.\n Instead use ES6 style imports or \\`import foo = require('foo')\\` imports."]))),
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. type: "typescript",
  40. typescriptOnly: true,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING = "require statement not part of an import statement";
  44. return Rule;
  45. }(Lint.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. // tslint:disable-next-line:deprecation
  48. var NoVarRequiresWalker = /** @class */ (function (_super) {
  49. tslib_1.__extends(NoVarRequiresWalker, _super);
  50. function NoVarRequiresWalker() {
  51. return _super !== null && _super.apply(this, arguments) || this;
  52. }
  53. NoVarRequiresWalker.prototype.createScope = function () {
  54. return {};
  55. };
  56. NoVarRequiresWalker.prototype.visitCallExpression = function (node) {
  57. var expression = node.expression;
  58. if (this.getCurrentDepth() <= 1 && expression.kind === ts.SyntaxKind.Identifier) {
  59. var identifierName = expression.text;
  60. if (identifierName === "require") {
  61. // if we're calling (invoking) require, then it's not part of an import statement
  62. this.addFailureAtNode(node, Rule.FAILURE_STRING);
  63. }
  64. }
  65. _super.prototype.visitCallExpression.call(this, node);
  66. };
  67. return NoVarRequiresWalker;
  68. }(Lint.ScopeAwareRuleWalker));
  69. var templateObject_1;