Skip to content

Commit

Permalink
Return inserted/replaced paths
Browse files Browse the repository at this point in the history
This gives `Path`’s replacement and insertion methods a consistent
return value: the inserted/replaced paths.

Before, they could return `undefined`, a `node`, or a the current path
inside an array. It was kinda pointless.  But now they always return an
array of paths, which is useful for solving
babel#4935 (comment).
  • Loading branch information
jridgewell committed May 6, 2017
1 parent 6646707 commit 86d71e8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/babel-plugin-transform-es2015-classes/src/index.js
Expand Up @@ -38,7 +38,10 @@ export default function ({ types: t }) {
if (node[VISITED]) return;

const inferred = nameFunction(path);
if (inferred && inferred !== node) return path.replaceWith(inferred);
if (inferred && inferred !== node) {
path.replaceWith(inferred);
return;
}

node[VISITED] = true;

Expand Down
6 changes: 4 additions & 2 deletions packages/babel-plugin-transform-es2015-for-of/src/index.js
Expand Up @@ -95,9 +95,11 @@ export default function ({ messages, template, types: t }) {
ForOfStatement(path, state) {
if (path.get("right").isArrayExpression()) {
if (path.parentPath.isLabeledStatement()) {
return path.parentPath.replaceWithMultiple(_ForOfStatementArray(path));
path.parentPath.replaceWithMultiple(_ForOfStatementArray(path));
return;
} else {
return path.replaceWithMultiple(_ForOfStatementArray(path));
path.replaceWithMultiple(_ForOfStatementArray(path));
return;
}
}

Expand Down
12 changes: 4 additions & 8 deletions packages/babel-traverse/src/path/modification.js
Expand Up @@ -21,21 +21,19 @@ export function insertBefore(nodes) {
(this.parentPath.isForStatement() && this.key === "init")
) {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
return this.replaceExpressionWithStatements(nodes);
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertBefore(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.push(this.node);
this._replaceWith(t.blockStatement(nodes));
return this._replaceWith(t.blockStatement(nodes));
} else {
throw new Error("We don't know what to do with this node type. " +
"We were previously a Statement but we can't fit in here?");
}
}

return [this];
}

export function _containerInsert(from, nodes) {
Expand Down Expand Up @@ -120,21 +118,19 @@ export function insertAfter(nodes) {
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
nodes.push(t.expressionStatement(temp));
}
this.replaceExpressionWithStatements(nodes);
return this.replaceExpressionWithStatements(nodes);
} else {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
return this._containerInsertAfter(nodes);
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.unshift(this.node);
this._replaceWith(t.blockStatement(nodes));
return this._replaceWith(t.blockStatement(nodes));
} else {
throw new Error("We don't know what to do with this node type. " +
"We were previously a Statement but we can't fit in here?");
}
}

return [this];
}

/**
Expand Down
18 changes: 11 additions & 7 deletions packages/babel-traverse/src/path/replacement.js
Expand Up @@ -48,13 +48,14 @@ export function replaceWithMultiple(nodes: Array<Object>) {
t.inheritLeadingComments(nodes[0], this.node);
t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
this.node = this.container[this.key] = null;
this.insertAfter(nodes);
const paths = this.insertAfter(nodes);

if (this.node) {
this.requeue();
} else {
this.remove();
}
return paths;
}

/**
Expand Down Expand Up @@ -158,6 +159,8 @@ export function replaceWith(replacement) {

// requeue for visiting
this.requeue();

return [this];
}

/**
Expand Down Expand Up @@ -200,12 +203,12 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {

// could be just one element due to the previous maybe popping
if (exprs.length === 1) {
this.replaceWith(exprs[0]);
return this.replaceWith(exprs[0]);
} else {
this.replaceWith(toSequenceExpression);
return this.replaceWith(toSequenceExpression);
}
} else if (toSequenceExpression) {
this.replaceWith(toSequenceExpression);
return this.replaceWith(toSequenceExpression);
} else {
const container = t.arrowFunctionExpression([], t.blockStatement(nodes));

Expand Down Expand Up @@ -240,7 +243,7 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {

this.get("callee").arrowFunctionToExpression();

return this.node;
return [this];
}
}

Expand All @@ -250,8 +253,9 @@ export function replaceInline(nodes: Object | Array<Object>) {
if (Array.isArray(nodes)) {
if (Array.isArray(this.container)) {
nodes = this._verifyNodeList(nodes);
this._containerInsertAfter(nodes);
return this.remove();
const paths = this._containerInsertAfter(nodes);
this.remove();
return paths;
} else {
return this.replaceWithMultiple(nodes);
}
Expand Down

0 comments on commit 86d71e8

Please sign in to comment.