Skip to content

Commit 9f317a9

Browse files
committed
fix(typescript): don't treat ':' as annotation in 'x?++y:z'
1 parent 12612bb commit 9f317a9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Semantic Versioning.
1515
falsely reports [E0185][] ("assignment to imported variable").
1616
* Interface index signature variables can now be named contextual keywords
1717
such as `type`.
18+
* Writing `++x` inside `? :` no longer falsely reports [E0254][] ("unexpected
19+
':' in expression; did you mean 'as'?").
1820

1921
## 2.19.0 (2023-12-30)
2022

src/quick-lint-js/fe/parse-expression.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,15 @@ Expression* Parser::parse_primary_expression(Parse_Visitor_Base& v,
533533
case Token_Type::plus_plus: {
534534
Source_Code_Span operator_span = this->peek().span();
535535
this->skip();
536-
Expression* child =
537-
this->parse_expression(v, Precedence{
538-
.binary_operators = false,
539-
.math_or_logical_or_assignment = false,
540-
.commas = false,
541-
.in_operator = prec.in_operator,
542-
.conditional_operator = false,
543-
});
536+
Expression* child = this->parse_expression(
537+
v, Precedence{
538+
.binary_operators = false,
539+
.math_or_logical_or_assignment = false,
540+
.commas = false,
541+
.in_operator = prec.in_operator,
542+
.colon_type_annotation = Allow_Type_Annotations::never,
543+
.conditional_operator = false,
544+
});
544545
if (child->kind() == Expression_Kind::Missing) {
545546
this->diag_reporter_->report(Diag_Missing_Operand_For_Operator{
546547
.where = operator_span,

test/test-parse-expression-typescript.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ TEST_F(Test_Parse_Expression_TypeScript,
9090
EXPECT_EQ(summarize(ast),
9191
"cond(var cond, paren(var t), paren(jsxelement(F)))");
9292
}
93+
94+
{
95+
Test_Parser p(u8"cond ? ++t : f"_sv, typescript_jsx_options);
96+
Expression* ast = p.parse_expression();
97+
EXPECT_EQ(summarize(ast), "cond(var cond, rwunary(var t), var f)");
98+
}
9399
}
94100

95101
TEST_F(Test_Parse_Expression_TypeScript,

0 commit comments

Comments
 (0)