Run prettier
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import syntaxObjectRestSpread from "babel-plugin-syntax-object-rest-spread";
|
||||
|
||||
export default function ({ types: t }) {
|
||||
export default function({ types: t }) {
|
||||
function hasRestElement(path) {
|
||||
let foundRestElement = false;
|
||||
path.traverse({
|
||||
@@ -13,7 +13,7 @@ export default function ({ types: t }) {
|
||||
}
|
||||
|
||||
function hasSpread(node) {
|
||||
for (const prop of (node.properties)) {
|
||||
for (const prop of node.properties) {
|
||||
if (t.isSpreadElement(prop)) {
|
||||
return true;
|
||||
}
|
||||
@@ -35,12 +35,10 @@ export default function ({ types: t }) {
|
||||
|
||||
return [
|
||||
restElement.argument,
|
||||
t.callExpression(
|
||||
file.addHelper("objectWithoutProperties"), [
|
||||
objRef,
|
||||
t.arrayExpression(keys),
|
||||
]
|
||||
),
|
||||
t.callExpression(file.addHelper("objectWithoutProperties"), [
|
||||
objRef,
|
||||
t.arrayExpression(keys),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -87,76 +85,85 @@ export default function ({ types: t }) {
|
||||
// adapted from transform-es2015-destructuring/src/index.js#pushObjectRest
|
||||
// const { a, ...b } = c;
|
||||
VariableDeclarator(path, file) {
|
||||
if (!path.get("id").isObjectPattern()) { return; }
|
||||
if (!path.get("id").isObjectPattern()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let insertionPath = path;
|
||||
|
||||
path.get("id").traverse({
|
||||
RestElement(path) {
|
||||
if (
|
||||
// skip single-property case, e.g.
|
||||
// const { ...x } = foo();
|
||||
// since the RHS will not be duplicated
|
||||
this.originalPath.node.id.properties.length > 1 &&
|
||||
!t.isIdentifier(this.originalPath.node.init)
|
||||
) {
|
||||
// const { a, ...b } = foo();
|
||||
// to avoid calling foo() twice, as a first step convert it to:
|
||||
// const _foo = foo(),
|
||||
// { a, ...b } = _foo;
|
||||
const initRef = path.scope.generateUidIdentifierBasedOnNode(
|
||||
this.originalPath.node.init, "ref");
|
||||
// insert _foo = foo()
|
||||
this.originalPath.insertBefore(t.variableDeclarator(initRef,
|
||||
this.originalPath.node.init));
|
||||
// replace foo() with _foo
|
||||
this.originalPath.replaceWith(t.variableDeclarator(
|
||||
this.originalPath.node.id, initRef));
|
||||
path.get("id").traverse(
|
||||
{
|
||||
RestElement(path) {
|
||||
if (
|
||||
// skip single-property case, e.g.
|
||||
// const { ...x } = foo();
|
||||
// since the RHS will not be duplicated
|
||||
this.originalPath.node.id.properties.length > 1 &&
|
||||
!t.isIdentifier(this.originalPath.node.init)
|
||||
) {
|
||||
// const { a, ...b } = foo();
|
||||
// to avoid calling foo() twice, as a first step convert it to:
|
||||
// const _foo = foo(),
|
||||
// { a, ...b } = _foo;
|
||||
const initRef = path.scope.generateUidIdentifierBasedOnNode(
|
||||
this.originalPath.node.init,
|
||||
"ref",
|
||||
);
|
||||
// insert _foo = foo()
|
||||
this.originalPath.insertBefore(
|
||||
t.variableDeclarator(initRef, this.originalPath.node.init),
|
||||
);
|
||||
// replace foo() with _foo
|
||||
this.originalPath.replaceWith(
|
||||
t.variableDeclarator(this.originalPath.node.id, initRef),
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let ref = this.originalPath.node.init;
|
||||
const refPropertyPath = [];
|
||||
|
||||
path.findParent((path) => {
|
||||
if (path.isObjectProperty()) {
|
||||
refPropertyPath.unshift(path.node.key.name);
|
||||
} else if (path.isVariableDeclarator()) {
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
if (refPropertyPath.length) {
|
||||
refPropertyPath.forEach((prop) => {
|
||||
ref = t.memberExpression(ref, t.identifier(prop));
|
||||
let ref = this.originalPath.node.init;
|
||||
const refPropertyPath = [];
|
||||
|
||||
path.findParent(path => {
|
||||
if (path.isObjectProperty()) {
|
||||
refPropertyPath.unshift(path.node.key.name);
|
||||
} else if (path.isVariableDeclarator()) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const [ argument, callExpression ] = createObjectSpread(
|
||||
file,
|
||||
path.parentPath.node.properties,
|
||||
ref
|
||||
);
|
||||
if (refPropertyPath.length) {
|
||||
refPropertyPath.forEach(prop => {
|
||||
ref = t.memberExpression(ref, t.identifier(prop));
|
||||
});
|
||||
}
|
||||
|
||||
insertionPath.insertAfter(
|
||||
t.variableDeclarator(
|
||||
argument,
|
||||
callExpression
|
||||
)
|
||||
);
|
||||
const [argument, callExpression] = createObjectSpread(
|
||||
file,
|
||||
path.parentPath.node.properties,
|
||||
ref,
|
||||
);
|
||||
|
||||
insertionPath = insertionPath.getSibling(insertionPath.key + 1);
|
||||
insertionPath.insertAfter(
|
||||
t.variableDeclarator(argument, callExpression),
|
||||
);
|
||||
|
||||
if (path.parentPath.node.properties.length === 0) {
|
||||
path.findParent(
|
||||
(path) => path.isObjectProperty() || path.isVariableDeclarator()
|
||||
).remove();
|
||||
}
|
||||
insertionPath = insertionPath.getSibling(insertionPath.key + 1);
|
||||
|
||||
if (path.parentPath.node.properties.length === 0) {
|
||||
path
|
||||
.findParent(
|
||||
path =>
|
||||
path.isObjectProperty() || path.isVariableDeclarator(),
|
||||
)
|
||||
.remove();
|
||||
}
|
||||
},
|
||||
},
|
||||
}, {
|
||||
originalPath: path,
|
||||
});
|
||||
{
|
||||
originalPath: path,
|
||||
},
|
||||
);
|
||||
},
|
||||
// taken from transform-es2015-destructuring/src/index.js#visitor
|
||||
// export var { a, ...b } = c;
|
||||
@@ -190,28 +197,36 @@ export default function ({ types: t }) {
|
||||
const nodes = [];
|
||||
|
||||
let ref;
|
||||
if (path.isCompletionRecord() || path.parentPath.isExpressionStatement()) {
|
||||
ref = path.scope.generateUidIdentifierBasedOnNode(path.node.right, "ref");
|
||||
if (
|
||||
path.isCompletionRecord() ||
|
||||
path.parentPath.isExpressionStatement()
|
||||
) {
|
||||
ref = path.scope.generateUidIdentifierBasedOnNode(
|
||||
path.node.right,
|
||||
"ref",
|
||||
);
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, path.node.right),
|
||||
]));
|
||||
nodes.push(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, path.node.right),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const [ argument, callExpression ] = createObjectSpread(
|
||||
const [argument, callExpression] = createObjectSpread(
|
||||
file,
|
||||
path.node.left.properties,
|
||||
ref
|
||||
ref,
|
||||
);
|
||||
|
||||
const nodeWithoutSpread = t.clone(path.node);
|
||||
nodeWithoutSpread.right = ref;
|
||||
nodes.push(t.expressionStatement(nodeWithoutSpread));
|
||||
nodes.push(t.toStatement(t.assignmentExpression(
|
||||
"=",
|
||||
argument,
|
||||
callExpression
|
||||
)));
|
||||
nodes.push(
|
||||
t.toStatement(
|
||||
t.assignmentExpression("=", argument, callExpression),
|
||||
),
|
||||
);
|
||||
|
||||
if (ref) {
|
||||
nodes.push(t.expressionStatement(ref));
|
||||
@@ -236,9 +251,9 @@ export default function ({ types: t }) {
|
||||
|
||||
path.ensureBlock();
|
||||
|
||||
node.body.body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(left, temp),
|
||||
]));
|
||||
node.body.body.unshift(
|
||||
t.variableDeclaration("var", [t.variableDeclarator(left, temp)]),
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -258,7 +273,7 @@ export default function ({ types: t }) {
|
||||
node.body.body.unshift(
|
||||
t.variableDeclaration(node.left.kind, [
|
||||
t.variableDeclarator(pattern, key),
|
||||
])
|
||||
]),
|
||||
);
|
||||
},
|
||||
// var a = { ...b, ...c }
|
||||
@@ -267,8 +282,10 @@ export default function ({ types: t }) {
|
||||
|
||||
const useBuiltIns = file.opts.useBuiltIns || false;
|
||||
if (typeof useBuiltIns !== "boolean") {
|
||||
throw new Error("transform-object-rest-spread currently only accepts a boolean " +
|
||||
"option for useBuiltIns (defaults to false)");
|
||||
throw new Error(
|
||||
"transform-object-rest-spread currently only accepts a boolean " +
|
||||
"option for useBuiltIns (defaults to false)",
|
||||
);
|
||||
}
|
||||
|
||||
const args = [];
|
||||
@@ -295,9 +312,9 @@ export default function ({ types: t }) {
|
||||
args.unshift(t.objectExpression([]));
|
||||
}
|
||||
|
||||
const helper = useBuiltIns ?
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("assign")) :
|
||||
file.addHelper("extends");
|
||||
const helper = useBuiltIns
|
||||
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
|
||||
: file.addHelper("extends");
|
||||
|
||||
path.replaceWith(t.callExpression(helper, args));
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user