Parenthesized expressions (#8025)

* Add parser createParenthesizedExpressions option  …

When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.

* Also update babel-parser.d.ts
This commit is contained in:
Erik Arvidsson
2019-02-22 23:45:25 -08:00
committed by Justin Ridgewell
parent 417e72ebfd
commit dd8b700a2c
43 changed files with 1186 additions and 65 deletions

View File

@@ -0,0 +1,12 @@
// One
(1);
/* Two */
(2);
(
// Three
3
);
(/* Four */ 4);

View File

@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}

View File

@@ -0,0 +1,391 @@
{
"type": "File",
"start": 0,
"end": 65,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"column": 15
}
},
"program": {
"type": "Program",
"start": 0,
"end": 65,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"column": 15
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 7,
"end": 11,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 4
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
}
},
"expression": {
"type": "NumericLiteral",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 2
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
"leadingComments": [
{
"type": "CommentLine",
"value": " One",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
}
}
],
"trailingComments": [
{
"type": "CommentBlock",
"value": " Two ",
"start": 13,
"end": 22,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 9
}
}
}
]
},
{
"type": "ExpressionStatement",
"start": 23,
"end": 27,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 4
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 23,
"end": 26,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 3
}
},
"expression": {
"type": "NumericLiteral",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 5,
"column": 1
},
"end": {
"line": 5,
"column": 2
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " Two ",
"start": 13,
"end": 22,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 9
}
}
}
]
},
{
"type": "ExpressionStatement",
"start": 29,
"end": 48,
"loc": {
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 10,
"column": 2
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 29,
"end": 47,
"loc": {
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 10,
"column": 1
}
},
"expression": {
"type": "NumericLiteral",
"start": 44,
"end": 45,
"loc": {
"start": {
"line": 9,
"column": 2
},
"end": {
"line": 9,
"column": 3
}
},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3,
"leadingComments": [
{
"type": "CommentLine",
"value": " Three",
"start": 33,
"end": 41,
"loc": {
"start": {
"line": 8,
"column": 2
},
"end": {
"line": 8,
"column": 10
}
}
}
]
}
}
},
{
"type": "ExpressionStatement",
"start": 50,
"end": 65,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 15
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 50,
"end": 64,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 14
}
},
"expression": {
"type": "NumericLiteral",
"start": 62,
"end": 63,
"loc": {
"start": {
"line": 12,
"column": 12
},
"end": {
"line": 12,
"column": 13
}
},
"extra": {
"rawValue": 4,
"raw": "4"
},
"value": 4,
"leadingComments": [
{
"type": "CommentBlock",
"value": " Four ",
"start": 51,
"end": 61,
"loc": {
"start": {
"line": 12,
"column": 1
},
"end": {
"line": 12,
"column": 11
}
}
}
]
}
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " One",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
}
},
{
"type": "CommentBlock",
"value": " Two ",
"start": 13,
"end": 22,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 9
}
}
},
{
"type": "CommentLine",
"value": " Three",
"start": 33,
"end": 41,
"loc": {
"start": {
"line": 8,
"column": 2
},
"end": {
"line": 8,
"column": 10
}
}
},
{
"type": "CommentBlock",
"value": " Four ",
"start": 51,
"end": 61,
"loc": {
"start": {
"line": 12,
"column": 1
},
"end": {
"line": 12,
"column": 11
}
}
}
]
}

View File

@@ -0,0 +1 @@
({x}) = {x: 1};

View File

@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": true,
"throws": "You're trying to assign to a parenthesized expression, eg. instead of `({a}) = 0` use `({a} = 0)` (1:1)"
}

View File

@@ -0,0 +1 @@
("hello");

View File

@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}

View File

@@ -0,0 +1,85 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"expression": {
"type": "StringLiteral",
"start": 1,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 8
}
},
"extra": {
"rawValue": "hello",
"raw": "\"hello\""
},
"value": "hello"
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
([a]) = []

View File

@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": true,
"throws": "You're trying to assign to a parenthesized expression, eg. instead of `([a]) = 0` use `([a] = 0)` (1:1)"
}

View File

@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}

View File

@@ -0,0 +1,104 @@
{
"type": "File",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"program": {
"type": "Program",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "foo"
},
"name": "foo"
}
],
"body": {
"type": "BlockStatement",
"start": 9,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 11
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,4 @@
var a;
(a) = {};
(a.b) = {};
(a['c']) = {};

View File

@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}

View File

@@ -0,0 +1,392 @@
{
"type": "File",
"start": 0,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 14
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "a"
},
"name": "a"
},
"init": null
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start": 7,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 9
}
},
"expression": {
"type": "AssignmentExpression",
"start": 7,
"end": 15,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 8
}
},
"operator": "=",
"left": {
"type": "ParenthesizedExpression",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
}
},
"expression": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 2
},
"identifierName": "a"
},
"name": "a"
}
},
"right": {
"type": "ObjectExpression",
"start": 13,
"end": 15,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 8
}
},
"properties": []
}
}
},
{
"type": "ExpressionStatement",
"start": 17,
"end": 28,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 11
}
},
"expression": {
"type": "AssignmentExpression",
"start": 17,
"end": 27,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 10
}
},
"operator": "=",
"left": {
"type": "ParenthesizedExpression",
"start": 17,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 5
}
},
"expression": {
"type": "MemberExpression",
"start": 18,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 1
},
"end": {
"line": 3,
"column": 4
}
},
"object": {
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 1
},
"end": {
"line": 3,
"column": 2
},
"identifierName": "a"
},
"name": "a"
},
"property": {
"type": "Identifier",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 4
},
"identifierName": "b"
},
"name": "b"
},
"computed": false
}
},
"right": {
"type": "ObjectExpression",
"start": 25,
"end": 27,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 10
}
},
"properties": []
}
}
},
{
"type": "ExpressionStatement",
"start": 29,
"end": 43,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 14
}
},
"expression": {
"type": "AssignmentExpression",
"start": 29,
"end": 42,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 13
}
},
"operator": "=",
"left": {
"type": "ParenthesizedExpression",
"start": 29,
"end": 37,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 8
}
},
"expression": {
"type": "MemberExpression",
"start": 30,
"end": 36,
"loc": {
"start": {
"line": 4,
"column": 1
},
"end": {
"line": 4,
"column": 7
}
},
"object": {
"type": "Identifier",
"start": 30,
"end": 31,
"loc": {
"start": {
"line": 4,
"column": 1
},
"end": {
"line": 4,
"column": 2
},
"identifierName": "a"
},
"name": "a"
},
"property": {
"type": "StringLiteral",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 6
}
},
"extra": {
"rawValue": "c",
"raw": "'c'"
},
"value": "c"
},
"computed": true
}
},
"right": {
"type": "ObjectExpression",
"start": 40,
"end": 42,
"loc": {
"start": {
"line": 4,
"column": 11
},
"end": {
"line": 4,
"column": 13
}
},
"properties": []
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
([a.a]) => 42

View File

@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": true,
"throws": "Invalid left-hand side in arrow function parameters (1:2)"
}

View File

@@ -0,0 +1 @@
-(5) ** 6;

View File

@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": true,
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:1)"
}

View File

@@ -0,0 +1 @@
(-5 ** 6);

View File

@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": true,
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)"
}

View File

@@ -124,4 +124,4 @@
],
"directives": []
}
}
}

View File

@@ -160,4 +160,4 @@
],
"directives": []
}
}
}

View File

@@ -156,4 +156,4 @@
],
"directives": []
}
}
}

View File

@@ -124,4 +124,4 @@
],
"directives": []
}
}
}