From 1bbf3f582bfdf61cc5ea234bd82e2dbc9c739d58 Mon Sep 17 00:00:00 2001 From: Ariel Don Date: Fri, 27 Oct 2023 12:42:50 -0500 Subject: [PATCH] fix(fe): don't report missing `else` for bad `if` expression --- src/quick-lint-js/fe/parse.h | 2 +- test/test-parse-statement.cpp | 19 +++++++++++++++++++ test/test-parse.cpp | 5 +++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/quick-lint-js/fe/parse.h b/src/quick-lint-js/fe/parse.h index 69d55e4f1c..09bd512b17 100644 --- a/src/quick-lint-js/fe/parse.h +++ b/src/quick-lint-js/fe/parse.h @@ -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) { diff --git a/test/test-parse-statement.cpp b/test/test-parse-statement.cpp index befaa08fce..894f7511cf 100644 --- a/test/test-parse-statement.cpp +++ b/test/test-parse-statement.cpp @@ -717,6 +717,25 @@ TEST_F(Test_Parse_Statement, else_without_if) { "visit_exit_block_scope", })); } + + { + 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, missing_if_after_else) { diff --git a/test/test-parse.cpp b/test/test-parse.cpp index 474815828e..2f9dbff235 100644 --- a/test/test-parse.cpp +++ b/test/test-parse.cpp @@ -320,8 +320,9 @@ TEST_F(Test_Parse, utter_garbage) { assert_diagnostics( 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, // + u8" ^^^^^^^^^^^ Diag_Expected_Parentheses_Around_If_Condition"_diag, }); } }