/
exports.test.ts
193 lines (184 loc) · 6.21 KB
/
exports.test.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { join } from 'path'
import { describe, it, expect } from 'vitest'
import { ESMExport, findExports, findExportNames, resolveModuleExportNames } from '../src'
describe('findExports', () => {
const tests: Record<string, Partial<ESMExport>> = {
'export function useA () { return \'a\' }': { name: 'useA', type: 'declaration' },
'export const useD = () => { return \'d\' }': { name: 'useD', type: 'declaration' },
'export { useB, _useC as useC }': { names: ['useB', 'useC'], type: 'named' },
'export default foo': { type: 'default', name: 'default', names: ['default'] },
'export { default } from "./other"': { type: 'default', name: 'default', names: ['default'], specifier: './other' },
'export { default , } from "./other"': { type: 'default', name: 'default', names: ['default'], specifier: './other' },
'export { useA , } from "./path"': { type: 'named', name: 'useA', names: ['useA'], specifier: './path' },
'export { useA , useB , } from "./path"': { type: 'named', names: ['useA', 'useB'], specifier: './path' },
'export async function foo ()': { type: 'declaration', names: ['foo'] },
'export const $foo = () => {}': { type: 'declaration', names: ['$foo'] },
'export { foo as default }': { type: 'default', name: 'default', names: ['default'] },
'export * from "./other"': { type: 'star', specifier: './other' },
'export * as foo from "./other"': { type: 'star', specifier: './other', name: 'foo' },
// eslint-disable-next-line no-template-curly-in-string
'const a = `<div${JSON.stringify({ class: 42 })}>`;\nexport default true;': { type: 'default', name: 'default', names: ['default'] },
'export const enum foo { a = \'xx\' }': { type: 'declaration', names: ['foo'] },
'export enum bar { a = \'xx\' }': { type: 'declaration', names: ['bar'] }
}
for (const [input, test] of Object.entries(tests)) {
it(input.replace(/\n/g, '\\n'), () => {
const matches = findExports(input)
expect(matches.length).toEqual(1)
const match = matches[0]
if (test.type) {
expect(match.type).toEqual(test.type)
}
if (test.name) {
expect(match.name).toEqual(test.name)
}
if (test.names) {
expect(match.names).toEqual(test.names)
}
if (test.specifier) {
expect(match.specifier).toEqual(test.specifier)
}
})
}
it('handles multiple exports', () => {
const matches = findExports(`
export { useTestMe1 } from "@/test/foo1";
export { useTestMe2 } from "@/test/foo2";
export { useTestMe3 } from "@/test/foo3";
`)
expect(matches.length).to.eql(3)
})
it('works with multiple named exports', () => {
const code = `
export { foo } from 'foo1';
export { bar } from 'foo2';
export { foobar } from 'foo2';
`
const matches = findExports(code)
expect(matches).to.have.lengthOf(3)
})
it('the commented out export should be filtered out', () => {
const code = `
// export { foo } from 'foo1';
// exports default 'foo';
// export { useB, _useC as useC };
// export function useA () { return 'a' }
// export { default } from "./other"
// export async function foo () {}
// export { foo as default }
//export * from "./other"
//export * as foo from "./other"
/**
* export const a = 123
* export { foo } from 'foo1';
* exports default 'foo'
* export function useA () { return 'a' }
* export { useB, _useC as useC };
*export { default } from "./other"
*export async function foo () {}
* export { foo as default }
* export * from "./other"
export * as foo from "./other"
*/
export { bar } from 'foo2';
export { foobar } from 'foo2';
`
const matches = findExports(code)
expect(matches).to.have.lengthOf(2)
})
it('export in string', () => {
const tests: string[] = [
'export function useA () { return \'a\' }',
'export const useD = () => { return \'d\' }',
'export { useB, _useC as useC }',
'export default foo',
'export { default } from "./other"',
'export async function foo ()',
'export const $foo = () => {}',
'export { foo as default }',
'export * from "./other"',
'export * as foo from "./other"'
]
const code = tests.reduce((codeStr, statement, idx) => {
codeStr = `
${codeStr}
const test${idx}0 = "${statement}"
const test${idx}1 = \`
test1
${statement}
test2
\`
`
return codeStr
}, 'export { bar } from \'foo2\'; \n export { foobar } from \'foo2\';')
const matches = findExports(code)
expect(matches).to.have.lengthOf(2)
})
})
describe('fineExportNames', () => {
it('findExportNames', () => {
expect(findExportNames(`
export const foo = 'bar'
export { bar, baz }
export default something
`)).toMatchInlineSnapshot(`
[
"foo",
"bar",
"baz",
"default",
]
`)
})
})
describe('resolveModuleExportNames', () => {
it('direct exports', async () => {
expect(await resolveModuleExportNames('pathe')).toMatchInlineSnapshot(`
[
"basename",
"delimiter",
"dirname",
"extname",
"format",
"isAbsolute",
"join",
"normalize",
"normalizeString",
"parse",
"relative",
"resolve",
"sep",
"toNamespacedPath",
]
`)
})
it('star exports', async () => {
expect(await resolveModuleExportNames(new URL('./fixture/exports.mjs', import.meta.url).toString())).toMatchInlineSnapshot(`
[
"foo",
"_resolve",
"basename",
"delimiter",
"dirname",
"extname",
"format",
"isAbsolute",
"join",
"normalize",
"normalizeString",
"parse",
"relative",
"resolve",
"sep",
"toNamespacedPath",
]
`)
})
it('multiple inline', () => {
const code = `
export { foo } from 'foo1';export { bar } from 'foo2';export * as foobar from 'foo2';
`
const matches = findExports(code)
expect(matches).to.have.lengthOf(3)
})
})