diff --git a/lex.h b/lex.h index f21637b..9ae44d5 100644 --- a/lex.h +++ b/lex.h @@ -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; + } } } } diff --git a/test.js b/test.js index 148b69e..78e4265 100644 --- a/test.js +++ b/test.js @@ -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: [] + }) +})