[Pkg-javascript-commits] [node-acorn-jsx] 217/484: ArrowExpression rest parameter support, brackets check and test fixes.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:31 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 1f801001cf00dc416888741866ed49f8c08d6d7b
Author: Ingvar Stepanyan <me at rreverser.com>
Date:   Thu Jul 24 22:36:10 2014 +0300

    ArrowExpression rest parameter support, brackets check and test fixes.
---
 acorn.js              |  26 ++++++---
 test/tests-harmony.js | 150 ++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 157 insertions(+), 19 deletions(-)

diff --git a/acorn.js b/acorn.js
index 1ea2782..a879a93 100644
--- a/acorn.js
+++ b/acorn.js
@@ -1694,6 +1694,10 @@
       }
       expect(_parenR);
       if (eat(_arrow)) {
+        if (val) {
+          var innerParenL = input.slice(val.start, val.end).indexOf('(');
+          if (innerParenL >= 0) unexpected(val.start + innerParenL);
+        }
         val = parseArrowExpression(node, !val ? [] : val.type === "SequenceExpression" ? val.expressions : [val]);
       } else
       // disallow '()' before everything but error
@@ -1837,26 +1841,28 @@
     prop.key = (tokType === _num || tokType === _string) ? parseExprAtom() : parseIdent(true);
   }
 
-  // Initialize empty function node with given name.
+  // Initialize empty function node.
 
-  function initFunction(node, id) {
-    node.id = id || null;
+  function initFunction(node) {
+    node.id = null;
     node.params = [];
     if (options.ecmaVersion >= 6) {
       node.defaults = [];
       node.rest = null;
       node.generator = false;
     }
-    return node;
   }
 
   // Parse a function declaration or literal (depending on the
   // `isStatement` parameter).
 
-  function parseFunction(node, isStatement, allowExpression) {
-    initFunction(node, tokType === _name ? parseIdent() : isStatement ? unexpected() : null);
+  function parseFunction(node, isStatement, allowExpressionBody) {
+    initFunction(node);
+    if (isStatement || tokType === _name) {
+      node.id = parseIdent();
+    }
     parseFunctionParams(node);
-    parseFunctionBody(node, allowExpression);
+    parseFunctionBody(node, allowExpressionBody);
     return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
   }
 
@@ -1881,6 +1887,12 @@
           params[i] = param.left;
           break;
 
+        case "SpreadElement":
+          if (i === --params.length) {
+            node.rest = param.argument;
+            break;
+          }
+
         default:
           unexpected(param.start);
         }
diff --git a/test/tests-harmony.js b/test/tests-harmony.js
index 3ded3f4..acd8056 100644
--- a/test/tests-harmony.js
+++ b/test/tests-harmony.js
@@ -2680,6 +2680,70 @@ test("foo((x, y) => {})", {
   locations: true
 });
 
+test("(a, a) => 42", {
+  type: "Program",
+  loc: {
+    start: {line: 1, column: 0},
+    end: {line: 1, column: 12}
+  },
+  range: [0, 12],
+  body: [{
+    type: "ExpressionStatement",
+    loc: {
+      start: {line: 1, column: 0},
+      end: {line: 1, column: 12}
+    },
+    range: [0, 12],
+    expression: {
+      type: "ArrowFunctionExpression",
+      loc: {
+        start: {line: 1, column: 0},
+        end: {line: 1, column: 12}
+      },
+      range: [0, 12],
+      id: null,
+      params: [
+        {
+          type: "Identifier",
+          loc: {
+            start: {line: 1, column: 1},
+            end: {line: 1, column: 2}
+          },
+          range: [1, 2],
+          name: "a"
+        },
+        {
+          type: "Identifier",
+          loc: {
+            start: {line: 1, column: 4},
+            end: {line: 1, column: 5}
+          },
+          range: [4, 5],
+          name: "a"
+        }
+      ],
+      defaults: [],
+      rest: null,
+      generator: false,
+      body: {
+        type: "Literal",
+        loc: {
+          start: {line: 1, column: 10},
+          end: {line: 1, column: 12}
+        },
+        range: [10, 12],
+        value: 42,
+        raw: "42"
+      },
+      expression: true
+    }
+  }]
+}, {
+  ecmaVersion: 6,
+  ranges: true,
+  locations: true
+});
+
 // ES6: Method Definition
 
 test("x = { method() { } }", {
@@ -15715,6 +15779,72 @@ test("func(a, ...b)", {
   locations: true
 });
 
+test("func(...a, b)", {
+  type: "Program",
+  loc: {
+    start: {line: 1, column: 0},
+    end: {line: 1, column: 13}
+  },
+  range: [0, 13],
+  body: [{
+    type: "ExpressionStatement",
+    loc: {
+      start: {line: 1, column: 0},
+      end: {line: 1, column: 13}
+    },
+    range: [0, 13],
+    expression: {
+      type: "CallExpression",
+      loc: {
+        start: {line: 1, column: 0},
+        end: {line: 1, column: 13}
+      },
+      range: [0, 13],
+      callee: {
+        type: "Identifier",
+        loc: {
+          start: {line: 1, column: 0},
+          end: {line: 1, column: 4}
+        },
+        range: [0, 4],
+        name: "func"
+      },
+      arguments: [
+        {
+          type: "SpreadElement",
+          loc: {
+            start: {line: 1, column: 5},
+            end: {line: 1, column: 9}
+          },
+          range: [5, 9],
+          argument: {
+            type: "Identifier",
+            loc: {
+              start: {line: 1, column: 8},
+              end: {line: 1, column: 9}
+            },
+            range: [8, 9],
+            name: "a"
+          }
+        },
+        {
+          type: "Identifier",
+          loc: {
+            start: {line: 1, column: 11},
+            end: {line: 1, column: 12}
+          },
+          range: [11, 12],
+          name: "b"
+        }
+      ]
+    }
+  }]
+}, {
+  ecmaVersion: 6,
+  ranges: true,
+  locations: true
+});
+
 // Harmony Invalid syntax
 
 testFail("0o", "Expected number in radix 8 (1:2)", {ecmaVersion: 6});
@@ -15779,7 +15909,7 @@ testFail("function hello() {'use strict'; ({ i: 10, s(eval) { } }); }", "Definin
 
 testFail("function a() { \"use strict\"; ({ b(t, t) { } }); }", "Argument name clash in strict mode (1:37)", {ecmaVersion: 6});
 
-testFail("var super", "Unexpected token (1:5)", {ecmaVersion: 6});
+testFail("var super", "Unexpected token (1:5)", {ecmaVersion: 6, forbidReserved: true});
 
 testFail("var default", "Unexpected token (1:4)", {ecmaVersion: 6});
 
@@ -15815,9 +15945,9 @@ testFail("import { foo, bar }", "Unexpected token (1:20)", {ecmaVersion: 6});
 
 testFail("import foo from bar", "Unexpected token (1:20)", {ecmaVersion: 6});
 
-testFail("((a)) => 42", "Unexpected token (1:7)", {ecmaVersion: 6});
+testFail("((a)) => 42", "Unexpected token (1:1)", {ecmaVersion: 6});
 
-testFail("(a, (b)) => 42", "Unexpected token (1:10)", {ecmaVersion: 6});
+testFail("(a, (b)) => 42", "Unexpected token (1:4)", {ecmaVersion: 6});
 
 testFail("\"use strict\"; (eval = 10) => 42", "Assigning to eval in strict mode (1:15)", {ecmaVersion: 6});
 
@@ -15831,8 +15961,6 @@ testFail("\"use strict\"; (arguments, a) => 42", "Defining 'arguments' in strict
 
 testFail("\"use strict\"; (eval, a = 10) => 42", "Defining 'eval' in strict mode (1:15)", {ecmaVersion: 6});
 
-testFail("(a, a) => 42", "Unexpected token (1:7)", {ecmaVersion: 6});
-
 testFail("\"use strict\"; (a, a) => 42", "Argument name clash in strict mode (1:18)", {ecmaVersion: 6});
 
 testFail("\"use strict\"; (a) => 00", "Invalid number (1:21)", {ecmaVersion: 6});
@@ -16064,7 +16192,7 @@ testFail("`hello ${10 `test`", "Unexpected token (1:19)", {ecmaVersion: 6});
 
 testFail("`hello ${10;test`", "Unexpected token (1:12)", {ecmaVersion: 6});
 
-testFail("function a() 1 // expression closure is not supported", "Unexpected token (1:14)", {ecmaVersion: 6});
+testFail("function a() 1 // expression closure is not supported", "Unexpected token (1:13)", {ecmaVersion: 6});
 
 testFail("[a,b if (a)] // (a,b)", "Unexpected token (1:5)", {ecmaVersion: 6});
 
@@ -16088,9 +16216,9 @@ testFail("({ \"chance\" }) = obj", "Unexpected token (1:13)", {ecmaVersion: 6});
 
 testFail("({ 42 }) = obj", "Unexpected token (1:7)", {ecmaVersion: 6});
 
-testFail("function f(a, ...b, c)", "Unexpected token (1:19)", {ecmaVersion: 6});
+testFail("function f(a, ...b, c)", "Unexpected token (1:18)", {ecmaVersion: 6});
 
-testFail("function f(a, ...b = 0)", "Unexpected token (1:20)", {ecmaVersion: 6});
+testFail("function f(a, ...b = 0)", "Unexpected token (1:19)", {ecmaVersion: 6});
 
 testFail("function x(...{ a }){}", "Unexpected token (1:15)", {ecmaVersion: 6});
 
@@ -16100,7 +16228,7 @@ testFail("\"use strict\"; function x({ b: { a } }, [{ b: { a } }]){}", "Unexpect
 
 testFail("\"use strict\"; function x(a, ...[a]){}", "Unexpected token (1:38)", {ecmaVersion: 6});
 
-testFail("(...a, b) => {}", "Unexpected token (1:6)", {ecmaVersion: 6});
+testFail("(...a, b) => {}", "Unexpected token (1:1)", {ecmaVersion: 6});
 
 testFail("([ 5 ]) => {}", "Unexpected token (1:8)", {ecmaVersion: 6});
 
@@ -16112,9 +16240,7 @@ testFail("[...{ a }] = b", "Unexpected token (1:11)", {ecmaVersion: 6});
 
 testFail("[...a, b] = c", "Unexpected token (1:6)", {ecmaVersion: 6});
 
-testFail("func(...a, b)", "Unexpected token (1:10)", {ecmaVersion: 6});
-
-testFail("({ t(eval) { \"use strict\"; } });", "Unexpected token (1:6)", {ecmaVersion: 6});
+testFail("({ t(eval) { \"use strict\"; } });", "Defining 'eval' in strict mode (1:5)", {ecmaVersion: 6});
 
 testFail("\"use strict\"; `${test}\\02`;", "Unexpected token (1:22)", {ecmaVersion: 6});
 

-- 
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