4 * Copyright 2014 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
25 return _super !== null && _super.apply(this, arguments) || this;
27 Rule.prototype.apply = function (sourceFile) {
28 var requiresWalker = new NoVarRequiresWalker(sourceFile, this.getOptions());
29 return this.applyWithWalker(requiresWalker);
31 /* tslint:disable:object-literal-sort-keys */
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.",
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 "]))),
43 /* tslint:enable:object-literal-sort-keys */
44 Rule.FAILURE_STRING = "require statement not part of an import statement";
46 }(Lint.Rules.AbstractRule));
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;
54 NoVarRequiresWalker.prototype.createScope = function () {
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);
66 _super.prototype.visitCallExpression.call(this, node);
68 return NoVarRequiresWalker;
69 }(Lint.ScopeAwareRuleWalker));
70 var templateObject_1, templateObject_2;