-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
no-named-as-default.js
45 lines (37 loc) · 1.26 KB
/
no-named-as-default.js
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
import ExportMapBuilder from '../exportMapBuilder';
import importDeclaration from '../importDeclaration';
import docsUrl from '../docsUrl';
module.exports = {
meta: {
type: 'problem',
docs: {
category: 'Helpful warnings',
description: 'Forbid use of exported name as identifier of default export.',
url: docsUrl('no-named-as-default'),
},
schema: [],
},
create(context) {
function checkDefault(nameKey, defaultSpecifier) {
// #566: default is a valid specifier
if (defaultSpecifier[nameKey].name === 'default') { return; }
const declaration = importDeclaration(context);
const imports = ExportMapBuilder.get(declaration.source.value, context);
if (imports == null) { return; }
if (imports.errors.length) {
imports.reportErrors(context, declaration);
return;
}
if (imports.has('default') && imports.has(defaultSpecifier[nameKey].name)) {
context.report(
defaultSpecifier,
`Using exported name '${defaultSpecifier[nameKey].name}' as identifier for default export.`,
);
}
}
return {
ImportDefaultSpecifier: checkDefault.bind(null, 'local'),
ExportDefaultSpecifier: checkDefault.bind(null, 'exported'),
};
},
};