Skip to content

Commit

Permalink
feat: add specifier matcher for findExports (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jan 17, 2022
1 parent 4a8640a commit 5ddeba1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export interface ESMExport {
type: 'declaration' | 'named' | 'default',
code: string
start: number
end: number,
name?: string,
end: number
name?: string
names: string[]
}

Expand All @@ -54,7 +54,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>[^}]+)}/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\s*from\s*["']\s*(?<specifier>.*[@\w_-]+)\s*["'][^\n]*)?/g
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g

export function findStaticImports (code: string): StaticImport[] {
Expand Down
4 changes: 4 additions & 0 deletions test/exports.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('findExports', () => {
'export const useD = () => { return \'d\' }': { name: 'useD', type: 'declaration' },
'export { useB, _useC as useC }': { names: ['useB', 'useC'], type: 'named' },
'export default foo': { type: 'default', name: 'default', names: ['default'] },
'export { default } from "./other"': { type: 'default', name: 'default', names: ['default'], specifier: './other' },
'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 All @@ -27,6 +28,9 @@ describe('findExports', () => {
if (test.names) {
expect(match.names).to.deep.eql(test.names)
}
if (test.specifier) {
expect(match.specifier).to.eql(test.specifier)
}
})
}
})
Expand Down

0 comments on commit 5ddeba1

Please # to comment.