Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(simplify): hoist path.get calls #858

Merged
merged 1 commit into from
May 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions packages/babel-plugin-minify-simplify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,47 +946,54 @@ module.exports = ({ types: t }) => {
}

function earlyReturnTransform(path) {
const { node } = path;
const block = path.get("body");

if (!t.isBlockStatement(node.body)) {
if (!block.isBlockStatement()) {
return;
}

for (let i = node.body.body.length; i >= 0; i--) {
const statement = node.body.body[i];
const body = block.get("body");

for (let i = body.length - 1; i >= 0; i--) {
const statement = body[i];
if (
t.isIfStatement(statement) &&
!statement.alternate &&
t.isReturnStatement(statement.consequent) &&
!statement.consequent.argument
t.isIfStatement(statement.node) &&
!statement.node.alternate &&
t.isReturnStatement(statement.node.consequent) &&
!statement.node.consequent.argument
) {
genericEarlyExitTransform(path.get("body").get("body")[i]);
genericEarlyExitTransform(statement);
}
}
}

function earlyContinueTransform(path) {
const { node } = path;
const block = path.get("body");

if (!t.isBlockStatement(node.body)) {
if (!block.isBlockStatement()) {
return;
}

for (let i = node.body.body.length; i >= 0; i--) {
const statement = node.body.body[i];
let body = block.get("body");

for (let i = body.length - 1; i >= 0; i--) {
const statement = body[i];
if (
t.isIfStatement(statement) &&
!statement.alternate &&
t.isContinueStatement(statement.consequent) &&
!statement.consequent.label
t.isIfStatement(statement.node) &&
!statement.node.alternate &&
t.isContinueStatement(statement.node.consequent) &&
!statement.node.consequent.label
) {
genericEarlyExitTransform(path.get("body").get("body")[i]);
genericEarlyExitTransform(statement);
}
}

// because we might have folded or removed statements
body = block.get("body");

// We may have reduced the body to a single statement.
if (node.body.body.length === 1 && !needsBlock(node.body, node)) {
path.get("body").replaceWith(node.body.body[0]);
if (body.length === 1 && !needsBlock(block.node, path.node)) {
block.replaceWith(body[0].node);
}
}

Expand Down