Skip to content

Commit df908fd

Browse files
authoredJun 29, 2022
fix(findExports): filtering for named exports (#55)
* fix(findExports): filtering for named exports * chore: update tests * chore: update tests
1 parent 1f8e8d1 commit df908fd

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed
 

‎src/analyze.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ export function findExports (code: string): ESMExport[] {
127127
return exports.filter((exp, index, exports) => {
128128
// Prevent multiple exports of same function, only keep latest iteration of signatures
129129
const nextExport = exports[index + 1]
130-
return !nextExport || exp.type !== nextExport.type || exp.name !== nextExport.name
130+
return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name
131131
})
132132
}

‎test/exports.test.ts

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, it, expect } from 'vitest'
2-
import { findExports } from '../src'
2+
import { ESMExport, findExports } from '../src'
33

44
describe('findExports', () => {
5-
const tests = {
5+
const tests: Record<string, Partial<ESMExport>> = {
66
'export function useA () { return \'a\' }': { name: 'useA', type: 'declaration' },
77
'export const useD = () => { return \'d\' }': { name: 'useD', type: 'declaration' },
88
'export { useB, _useC as useC }': { names: ['useB', 'useC'], type: 'named' },
@@ -15,33 +15,41 @@ describe('findExports', () => {
1515
'export * as foo from "./other"': { type: 'star', specifier: './other', name: 'foo' }
1616
}
1717

18-
describe('findExports', () => {
19-
for (const [input, test] of Object.entries(tests)) {
20-
it(input.replace(/\n/g, '\\n'), () => {
21-
const matches = findExports(input)
22-
expect(matches.length).to.equal(1)
23-
const match = matches[0]
24-
if (test.type) {
25-
expect(match.type).to.eql(test.type)
26-
}
27-
if (test.name) {
28-
expect(match.name).to.eql(test.name)
29-
}
30-
if (test.names) {
31-
expect(match.names).to.deep.eql(test.names)
32-
}
33-
if (test.specifier) {
34-
expect(match.specifier).to.eql(test.specifier)
35-
}
36-
})
37-
}
38-
it('handles multiple exports', () => {
39-
const matches = findExports(`
18+
for (const [input, test] of Object.entries(tests)) {
19+
it(input.replace(/\n/g, '\\n'), () => {
20+
const matches = findExports(input)
21+
expect(matches.length).toEqual(1)
22+
const match = matches[0]
23+
if (test.type) {
24+
expect(match.type).toEqual(test.type)
25+
}
26+
if (test.name) {
27+
expect(match.name).toEqual(test.name)
28+
}
29+
if (test.names) {
30+
expect(match.names).toEqual(test.names)
31+
}
32+
if (test.specifier) {
33+
expect(match.specifier).toEqual(test.specifier)
34+
}
35+
})
36+
}
37+
it('handles multiple exports', () => {
38+
const matches = findExports(`
4039
export { useTestMe1 } from "@/test/foo1";
4140
export { useTestMe2 } from "@/test/foo2";
4241
export { useTestMe3 } from "@/test/foo3";
4342
`)
44-
expect(matches.length).to.eql(3)
45-
})
43+
expect(matches.length).to.eql(3)
44+
})
45+
46+
it('works with multiple named exports', () => {
47+
const code = `
48+
export { foo } from 'foo1';
49+
export { bar } from 'foo2';
50+
export { foobar } from 'foo2';
51+
`
52+
const matches = findExports(code)
53+
expect(matches).to.have.lengthOf(3)
4654
})
4755
})

0 commit comments

Comments
 (0)
Please sign in to comment.