Files
sdk/pkg/js_ast/test/print_exponentiation_test.dart
Stephen Adams cb16f98c0a [js_ast] Fix js_ast printer bugs
* Parenthesize calls within `new`-expression targets to prevent the call
  arguments being associated with the `new`-expression.

* Negation of exponentiation and negation as the left operand of
  exponentiation are syntax errors, so parenthesize the exponentiation
  or left operand.
  "-2 ** n" --> "(-2) ** n" or "-(2 ** n)"

* "e in e" needs to be parenthesized in more places in a for-statement
  to avoid printing a for-in statement. The missing place was the
  right operand of a binary operator.

  "for (i = (0 in o) || 1 in o"
  -->
  "for (i = (0 in o) || (1 in o)"

Bug: #54534
Bug: b/313833546
Bug: b/314023208
Bug: b/314041897
Change-Id: I12fcd75ac5546a425b21cd68a9bda3786ceb243d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347423
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2024-01-20 02:06:59 +00:00

37 lines
1.1 KiB
Dart

// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:js_ast/js_ast.dart';
import 'print_helper.dart';
void main() {
final aPowB = testExpression('a ** b');
testExpression('# ** c', aPowB, '(a ** b) ** c');
testExpression('c ** #', aPowB, 'c ** a ** b');
// Miniparser parses with incorrect association:
testExpression('a ** b ** c', '(a ** b) ** c');
testExpression('(a ** b) ** c', '(a ** b) ** c');
testExpression('a ** (b ** c)', 'a ** b ** c');
testExpression('a **= b');
// `-a**b` is a JavaScript parse error. Parentheses are required to
// disambiguate between `(-a)**b` and `-(a**b)`.
testExpression('-(2 ** n)');
testExpression('(-(2)) ** n', '(-2) ** n');
testExpression('(-2) ** n', '(-2) ** n');
final minus2 = js.number(-2);
final negated2 = js('-#', js.number(2));
testExpression('# ** x', minus2, '(-2) ** x');
testExpression('# ** x', negated2, '(-2) ** x');
testExpression('-(2 ** n)');
}