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

feat(findExports): use acorn tokenizer to filter false positive exports #56

Merged
merged 14 commits into from Aug 3, 2022
11 changes: 11 additions & 0 deletions src/analyze.ts
Expand Up @@ -59,6 +59,7 @@ export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(async function|functi
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\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
const COMMENT_RE = /(\/\*[\s\S]*\*\/)|(\/\/.*)/g

export function findStaticImports (code: string): StaticImport[] {
return matchAll(ESM_STATIC_IMPORT_RE, code, { type: 'static' })
Expand Down Expand Up @@ -93,6 +94,8 @@ export function parseStaticImport (matched: StaticImport): ParsedStaticImport {
}

export function findExports (code: string): ESMExport[] {
// Filter out commented code to eliminate the effect of regular match export
code = filterCommentCode(code)
// Find declarations like export const foo = 'bar'
const declaredExports = matchAll(EXPORT_DECAL_RE, code, { type: 'declaration' })

Expand Down Expand Up @@ -130,3 +133,11 @@ export function findExports (code: string): ESMExport[] {
return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name
})
}

function filterCommentCode (code: string) {
const matchedComments = matchAll(COMMENT_RE, code, { type: 'comment' })
for (const matched of matchedComments) {
code = code.replace(matched.code, '')
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}
return code
}
32 changes: 32 additions & 0 deletions test/exports.test.ts
Expand Up @@ -52,4 +52,36 @@ export { foobar } from 'foo2';
const matches = findExports(code)
expect(matches).to.have.lengthOf(3)
})

it('the commented out export should be filtered out', () => {
const code = `
// export { foo } from 'foo1';
// exports default 'foo';
// export { useB, _useC as useC };
// export function useA () { return 'a' }
// export { default } from "./other"
// export async function foo () {}
// export { foo as default }
//export * from "./other"
//export * as foo from "./other"

/**
* export const a = 123
* export { foo } from 'foo1';
* exports default 'foo'
* export function useA () { return 'a' }
* export { useB, _useC as useC };
*export { default } from "./other"
*export async function foo () {}
* export { foo as default }
* export * from "./other"
export * as foo from "./other"
*/

export { bar } from 'foo2';
export { foobar } from 'foo2';
`
const matches = findExports(code)
expect(matches).to.have.lengthOf(2)
})
})