Skip to content

Commit

Permalink
feat: skip trailing white space in dynamic import (#162)
Browse files Browse the repository at this point in the history
* feat: skip trailing white space in dynamic import

* test: add more test cases
  • Loading branch information
sapphi-red committed Nov 10, 2023
1 parent d41c1c8 commit 2911914
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
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.
2 changes: 1 addition & 1 deletion src/lexer.c
Expand Up @@ -133,7 +133,7 @@ bool parse () {
if (dynamicImportStackDepth > 0 && openTokenStack[openTokenDepth].token == ImportParen) {
Import* cur_dynamic_import = dynamicImportStack[dynamicImportStackDepth - 1];
if (cur_dynamic_import->end == 0)
cur_dynamic_import->end = pos;
cur_dynamic_import->end = lastTokenPos + 1;
cur_dynamic_import->statement_end = pos + 1;
dynamicImportStackDepth--;
}
Expand Down
35 changes: 35 additions & 0 deletions test/_unit.cjs
Expand Up @@ -46,6 +46,41 @@ suite('Lexer', () => {
assert.strictEqual(source.slice(impt.s, impt.e), '("asdf")');
});

test(`Dynamic import expression range 2`, () => {
const source = 'import(/* comment */ `asdf` /* comment */)';
const [[impt]] = parse(source);
assert.strictEqual(source.slice(impt.ss, impt.se), 'import(/* comment */ `asdf` /* comment */)');
assert.strictEqual(source.slice(impt.s, impt.e), '`asdf`');
});

test(`Dynamic import expression range 3`, () => {
const source = 'import(`asdf` // comment\n)';
const [[impt]] = parse(source);
assert.strictEqual(source.slice(impt.ss, impt.se), 'import(`asdf` // comment\n)');
assert.strictEqual(source.slice(impt.s, impt.e), '`asdf`');
});

test(`Dynamic import expression range 4`, () => {
const source = 'import("foo" + /* comment */ "bar")';
const [[impt]] = parse(source);
assert.strictEqual(source.slice(impt.ss, impt.se), 'import("foo" + /* comment */ "bar")');
assert.strictEqual(source.slice(impt.s, impt.e), '"foo" + /* comment */ "bar"');
});

test(`Dynamic import expression range 5`, () => {
const source = 'import((() => { return "foo" })() /* comment */)';
const [[impt]] = parse(source);
assert.strictEqual(source.slice(impt.ss, impt.se), 'import((() => { return "foo" })() /* comment */)');
assert.strictEqual(source.slice(impt.s, impt.e), '(() => { return "foo" })()');
});

test(`Dynamic import expression range 6`, () => {
const source = 'import(/* comment */ `asdf` /* comment */ /* comment 2 */)';
const [[impt]] = parse(source);
assert.strictEqual(source.slice(impt.ss, impt.se), 'import(/* comment */ `asdf` /* comment */ /* comment 2 */)');
assert.strictEqual(source.slice(impt.s, impt.e), '`asdf`');
});

test(`Simple export destructuring`, () => {
const source = `
export const{URI,Utils,...Another}=LIB
Expand Down

0 comments on commit 2911914

Please sign in to comment.