From f7fe322490cbb82320edecbc3dcfe4881808fbdd Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 24 Jul 2014 23:08:49 +0300 Subject: [PATCH] Better parentheses check for arrow expression argument list. --- acorn.js | 20 +++++++++++--------- test/tests-harmony.js | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/acorn.js b/acorn.js index a879a93c63..5d622bb1e2 100644 --- a/acorn.js +++ b/acorn.js @@ -226,6 +226,11 @@ var inFunction, labels, strict; + // This counter is used for checking that arrow expressions did + // not contain nested parentheses in argument list. + + var metParenL; + // This function is used to raise exceptions on parse errors. It // takes an offset integer (into the current `input`) to indicate // the location of the error, attaches the position to the end @@ -491,6 +496,7 @@ tokCurLine = 1; tokPos = tokLineStart = 0; tokRegexpAllowed = true; + metParenL = 0; skipSpace(); } @@ -1689,20 +1695,16 @@ case _parenL: var node = startNode(), tokStartLoc1 = tokStartLoc, tokStart1 = tokStart, val; next(); + var oldParenL = ++metParenL; if (tokType !== _parenR) { val = parseExpression(); } expect(_parenR); - if (eat(_arrow)) { - if (val) { - var innerParenL = input.slice(val.start, val.end).indexOf('('); - if (innerParenL >= 0) unexpected(val.start + innerParenL); - } + if (metParenL === oldParenL && eat(_arrow)) { val = parseArrowExpression(node, !val ? [] : val.type === "SequenceExpression" ? val.expressions : [val]); - } else - // disallow '()' before everything but error - if (!val) { - unexpected(tokPos - 1); + } else { + // disallow '()' before everything but error + if (!val) unexpected(lastStart); } val.start = tokStart1; val.end = lastEnd; diff --git a/test/tests-harmony.js b/test/tests-harmony.js index acd8056340..4982dc8dc5 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -15945,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:1)", {ecmaVersion: 6}); +testFail("((a)) => 42", "Unexpected token (1:6)", {ecmaVersion: 6}); -testFail("(a, (b)) => 42", "Unexpected token (1:4)", {ecmaVersion: 6}); +testFail("(a, (b)) => 42", "Unexpected token (1:9)", {ecmaVersion: 6}); testFail("\"use strict\"; (eval = 10) => 42", "Assigning to eval in strict mode (1:15)", {ecmaVersion: 6});