Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ function templateString () {
if (ch === 92/*\*/)
pos++;
}
syntaxError();
throw new Error('Unterminated template.');
}

function blockComment () {
Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
7 changes: 5 additions & 2 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ uint32_t parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(co
break;
case '}':
if (openTokenDepth == 0)
return syntaxError(2), false;
return syntaxError(2), error;
if (openTokenDepth-- == templateDepth) {
templateDepth = templateStack[--templateStackDepth];
templateString();
Expand Down Expand Up @@ -259,8 +259,11 @@ uint32_t parseCJS (uint16_t* _source, uint32_t _sourceLen, void (*_addExport)(co
lastTokenPos = pos;
}

if (templateDepth != UINT16_MAX || openTokenDepth || error)
if (templateDepth != UINT16_MAX || openTokenDepth || error) {
if (!error)
syntaxError(1);
return error;
}

// success
return 0;
Expand Down
24 changes: 24 additions & 0 deletions test/_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ suite('Lexer', () => {
assert.deepStrictEqual(first, { exports: ['first'], reexports: [] });
});

test('rejects unterminated and unexpected nesting', () => {
for (const source of ['(', '{', ')', '}', '({', '{(', 'exports.value = 1; }']) {
assert.throws(() => parse(source, 'nesting.js'), { name: 'Error' });
assert.deepStrictEqual(parse('exports.recovery = 1;'), {
exports: ['recovery'],
reexports: []
});
}
assert.deepStrictEqual(parse('({}); exports.value = 1;'), {
exports: ['value'],
reexports: []
});
});

test('rejects unterminated templates with a parser error', () => {
for (const source of ['`open', '`open ${value', 'const value = `open']) {
assert.throws(() => parse(source, 'template.js'), { name: 'Error' });
assert.deepStrictEqual(parse('exports.recovery = 1;'), {
exports: ['recovery'],
reexports: []
});
}
});

test('automatic Wasm initialization', async () => {
const lexer = await import('../dist/lexer.mjs?automatic-initialization');

Expand Down
Loading