diff --git a/docs/errors/E0721.md b/docs/errors/E0721.md new file mode 100644 index 000000000..993bf61aa --- /dev/null +++ b/docs/errors/E0721.md @@ -0,0 +1,18 @@ +# E0721: invalid syntax; missing parentheses around postfix expression + +In JavaScript, putting a `.` or a `?.` directly following a postfix increment +or decrement is a syntax error: + +```javascript +let x = 3; +console.log(x++.toString()); // Invalid syntax +console.log(x--?.constructor); // Invalid syntax +``` + +Add parentheses around the postfix expression to resolve this: + +```javascript +let x = 3; +console.log((x++).toString()); +console.log((x--)?.constructor); +``` diff --git a/po/messages.pot b/po/messages.pot index 6c591842f..69efe4902 100644 --- a/po/messages.pot +++ b/po/messages.pot @@ -2429,6 +2429,10 @@ msgstr "" msgid "function 'let' call may be confused for destructuring; remove parentheses to declare a variable" msgstr "" +#: src/quick-lint-js/diag/diagnostic-metadata-generated.cpp +msgid "invalid syntax; missing parentheses around '{0}'" +msgstr "" + #: test/test-diagnostic-formatter.cpp #: test/test-vim-qflist-json-diag-reporter.cpp msgid "something happened" diff --git a/src/quick-lint-js/diag/diagnostic-metadata-generated.cpp b/src/quick-lint-js/diag/diagnostic-metadata-generated.cpp index 24f01f806..66c1681a9 100644 --- a/src/quick-lint-js/diag/diagnostic-metadata-generated.cpp +++ b/src/quick-lint-js/diag/diagnostic-metadata-generated.cpp @@ -6975,6 +6975,20 @@ const QLJS_CONSTINIT Diagnostic_Info all_diagnostic_infos[] = { }, }, }, + + // Diag_Invalid_Operator_Directly_After_Postfix + { + .code = 721, + .severity = Diagnostic_Severity::error, + .message_formats = { + QLJS_TRANSLATABLE("invalid syntax; missing parentheses around '{0}'"), + }, + .message_args = { + { + Diagnostic_Message_Arg_Info(offsetof(Diag_Invalid_Operator_Directly_After_Postfix, postfix_expression), Diagnostic_Arg_Type::source_code_span), + }, + }, + }, }; } diff --git a/src/quick-lint-js/diag/diagnostic-metadata-generated.h b/src/quick-lint-js/diag/diagnostic-metadata-generated.h index d9eadc717..4aa93f5b7 100644 --- a/src/quick-lint-js/diag/diagnostic-metadata-generated.h +++ b/src/quick-lint-js/diag/diagnostic-metadata-generated.h @@ -476,10 +476,11 @@ namespace quick_lint_js { QLJS_DIAG_TYPE_NAME(Diag_Unintuitive_Bitshift_Precedence) \ QLJS_DIAG_TYPE_NAME(Diag_TypeScript_Namespace_Alias_Cannot_Use_Import_Type) \ QLJS_DIAG_TYPE_NAME(Diag_Confusing_Let_Call) \ + QLJS_DIAG_TYPE_NAME(Diag_Invalid_Operator_Directly_After_Postfix) \ /* END */ // clang-format on -inline constexpr int Diag_Type_Count = 465; +inline constexpr int Diag_Type_Count = 466; extern const Diagnostic_Info all_diagnostic_infos[Diag_Type_Count]; } diff --git a/src/quick-lint-js/diag/diagnostic-types-2.h b/src/quick-lint-js/diag/diagnostic-types-2.h index 60ff0ceef..1ecf5e09b 100644 --- a/src/quick-lint-js/diag/diagnostic-types-2.h +++ b/src/quick-lint-js/diag/diagnostic-types-2.h @@ -3628,6 +3628,12 @@ struct Diag_Confusing_Let_Call { Source_Code_Span let_function_call; }; +struct Diag_Invalid_Operator_Directly_After_Postfix { + [[qljs::diag("E0721", Diagnostic_Severity::error)]] // + [[qljs::message("invalid syntax; missing parentheses around '{0}'", + ARG(postfix_expression))]] // + Source_Code_Span postfix_expression; +}; } QLJS_WARNING_POP diff --git a/src/quick-lint-js/fe/parse-expression.cpp b/src/quick-lint-js/fe/parse-expression.cpp index 7057b1cef..4216143da 100644 --- a/src/quick-lint-js/fe/parse-expression.cpp +++ b/src/quick-lint-js/fe/parse-expression.cpp @@ -1898,6 +1898,14 @@ Expression* Parser::parse_expression_remainder(Parse_Visitor_Base& v, binary_builder.replace_last( this->make_expression( binary_builder.last_expression(), operator_span)); + Token_Type next_type = this->peek().type; + if (next_type == Token_Type::dot || + next_type == Token_Type::question_dot) { + this->diag_reporter_->report( + Diag_Invalid_Operator_Directly_After_Postfix{ + .postfix_expression = + binary_builder.last_expression()->span()}); + } } goto next; diff --git a/src/quick-lint-js/i18n/translation-table-generated.cpp b/src/quick-lint-js/i18n/translation-table-generated.cpp index 74c41a186..badd43cb5 100644 --- a/src/quick-lint-js/i18n/translation-table-generated.cpp +++ b/src/quick-lint-js/i18n/translation-table-generated.cpp @@ -365,7 +365,8 @@ const Translation_Table translation_data = { {47, 43, 47, 54, 35, 38}, // {31, 39, 32, 33, 28, 27}, // {34, 15, 43, 40, 37, 33}, // - {39, 51, 50, 40, 38, 39}, // + {0, 0, 0, 0, 0, 39}, // + {39, 51, 50, 40, 38, 49}, // {57, 72, 67, 55, 0, 41}, // {0, 0, 0, 58, 0, 50}, // {0, 0, 0, 22, 0, 19}, // @@ -2240,6 +2241,7 @@ const Translation_Table translation_data = { u8"invalid function parameter\0" u8"invalid hex escape sequence: {0}\0" u8"invalid lone literal in object literal\0" + u8"invalid syntax; missing parentheses around '{0}'\0" u8"keywords cannot contain escape sequences\0" u8"label named 'await' not allowed in async function\0" u8"labelled statement\0" diff --git a/src/quick-lint-js/i18n/translation-table-generated.h b/src/quick-lint-js/i18n/translation-table-generated.h index 8d74ec6f4..c7d8af7d0 100644 --- a/src/quick-lint-js/i18n/translation-table-generated.h +++ b/src/quick-lint-js/i18n/translation-table-generated.h @@ -18,8 +18,8 @@ namespace quick_lint_js { using namespace std::literals::string_view_literals; constexpr std::uint32_t translation_table_locale_count = 5; -constexpr std::uint16_t translation_table_mapping_table_size = 609; -constexpr std::size_t translation_table_string_table_size = 82687; +constexpr std::uint16_t translation_table_mapping_table_size = 610; +constexpr std::size_t translation_table_string_table_size = 82736; constexpr std::size_t translation_table_locale_table_size = 35; QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up( @@ -380,6 +380,7 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up( "invalid function parameter"sv, "invalid hex escape sequence: {0}"sv, "invalid lone literal in object literal"sv, + "invalid syntax; missing parentheses around '{0}'"sv, "keywords cannot contain escape sequences"sv, "label named 'await' not allowed in async function"sv, "labelled statement"sv, diff --git a/src/quick-lint-js/i18n/translation-table-test-generated.h b/src/quick-lint-js/i18n/translation-table-test-generated.h index 504c22755..b776596e0 100644 --- a/src/quick-lint-js/i18n/translation-table-test-generated.h +++ b/src/quick-lint-js/i18n/translation-table-test-generated.h @@ -27,7 +27,7 @@ struct Translated_String { }; // clang-format off -inline const Translated_String test_translation_table[608] = { +inline const Translated_String test_translation_table[609] = { { "\"global-groups\" entries must be strings"_translatable, u8"\"global-groups\" entries must be strings", @@ -3922,6 +3922,17 @@ inline const Translated_String test_translation_table[608] = { u8"ogiltigt l\u00e5neuttryck i objektliteral", }, }, + { + "invalid syntax; missing parentheses around '{0}'"_translatable, + u8"invalid syntax; missing parentheses around '{0}'", + { + u8"invalid syntax; missing parentheses around '{0}'", + u8"invalid syntax; missing parentheses around '{0}'", + u8"invalid syntax; missing parentheses around '{0}'", + u8"invalid syntax; missing parentheses around '{0}'", + u8"invalid syntax; missing parentheses around '{0}'", + }, + }, { "keywords cannot contain escape sequences"_translatable, u8"keywords cannot contain escape sequences", diff --git a/test/test-parse-warning.cpp b/test/test-parse-warning.cpp index 22d76141b..57b61382f 100644 --- a/test/test-parse-warning.cpp +++ b/test/test-parse-warning.cpp @@ -538,6 +538,21 @@ TEST_F(Test_Parse_Warning, warn_on_confusing_let_use) { test_parse_and_visit_statement(u8"let.prop({x} = y);"_sv, no_diags); test_parse_and_visit_statement(u8"let (x = y);"_sv, no_diags); } + +TEST_F(Test_Parse_Warning, invalid_operator_directly_after_postfix) { + test_parse_and_visit_expression( + u8"x++.toString()"_sv, // + u8"^^^ Diag_Invalid_Operator_Directly_After_Postfix"_diag); + test_parse_and_visit_expression(u8"(x++).toString()"_sv, no_diags); + test_parse_and_visit_expression( + u8"x++.constructor"_sv, // + u8"^^^ Diag_Invalid_Operator_Directly_After_Postfix"_diag); + test_parse_and_visit_expression(u8"(x++).constructor"_sv, no_diags); + test_parse_and_visit_expression( + u8"x--?.constructor"_sv, // + u8"^^^ Diag_Invalid_Operator_Directly_After_Postfix"_diag); + test_parse_and_visit_expression(u8"(x--)?.constructor"_sv, no_diags); +} } }