[Pkg-javascript-commits] [node-acorn-jsx] 297/484: Add option for representing parenthesized expressions in the AST.
Bastien Roucariès
rouca at moszumanska.debian.org
Sat Aug 19 14:20:46 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 d525c4564442a77d8b8fac88393767a430ef01f7
Author: Max Schaefer <max at semmle.com>
Date: Wed Sep 3 18:37:23 2014 +0100
Add option for representing parenthesized expressions in the AST.
---
README.md | 5 +++
acorn.js | 15 ++++++-
test/tests-harmony.js | 22 ++++++++++
test/tests.js | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 158 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index bf53c31..269c0bf 100644
--- a/README.md
+++ b/README.md
@@ -132,6 +132,11 @@ object referring to that same position.
- **directSourceFile**: Like `sourceFile`, but a `sourceFile` property
will be added directly to the nodes, rather than the `loc` object.
+- **preserveParens**: If this option is `true`, parenthesized expressions
+ are represented by (non-standard) `ParenthesizedExpression` nodes
+ that have a single `expression` property containing the expression
+ inside parentheses.
+
[range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
**parseExpressionAt**`(input, offset, options)` will parse a single
diff --git a/acorn.js b/acorn.js
index 2da669c..2d740e8 100644
--- a/acorn.js
+++ b/acorn.js
@@ -112,7 +112,10 @@
sourceFile: null,
// This value, if given, is stored in every node, whether
// `locations` is on or off.
- directSourceFile: null
+ directSourceFile: null,
+ // When enabled, parenthesized expressions are represented by
+ // (non-standard) ParenthesizedExpression nodes
+ preserveParens: false
};
// This function tries to parse a single expression at a given
@@ -1459,6 +1462,10 @@
case "SpreadElement":
break;
+ case "ParenthesizedExpression":
+ checkLVal(expr.expression);
+ break;
+
default:
raise(expr.start, "Assigning to rvalue");
}
@@ -2041,6 +2048,12 @@
if (exprList[i].type === "SpreadElement") unexpected();
}
}
+
+ if (options.preserveParens) {
+ var par = startNodeAt(start);
+ par.expression = val;
+ val = finishNode(par, "ParenthesizedExpression");
+ }
}
}
return val;
diff --git a/test/tests-harmony.js b/test/tests-harmony.js
index 01010e9..1a883ee 100644
--- a/test/tests-harmony.js
+++ b/test/tests-harmony.js
@@ -15391,3 +15391,25 @@ test('function normal(x, y = 10) {}', {
}, {ecmaVersion: 6});
test("'use strict'; function f([x,,z]) {}", {}, {ecmaVersion: 6});
+
+// test preserveParens option with arrow functions
+test("() => 42", {
+ type: "Program",
+ body: [{
+ type: "ExpressionStatement",
+ expression: {
+ type: "ArrowFunctionExpression"
+ }
+ }]
+}, {ecmaVersion: 6, preserveParens: true});
+
+// test preserveParens with generators
+test("(for (x of array) for (y of array2) if (x === test) x)", {
+ type: "Program",
+ body: [{
+ type: "ExpressionStatement",
+ expression: {
+ type: "ComprehensionExpression"
+ }
+ }]
+}, {ecmaVersion: 6, preserveParens: true});
diff --git a/test/tests.js b/test/tests.js
index 5a86572..4fedc72 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -234,6 +234,123 @@ test("(1 + 2 ) * 3", {
}
});
+test("(1 + 2 ) * 3", {
+ type: "Program",
+ body: [
+ {
+ type: "ExpressionStatement",
+ expression: {
+ type: "BinaryExpression",
+ left: {
+ type: "ParenthesizedExpression",
+ expression: {
+ type: "BinaryExpression",
+ left: {
+ type: "Literal",
+ value: 1,
+ loc: {
+ start: {
+ line: 1,
+ column: 1
+ },
+ end: {
+ line: 1,
+ column: 2
+ }
+ }
+ },
+ operator: "+",
+ right: {
+ type: "Literal",
+ value: 2,
+ loc: {
+ start: {
+ line: 1,
+ column: 5
+ },
+ end: {
+ line: 1,
+ column: 6
+ }
+ }
+ },
+ loc: {
+ start: {
+ line: 1,
+ column: 1
+ },
+ end: {
+ line: 1,
+ column: 6
+ }
+ }
+ },
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 1,
+ column: 8
+ }
+ }
+ },
+ operator: "*",
+ right: {
+ type: "Literal",
+ value: 3,
+ loc: {
+ start: {
+ line: 1,
+ column: 11
+ },
+ end: {
+ line: 1,
+ column: 12
+ }
+ }
+ },
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 1,
+ column: 12
+ }
+ }
+ },
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 1,
+ column: 12
+ }
+ }
+ }
+ ],
+ loc: {
+ start: {
+ line: 1,
+ column: 0
+ },
+ end: {
+ line: 1,
+ column: 12
+ }
+ }
+}, {
+ locations: true,
+ preserveParens: true
+});
+
+test("(x) = 23", {}, { preserveParens: true });
+
test("x = []", {
type: "Program",
body: [
--
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