Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fe): don't report missing else for bad if expression #1101

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/quick-lint-js/fe/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ void Parser::parse_and_visit_parenthesized_expression(

const Char8 *expression_begin = this->peek().begin;

Expression *ast = this->parse_expression(v);
Expression *ast = this->parse_expression(v, {.trailing_identifiers = true});
this->visit_expression(ast, v, Variable_Context::rhs);

if constexpr (check_for_sketchy_conditions) {
Expand Down
21 changes: 21 additions & 0 deletions test/test-parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,27 @@ TEST_F(Test_Parse_Statement, if_with_else) {
}
}

TEST_F(Test_Parse_Statement, if_else_with_malformed_condition) {
{
Test_Parser p(
u8"if (e isntanceof DataNotLoadedError) { } else { throw e; }"_sv,
capture_diags);
p.parse_and_visit_statement();
EXPECT_THAT(p.visits,
ElementsAreArray({"visit_variable_use", // e
"visit_variable_use", // typoed instanceof
"visit_variable_use", // DataNotLoadedError
"visit_enter_block_scope", //
"visit_exit_block_scope", //
"visit_enter_block_scope", //
"visit_variable_use", // e
"visit_exit_block_scope"}));
EXPECT_THAT(
p.errors,
::testing::Not(::testing::Contains(DIAG_TYPE(Diag_Else_Has_No_If))));
}
}

TEST_F(Test_Parse_Statement, if_without_body) {
{
Spy_Visitor p = test_parse_and_visit_statement(
Expand Down
5 changes: 3 additions & 2 deletions test/test-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ TEST_F(Test_Parse, utter_garbage) {
assert_diagnostics(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not your patch: I probably should have documented why I introduced this test in the first place. 😅 It's probably just testing a crash, so changing the diagnostics sounds fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's related to issue #88 based on the Git log, and it is testing a crash.

p.code, p.errors,
{
u8" ^ Diag_Expected_Parentheses_Around_If_Condition"_diag, //
u8" ^ Diag_Unexpected_Token"_diag,
u8" ^ Diag_Unexpected_Token"_diag, // :
u8" ^^^^^^^^ Diag_Unexpected_Identifier_In_Expression"_diag, // kjaslkjd
u8" ^^^^^^^^^^^ Diag_Expected_Parentheses_Around_If_Condition"_diag, // :\nkjaskljd
});
}
}
Expand Down
Loading