4 * Copyright 2017 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");
21 var utils = require("tsutils");
22 var Lint = require("../index");
23 var OPTION_IGNORE_MODULE = "ignore-module";
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 var patternConfig = this.ruleArguments[this.ruleArguments.length - 1];
31 var ignorePattern = patternConfig === undefined
33 : new RegExp(patternConfig[OPTION_IGNORE_MODULE]);
34 return this.applyWithFunction(sourceFile, walk, ignorePattern);
37 description: "Avoid import statements with side-effect.",
38 optionExamples: [true, [true, (_a = {}, _a[OPTION_IGNORE_MODULE] = "(\\.html|\\.css)$", _a)]],
52 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One argument may be optionally provided:\n\n * `", "` allows to specify a regex and ignore modules which it matches."], ["\n One argument may be optionally provided:\n\n * \\`", "\\` allows to specify a regex and ignore modules which it matches."])), OPTION_IGNORE_MODULE),
53 rationale: "Imports with side effects may have behavior which is hard for static verification.",
54 ruleName: "no-import-side-effect",
56 typescriptOnly: false,
58 Rule.FAILURE_STRING = "import with explicit side-effect";
60 }(Lint.Rules.AbstractRule));
63 var ignorePattern = ctx.options, sourceFile = ctx.sourceFile;
64 for (var _i = 0, _a = sourceFile.statements; _i < _a.length; _i++) {
65 var statement = _a[_i];
66 if (!utils.isImportDeclaration(statement)) {
69 var importClause = statement.importClause, moduleSpecifier = statement.moduleSpecifier;
70 if (importClause !== undefined || !utils.isStringLiteral(moduleSpecifier)) {
73 if (ignorePattern === undefined || !ignorePattern.test(moduleSpecifier.text)) {
74 ctx.addFailureAtNode(statement, Rule.FAILURE_STRING);