.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noDuplicateImportsRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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 _a, _b;
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);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     Rule.FAILURE_STRING = function (module) {
31         return "Multiple imports from '" + module + "' can be combined into one.";
32     };
33     Rule.NAMESPACE_FAILURE_STRING = function (module) {
34         return "Multiple wildcard imports from the same module, '" + module + "', are prohibited.";
35     };
36     Rule.prototype.apply = function (sourceFile) {
37         var _a;
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]),
42             _a));
43     };
44     /* tslint:disable:object-literal-sort-keys */
45     Rule.metadata = {
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),
50         options: {
51             type: "object",
52             properties: (_a = {},
53                 _a[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS] = {
54                     type: "boolean",
55                 },
56                 _a),
57         },
58         optionExamples: [[true, (_b = {}, _b[OPTION_ALLOW_SEPARATE_NAMESPACE_IMPORTS] = true, _b)]],
59         type: "maintainability",
60         typescriptOnly: false,
61     };
62     return Rule;
63 }(Lint.Rules.AbstractRule));
64 exports.Rule = Rule;
65 function walk(ctx) {
66     walkWorker(ctx, ctx.sourceFile.statements, {
67         imports: new Set(),
68         namespaceImports: new Set(),
69     });
70 }
71 function statementIsNamespaceImport(statement) {
72     return !!(statement.importClause !== undefined &&
73         statement.importClause.namedBindings !== undefined &&
74         tsutils_1.isNamespaceImport(statement.importClause.namedBindings));
75 }
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));
86             }
87             seen.imports.add(text);
88         }
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));
96             }
97             seen.namespaceImports.add(text);
98         }
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)
106                 ? seen
107                 : {
108                     imports: new Set(),
109                     namespaceImports: new Set(),
110                 });
111         }
112     }
113 }
114 var templateObject_1, templateObject_2, templateObject_3;