Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94e0f6e

Browse files
committed
[New]no-rename-default: Forbid importing a default export by a different name
1 parente1bd0ba commit94e0f6e

File tree

33 files changed

+1397
-0
lines changed

33 files changed

+1397
-0
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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+
exportdefaultasyncfunctiongetUsers() {}
16+
```
17+
18+
...this would be valid:
19+
20+
```js
21+
importgetUsersfrom'./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+
importfindUsersfrom'./get-users';
31+
```

‎src/index.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/**
2+
*@fileOverview Rule to warn about importing a default export by different name
3+
*@author James Whitney
4+
*/
5+
6+
importdocsUrlfrom'../docsUrl';
7+
importExportMapBuilderfrom'../exportMap/builder';
8+
importpathfrom'path';
9+
10+
//------------------------------------------------------------------------------
11+
// Rule Definition
12+
//------------------------------------------------------------------------------
13+
14+
/**@type {import('@typescript-eslint/utils').TSESLint.RuleModule} */
15+
construle={
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+
default:false,
30+
type:'boolean',
31+
},
32+
preventRenamingBindings:{
33+
default:true,
34+
type:'boolean',
35+
},
36+
},
37+
additionalProperties:false,
38+
},
39+
],
40+
},
41+
42+
create(context){
43+
const{
44+
commonjs=false,
45+
preventRenamingBindings=true,
46+
}=context.options[0]||{};
47+
48+
functionfindDefaultDestructure(properties){
49+
constfound=properties.find((property)=>{
50+
if(property.key.name==='default'){
51+
returnproperty;
52+
}
53+
});
54+
returnfound;
55+
}
56+
57+
functiongetDefaultExportName(targetNode){
58+
if(targetNode==null){
59+
return;
60+
}
61+
switch(targetNode.type){
62+
case'AssignmentExpression':{
63+
if(!preventRenamingBindings){
64+
// Allow assignments to be renamed when the `preventRenamingBindings`
65+
// option is set to `false`.
66+
//
67+
// export default Foo = 1;
68+
return;
69+
}
70+
returntargetNode.left.name;
71+
}
72+
case'CallExpression':{
73+
const[argumentNode]=targetNode.arguments;
74+
returngetDefaultExportName(argumentNode);
75+
}
76+
case'ClassDeclaration':{
77+
if(targetNode.id&&typeoftargetNode.id.name==='string'){
78+
returntargetNode.id.name;
79+
}
80+
// Here we have an anonymous class. We can skip here.
81+
return;
82+
}
83+
case'FunctionDeclaration':{
84+
returntargetNode.id.name;
85+
}
86+
case'Identifier':{
87+
if(!preventRenamingBindings){
88+
// Allow identifier to be renamed when the `preventRenamingBindings`
89+
// option is set to `false`.
90+
//
91+
// const foo = 'foo';
92+
// export default foo;
93+
return;
94+
}
95+
returntargetNode.name;
96+
}
97+
default:
98+
// This type of node is not handled.
99+
// Returning `undefined` here signifies this and causes the check to
100+
// exit early.
101+
}
102+
}
103+
104+
functiongetDefaultExportNode(exportMap){
105+
constdefaultExportNode=exportMap.exports.get('default');
106+
if(defaultExportNode==null){
107+
return;
108+
}
109+
returndefaultExportNode;
110+
}
111+
112+
functiongetExportMap(source,context){
113+
constexportMap=ExportMapBuilder.get(source.value,context);
114+
if(exportMap==null){
115+
return;
116+
}
117+
if(exportMap.errors.length>0){
118+
exportMap.reportErrors(context,source.value);
119+
return;
120+
}
121+
returnexportMap;
122+
}
123+
124+
functionhandleImport(node){
125+
constexportMap=getExportMap(node.parent.source,context);
126+
if(exportMap==null){
127+
return;
128+
}
129+
130+
constdefaultExportNode=getDefaultExportNode(exportMap);
131+
if(defaultExportNode==null){
132+
return;
133+
}
134+
135+
constdefaultExportName=getDefaultExportName(defaultExportNode.declaration);
136+
if(defaultExportName===undefined){
137+
return;
138+
}
139+
140+
constimportTarget=node.parent.source.value;
141+
constimportBasename=path.basename(exportMap.path);
142+
143+
if(node.type==='ImportDefaultSpecifier'){
144+
constimportName=node.local.name;
145+
146+
if(importName===defaultExportName){
147+
return;
148+
}
149+
150+
context.report({
151+
node,
152+
message:`Caution: \`${importBasename}\` has a default export \`${defaultExportName}\`. This imports \`${defaultExportName}\` as \`${importName}\`. Check if you meant to write \`import${defaultExportName} from '${importTarget}'\` instead.`,
153+
});
154+
155+
return;
156+
}
157+
158+
if(node.type!=='ImportSpecifier'){
159+
return;
160+
}
161+
162+
if(node.imported.name!=='default'){
163+
return;
164+
}
165+
166+
constactualImportedName=node.local.name;
167+
168+
if(actualImportedName===defaultExportName){
169+
return;
170+
}
171+
172+
context.report({
173+
node,
174+
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.`,
175+
});
176+
}
177+
178+
functionhandleRequire(node){
179+
if(
180+
!commonjs
181+
||node.type!=='VariableDeclarator'
182+
||!node.id||!(node.id.type==='Identifier'||node.id.type==='ObjectPattern')
183+
||!node.init||node.init.type!=='CallExpression'
184+
){
185+
return;
186+
}
187+
188+
letdefaultDestructure;
189+
if(node.id.type==='ObjectPattern'){
190+
defaultDestructure=findDefaultDestructure(node.id.properties);
191+
if(defaultDestructure===undefined){
192+
return;
193+
}
194+
}
195+
196+
constcall=node.init;
197+
const[source]=call.arguments;
198+
199+
if(
200+
call.callee.type!=='Identifier'||call.callee.name!=='require'||call.arguments.length!==1
201+
||source.type!=='Literal'
202+
){
203+
return;
204+
}
205+
206+
constexportMap=getExportMap(source,context);
207+
if(exportMap==null){
208+
return;
209+
}
210+
211+
constdefaultExportNode=getDefaultExportNode(exportMap);
212+
if(defaultExportNode==null){
213+
return;
214+
}
215+
216+
constdefaultExportName=getDefaultExportName(defaultExportNode.declaration);
217+
constrequireTarget=source.value;
218+
constrequireBasename=path.basename(exportMap.path);
219+
constrequireName=node.id.type==='Identifier' ?node.id.name :defaultDestructure.value.name;
220+
221+
if(defaultExportName===undefined){
222+
return;
223+
}
224+
225+
if(requireName===defaultExportName){
226+
return;
227+
}
228+
229+
if(node.id.type==='Identifier'){
230+
context.report({
231+
node,
232+
message:`Caution: \`${requireBasename}\` has a default export \`${defaultExportName}\`. This requires \`${defaultExportName}\` as \`${requireName}\`. Check if you meant to write \`const${defaultExportName} = require('${requireTarget}')\` instead.`,
233+
});
234+
return;
235+
}
236+
237+
context.report({
238+
node,
239+
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.`,
240+
});
241+
}
242+
243+
return{
244+
ImportDefaultSpecifier(node){
245+
handleImport(node);
246+
},
247+
ImportSpecifier(node){
248+
handleImport(node);
249+
},
250+
VariableDeclarator(node){
251+
handleRequire(node);
252+
},
253+
};
254+
},
255+
};
256+
257+
module.exports=rule;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exportdefaultasync()=>{};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exportdefault()=>{};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exportdefaultclass{};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exportdefault{};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp