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 Nicolò Ribaudo
parent 28c71e88e2
commit fba5655a44
43 changed files with 1187 additions and 65 deletions

View File

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

View File

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

View File

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

View File

@ -492,6 +492,7 @@ suites.forEach(function(testSuite) {
strictMode: task.options.strictMode === false ? false : true, strictMode: task.options.strictMode === false ? false : true,
sourceType: "module", sourceType: "module",
sourceMaps: !!task.sourceMap, sourceMaps: !!task.sourceMap,
...task.options.parserOpts,
}); });
const options = { const options = {
sourceFileName: path.relative(__dirname, actual.loc), sourceFileName: path.relative(__dirname, actual.loc),

View File

@ -79,6 +79,7 @@ These are the core @babel/parser (babylon) AST node types.
- [CallExpression](#callexpression) - [CallExpression](#callexpression)
- [NewExpression](#newexpression) - [NewExpression](#newexpression)
- [SequenceExpression](#sequenceexpression) - [SequenceExpression](#sequenceexpression)
- [ParenthesizedExpression](#parenthesizedexpression)
- [DoExpression](#doexpression) - [DoExpression](#doexpression)
- [Template Literals](#template-literals) - [Template Literals](#template-literals)
- [TemplateLiteral](#templateliteral) - [TemplateLiteral](#templateliteral)
@ -945,6 +946,17 @@ interface SequenceExpression <: Expression {
A sequence expression, i.e., a comma-separated sequence of expressions. A sequence expression, i.e., a comma-separated sequence of expressions.
## ParenthesizedExpression
```js
interface ParenthesizedExpression <: Expression {
type "ParenthesizedExpression";
expression: Expression;
}
```
An expression wrapped by parentheses.
## DoExpression ## DoExpression
```js ```js

View File

@ -19,6 +19,7 @@ export type Options = {
strictMode: ?boolean, strictMode: ?boolean,
ranges: boolean, ranges: boolean,
tokens: boolean, tokens: boolean,
createParenthesizedExpressions: boolean,
}; };
export const defaultOptions: Options = { export const defaultOptions: Options = {
@ -55,6 +56,9 @@ export const defaultOptions: Options = {
ranges: false, ranges: false,
// Adds all parsed tokens to a `tokens` property on the `File` node // Adds all parsed tokens to a `tokens` property on the `File` node
tokens: false, tokens: false,
// Whether to create ParenthesizedExpression AST nodes (if false
// the parser sets extra.parenthesized on the expression nodes instead).
createParenthesizedExpressions: false,
}; };
// Interpret and default an options object // Interpret and default an options object

View File

@ -40,6 +40,12 @@ import {
SCOPE_PROGRAM, SCOPE_PROGRAM,
} from "../util/scopeflags"; } from "../util/scopeflags";
const unwrapParenthesizedExpression = node => {
return node.type === "ParenthesizedExpression"
? unwrapParenthesizedExpression(node.expression)
: node;
};
export default class ExpressionParser extends LValParser { export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js // Forward-declaration: defined in statement.js
+parseBlock: ( +parseBlock: (
@ -204,16 +210,22 @@ export default class ExpressionParser extends LValParser {
this.checkLVal(left, undefined, undefined, "assignment expression"); this.checkLVal(left, undefined, undefined, "assignment expression");
const maybePattern = unwrapParenthesizedExpression(left);
let patternErrorMsg; let patternErrorMsg;
if (left.type === "ObjectPattern") { if (maybePattern.type === "ObjectPattern") {
patternErrorMsg = "`({a}) = 0` use `({a} = 0)`"; patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
} else if (left.type === "ArrayPattern") { } else if (maybePattern.type === "ArrayPattern") {
patternErrorMsg = "`([a]) = 0` use `([a] = 0)`"; patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
} }
if (patternErrorMsg && left.extra && left.extra.parenthesized) { if (
patternErrorMsg &&
((left.extra && left.extra.parenthesized) ||
left.type === "ParenthesizedExpression")
) {
this.raise( this.raise(
left.start, maybePattern.start,
`You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`,
); );
} }
@ -326,7 +338,8 @@ export default class ExpressionParser extends LValParser {
if ( if (
operator === "**" && operator === "**" &&
left.type === "UnaryExpression" && left.type === "UnaryExpression" &&
!(left.extra && left.extra.parenthesized) (this.options.createParenthesizedExpressions ||
!(left.extra && left.extra.parenthesized))
) { ) {
this.raise( this.raise(
left.argument.start, left.argument.start,
@ -1161,13 +1174,6 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, type); return this.finishNode(node, type);
} }
parseParenExpression(): N.Expression {
this.expect(tt.parenL);
const val = this.parseExpression();
this.expect(tt.parenR);
return val;
}
parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression { parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression {
const startPos = this.state.start; const startPos = this.state.start;
const startLoc = this.state.startLoc; const startLoc = this.state.startLoc;
@ -1278,10 +1284,16 @@ export default class ExpressionParser extends LValParser {
val = exprList[0]; val = exprList[0];
} }
this.addExtra(val, "parenthesized", true); if (!this.options.createParenthesizedExpressions) {
this.addExtra(val, "parenStart", startPos); this.addExtra(val, "parenthesized", true);
this.addExtra(val, "parenStart", startPos);
return val;
}
return val; const parenExpression = this.startNodeAt(startPos, startLoc);
parenExpression.expression = val;
this.finishNode(parenExpression, "ParenthesizedExpression");
return parenExpression;
} }
shouldParseArrow(): boolean { shouldParseArrow(): boolean {

View File

@ -93,6 +93,14 @@ export default class LValParser extends NodeUtils {
} }
break; break;
case "ParenthesizedExpression":
node.expression = this.toAssignable(
node.expression,
isBinding,
contextDescription,
);
break;
case "MemberExpression": case "MemberExpression":
if (!isBinding) break; if (!isBinding) break;
@ -413,6 +421,15 @@ export default class LValParser extends NodeUtils {
); );
break; break;
case "ParenthesizedExpression":
this.checkLVal(
expr.expression,
bindingType,
checkClashes,
"parenthesized expression",
);
break;
default: { default: {
const message = const message =
(bindingType === BIND_NONE (bindingType === BIND_NONE

View File

@ -453,6 +453,13 @@ export default class StatementParser extends ExpressionParser {
return this.finishNode(node, "DebuggerStatement"); return this.finishNode(node, "DebuggerStatement");
} }
parseHeaderExpression(): N.Expression {
this.expect(tt.parenL);
const val = this.parseExpression();
this.expect(tt.parenR);
return val;
}
parseDoStatement(node: N.DoWhileStatement): N.DoWhileStatement { parseDoStatement(node: N.DoWhileStatement): N.DoWhileStatement {
this.next(); this.next();
this.state.labels.push(loopLabel); this.state.labels.push(loopLabel);
@ -469,7 +476,7 @@ export default class StatementParser extends ExpressionParser {
this.state.labels.pop(); this.state.labels.pop();
this.expect(tt._while); this.expect(tt._while);
node.test = this.parseParenExpression(); node.test = this.parseHeaderExpression();
this.eat(tt.semi); this.eat(tt.semi);
return this.finishNode(node, "DoWhileStatement"); return this.finishNode(node, "DoWhileStatement");
} }
@ -567,7 +574,7 @@ export default class StatementParser extends ExpressionParser {
parseIfStatement(node: N.IfStatement): N.IfStatement { parseIfStatement(node: N.IfStatement): N.IfStatement {
this.next(); this.next();
node.test = this.parseParenExpression(); node.test = this.parseHeaderExpression();
node.consequent = this.parseStatement("if"); node.consequent = this.parseStatement("if");
node.alternate = this.eat(tt._else) ? this.parseStatement("if") : null; node.alternate = this.eat(tt._else) ? this.parseStatement("if") : null;
return this.finishNode(node, "IfStatement"); return this.finishNode(node, "IfStatement");
@ -596,7 +603,7 @@ export default class StatementParser extends ExpressionParser {
parseSwitchStatement(node: N.SwitchStatement): N.SwitchStatement { parseSwitchStatement(node: N.SwitchStatement): N.SwitchStatement {
this.next(); this.next();
node.discriminant = this.parseParenExpression(); node.discriminant = this.parseHeaderExpression();
const cases = (node.cases = []); const cases = (node.cases = []);
this.expect(tt.braceL); this.expect(tt.braceL);
this.state.labels.push(switchLabel); this.state.labels.push(switchLabel);
@ -715,7 +722,7 @@ export default class StatementParser extends ExpressionParser {
parseWhileStatement(node: N.WhileStatement): N.WhileStatement { parseWhileStatement(node: N.WhileStatement): N.WhileStatement {
this.next(); this.next();
node.test = this.parseParenExpression(); node.test = this.parseHeaderExpression();
this.state.labels.push(loopLabel); this.state.labels.push(loopLabel);
node.body = node.body =
@ -737,7 +744,7 @@ export default class StatementParser extends ExpressionParser {
this.raise(this.state.start, "'with' in strict mode"); this.raise(this.state.start, "'with' in strict mode");
} }
this.next(); this.next();
node.object = this.parseParenExpression(); node.object = this.parseHeaderExpression();
node.body = node.body =
// For the smartPipelines plugin: // For the smartPipelines plugin:

View File

@ -574,6 +574,11 @@ export type SequenceExpression = NodeBase & {
expressions: $ReadOnlyArray<Expression>, expressions: $ReadOnlyArray<Expression>,
}; };
export type ParenthesizedExpression = NodeBase & {
type: "ParenthesizedExpression",
expression: Expression,
};
// Pipelines // Pipelines
export type PipelineBody = NodeBase & { export type PipelineBody = NodeBase & {

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

@ -78,6 +78,14 @@ export interface ParserOptions {
* Adds all parsed tokens to a tokens property on the File node. * Adds all parsed tokens to a tokens property on the File node.
*/ */
tokens?: boolean; tokens?: boolean;
/**
* By default, the parser adds information about parentheses by setting
* `extra.parenthesized` to `true` as needed.
* When this option is `true` the parser creates `ParenthesizedExpression`
* AST nodes instead of using the `extra` property.
*/
createParenthesizedExpressions?: boolean;
} }
export type ParserPlugin = export type ParserPlugin =

View File

@ -102,6 +102,10 @@ export function SequenceExpression() {
.getTypeAnnotation(); .getTypeAnnotation();
} }
export function ParenthesizedExpression() {
return this.get("expression").getTypeAnnotation();
}
export function AssignmentExpression() { export function AssignmentExpression() {
return this.get("right").getTypeAnnotation(); return this.get("right").getTypeAnnotation();
} }

View File

@ -162,6 +162,12 @@ export function assertSequenceExpression(
): void { ): void {
assert("SequenceExpression", node, opts); assert("SequenceExpression", node, opts);
} }
export function assertParenthesizedExpression(
node: Object,
opts?: Object = {},
): void {
assert("ParenthesizedExpression", node, opts);
}
export function assertSwitchCase(node: Object, opts?: Object = {}): void { export function assertSwitchCase(node: Object, opts?: Object = {}): void {
assert("SwitchCase", node, opts); assert("SwitchCase", node, opts);
} }
@ -654,12 +660,6 @@ export function assertJSXClosingFragment(
export function assertNoop(node: Object, opts?: Object = {}): void { export function assertNoop(node: Object, opts?: Object = {}): void {
assert("Noop", node, opts); assert("Noop", node, opts);
} }
export function assertParenthesizedExpression(
node: Object,
opts?: Object = {},
): void {
assert("ParenthesizedExpression", node, opts);
}
export function assertArgumentPlaceholder( export function assertArgumentPlaceholder(
node: Object, node: Object,
opts?: Object = {}, opts?: Object = {},

View File

@ -161,6 +161,10 @@ export function SequenceExpression(...args: Array<any>): Object {
return builder("SequenceExpression", ...args); return builder("SequenceExpression", ...args);
} }
export { SequenceExpression as sequenceExpression }; export { SequenceExpression as sequenceExpression };
export function ParenthesizedExpression(...args: Array<any>): Object {
return builder("ParenthesizedExpression", ...args);
}
export { ParenthesizedExpression as parenthesizedExpression };
export function SwitchCase(...args: Array<any>): Object { export function SwitchCase(...args: Array<any>): Object {
return builder("SwitchCase", ...args); return builder("SwitchCase", ...args);
} }
@ -592,10 +596,6 @@ export function Noop(...args: Array<any>): Object {
return builder("Noop", ...args); return builder("Noop", ...args);
} }
export { Noop as noop }; export { Noop as noop };
export function ParenthesizedExpression(...args: Array<any>): Object {
return builder("ParenthesizedExpression", ...args);
}
export { ParenthesizedExpression as parenthesizedExpression };
export function ArgumentPlaceholder(...args: Array<any>): Object { export function ArgumentPlaceholder(...args: Array<any>): Object {
return builder("ArgumentPlaceholder", ...args); return builder("ArgumentPlaceholder", ...args);
} }

View File

@ -736,6 +736,16 @@ defineType("SequenceExpression", {
aliases: ["Expression"], aliases: ["Expression"],
}); });
defineType("ParenthesizedExpression", {
visitor: ["expression"],
aliases: ["Expression", "ExpressionWrapper"],
fields: {
expression: {
validate: assertNodeType("Expression"),
},
},
});
defineType("SwitchCase", { defineType("SwitchCase", {
visitor: ["test", "consequent"], visitor: ["test", "consequent"],
fields: { fields: {

View File

@ -1,16 +1,6 @@
// @flow // @flow
import defineType, { assertNodeType } from "./utils"; import defineType from "./utils";
defineType("Noop", { defineType("Noop", {
visitor: [], visitor: [],
}); });
defineType("ParenthesizedExpression", {
visitor: ["expression"],
aliases: ["Expression", "ExpressionWrapper"],
fields: {
expression: {
validate: assertNodeType("Expression"),
},
},
});

View File

@ -551,6 +551,23 @@ export function isSequenceExpression(node: ?Object, opts?: Object): boolean {
return false; return false;
} }
export function isParenthesizedExpression(
node: ?Object,
opts?: Object,
): boolean {
if (!node) return false;
const nodeType = node.type;
if (nodeType === "ParenthesizedExpression") {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}
return false;
}
export function isSwitchCase(node: ?Object, opts?: Object): boolean { export function isSwitchCase(node: ?Object, opts?: Object): boolean {
if (!node) return false; if (!node) return false;
@ -2076,23 +2093,6 @@ export function isNoop(node: ?Object, opts?: Object): boolean {
return false; return false;
} }
export function isParenthesizedExpression(
node: ?Object,
opts?: Object,
): boolean {
if (!node) return false;
const nodeType = node.type;
if (nodeType === "ParenthesizedExpression") {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}
return false;
}
export function isArgumentPlaceholder(node: ?Object, opts?: Object): boolean { export function isArgumentPlaceholder(node: ?Object, opts?: Object): boolean {
if (!node) return false; if (!node) return false;
@ -3256,6 +3256,7 @@ export function isExpression(node: ?Object, opts?: Object): boolean {
"NewExpression" === nodeType || "NewExpression" === nodeType ||
"ObjectExpression" === nodeType || "ObjectExpression" === nodeType ||
"SequenceExpression" === nodeType || "SequenceExpression" === nodeType ||
"ParenthesizedExpression" === nodeType ||
"ThisExpression" === nodeType || "ThisExpression" === nodeType ||
"UnaryExpression" === nodeType || "UnaryExpression" === nodeType ||
"UpdateExpression" === nodeType || "UpdateExpression" === nodeType ||
@ -3269,7 +3270,6 @@ export function isExpression(node: ?Object, opts?: Object): boolean {
"TypeCastExpression" === nodeType || "TypeCastExpression" === nodeType ||
"JSXElement" === nodeType || "JSXElement" === nodeType ||
"JSXFragment" === nodeType || "JSXFragment" === nodeType ||
"ParenthesizedExpression" === nodeType ||
"AwaitExpression" === nodeType || "AwaitExpression" === nodeType ||
"BindExpression" === nodeType || "BindExpression" === nodeType ||
"OptionalMemberExpression" === nodeType || "OptionalMemberExpression" === nodeType ||
@ -3559,8 +3559,8 @@ export function isExpressionWrapper(node: ?Object, opts?: Object): boolean {
if ( if (
nodeType === "ExpressionWrapper" || nodeType === "ExpressionWrapper" ||
"ExpressionStatement" === nodeType || "ExpressionStatement" === nodeType ||
"TypeCastExpression" === nodeType || "ParenthesizedExpression" === nodeType ||
"ParenthesizedExpression" === nodeType "TypeCastExpression" === nodeType
) { ) {
if (typeof opts === "undefined") { if (typeof opts === "undefined") {
return true; return true;