-
Notifications
You must be signed in to change notification settings - Fork 31
/
exports.test.ts
306 lines (293 loc) · 8.26 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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"] },
"export const { a, b } = foo": { type: "named", names: ["a", "b"] },
"export const [ a, b ] = foo": { type: "named", names: ["a", "b"] },
"export const [\na\n, b ] = foo": { type: "named", names: ["a", "b"] },
"export const [ a:b,\nc = 1] = foo": { type: "named", names: ["b", "c"] },
};
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"',
];
// eslint-disable-next-line unicorn/no-array-reduce
const code = tests.reduce((codeString, statement, index) => {
codeString = `
${codeString}
const test${index}0 = "${statement}"
const test${index}1 = \`
test1
${statement}
test2
\`
`;
return codeString;
}, "export { bar } from 'foo2'; \n export { foobar } from 'foo2';");
const matches = findExports(code);
expect(matches).to.have.lengthOf(2);
});
it("works with line feed named exports", () => {
const code = `
export {
_foo as foo,
bar,
};
`;
const matches = findExports(code);
expect(matches[0].names).toEqual(["foo", "bar"]);
});
// https://github.com/nuxt/framework/issues/7658
it("works the same with or without comment", () => {
const code1 = `
export default function useMain() {}
`;
const code2 = `
export default function useMain() {}
// export default function useMain() {}
`;
const matches1 = findExports(code1);
const matches2 = findExports(code2);
expect(matches1).toHaveLength(1);
expect(matches1[0].name).toEqual("default");
expect(matches2).toEqual(matches1);
});
it("ignore export type", () => {
const code = `
export { type AType, type B as BType, foo } from 'foo'
`;
const matches = findExports(code);
expect(matches).toHaveLength(1);
expect(matches[0].names).toHaveLength(1);
expect(matches[0].names[0]).toEqual("foo");
expect(matches[0].name).toEqual("foo");
});
});
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",
"default",
"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",
"default",
"delimiter",
"dirname",
"extname",
"format",
"isAbsolute",
"join",
"normalize",
"normalizeString",
"parse",
"relative",
"resolve",
"sep",
"toNamespacedPath",
]
`);
});
it("star exports with package", async () => {
expect(
await resolveModuleExportNames(
new URL("fixture/package/exports.mjs", import.meta.url).toString()
)
).toMatchInlineSnapshot(`
[
"StaticRouter",
"unstable_StaticRouterProvider",
"unstable_createStaticRouter",
]
`);
});
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);
});
});