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: handle regexp as default export #167

Merged
merged 1 commit into from Mar 19, 2024
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
6 changes: 6 additions & 0 deletions lexer.js
Expand Up @@ -186,6 +186,7 @@ export function parse (_source, _name) {
// - if a closing brace or paren, what token came before the corresponding
// opening brace or paren (lastOpenTokenIndex)
const lastToken = source.charCodeAt(lastTokenPos);
const lastExport = exports[exports.length - 1];
if (isExpressionPunctuator(lastToken) &&
!(lastToken === 46/*.*/ && (source.charCodeAt(lastTokenPos - 1) >= 48/*0*/ && source.charCodeAt(lastTokenPos - 1) <= 57/*9*/)) &&
!(lastToken === 43/*+*/ && source.charCodeAt(lastTokenPos - 1) === 43/*+*/) && !(lastToken === 45/*-*/ && source.charCodeAt(lastTokenPos - 1) === 45/*-*/) ||
Expand All @@ -197,6 +198,11 @@ export function parse (_source, _name) {
regularExpression();
lastSlashWasDivision = false;
}
else if (lastExport && lastTokenPos >= lastExport.s && lastTokenPos <= lastExport.e) {
// export default /some-regexp/
regularExpression();
lastSlashWasDivision = false;
}
else {
lastSlashWasDivision = true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/lexer.asm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/lexer.emcc.asm.js

Large diffs are not rendered by default.

Binary file modified lib/lexer.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions src/lexer.c
Expand Up @@ -195,6 +195,11 @@ bool parse () {
regularExpression();
lastSlashWasDivision = false;
}
else if (export_write_head != NULL && lastTokenPos >= export_write_head->start && lastTokenPos <= export_write_head->end) {
// export default /some-regexp/
regularExpression();
lastSlashWasDivision = false;
}
else {
// Final check - if the last token was "break x" or "continue x"
while (lastTokenPos > source && !isBrOrWsOrPunctuatorNotDot(*(--lastTokenPos)));
Expand Down
17 changes: 17 additions & 0 deletions test/_unit.cjs
Expand Up @@ -252,6 +252,23 @@ suite('Lexer', () => {
`);
});

test('Regexp default export', () => {
const source = `
export default /[\`]/
export default 1/2
export default /* asdf */ 1/2
export default /* asdf */ /regex/
export default
// line comment
/regex/
export default
// line comment
1 / 2
`;
const [, exports] = parse(source);
assert.deepStrictEqual(exports.map(expt => expt.n), ['default', 'default', 'default', 'default', 'default', 'default']);
});

if (!js)
test('Regexp keyword prefixes', () => {
const [imports] = parse(`
Expand Down