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

Unified Diff: lib/src/js/printer.dart

Issue 1122283002: fixes #158, precendence of call and access in new (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: redesgin Created 5 years, 7 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
« no previous file with comments | « lib/runtime/dart/isolate.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/js/printer.dart
diff --git a/lib/src/js/printer.dart b/lib/src/js/printer.dart
index 337cc092ddc6703681111ff48d365c11638b1660..41c66765722963092c9abbad3a08b873875c778c 100644
--- a/lib/src/js/printer.dart
+++ b/lib/src/js/printer.dart
@@ -60,6 +60,7 @@ class Printer implements NodeVisitor {
bool inForInit = false;
bool atStatementBegin = false;
+ bool inNewTarget = false;
bool pendingSemicolon = false;
bool pendingSpace = false;
@@ -560,6 +561,7 @@ class Printer implements NodeVisitor {
if (needsParentheses) {
inForInit = false;
atStatementBegin = false;
+ inNewTarget = false;
out("(");
visit(node);
out(")");
@@ -616,8 +618,10 @@ class Printer implements NodeVisitor {
visitNew(New node) {
out("new ");
+ inNewTarget = true;
visitNestedExpression(node.target, ACCESS,
newInForInit: inForInit, newAtStatementBegin: false);
+ inNewTarget = false;
out("(");
visitCommaSeparated(node.arguments, ASSIGNMENT,
newInForInit: false, newAtStatementBegin: false);
@@ -809,7 +813,23 @@ class Printer implements NodeVisitor {
}
visitAccess(PropertyAccess access) {
- visitNestedExpression(access.receiver, CALL,
+ // Normally we can omit parens on the receiver if it is a Call, even though
+ // Call expressions have lower precedence. However this optimization doesn't
+ // work inside New expressions:
+ //
+ // new obj.foo().bar()
+ //
+ // This will be parsed as:
+ //
+ // (new obj.foo()).bar()
+ //
+ // Which is incorrect. So we must have parenthesis in this case:
+ //
+ // new (obj.foo()).bar()
+ //
+ int precedence = inNewTarget ? ACCESS : CALL;
+
+ visitNestedExpression(access.receiver, precedence,
newInForInit: inForInit,
newAtStatementBegin: atStatementBegin);
propertyNameOut(access.selector, inAccess: true);
« no previous file with comments | « lib/runtime/dart/isolate.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698