From cf3b3144a07c801a5e227ce730c0d9039adf99a5 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 | 21 +++++++++++++++++++++ test/test-parse.cpp | 5 +++-- 3 files changed, 25 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..4c62f1996b 100644 --- a/test/test-parse-statement.cpp +++ b/test/test-parse-statement.cpp @@ -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( diff --git a/test/test-parse.cpp b/test/test-parse.cpp index 474815828e..6fe88d6b0a 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, // kjaslkjd + u8" ^^^^^^^^^^^ Diag_Expected_Parentheses_Around_If_Condition"_diag, // :\nkjaskljd }); } }