Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(findExports): get exports with trailing comma #61

Merged
merged 1 commit into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/analyze.ts
Expand Up @@ -56,7 +56,7 @@ export const ESM_STATIC_IMPORT_RE = /(?<=\s|^|;)import\s*(["'\s]*(?<imports>[\w*
export const DYNAMIC_IMPORT_RE = /import\s*\((?<expression>(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm

export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(async function|function|let|const|var|class))\s+(?<name>[\w$_]+)/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+?)(?:[,\s]*)}(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_STAR_RE = /\bexport\s*(\*)(\s*as\s+(?<name>[\w$_]+)\s+)?\s*(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/exports.test.ts
Expand Up @@ -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'] },
Expand Down