-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathexport.js
147 lines (130 loc) · 4.64 KB
/
export.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
import { test, SYNTAX_CASES } from '../utils'
import { RuleTester, linter } from 'eslint'
import semver from 'semver'
var ruleTester = new RuleTester()
, rule = require('rules/export')
ruleTester.run('export', rule, {
valid: [
test({ code: 'import "./malformed.js"' }),
// default
test({ code: 'var foo = "foo"; export default foo;' }),
test({ code: 'export var foo = "foo"; export var bar = "bar";'}),
test({ code: 'export var foo = "foo", bar = "bar";' }),
test({ code: 'export var { foo, bar } = object;' }),
test({ code: 'export var [ foo, bar ] = array;' }),
test({ code: 'export var { foo, bar } = object;' }),
test({ code: 'export var [ foo, bar ] = array;' }),
test({ code: 'let foo; export { foo, foo as bar }' }),
test({ code: 'let bar; export { bar }; export * from "./export-all"' }),
test({ code: 'export * from "./export-all"' }),
test({ code: 'export * from "./does-not-exist"' }),
// #328: "export * from" does not export a default
test({ code: 'export default foo; export * from "./bar"' }),
...SYNTAX_CASES,
],
invalid: [
// multiple defaults
// test({
// code: 'export default foo; export default bar',
// errors: ['Multiple default exports.', 'Multiple default exports.'],
// }),
// test({
// code: 'export default function foo() {}; ' +
// 'export default function bar() {}',
// errors: ['Multiple default exports.', 'Multiple default exports.'],
// }),
// test({
// code: 'export function foo() {}; ' +
// 'export { bar as foo }',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export {foo}; export {foo};',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export {foo}; export {bar as foo};',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export var foo = "foo"; export var foo = "bar";',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export var foo = "foo", foo = "bar";',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
test({
code: 'let foo; export { foo }; export * from "./export-all"',
errors: ['Multiple exports of name \'foo\'.',
'Multiple exports of name \'foo\'.'],
}),
// test({ code: 'export * from "./default-export"'
// , errors: [{ message: 'No named exports found in module ' +
// '\'./default-export\'.'
// , type: 'Literal' }] }),
// note: Espree bump to Acorn 4+ changed this test's error message.
// `npm up` first if it's failing.
test({
code: 'export * from "./malformed.js"',
errors: [{
message: "Parse errors in imported module './malformed.js': 'return' outside of function (1:1)",
type: 'Literal',
}],
}),
// test({
// code: 'export var { foo, bar } = object; export var foo = "bar"',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export var { bar: { foo } } = object; export var foo = "bar"',
// errors: ['Parsing error: Duplicate export \'foo\''],
// }),
// test({
// code: 'export var [ foo, bar ] = array; export var bar = "baz"',
// errors: ['Parsing error: Duplicate export \'bar\''],
// }),
// test({
// code: 'export var [ foo, /*sparse*/, { bar } ] = array; export var bar = "baz"',
// errors: ['Parsing error: Duplicate export \'bar\''],
// }),
// #328: "export * from" does not export a default
test({
code: 'export * from "./default-export"',
errors: [`No named exports found in module './default-export'.`],
}),
],
})
context('Typescript', function () {
// Typescript
const parsers = ['typescript-eslint-parser']
if (semver.satisfies(linter.version, '>5.0.0')) {
parsers.push('@typescript-eslint/parser')
}
parsers.forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}
ruleTester.run('export', rule, {
valid: [
test(Object.assign({
code: `
export const Foo = 1;
export type Foo = number;
`,
}, parserConfig),
test(Object.assign({
code: `
export const Foo = 1;
export interface Foo {}
`,
}, parserConfig),
],
invalid: [],
})
})
})