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

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

View File

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

View File

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

View File

@@ -1,16 +1,6 @@
// @flow
import defineType, { assertNodeType } from "./utils";
import defineType from "./utils";
defineType("Noop", {
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;
}
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 {
if (!node) return false;
@@ -2076,23 +2093,6 @@ export function isNoop(node: ?Object, opts?: Object): boolean {
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 isAwaitExpression(node: ?Object, opts?: Object): boolean {
if (!node) return false;
@@ -3242,6 +3242,7 @@ export function isExpression(node: ?Object, opts?: Object): boolean {
"NewExpression" === nodeType ||
"ObjectExpression" === nodeType ||
"SequenceExpression" === nodeType ||
"ParenthesizedExpression" === nodeType ||
"ThisExpression" === nodeType ||
"UnaryExpression" === nodeType ||
"UpdateExpression" === nodeType ||
@@ -3255,7 +3256,6 @@ export function isExpression(node: ?Object, opts?: Object): boolean {
"TypeCastExpression" === nodeType ||
"JSXElement" === nodeType ||
"JSXFragment" === nodeType ||
"ParenthesizedExpression" === nodeType ||
"AwaitExpression" === nodeType ||
"BindExpression" === nodeType ||
"OptionalMemberExpression" === nodeType ||
@@ -3545,8 +3545,8 @@ export function isExpressionWrapper(node: ?Object, opts?: Object): boolean {
if (
nodeType === "ExpressionWrapper" ||
"ExpressionStatement" === nodeType ||
"TypeCastExpression" === nodeType ||
"ParenthesizedExpression" === nodeType
"ParenthesizedExpression" === nodeType ||
"TypeCastExpression" === nodeType
) {
if (typeof opts === "undefined") {
return true;