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 tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS = "allow-namespace-imports";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.FAILURE_STRING = function (module) {
31 return "Multiple imports from '" + module + "' can be combined into one.";
33 Rule.NAMESPACE_FAILURE_STRING = function (module) {
34 return "Multiple wildcard imports from the same module, '" + module + "', are prohibited.";
36 Rule.prototype.apply = function (sourceFile) {
38 return this.applyWithFunction(sourceFile, walk, (_a = {},
39 _a[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS] = !!(this.ruleArguments.length > 0 &&
40 this.ruleArguments[0] !== null &&
41 this.ruleArguments[0][OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS]),
44 /* tslint:disable:object-literal-sort-keys */
46 ruleName: "no-duplicate-imports",
47 description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows multiple import statements from the same module."], ["\n Disallows multiple import statements from the same module."]))),
48 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Using a single import statement per module will make the code clearer because you can see everything being imported\n from that module on one line."], ["\n Using a single import statement per module will make the code clearer because you can see everything being imported\n from that module on one line."]))),
49 optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n \"", "\" allows you to import namespaces on separate lines."], ["\n \"", "\" allows you to import namespaces on separate lines."])), OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS),
53 _a[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS] = {
58 optionExamples: [[true, (_b = {}, _b[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS] = true, _b)]],
59 type: "maintainability",
60 typescriptOnly: false,
63 }(Lint.Rules.AbstractRule));
66 walkWorker(ctx, ctx.sourceFile.statements, {
68 namespaceImports: new Set(),
71 function statementIsNamespaceImport(statement) {
72 return !!(statement.importClause !== undefined &&
73 statement.importClause.namedBindings !== undefined &&
74 tsutils_1.isNamespaceImport(statement.importClause.namedBindings));
76 function walkWorker(ctx, statements, seen) {
77 for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) {
78 var statement = statements_1[_i];
79 if (tsutils_1.isImportDeclaration(statement) &&
80 tsutils_1.isLiteralExpression(statement.moduleSpecifier) &&
81 (!statementIsNamespaceImport(statement) ||
82 !ctx.options[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS])) {
83 var text = statement.moduleSpecifier.text;
84 if (seen.imports.has(text)) {
85 ctx.addFailureAtNode(statement, Rule.FAILURE_STRING(text));
87 seen.imports.add(text);
89 else if (tsutils_1.isImportDeclaration(statement) &&
90 tsutils_1.isLiteralExpression(statement.moduleSpecifier) &&
91 statementIsNamespaceImport(statement) &&
92 ctx.options[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS]) {
93 var text = statement.moduleSpecifier.text;
94 if (seen.namespaceImports.has(text)) {
95 ctx.addFailureAtNode(statement, Rule.NAMESPACE_FAILURE_STRING(text));
97 seen.namespaceImports.add(text);
99 if (tsutils_1.isModuleDeclaration(statement) &&
100 statement.body !== undefined &&
101 statement.name.kind === ts.SyntaxKind.StringLiteral) {
102 // If this is a module augmentation, re-use `seen` since those imports could be moved outside.
103 // If this is an ambient module, create a fresh `seen`
104 // because they should have separate imports to avoid becoming augmentations.
105 walkWorker(ctx, statement.body.statements, ts.isExternalModule(ctx.sourceFile)
109 namespaceImports: new Set(),
114 var templateObject_1, templateObject_2, templateObject_3;