Skip to content

Commit

Permalink
Update babylon beta 3 (babel#5394)
Browse files Browse the repository at this point in the history
* Update babylon to v7-beta.3

* convert RestProperty/SpreadProperty to RestElement/SpreadElement

* add virtual types to make it easier to upgrade
  • Loading branch information
hzoo authored and nitin42 committed Mar 14, 2017
1 parent d13547e commit 1f9a284
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 174 deletions.
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -34,7 +34,7 @@
"babel-register": "^6.23.0",
"babel-traverse": "^6.23.1",
"babel-types": "^6.23.0",
"babylon": "7.0.0-beta.0",
"babylon": "7.0.0-beta.3",
"convert-source-map": "^1.1.0",
"debug": "^2.1.1",
"json5": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -21,6 +21,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "^6.22.0",
"babylon": "7.0.0-beta.0"
"babylon": "7.0.0-beta.3"
}
}
2 changes: 0 additions & 2 deletions packages/babel-generator/src/generators/types.js
Expand Up @@ -12,8 +12,6 @@ export function RestElement(node: Object) {

export {
RestElement as SpreadElement,
RestElement as SpreadProperty,
RestElement as RestProperty,
};

export function ObjectExpression(node: Object) {
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-generator/src/node/whitespace.js
Expand Up @@ -161,13 +161,12 @@ export const nodes = {
};

/**
* Test if Property or SpreadProperty needs whitespace.
* Test if Property needs whitespace.
*/

nodes.ObjectProperty =
nodes.ObjectTypeProperty =
nodes.ObjectMethod =
nodes.SpreadProperty = function (node: Object, parent): ?WhitespaceObject {
nodes.ObjectMethod = function (node: Object, parent): ?WhitespaceObject {
if (parent.properties[0] === node) {
return {
before: true
Expand Down
Expand Up @@ -140,7 +140,7 @@ export default function ({ types: t }) {
if (i >= spreadPropIndex) break;

// ignore other spread properties
if (t.isRestProperty(prop)) continue;
if (t.isRestElement(prop)) continue;

let key = prop.key;
if (t.isIdentifier(key) && !prop.computed) key = t.stringLiteral(prop.key.name);
Expand Down Expand Up @@ -192,7 +192,7 @@ export default function ({ types: t }) {

for (let i = 0; i < pattern.properties.length; i++) {
const prop = pattern.properties[i];
if (t.isRestProperty(prop)) {
if (t.isRestElement(prop)) {
this.pushObjectRest(pattern, objRef, prop, i);
} else {
this.pushObjectProperty(prop, objRef);
Expand Down
Expand Up @@ -12,7 +12,7 @@ export default function() {
visitor: {
ObjectExpression(path) {
const { node } = path;
const plainProps = node.properties.filter((prop) => !t.isSpreadProperty(prop) && !prop.computed);
const plainProps = node.properties.filter((prop) => !t.isSpreadElement(prop) && !prop.computed);

// A property is a duplicate key if:
// * the property is a data property, and is preceeded by a data,
Expand Down
36 changes: 18 additions & 18 deletions packages/babel-plugin-transform-object-rest-spread/src/index.js
@@ -1,28 +1,28 @@
import syntaxObjectRestSpread from "babel-plugin-syntax-object-rest-spread";

export default function ({ types: t }) {
function hasRestProperty(path) {
let foundRestProperty = false;
function hasRestElement(path) {
let foundRestElement = false;
path.traverse({
RestProperty() {
foundRestProperty = true;
RestElement() {
foundRestElement = true;
path.stop();
}
});
return foundRestProperty;
return foundRestElement;
}

function hasSpread(node) {
for (const prop of (node.properties)) {
if (t.isSpreadProperty(prop)) {
if (t.isSpreadElement(prop)) {
return true;
}
}
return false;
}

function createObjectSpread(file, props, objRef) {
const restProperty = props.pop();
const restElement = props.pop();

const keys = [];
for (const prop of props) {
Expand All @@ -34,7 +34,7 @@ export default function ({ types: t }) {
}

return [
restProperty.argument,
restElement.argument,
t.callExpression(
file.addHelper("objectWithoutProperties"), [
objRef,
Expand All @@ -44,13 +44,13 @@ export default function ({ types: t }) {
];
}

function replaceRestProperty(parentPath, paramPath, i, numParams) {
function replaceRestElement(parentPath, paramPath, i, numParams) {
if (paramPath.isAssignmentPattern()) {
replaceRestProperty(parentPath, paramPath.get("left"), i, numParams);
replaceRestElement(parentPath, paramPath.get("left"), i, numParams);
return;
}

if (paramPath.isObjectPattern() && hasRestProperty(paramPath)) {
if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
const uid = parentPath.scope.generateUidIdentifier("ref");

const declar = t.variableDeclaration("let", [
Expand All @@ -73,7 +73,7 @@ export default function ({ types: t }) {
Function(path) {
const params = path.get("params");
for (let i = 0; i < params.length; i++) {
replaceRestProperty(params[i].parentPath, params[i], i, params.length);
replaceRestElement(params[i].parentPath, params[i], i, params.length);
}
},
// adapted from transform-es2015-destructuring/src/index.js#pushObjectRest
Expand All @@ -84,7 +84,7 @@ export default function ({ types: t }) {
let insertionPath = path;

path.get("id").traverse({
RestProperty(path) {
RestElement(path) {
if (
// skip single-property case, e.g.
// const { ...x } = foo();
Expand Down Expand Up @@ -148,7 +148,7 @@ export default function ({ types: t }) {
ExportNamedDeclaration(path) {
const declaration = path.get("declaration");
if (!declaration.isVariableDeclaration()) return;
if (!hasRestProperty(declaration)) return;
if (!hasRestElement(declaration)) return;

const specifiers = [];

Expand All @@ -166,12 +166,12 @@ export default function ({ types: t }) {
// try {} catch ({a, ...b}) {}
CatchClause(path) {
const paramPath = path.get("param");
replaceRestProperty(paramPath.parentPath, paramPath);
replaceRestElement(paramPath.parentPath, paramPath);
},
// ({a, ...b} = c);
AssignmentExpression(path, file) {
const leftPath = path.get("left");
if (leftPath.isObjectPattern() && hasRestProperty(leftPath)) {
if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
const nodes = [];

let ref;
Expand Down Expand Up @@ -212,7 +212,7 @@ export default function ({ types: t }) {
const left = node.left;

// for ({a, ...b} of []) {}
if (t.isObjectPattern(left) && hasRestProperty(leftPath)) {
if (t.isObjectPattern(left) && hasRestElement(leftPath)) {
const temp = scope.generateUidIdentifier("ref");

node.left = t.variableDeclaration("var", [
Expand Down Expand Up @@ -266,7 +266,7 @@ export default function ({ types: t }) {
}

for (const prop of (path.node.properties: Array)) {
if (t.isSpreadProperty(prop)) {
if (t.isSpreadElement(prop)) {
push();
args.push(prop.argument);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-template/package.json
Expand Up @@ -8,7 +8,7 @@
"repository": "https://github.com/babel/babel/tree/master/packages/babel-template",
"main": "lib/index.js",
"dependencies": {
"babylon": "7.0.0-beta.0",
"babylon": "7.0.0-beta.3",
"babel-traverse": "^6.23.0",
"babel-types": "^6.23.0",
"lodash": "^4.2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/package.json
Expand Up @@ -11,7 +11,7 @@
"babel-code-frame": "^6.22.0",
"babel-messages": "^6.23.0",
"babel-types": "^6.23.0",
"babylon": "7.0.0-beta.0",
"babylon": "7.0.0-beta.3",
"debug": "^2.2.0",
"globals": "^9.0.0",
"invariant": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/evaluation.js
Expand Up @@ -233,7 +233,7 @@ export function evaluate(): { confident: boolean; value: any } {
const obj = {};
const props: Array<NodePath> = path.get("properties");
for (const prop of props) {
if (prop.isObjectMethod() || prop.isSpreadProperty()) {
if (prop.isObjectMethod() || prop.isSpreadElement()) {
return deopt(prop);
}
const keyPath = prop.get("key");
Expand Down
30 changes: 30 additions & 0 deletions packages/babel-traverse/src/path/lib/virtual-types.js
Expand Up @@ -120,3 +120,33 @@ export const Flow = {
}
}
};

// TODO: 7.0 Backwards Compat
export const RestProperty = {
types: ["RestElement"],
checkPath(path: NodePath): boolean {
return path.parentPath && path.parentPath.isObjectPattern();
},
};

export const SpreadProperty = {
types: ["RestElement"],
checkPath(path: NodePath): boolean {
return path.parentPath && path.parentPath.isObjectExpression();
},
};

export const ExistentialTypeParam = {
types: ["ExistsTypeAnnotation"]
};

export const NumericLiteralTypeAnnotation = {
types: ["NumberLiteralTypeAnnotation"]
};

export const ForAwaitStatement = {
types: ["ForOfStatement"],
checkPath({ node }: NodePath): boolean {
return node.await === true;
}
};
2 changes: 1 addition & 1 deletion packages/babel-types/package.json
Expand Up @@ -13,6 +13,6 @@
"to-fast-properties": "^1.0.1"
},
"devDependencies": {
"babylon": "7.0.0-beta.0"
"babylon": "7.0.0-beta.3"
}
}
2 changes: 1 addition & 1 deletion packages/babel-types/src/definitions/core.js
Expand Up @@ -470,7 +470,7 @@ defineType("ObjectExpression", {
properties: {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("ObjectMethod", "ObjectProperty", "SpreadProperty"))
assertEach(assertNodeType("ObjectMethod", "ObjectProperty", "SpreadElement"))
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/src/definitions/es2015.js
Expand Up @@ -298,7 +298,7 @@ defineType("ObjectPattern", {
aliases: ["Pattern", "LVal"],
fields: {
properties: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("RestProperty", "Property")))
validate: chain(assertValueType("array"), assertEach(assertNodeType("RestElement", "Property")))
},
decorators: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Decorator")))
Expand Down
20 changes: 0 additions & 20 deletions packages/babel-types/src/definitions/experimental.js
Expand Up @@ -61,23 +61,3 @@ defineType("ExportNamespaceSpecifier", {
}
}
});

defineType("RestProperty", {
visitor: ["argument"],
aliases: ["UnaryLike"],
fields: {
argument: {
validate: assertNodeType("LVal")
}
}
});

defineType("SpreadProperty", {
visitor: ["argument"],
aliases: ["UnaryLike"],
fields: {
argument: {
validate: assertNodeType("Expression")
}
}
});
1 change: 0 additions & 1 deletion packages/babel-types/src/retrievers.js
Expand Up @@ -94,7 +94,6 @@ getBindingIdentifiers.keys = {
RestElement: ["argument"],
UpdateExpression: ["argument"],

RestProperty: ["argument"],
ObjectProperty: ["value"],

AssignmentPattern: ["left"],
Expand Down

0 comments on commit 1f9a284

Please sign in to comment.