noVarRequiresRule.js 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ES2015-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 ES2015-style imports or \\`import foo = require('foo')\\` imports."]))),
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n AMD-style `require([])` and CommonJS-style `require(\"\")` statements are environment-specific\n and more difficult to statically analyze.\n\n ES2015-style `import`s are part of the JavaScript language specfication and recommended as the path going forward.\n TypeScript will compile them to environment-specific forms as needed.\n "], ["\n AMD-style \\`require([])\\` and CommonJS-style \\`require(\"\")\\` statements are environment-specific\n and more difficult to statically analyze.\n\n ES2015-style \\`import\\`s are part of the JavaScript language specfication and recommended as the path going forward.\n TypeScript will compile them to environment-specific forms as needed.\n "]))),
  40. type: "typescript",
  41. typescriptOnly: true,
  42. };
  43. /* tslint:enable:object-literal-sort-keys */
  44. Rule.FAILURE_STRING = "require statement not part of an import statement";
  45. return Rule;
  46. }(Lint.Rules.AbstractRule));
  47. exports.Rule = Rule;
  48. // tslint:disable-next-line:deprecation
  49. var NoVarRequiresWalker = /** @class */ (function (_super) {
  50. tslib_1.__extends(NoVarRequiresWalker, _super);
  51. function NoVarRequiresWalker() {
  52. return _super !== null && _super.apply(this, arguments) || this;
  53. }
  54. NoVarRequiresWalker.prototype.createScope = function () {
  55. return {};
  56. };
  57. NoVarRequiresWalker.prototype.visitCallExpression = function (node) {
  58. var expression = node.expression;
  59. if (this.getCurrentDepth() <= 1 && expression.kind === ts.SyntaxKind.Identifier) {
  60. var identifierName = expression.text;
  61. if (identifierName === "require") {
  62. // if we're calling (invoking) require, then it's not part of an import statement
  63. this.addFailureAtNode(node, Rule.FAILURE_STRING);
  64. }
  65. }
  66. _super.prototype.visitCallExpression.call(this, node);
  67. };
  68. return NoVarRequiresWalker;
  69. }(Lint.ScopeAwareRuleWalker));
  70. var templateObject_1, templateObject_2;