Skip to content

Commit ee3fd7e

Browse files
committed
[New] no-rename-default: Forbid importing a default export by a different name
1 parent e1bd0ba commit ee3fd7e

File tree

11 files changed

+489
-0
lines changed

11 files changed

+489
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88

99
### Added
1010
- [`dynamic-import-chunkname`]: add `allowEmpty` option to allow empty leading comments ([#2942], thanks [@JiangWeixian])
11+
- [`no-rename-default`]: Forbid importing a default export by a different name ([#3006], thanks [@whitneyit])
1112

1213
### Changed
1314
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
3737
| [no-mutable-exports](docs/rules/no-mutable-exports.md) | Forbid the use of mutable exports with `var` or `let`. | | | | | | |
3838
| [no-named-as-default](docs/rules/no-named-as-default.md) | Forbid use of exported name as identifier of default export. | | ☑️ 🚸 | | | | |
3939
| [no-named-as-default-member](docs/rules/no-named-as-default-member.md) | Forbid use of exported name as property of default export. | | ☑️ 🚸 | | | | |
40+
| [no-rename-default](docs/rules/no-rename-default.md) | Forbid importing a default export by a different name. | | 🚸 | | | | |
4041
| [no-unused-modules](docs/rules/no-unused-modules.md) | Forbid modules without exports, or exports without matching import in another module. | | | | | | |
4142

4243
### Module systems

config/warnings.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
rules: {
88
'import/no-named-as-default': 1,
99
'import/no-named-as-default-member': 1,
10+
'import/no-rename-default': 1,
1011
'import/no-duplicates': 1,
1112
},
1213
};

docs/rules/no-rename-default.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# import/no-rename-default
2+
3+
⚠️ This rule _warns_ in the 🚸 `warnings` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Prohibit importing a default export by another name.
8+
9+
## Rule Details
10+
11+
Given:
12+
13+
```js
14+
// api/get-users.js
15+
export default async function getUsers() {}
16+
```
17+
18+
...this would be valid:
19+
20+
```js
21+
import getUsers from './api/get-users.js';
22+
```
23+
24+
...and the following would be reported:
25+
26+
```js
27+
// Caution: `get-users.js` has a default export `getUsers`.
28+
// This imports `getUsers` as `findUsers`.
29+
// Check if you meant to write `import getUsers from './api/get-users'` instead.
30+
import findUsers from './get-users';
31+
```

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const rules = {
2020
'no-named-as-default': require('./rules/no-named-as-default'),
2121
'no-named-as-default-member': require('./rules/no-named-as-default-member'),
2222
'no-anonymous-default-export': require('./rules/no-anonymous-default-export'),
23+
'no-rename-default': require('./rules/no-rename-default'),
2324
'no-unused-modules': require('./rules/no-unused-modules'),
2425

2526
'no-commonjs': require('./rules/no-commonjs'),

src/rules/no-rename-default.js

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/**
2+
* @fileOverview Rule to warn about importing a default export by different name
3+
* @author James Whitney
4+
*/
5+
6+
import docsUrl from '../docsUrl';
7+
import ExportMapBuilder from '../exportMap/builder';
8+
import path from 'path';
9+
10+
//------------------------------------------------------------------------------
11+
// Rule Definition
12+
//------------------------------------------------------------------------------
13+
14+
/** @type {import('@typescript-eslint/utils').TSESLint.RuleModule} */
15+
const rule = {
16+
meta: {
17+
type: 'suggestion',
18+
docs: {
19+
category: 'Helpful warnings',
20+
description: 'Forbid importing a default export by a different name.',
21+
recommended: false,
22+
url: docsUrl('no-named-as-default'),
23+
},
24+
schema: [
25+
{
26+
type: 'object',
27+
properties: {
28+
commonjs: {
29+
type: 'boolean',
30+
},
31+
},
32+
additionalProperties: false,
33+
},
34+
],
35+
},
36+
37+
create(context) {
38+
function findDefaultDestructure(properties) {
39+
const found = properties.find((property) => {
40+
if (property.key.name === 'default') {
41+
return property;
42+
}
43+
});
44+
return found;
45+
}
46+
47+
function getDefaultExportName(defaultExportNode) {
48+
return defaultExportNode.declaration.name;
49+
}
50+
51+
function getDefaultExportNode(exportMap) {
52+
const defaultExportNode = exportMap.exports.get('default');
53+
if (defaultExportNode == null) {
54+
return;
55+
}
56+
return defaultExportNode;
57+
}
58+
59+
function getExportMap(source, context) {
60+
const exportMap = ExportMapBuilder.get(source.value, context);
61+
if (exportMap == null) {
62+
return;
63+
}
64+
if (exportMap.errors.length > 0) {
65+
exportMap.reportErrors(context, source.value);
66+
return;
67+
}
68+
return exportMap;
69+
}
70+
71+
function handleImport(node) {
72+
const exportMap = getExportMap(node.parent.source, context);
73+
if (exportMap == null) {
74+
return;
75+
}
76+
77+
const defaultExportNode = getDefaultExportNode(exportMap);
78+
if (defaultExportNode == null) {
79+
return;
80+
}
81+
82+
const defaultExportName = getDefaultExportName(defaultExportNode);
83+
84+
if (defaultExportName === undefined) {
85+
return;
86+
}
87+
88+
const importTarget = node.parent.source.value;
89+
const importBasename = path.basename(exportMap.path);
90+
91+
if (node.type === 'ImportDefaultSpecifier') {
92+
const importName = node.local.name;
93+
94+
if (importName === defaultExportName) {
95+
return;
96+
}
97+
98+
context.report({
99+
node,
100+
message: `Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${importName}\`. Check if you meant to write \`import ${defaultExportName} from '${importTarget}'\` instead.`,
101+
});
102+
103+
return;
104+
}
105+
106+
if (node.type !== 'ImportSpecifier') {
107+
return;
108+
}
109+
110+
if (node.imported.name !== 'default') {
111+
return;
112+
}
113+
114+
const actualImportedName = node.local.name;
115+
116+
if (actualImportedName === defaultExportName) {
117+
return;
118+
}
119+
120+
context.report({
121+
node,
122+
message: `Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${actualImportedName}\`. Check if you meant to write \`import { default as ${defaultExportName} } from '${importTarget}'\` instead.`,
123+
});
124+
}
125+
126+
function handleRequire(node) {
127+
const options = context.options[0] || {};
128+
129+
if (
130+
!options.commonjs
131+
|| node.type !== 'VariableDeclarator'
132+
|| !node.id || !(node.id.type === 'Identifier' || node.id.type === 'ObjectPattern')
133+
|| !node.init || node.init.type !== 'CallExpression'
134+
) {
135+
return;
136+
}
137+
138+
let defaultDestructure;
139+
if (node.id.type === 'ObjectPattern') {
140+
defaultDestructure = findDefaultDestructure(node.id.properties);
141+
if (defaultDestructure === undefined) {
142+
return;
143+
}
144+
}
145+
146+
const call = node.init;
147+
const [source] = call.arguments;
148+
149+
if (
150+
call.callee.type !== 'Identifier' || call.callee.name !== 'require' || call.arguments.length !== 1
151+
|| source.type !== 'Literal'
152+
) {
153+
return;
154+
}
155+
156+
const exportMap = getExportMap(source, context);
157+
if (exportMap == null) {
158+
return;
159+
}
160+
161+
const defaultExportNode = getDefaultExportNode(exportMap);
162+
if (defaultExportNode == null) {
163+
return;
164+
}
165+
166+
const defaultExportName = getDefaultExportName(defaultExportNode);
167+
const requireTarget = source.value;
168+
const requireBasename = path.basename(exportMap.path);
169+
const requireName = node.id.type === 'Identifier' ? node.id.name : defaultDestructure.value.name;
170+
171+
if (defaultExportName === undefined) {
172+
return;
173+
}
174+
175+
if (requireName === defaultExportName) {
176+
return;
177+
}
178+
179+
if (node.id.type === 'Identifier') {
180+
context.report({
181+
node,
182+
message: `Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const ${defaultExportName} = require('${requireTarget}')\` instead.`,
183+
});
184+
return;
185+
}
186+
187+
context.report({
188+
node,
189+
message: `Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const { default: ${defaultExportName} } = require('${requireTarget}')\` instead.`,
190+
});
191+
}
192+
193+
return {
194+
ImportDefaultSpecifier(node) {
195+
handleImport(node);
196+
},
197+
ImportSpecifier(node) {
198+
handleImport(node);
199+
},
200+
VariableDeclarator(node) {
201+
handleRequire(node);
202+
},
203+
};
204+
},
205+
};
206+
207+
module.exports = rule;

tests/files/no-rename-default/anon.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const barNamed1 = 'bar-named-1';
2+
export const barNamed2 = 'bar-named-2';
3+
4+
const bar = 'bar';
5+
6+
export default bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const fooNamed1 = 'foo-named-1';
2+
export const fooNamed2 = 'foo-named-2';
3+
4+
const foo = 'foo';
5+
6+
export default foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 123;

0 commit comments

Comments
 (0)