diff --git a/src/analyze.ts b/src/analyze.ts index 280c0c4..41b13ee 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -130,14 +130,14 @@ export function findExports (code: string): ESMExport[] { if (!exports.length) { return [] } - const exportLocations = _getExportLocations(code) - if (!exportLocations.length) { + const exportLocations = _tryGetExportLocations(code) + if (exportLocations && !exportLocations.length) { return [] } return exports.filter((exp, index, exports) => { // Filter false positive export matches - if (!_isExportStatement(exportLocations, exp)) { + if (exportLocations && !_isExportStatement(exportLocations, exp)) { return false } // Prevent multiple exports of same function, only keep latest iteration of signatures @@ -168,6 +168,14 @@ function _isExportStatement (exportsLocation: TokenLocation[], exp: ESMExport) { return exportsLocation.some(location => exp.start <= location.start && exp.end >= location.end) } +function _tryGetExportLocations (code: string) { + try { + return _getExportLocations(code) + } catch (err) { + return null + } +} + function _getExportLocations (code: string) { const tokens = tokenizer(code, { ecmaVersion: 'latest', diff --git a/test/exports.test.ts b/test/exports.test.ts index 3577472..c3750d2 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -15,7 +15,9 @@ describe('findExports', () => { 'export const $foo = () => {}': { type: 'declaration', names: ['$foo'] }, 'export { foo as default }': { type: 'default', name: 'default', names: ['default'] }, 'export * from "./other"': { type: 'star', specifier: './other' }, - 'export * as foo from "./other"': { type: 'star', specifier: './other', name: 'foo' } + 'export * as foo from "./other"': { type: 'star', specifier: './other', name: 'foo' }, + // eslint-disable-next-line no-template-curly-in-string + 'const a = ``;\nexport default true;': { type: 'default', name: 'default', names: ['default'] } } for (const [input, test] of Object.entries(tests)) {