@@ -11595,6 +11595,17 @@ impl<'a, C: ParserConfig> Parser<'a, C> {
1159511595 debug_assert!(matches!(tag, TokenTag::As | TokenTag::Satisfies));
1159611596 self.advance()?;
1159711597 let type_annotation = self.parse_type()?;
11598+ if assertion_erasure_changes_binary_grouping(left, self.current_token.tag, &self.ast) {
11599+ self.report(
11600+ self.current_token.span,
11601+ "An unparenthesized `as` or `satisfies` expression cannot be followed by an operator that would bind differently after type erasure",
11602+ ReportOptions {
11603+ help: Some(Cow::Borrowed("Wrap the expression in parentheses.")),
11604+ ..ReportOptions::default()
11605+ },
11606+ );
11607+ return None;
11608+ }
1159811609 let span = Span::new(
1159911610 left.span(&self.ast).start,
1160011611 type_annotation.span(&self.ast).end,
@@ -12368,6 +12379,33 @@ fn max_left_precedence(expression: Expr, ast: &Ast<'_>) -> Precedence {
1236812379 }
1236912380}
1237012381
12382+ fn assertion_erasure_changes_binary_grouping(
12383+ mut expression: Expr,
12384+ next_operator: TokenTag,
12385+ ast: &Ast<'_>,
12386+ ) -> bool {
12387+ loop {
12388+ expression = match ast.expr_data(expression) {
12389+ ExprData::TsAsExpression(assertion) => assertion.expression(ast),
12390+ ExprData::TsSatisfiesExpression(assertion) => assertion.expression(ast),
12391+ _ => break,
12392+ };
12393+ }
12394+
12395+ let current_precedence = match ast.expr_data(expression) {
12396+ ExprData::BinaryExpression(binary) => binary.operator(ast).precedence(),
12397+ ExprData::LogicalExpression(logical) => logical.operator(ast).precedence(),
12398+ _ => return false,
12399+ };
12400+ if !next_operator.is_binary_operator() && !next_operator.is_logical_operator() {
12401+ return false;
12402+ }
12403+
12404+ let next_precedence = next_operator.precedence();
12405+ next_precedence > current_precedence
12406+ || (next_precedence == current_precedence && next_operator == TokenTag::Exponent)
12407+ }
12408+
1237112409fn is_angle_open(tag: TokenTag) -> bool {
1237212410 matches!(tag, TokenTag::LessThan | TokenTag::LeftShift)
1237312411}
@@ -12727,6 +12765,21 @@ mod tests {
1272712765
1272812766 use super::*;
1272912767
12768+ fn parse_typescript_diagnostics(source: &str) -> Vec<Diagnostic<'static>> {
12769+ let allocator = Allocator::new();
12770+ Parser::init(
12771+ &allocator,
12772+ source,
12773+ Options {
12774+ lang: Lang::Ts,
12775+ ..Options::default()
12776+ },
12777+ NoTokenParserConfig,
12778+ )
12779+ .parse()
12780+ .diagnostics
12781+ }
12782+
1273012783 #[test]
1273112784 fn parse_expression_fragment() {
1273212785 fn parse<'a>(allocator: &'a Allocator, source: &'a str, options: Options) -> Ast<'a> {
@@ -12754,6 +12807,42 @@ mod tests {
1275412807 assert_eq!(ast.source_type(), SourceType::Script);
1275512808 }
1275612809
12810+ #[test]
12811+ fn disallow_unerasable_assertions() {
12812+ let diagnostics = parse_typescript_diagnostics("1 + 1 as number * 2");
12813+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12814+
12815+ let diagnostics = parse_typescript_diagnostics("1 + 1 satisfies number * 2");
12816+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12817+
12818+ let diagnostics = parse_typescript_diagnostics("1 + 1 as unknown as number * 2");
12819+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12820+
12821+ let diagnostics = parse_typescript_diagnostics("1 >> 1 as number + 2");
12822+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12823+
12824+ let diagnostics = parse_typescript_diagnostics("2 * 3 as number * 2");
12825+ assert!(diagnostics.is_empty(), "{diagnostics:#?}");
12826+
12827+ let diagnostics = parse_typescript_diagnostics("2 * 3 satisfies number * 2");
12828+ assert!(diagnostics.is_empty(), "{diagnostics:#?}");
12829+
12830+ let diagnostics = parse_typescript_diagnostics("2 ** 3 as number ** 2");
12831+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12832+
12833+ let diagnostics = parse_typescript_diagnostics("2 ** 3 satisfies number ** 2");
12834+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12835+
12836+ let diagnostics = parse_typescript_diagnostics("2 ** 3 as unknown as number ** 2");
12837+ assert!(!diagnostics.is_empty(), "{diagnostics:#?}");
12838+
12839+ let diagnostics = parse_typescript_diagnostics("1 * 1 as number + 2");
12840+ assert!(diagnostics.is_empty(), "{diagnostics:#?}");
12841+
12842+ let diagnostics = parse_typescript_diagnostics("1 as number * 2");
12843+ assert!(diagnostics.is_empty(), "{diagnostics:#?}");
12844+ }
12845+
1275712846 #[test]
1275812847 fn await_using_context() {
1275912848 fn parse(source: &str, source_type: SourceType) -> Vec<Diagnostic<'static>> {
0 commit comments