Skip to content

Commit

Permalink
Check that import is preceeded by whitespace in plain case
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Oct 27, 2024
1 parent 8461e25 commit 728809f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,25 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,
}
}

else {
while (i < n && ws(u(0))) i++;
// import\s
else if (ws(c(-1))) {
size_t j = i;

// import [^\s]+
while (i < n && !ws(u(0))) i++;

while (i < n && ws(u(0))) i++;
// import [^\s]+
if (j < i) {
while (i < n && ws(u(0))) i++;

// import [^\s]+ from
if (bc("from", 4)) {
i += 4;
// import [^\s]+ from
if (bc("from", 4)) {
i += 4;

err = bare_module_lexer__add_name(env, &names, &nl, (const utf8_t *) "default");
assert(err == 0);
err = bare_module_lexer__add_name(env, &names, &nl, (const utf8_t *) "default");
assert(err == 0);

goto from;
goto from;
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,17 @@ test('// require(\'id\')', (t) => {
exports: []
})
})

test('invalid require', (t) => {
t.alike(lex('requires(\'./foo.js\')'), {
imports: [],
exports: []
})
})

test('invalid import', (t) => {
t.alike(lex('imported from \'./foo.js\''), {
imports: [],
exports: []
})
})

0 comments on commit 728809f

Please sign in to comment.