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

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

Issue 1263593003: restore arrow function bind this workaround (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 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
« no previous file with comments | « lib/src/js/precedence.dart ('k') | lib/src/options.dart » ('j') | 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 c545073f2e25bbdc7321f2312f70e550795e9f8b..e07e37a7b16ccd84a4a4a3a034e6ba5305ece794 100644
--- a/lib/src/js/printer.dart
+++ b/lib/src/js/printer.dart
@@ -10,16 +10,19 @@ class JavaScriptPrintingOptions {
final bool minifyLocalVariables;
final bool preferSemicolonToNewlineInMinifiedOutput;
-
/// True to allow keywords in properties, such as `obj.var` or `obj.function`
/// Modern JS engines support this.
final bool allowKeywordsInProperties;
+ /// Workaround if `this` is not bound in arrow functions.
+ final bool arrowFnBindThisWorkaround;
+
JavaScriptPrintingOptions(
{this.shouldCompressOutput: false,
this.minifyLocalVariables: false,
this.preferSemicolonToNewlineInMinifiedOutput: false,
- this.allowKeywordsInProperties: false});
+ this.allowKeywordsInProperties: false,
+ this.arrowFnBindThisWorkaround: false});
}
@@ -548,10 +551,16 @@ class Printer implements NodeVisitor {
visitNestedExpression(Expression node, int requiredPrecedence,
{bool newInForInit, bool newAtStatementBegin}) {
+ int nodePrecedence = node.precedenceLevel;
+ if (options.arrowFnBindThisWorkaround) {
+ if (node is ArrowFun && node.closesOverThis) {
+ nodePrecedence = CALL;
+ }
+ }
bool needsParentheses =
// a - (b + c).
(requiredPrecedence != EXPRESSION &&
- node.precedenceLevel < requiredPrecedence) ||
+ nodePrecedence < requiredPrecedence) ||
// for (a = (x in o); ... ; ... ) { ... }
(newInForInit && node is Binary && node.op == "in") ||
// (function() { ... })().
@@ -858,6 +867,10 @@ class Printer implements NodeVisitor {
}
visitArrowFun(ArrowFun fun) {
+ bool bindThis = options.arrowFnBindThisWorkaround && fun.closesOverThis;
+ if (bindThis) {
+ out("(");
+ }
localNamer.enterScope(fun);
if (fun.params.length == 1) {
visitNestedExpression(fun.params.single, SPREAD,
@@ -884,6 +897,9 @@ class Printer implements NodeVisitor {
blockBody(fun.body, needsSeparation: false, needsNewline: false);
}
localNamer.leaveScope();
+ if (bindThis) {
+ out(").bind(this)");
+ }
}
visitLiteralBool(LiteralBool node) {
« no previous file with comments | « lib/src/js/precedence.dart ('k') | lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698