.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noDefaultImportRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2016 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 fromModulesConfigOptionName = "fromModules";
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.getNamedDefaultImport = function (namedBindings) {
31         for (var _i = 0, _a = namedBindings.elements; _i < _a.length; _i++) {
32             var importSpecifier = _a[_i];
33             if (importSpecifier.propertyName !== undefined &&
34                 importSpecifier.propertyName.text === "default") {
35                 return importSpecifier.propertyName;
36             }
37         }
38         return null;
39     };
40     Rule.prototype.apply = function (sourceFile) {
41         return this.applyWithFunction(sourceFile, walk, this.getRuleOptions(this.ruleArguments));
42     };
43     Rule.prototype.isFromModulesConfigOption = function (option) {
44         return typeof option === "object" && option[fromModulesConfigOptionName] !== undefined;
45     };
46     Rule.prototype.getRuleOptions = function (options) {
47         var _a, _b;
48         var fromModuleConfigOption = options.find(this.isFromModulesConfigOption);
49         if (fromModuleConfigOption !== undefined &&
50             typeof fromModuleConfigOption[fromModulesConfigOptionName] === "string") {
51             return _a = {},
52                 _a[fromModulesConfigOptionName] = new RegExp(fromModuleConfigOption[fromModulesConfigOptionName]),
53                 _a;
54         }
55         else {
56             return _b = {},
57                 _b[fromModulesConfigOptionName] = new RegExp("^\\./|^\\.\\./"),
58                 _b;
59         }
60     };
61     /* tslint:disable:object-literal-sort-keys */
62     Rule.metadata = {
63         ruleName: "no-default-import",
64         description: "Disallows importing default members from certain ES6-style modules.",
65         descriptionDetails: "Import named members instead.",
66         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Named imports/exports [promote clarity](https://github.com/palantir/tslint/issues/1182#issue-151780453).\n            In addition, current tooling differs on the correct way to handle default imports/exports.\n            Avoiding them all together can help avoid tooling bugs and conflicts.\n\n            The rule supposed to narrow the scope of your changes in the case of monorepo.\n            Say, you have packages `A`, `B`, `C` and `utils`, where `A`, `B`, `C` dependends on `utils`,\n            which is full of default exports.\n            `\"no-default-export\"` requires you to remove default _export_ from `utils`, which leads to changes\n            in packages `A`, `B`, `C`. It's harder to get merged bigger changeset by various reasons (harder to get your code approved\n            due to a number of required reviewers; longer build time due to a number of affected packages)\n            and could result in ignored `\"no-default-export\"` rule in `utils'`.\n\n            Unlike `\"no-default-export\"`, the rule requires you to replace default _import_ with named only in `A` you work on,\n            and `utils` you import from."], ["\n            Named imports/exports [promote clarity](https://github.com/palantir/tslint/issues/1182#issue-151780453).\n            In addition, current tooling differs on the correct way to handle default imports/exports.\n            Avoiding them all together can help avoid tooling bugs and conflicts.\n\n            The rule supposed to narrow the scope of your changes in the case of monorepo.\n            Say, you have packages \\`A\\`, \\`B\\`, \\`C\\` and \\`utils\\`, where \\`A\\`, \\`B\\`, \\`C\\` dependends on \\`utils\\`,\n            which is full of default exports.\n            \\`\"no-default-export\"\\` requires you to remove default _export_ from \\`utils\\`, which leads to changes\n            in packages \\`A\\`, \\`B\\`, \\`C\\`. It's harder to get merged bigger changeset by various reasons (harder to get your code approved\n            due to a number of required reviewers; longer build time due to a number of affected packages)\n            and could result in ignored \\`\"no-default-export\"\\` rule in \\`utils'\\`.\n\n            Unlike \\`\"no-default-export\"\\`, the rule requires you to replace default _import_ with named only in \\`A\\` you work on,\n            and \\`utils\\` you import from."]))),
67         optionsDescription: "optionsDescription",
68         options: {
69             type: "array",
70             items: {
71                 type: "object",
72                 properties: (_a = {},
73                     _a[fromModulesConfigOptionName] = { type: "string" },
74                     _a),
75                 required: ["fromModules"],
76             },
77         },
78         optionExamples: [
79             [true, (_b = {}, _b[fromModulesConfigOptionName] = "^palantir-|^_internal-*|^\\./|^\\.\\./", _b)],
80         ],
81         type: "maintainability",
82         typescriptOnly: false,
83     };
84     /* tslint:enable:object-literal-sort-keys */
85     Rule.FAILURE_STRING = "Import of default members from this module is forbidden. Import named member instead";
86     return Rule;
87 }(Lint.Rules.AbstractRule));
88 exports.Rule = Rule;
89 function walk(ctx) {
90     if (ctx.sourceFile.isDeclarationFile || !ts.isExternalModule(ctx.sourceFile)) {
91         return;
92     }
93     for (var _i = 0, _a = ctx.sourceFile.statements; _i < _a.length; _i++) {
94         var statement = _a[_i];
95         if (tsutils_1.isImportDeclaration(statement)) {
96             var importClause = statement.importClause, moduleSpecifier = statement.moduleSpecifier;
97             if (importClause !== undefined &&
98                 tsutils_1.isStringLiteral(moduleSpecifier) &&
99                 ctx.options[fromModulesConfigOptionName].test(moduleSpecifier.text)) {
100                 // module name matches specified in rule config
101                 if (importClause.name !== undefined) {
102                     // `import Foo...` syntax
103                     var defaultImportedName = importClause.name;
104                     ctx.addFailureAtNode(defaultImportedName, Rule.FAILURE_STRING);
105                 }
106                 else if (importClause.namedBindings !== undefined &&
107                     tsutils_1.isNamedImports(importClause.namedBindings)) {
108                     // `import { default...` syntax
109                     var defaultMember = Rule.getNamedDefaultImport(importClause.namedBindings);
110                     if (defaultMember !== null) {
111                         ctx.addFailureAtNode(defaultMember, Rule.FAILURE_STRING);
112                     }
113                 }
114             }
115         }
116     }
117 }
118 var templateObject_1;