From df908fd509d7357265038b5fec414c41bd3ebd67 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 29 Jun 2022 16:08:06 +0800 Subject: [PATCH] fix(findExports): filtering for named exports (#55) * fix(findExports): filtering for named exports * chore: update tests * chore: update tests --- src/analyze.ts | 2 +- test/exports.test.ts | 60 +++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index 742e342..2172a57 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -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 }) } diff --git a/test/exports.test.ts b/test/exports.test.ts index 02b0f88..c74a21d 100644 --- a/test/exports.test.ts +++ b/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> = { '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' }, @@ -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) }) })