Skip to content

Commit

Permalink
fix(findExports): filtering for named exports (#55)
Browse files Browse the repository at this point in the history
* fix(findExports): filtering for named exports

* chore: update tests

* chore: update tests
  • Loading branch information
antfu committed Jun 29, 2022
1 parent 1f8e8d1 commit df908fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/analyze.ts
Expand Up @@ -127,6 +127,6 @@ export function findExports (code: string): ESMExport[] {
return exports.filter((exp, index, exports) => {
// Prevent multiple exports of same function, only keep latest iteration of signatures
const nextExport = exports[index + 1]
return !nextExport || exp.type !== nextExport.type || exp.name !== nextExport.name
return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name
})
}
60 changes: 34 additions & 26 deletions test/exports.test.ts
@@ -1,8 +1,8 @@
import { describe, it, expect } from 'vitest'
import { findExports } from '../src'
import { ESMExport, findExports } from '../src'

describe('findExports', () => {
const tests = {
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' },
Expand All @@ -15,33 +15,41 @@ describe('findExports', () => {
'export * as foo from "./other"': { type: 'star', specifier: './other', name: 'foo' }
}

describe('findExports', () => {
for (const [input, test] of Object.entries(tests)) {
it(input.replace(/\n/g, '\\n'), () => {
const matches = findExports(input)
expect(matches.length).to.equal(1)
const match = matches[0]
if (test.type) {
expect(match.type).to.eql(test.type)
}
if (test.name) {
expect(match.name).to.eql(test.name)
}
if (test.names) {
expect(match.names).to.deep.eql(test.names)
}
if (test.specifier) {
expect(match.specifier).to.eql(test.specifier)
}
})
}
it('handles multiple exports', () => {
const matches = findExports(`
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)
})
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)
})
})

0 comments on commit df908fd

Please sign in to comment.