4 * Copyright 2016 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 fromModulesConfigOptionName = "fromModules";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
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;
40 Rule.prototype.apply = function (sourceFile) {
41 return this.applyWithFunction(sourceFile, walk, this.getRuleOptions(this.ruleArguments));
43 Rule.prototype.isFromModulesConfigOption = function (option) {
44 return typeof option === "object" && option[fromModulesConfigOptionName] !== undefined;
46 Rule.prototype.getRuleOptions = function (options) {
48 var fromModuleConfigOption = options.find(this.isFromModulesConfigOption);
49 if (fromModuleConfigOption !== undefined &&
50 typeof fromModuleConfigOption[fromModulesConfigOptionName] === "string") {
52 _a[fromModulesConfigOptionName] = new RegExp(fromModuleConfigOption[fromModulesConfigOptionName]),
57 _b[fromModulesConfigOptionName] = new RegExp("^\\./|^\\.\\./"),
61 /* tslint:disable:object-literal-sort-keys */
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",
73 _a[fromModulesConfigOptionName] = { type: "string" },
75 required: ["fromModules"],
79 [true, (_b = {}, _b[fromModulesConfigOptionName] = "^palantir-|^_internal-*|^\\./|^\\.\\./", _b)],
81 type: "maintainability",
82 typescriptOnly: false,
84 /* tslint:enable:object-literal-sort-keys */
85 Rule.FAILURE_STRING = "Import of default members from this module is forbidden. Import named member instead";
87 }(Lint.Rules.AbstractRule));
90 if (ctx.sourceFile.isDeclarationFile || !ts.isExternalModule(ctx.sourceFile)) {
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);
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);
118 var templateObject_1;