mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:27:35 +00:00
LibJS: Don't VERIFY that the token after 'import' is one of '.' and '('
Although those are the only valid options parse_primary_expression is sometimes called when only an expression is valid which means it did not check match_expression and might fail the now removed VERIFY.
This commit is contained in:
parent
56c425eec1
commit
e179cf2540
2 changed files with 27 additions and 5 deletions
|
@ -1425,14 +1425,18 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
|
||||||
}
|
}
|
||||||
case TokenType::Import: {
|
case TokenType::Import: {
|
||||||
auto lookahead_token = next_token();
|
auto lookahead_token = next_token();
|
||||||
VERIFY(lookahead_token.type() == TokenType::Period || lookahead_token.type() == TokenType::ParenOpen);
|
|
||||||
if (lookahead_token.type() == TokenType::ParenOpen)
|
if (lookahead_token.type() == TokenType::ParenOpen)
|
||||||
return { parse_import_call() };
|
return { parse_import_call() };
|
||||||
|
|
||||||
if (auto import_meta = try_parse_import_meta_expression()) {
|
if (lookahead_token.type() == TokenType::Period) {
|
||||||
if (m_program_type != Program::Type::Module)
|
if (auto import_meta = try_parse_import_meta_expression()) {
|
||||||
syntax_error("import.meta is only allowed in modules");
|
if (m_program_type != Program::Type::Module)
|
||||||
return { import_meta.release_nonnull() };
|
syntax_error("import.meta is only allowed in modules");
|
||||||
|
return { import_meta.release_nonnull() };
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
consume();
|
||||||
|
expected("import.meta or import call");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
describe("parsing", () => {
|
||||||
|
test("can parse call import call", () => {
|
||||||
|
expect("import('a')").toEval();
|
||||||
|
expect("import('a', )").toEval();
|
||||||
|
expect("import('a', {options: true})").toEval();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("does not crash on unexpected tokens after import", () => {
|
||||||
|
expect("f = import('a')").toEval();
|
||||||
|
|
||||||
|
expect("f= import").not.toEval();
|
||||||
|
expect("f= import;").not.toEval();
|
||||||
|
expect("f= import?").not.toEval();
|
||||||
|
expect("f= import'").not.toEval();
|
||||||
|
expect("f= import 'a'").not.toEval();
|
||||||
|
expect("f= import['a']").not.toEval();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue