-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
no-named-as-default.js
167 lines (154 loc) · 5.23 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { test, testVersion, SYNTAX_CASES, parsers } from '../utils';
import { RuleTester } from '../rule-tester';
const ruleTester = new RuleTester();
const rule = require('rules/no-named-as-default');
ruleTester.run('no-named-as-default', rule, {
valid: [].concat(
test({ code: 'import "./malformed.js"' }),
test({ code: 'import bar, { foo } from "./bar";' }),
test({ code: 'import bar, { foo } from "./empty-folder";' }),
// es7
test({
code: 'export bar, { foo } from "./bar";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export bar from "./bar";',
parser: parsers.BABEL_OLD,
}),
// #566: don't false-positive on `default` itself
test({
code: 'export default from "./bar";',
parser: parsers.BABEL_OLD,
}),
// es2022: Arbitrary module namespae identifier names
testVersion('>= 8.7', () => ({
code: 'import bar, { foo } from "./export-default-string-and-named"',
parserOptions: { ecmaVersion: 2022 },
})),
// #1594: Allow importing as default if object is exported both as default and named
test({ code: 'import something from "./no-named-as-default/re-exports.js";' }),
test({
code: 'import { something } from "./no-named-as-default/re-exports.js";',
}),
test({
code: 'import myOwnNameForVariable from "./no-named-as-default/exports.js";',
}),
test({
code: 'import { variable } from "./no-named-as-default/exports.js";',
}),
test({
code: 'import variable from "./no-named-as-default/misleading-re-exports.js";',
}),
test({
// incorrect import
code: 'import foobar from "./no-named-as-default/no-default-export.js";',
}),
// same tests, but for exports
test({
code: 'export something from "./no-named-as-default/re-exports.js";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export { something } from "./no-named-as-default/re-exports.js";',
}),
test({
code: 'export myOwnNameForVariable from "./no-named-as-default/exports.js";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export { variable } from "./no-named-as-default/exports.js";',
}),
test({
code: 'export variable from "./no-named-as-default/misleading-re-exports.js";',
parser: parsers.BABEL_OLD,
}),
test({
code: 'export foobar from "./no-named-as-default/no-default-export.js";',
parser: parsers.BABEL_OLD,
}),
...SYNTAX_CASES,
),
invalid: [].concat(
test({
code: 'import foo from "./bar";',
errors: [{
message: 'Using exported name \'foo\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
}),
test({
code: 'import foo, { foo as bar } from "./bar";',
errors: [{
message: 'Using exported name \'foo\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
}),
// es7
test({
code: 'export foo from "./bar";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ExportDefaultSpecifier',
}],
}),
test({
code: 'export foo, { foo as bar } from "./bar";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'foo\' as identifier for default export.',
type: 'ExportDefaultSpecifier',
}],
}),
test({
code: 'import foo from "./malformed.js"',
errors: [{
message: "Parse errors in imported module './malformed.js': 'return' outside of function (1:1)",
type: 'Literal',
}],
}),
// es2022: Arbitrary module namespae identifier names
testVersion('>= 8.7', () => ({
code: 'import foo from "./export-default-string-and-named"',
errors: [{
message: 'Using exported name \'foo\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: 'import foo, { foo as bar } from "./export-default-string-and-named"',
errors: [{
message: 'Using exported name \'foo\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
parserOptions: { ecmaVersion: 2022 },
})),
// #1594: Allow importing as default if object is exported both as default and named
test({
code: 'import something from "./no-named-as-default/misleading-re-exports.js";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'something\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
}),
// The only cases that are not covered by the fix
test({
code: 'import variable from "./no-named-as-default/exports.js";',
errors: [{
message: 'Using exported name \'variable\' as identifier for default import.',
type: 'ImportDefaultSpecifier',
}],
}),
test({
code: 'export variable from "./no-named-as-default/exports.js";',
parser: parsers.BABEL_OLD,
errors: [{
message: 'Using exported name \'variable\' as identifier for default export.',
type: 'ExportDefaultSpecifier',
}],
}),
),
});