Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(695)

Unified Diff: pkg/analyzer/test/src/summary/element_text.dart

Issue 2985503002: Parenthesize expressions as needed in elements text dump. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/test/src/summary/element_text.dart
diff --git a/pkg/analyzer/test/src/summary/element_text.dart b/pkg/analyzer/test/src/summary/element_text.dart
index 65d7c8064161bfe5b6039e38ab40e35e10829025..1e35d71cdf70d81a43cce715d25809f2dc9656a1 100644
--- a/pkg/analyzer/test/src/summary/element_text.dart
+++ b/pkg/analyzer/test/src/summary/element_text.dart
@@ -269,7 +269,15 @@ class _ElementWriter {
buffer.writeln(';');
}
- void writeExpression(AstNode e) {
+ void writeExpression(AstNode e, [Expression enclosing]) {
+ bool needsParenthesis = e is Expression &&
+ enclosing != null &&
+ e.precedence < enclosing.precedence;
+
+ if (needsParenthesis) {
+ buffer.write('(');
+ }
+
if (e is Annotation) {
buffer.write('@');
writeExpression(e.name);
@@ -290,11 +298,11 @@ class _ElementWriter {
}
buffer.write(')');
} else if (e is BinaryExpression) {
- writeExpression(e.leftOperand);
+ writeExpression(e.leftOperand, e);
buffer.write(' ');
buffer.write(e.operator.lexeme);
buffer.write(' ');
- writeExpression(e.rightOperand);
+ writeExpression(e.rightOperand, e);
} else if (e is BooleanLiteral) {
buffer.write(e.value);
} else if (e is ConditionalExpression) {
@@ -360,13 +368,13 @@ class _ElementWriter {
buffer.write('null');
} else if (e is PrefixExpression) {
buffer.write(e.operator.lexeme);
- writeExpression(e.operand);
+ writeExpression(e.operand, e);
} else if (e is PrefixedIdentifier) {
writeExpression(e.prefix);
buffer.write('.');
writeExpression(e.identifier);
} else if (e is PropertyAccess) {
- writeExpression(e.target);
+ writeExpression(e.target, e);
buffer.write('.');
writeExpression(e.propertyName);
} else if (e is RedirectingConstructorInvocation) {
@@ -421,6 +429,10 @@ class _ElementWriter {
} else {
fail('Unsupported expression type: ${e.runtimeType}');
}
+
+ if (needsParenthesis) {
+ buffer.write(')');
+ }
}
void writeFunctionElement(FunctionElement e) {

Powered by Google App Engine
This is Rietveld 408576698