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

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

Issue 1484263002: Use destructuring assignments for named parameters (#180) (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Destructure function params directly (no more opts in most cases) Created 5 years, 1 month 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: lib/src/js/builder.dart
diff --git a/lib/src/js/builder.dart b/lib/src/js/builder.dart
index 93d5c67711641631f5b5a4fbdcf9e1d675ca7c59..60bf7f9984e8d8e2e437b2e5343f8b03a8f07c8c 100644
--- a/lib/src/js/builder.dart
+++ b/lib/src/js/builder.dart
@@ -811,6 +811,22 @@ class MiniJsParser {
return expression;
}
+ InterpolatedIdentifier parseInterpolatedIdentifier() {
+ var id = new InterpolatedIdentifier(parseHash());
+ interpolatedValues.add(id);
+ return id;
+ }
+
+ Identifier parseIdentifier() {
+ if (acceptCategory(HASH)) {
+ return parseInterpolatedIdentifier();
+ } else {
+ var id = new Identifier(lastToken);
+ expectCategory(ALPHA);
+ return id;
+ }
+ }
+
/**
* CoverParenthesizedExpressionAndArrowParameterList[Yield] :
* ( Expression )
@@ -1136,45 +1152,95 @@ class MiniJsParser {
}
/** Parse a variable declaration list, with `var` or `let` [keyword] */
- VariableDeclarationList parseVariableDeclarationList(String keyword) {
- // Supports one form for interpolated variable declaration:
- // let # = ...
- if (acceptCategory(HASH)) {
- var name = new InterpolatedIdentifier(parseHash());
- interpolatedValues.add(name);
+ VariableDeclarationList parseVariableDeclarationList(String keyword) =>
+ finishVariableDeclarationList(keyword);
Jennifer Messerly 2015/12/01 02:10:27 why introduce this redirect? If they aren't differ
ochafik 2015/12/02 20:05:46 Done.
- Expression initializer = acceptString("=") ? parseAssignment() : null;
- return new VariableDeclarationList(keyword,
- [new VariableInitialization(name, initializer)]);
- }
-
- String firstVariable = lastToken;
- expectCategory(ALPHA);
- return finishVariableDeclarationList(keyword, firstVariable);
- }
-
- VariableDeclarationList finishVariableDeclarationList(
- String keyword, String firstVariable) {
+ VariableDeclarationList finishVariableDeclarationList(String keyword) {
var initialization = [];
- void declare(String variable) {
+ do {
+ var declarator;
+ switch (lastCategory) {
Jennifer Messerly 2015/12/01 02:10:27 this pattern occurs in a couple of places, pull it
ochafik 2015/12/02 20:05:46 +1, meet parseVariableBinding()
+ case ALPHA:
+ case HASH:
+ declarator = parseIdentifier();
+ break;
+ case LBRACE:
+ case LSQUARE:
+ declarator = parseDestructuring();
+ break;
+ default:
+ throw new StateError('Unexpected token $lastToken: ${categoryToString(lastCategory)}');
Jennifer Messerly 2015/12/01 02:10:27 long line. FYI, to prevent large churn here we exc
ochafik 2015/12/02 20:05:46 Done.
+ }
Expression initializer = null;
if (acceptString("=")) {
initializer = parseAssignment();
}
- var declaration = new Identifier(variable);
- initialization.add(new VariableInitialization(declaration, initializer));
- }
+ initialization.add(new VariableInitialization(declarator, initializer));
+ } while (acceptCategory(COMMA));
- declare(firstVariable);
- while (acceptCategory(COMMA)) {
- String variable = lastToken;
- expectCategory(ALPHA);
- declare(variable);
- }
return new VariableDeclarationList(keyword, initialization);
}
+ Destructuring parseDestructuring() {
Jennifer Messerly 2015/12/01 02:10:27 It'd be worth adding a link to where this is defin
ochafik 2015/12/02 20:05:46 Done.
+ if (acceptCategory(LBRACE)) {
+ return parseObjectDestructuring();
+ } else {
+ expectCategory(LSQUARE);
+ return parseArrayDestructuring();
+ }
+ }
+
+ ArrayDestructuring parseArrayDestructuring() {
+ var variables = <DestructuredVariable>[];
+ do {
+ var name;
+ var structure;
+ var defaultValue;
+
+ switch (lastCategory) {
+ case ALPHA:
+ case HASH:
+ name = parseIdentifier();
+ break;
+ case LBRACE:
+ case LSQUARE:
+ structure = parseDestructuring();
+ break;
+ default:
+ throw new StateError('Unexpected token $lastToken: ${categoryToString(lastCategory)}');
+ }
+ if (acceptString("=")) {
+ defaultValue = parseExpression();
+ }
+ variables.add(new DestructuredVariable(
+ name: name, structure: structure, defaultValue: defaultValue));
+ } while (acceptCategory(COMMA));
+
+ expectCategory(RSQUARE);
+ return new ArrayDestructuring(variables);
+ }
+
+ ObjectDestructuring parseObjectDestructuring() {
+ var variables = <DestructuredVariable>[];
+ do {
+ var name = parseIdentifier();
+ var structure;
+ var defaultValue;
+
+ if (acceptCategory(COLON)) {
Jennifer Messerly 2015/12/01 02:10:27 Hmmm, normally we'd do parseAssignment, like the o
ochafik 2015/12/02 20:05:46 Hehe, tried that, but... the grammar is a bit sens
+ structure = parseDestructuring();
+ } else if (acceptString("=")) {
+ defaultValue = parseExpression();
+ }
+ variables.add(new DestructuredVariable(
+ name: name, structure: structure, defaultValue: defaultValue));
+ } while (acceptCategory(COMMA));
+
+ expectCategory(RBRACE);
+ return new ObjectDestructuring(variables);
+ }
+
Expression parseVarDeclarationOrExpression() {
var keyword = acceptVarOrLet();
if (keyword != null) {
@@ -1394,7 +1460,7 @@ class MiniJsParser {
iterableExpression,
body);
}
- var declarations = finishVariableDeclarationList(keyword, identifier);
+ var declarations = finishVariableDeclarationList(keyword);
expectCategory(SEMICOLON);
return finishFor(declarations);
}
@@ -1498,15 +1564,7 @@ class MiniJsParser {
}
ClassExpression parseClass() {
- Identifier name;
- if (acceptCategory(HASH)) {
- var interpolatedName = new InterpolatedIdentifier(parseHash());
- interpolatedValues.add(interpolatedName);
- name = interpolatedName;
- } else {
- name = new Identifier(lastToken);
- expectCategory(ALPHA);
- }
+ Identifier name = parseIdentifier();
Expression heritage = null;
if (acceptString('extends')) {
heritage = parseLeftHandSide();

Powered by Google App Engine
This is Rietveld 408576698