From f7dd82dcfd07f87debc3f811023d7a15fe94d4fa Mon Sep 17 00:00:00 2001 From: westhide <1084465086@qq.com> Date: Wed, 27 Jul 2022 13:44:24 +0800 Subject: [PATCH] fix(#59): get export without trailing comma direct --- src/analyze.ts | 4 ++-- test/exports.test.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index d1bc2bc..b4b05e1 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -56,7 +56,7 @@ export const ESM_STATIC_IMPORT_RE = /(?<=\s|^|;)import\s*(["'\s]*(?[\w* export const DYNAMIC_IMPORT_RE = /import\s*\((?(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm export const EXPORT_DECAL_RE = /\bexport\s+(?(async function|function|let|const|var|class))\s+(?[\w$_]+)/g -const EXPORT_NAMED_RE = /\bexport\s+{(?[^}]+)}(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g +const EXPORT_NAMED_RE = /\bexport\s+{(?[^}]+?)(?:[,\s]*)}(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g const EXPORT_STAR_RE = /\bexport\s*(\*)(\s*as\s+(?[\w$_]+)\s+)?\s*(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g @@ -99,7 +99,7 @@ export function findExports (code: string): ESMExport[] { // Find named exports const namedExports = matchAll(EXPORT_NAMED_RE, code, { type: 'named' }) for (const namedExport of namedExports) { - namedExport.names = namedExport.exports.split(/\s*,\s*/g).map(name => name.replace(/^.*?\sas\s/, '').trim()).filter(name => !!name) + namedExport.names = namedExport.exports.split(/\s*,\s*/g).map(name => name.replace(/^.*?\sas\s/, '').trim()) } // Find export default diff --git a/test/exports.test.ts b/test/exports.test.ts index e12ec93..879dfa8 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -9,6 +9,8 @@ describe('findExports', () => { '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'] },