Skip to content

Commit

Permalink
feat: import attributes support (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Mar 27, 2023
1 parent b4e89f6 commit 2a299a8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 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.
4 changes: 2 additions & 2 deletions src/lexer.c
Expand Up @@ -604,12 +604,12 @@ void readImportString (const char16_t* ss, char16_t ch) {
addImport(ss, startPos, pos, STANDARD_IMPORT);
pos++;
ch = commentWhitespace(false);
if (ch != 'a' || memcmp(pos + 1, &SSERT[0], 5 * 2) != 0) {
if (!(ch == 'a' && memcmp(pos + 1, &SSERT[0], 5 * 2) == 0) && !(ch == 'w' && *(pos + 1) == 'i' && *(pos + 2) == 't' && *(pos + 3) == 'h')) {
pos--;
return;
}
char16_t* assertIndex = pos;
pos += 6;
pos += ch == 'a' ? 6 : 4;
ch = commentWhitespace(true);
if (ch != '{') {
pos = assertIndex;
Expand Down
24 changes: 24 additions & 0 deletions test/_unit.cjs
Expand Up @@ -131,6 +131,30 @@ suite('Lexer', () => {
assertExportIs(source, exports[0], {n: 'p', ln: 'p', a: false});
});

if (!js)
test('Import attributes', () => {
const source = `
import json from "./foo.json" with { type: "json" };
import("foo.json" , { with: { type: "json" } });
import test from './asdf'
with { not: 'an assertion!' }
export var p = 5;
`
const [imports, exports] = parse(source);
assert.strictEqual(imports.length, 3);
assert.strictEqual(imports[0].n, './foo.json');
assert.strictEqual(source.substring(imports[0].s, imports[0].e), './foo.json');
assert.strictEqual(source.substring(imports[0].a, imports[0].se), '{ type: "json" }');
assert.strictEqual(source.substring(imports[1].a, imports[1].se), '{ with: { type: "json" } })');
assert.strictEqual(source.substring(imports[1].s, imports[1].e), '"foo.json"');
assert.strictEqual(imports[1].n, 'foo.json');
assert.strictEqual(imports[2].n, './asdf');
assert.strictEqual(imports[2].a, -1);
assert.strictEqual(exports.length, 1);
assertExportIs(source, exports[0], {n: 'p', ln: 'p', a: false});
});

test('Import meta inside dynamic import', () => {
const source = `import(import.meta.url)`;
const [imports] = parse(source);
Expand Down

0 comments on commit 2a299a8

Please sign in to comment.