forked from palantir/tslint
/
noDefaultImportRule.ts
144 lines (134 loc) · 6 KB
/
noDefaultImportRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @license
* Copyright 2016 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isImportDeclaration, isNamedImports, isStringLiteral } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
const fromModulesConfigOptionName = "fromModules";
interface RawConfigOptions {
[fromModulesConfigOptionName]: string;
}
interface Options {
[fromModulesConfigOptionName]: RegExp;
}
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-default-import",
description: "Disallows importing default members from certain ES6-style modules.",
descriptionDetails: "Import named members instead.",
rationale: Lint.Utils.dedent`
Named imports/exports [promote clarity](https://github.com/palantir/tslint/issues/1182#issue-151780453).
In addition, current tooling differs on the correct way to handle default imports/exports.
Avoiding them all together can help avoid tooling bugs and conflicts.
The rule supposed to narrow the scope of your changes in the case of monorepo.
Say, you have packages \`A\`, \`B\`, \`C\` and \`utils\`, where \`A\`, \`B\`, \`C\` dependends on \`utils\`,
which is full of default exports.
\`"no-default-export"\` requires you to remove default _export_ from \`utils\`, which leads to changes
in packages \`A\`, \`B\`, \`C\`. It's harder to get merged bigger changeset by various reasons (harder to get your code approved
due to a number of required reviewers; longer build time due to a number of affected packages)
and could result in ignored \`"no-default-export"\` rule in \`utils'\`.
Unlike \`"no-default-export"\`, the rule requires you to replace default _import_ with named only in \`A\` you work on,
and \`utils\` you import from.`,
optionsDescription: "optionsDescription",
options: {
type: "array",
items: {
type: "object",
properties: {
[fromModulesConfigOptionName]: { type: "string" },
},
required: ["fromModules"],
},
},
optionExamples: [
[true, { [fromModulesConfigOptionName]: "^palantir-|^_internal-*|^\\./|^\\.\\./" }],
],
type: "maintainability",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING =
"Import of default members from this module is forbidden. Import named member instead";
public static getNamedDefaultImport(namedBindings: ts.NamedImports): ts.Identifier | null {
for (const importSpecifier of namedBindings.elements) {
if (
importSpecifier.propertyName !== undefined &&
importSpecifier.propertyName.text === "default"
) {
return importSpecifier.propertyName;
}
}
return null;
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, this.getRuleOptions(this.ruleArguments));
}
private isFromModulesConfigOption(
option: boolean | RawConfigOptions,
): option is RawConfigOptions {
return typeof option === "object" && option[fromModulesConfigOptionName] !== undefined;
}
private getRuleOptions(options: ReadonlyArray<boolean | RawConfigOptions>): Options {
const fromModuleConfigOption = options.find<RawConfigOptions>(
this.isFromModulesConfigOption,
);
if (
fromModuleConfigOption !== undefined &&
typeof fromModuleConfigOption[fromModulesConfigOptionName] === "string"
) {
return {
[fromModulesConfigOptionName]: new RegExp(
fromModuleConfigOption[fromModulesConfigOptionName],
),
};
}
return {
[fromModulesConfigOptionName]: new RegExp("^\\./|^\\.\\./"),
};
}
}
function walk(ctx: Lint.WalkContext<Options>) {
if (ctx.sourceFile.isDeclarationFile || !ts.isExternalModule(ctx.sourceFile)) {
return;
}
for (const statement of ctx.sourceFile.statements) {
if (isImportDeclaration(statement)) {
const { importClause, moduleSpecifier } = statement;
if (
importClause !== undefined &&
isStringLiteral(moduleSpecifier) &&
ctx.options[fromModulesConfigOptionName].test(moduleSpecifier.text)
) {
// module name matches specified in rule config
if (importClause.name !== undefined) {
// `import Foo...` syntax
const defaultImportedName = importClause.name;
ctx.addFailureAtNode(defaultImportedName, Rule.FAILURE_STRING);
} else if (
importClause.namedBindings !== undefined &&
isNamedImports(importClause.namedBindings)
) {
// `import { default...` syntax
const defaultMember = Rule.getNamedDefaultImport(importClause.namedBindings);
if (defaultMember !== null) {
ctx.addFailureAtNode(defaultMember, Rule.FAILURE_STRING);
}
}
}
}
}
}