[Pkg-javascript-commits] [node-acorn-jsx] 316/484: Loose: ES6 function params support.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:49 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-acorn-jsx.

commit 11ecb20e9e9e5862b688bbc61f50974328aefb1d
Author: Ingvar Stepanyan <me at rreverser.com>
Date:   Sun Oct 26 19:27:00 2014 +0200

    Loose: ES6 function params support.
---
 acorn.js       |  2 +-
 acorn_loose.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 73 insertions(+), 11 deletions(-)

diff --git a/acorn.js b/acorn.js
index d94aee2..14648d3 100644
--- a/acorn.js
+++ b/acorn.js
@@ -444,7 +444,7 @@
                       parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
                       dot: _dot, ellipsis: _ellipsis, question: _question, slash: _slash, eq: _eq,
                       name: _name, eof: _eof, num: _num, regexp: _regexp, string: _string,
-                      arrow: _arrow, bquote: _bquote, dollarBraceL: _dollarBraceL};
+                      arrow: _arrow, bquote: _bquote, dollarBraceL: _dollarBraceL, star: _star};
   for (var kw in keywordTypes) exports.tokTypes["_" + kw] = keywordTypes[kw];
 
   // This is a trick taken from Esprima. It turns out that, on
diff --git a/acorn_loose.js b/acorn_loose.js
index 6829f2f..39615aa 100644
--- a/acorn_loose.js
+++ b/acorn_loose.js
@@ -256,6 +256,8 @@
     if (token.type === type) {
       next();
       return true;
+    } else {
+      return false;
     }
   }
 
@@ -759,19 +761,79 @@
     return finishNode(node, "Identifier");
   }
 
-  function parseFunction(node, isStatement) {
-    if (token.type === tt.name) node.id = parseIdent();
-    else if (isStatement) node.id = dummyIdent();
-    else node.id = null;
+  function initFunction(node) {
+    node.id = null;
     node.params = [];
+    if (options.ecmaVersion >= 6) {
+      node.defaults = [];
+      node.rest = null;
+      node.generator = false;
+      node.expression = false;
+    }
+  }
+
+  // Convert existing expression atom to assignable pattern
+  // if possible.
+
+  function toAssignable(node) {
+    if (options.ecmaVersion >= 6 && node) {
+      switch (node.type) {
+        case "ObjectExpression":
+          node.type = "ObjectPattern";
+          var props = node.properties;
+          for (var i = 0; i < props.length; i++) {
+            toAssignable(props[i].value);
+          }
+          break;
+
+        case "ArrayExpression":
+          node.type = "ArrayPattern";
+          var elms = node.elements;
+          for (var i = 0; i < elms.length; i++) {
+            toAssignable(elms[i]);
+          }
+          break;
+
+        case "SpreadElement":
+          toAssignable(node.argument);
+          break;
+      }
+    }
+    return node;
+  }
+
+  function parseFunctionParams(node) {
+    var defaults = [], hasDefaults = false;
+
     pushCx();
-    expect(tt.parenL);
-    while (token.type == tt.name) {
-      node.params.push(parseIdent());
-      eat(tt.comma);
+    var params = parseExprList(tt.parenR);
+    for (var i = 0; i < params.length; i++) {
+      var param = toAssignable(params[i]), defValue = null;
+      if (param.type === "SpreadElement") {
+        if (i === params.length - 1) {
+          params.length--;
+          node.rest = param.argument;
+        }
+      } else if (param.type === "AssignmentExpression") {
+        defValue = param.right;
+        param = param.left;
+      }
+      node.params.push(param);
+      defaults.push(defValue);
+      if (defValue) hasDefaults = true;
     }
-    popCx();
-    eat(tt.parenR);
+
+    if (hasDefaults) node.defaults = defaults;
+  }
+
+  function parseFunction(node, isStatement) {
+    initFunction(node);
+    if (options.ecmaVersion >= 6) {
+      node.generator = eat(tt.star);
+    }
+    if (token.type === tt.name) node.id = parseIdent();
+    else if (isStatement) node.id = dummyIdent();
+    parseFunctionParams(node);
     node.body = parseBlock();
     return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
   }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git



More information about the Pkg-javascript-commits mailing list