Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Optimize if-statement tests #736

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if (!!foo) {
const thisIs = aStatement;
}
if (!!foo && bar) {
const thisIs = aStatement;
}
if (!!some.complex({ expression: () => "obviously" })) {
const thisIs = aStatement;
}

// --------------------------------------

!!foo && an.expression();
(!!foo && bar) && an.expression();
!!some.complex({ expression: () => "obviously" }) && an.expression();

!!foo || an.expression();
(!!foo && bar) || an.expression();
!!some.complex({ expression: () => "obviously" }) || an.expression();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if (foo) {
const thisIs = aStatement;
}

if (foo && bar) {
const thisIs = aStatement;
}

if (some.complex({
expression: () => "obviously"
})) {
const thisIs = aStatement;
} // --------------------------------------


foo && an.expression(), !!foo && bar && an.expression(), some.complex({
expression: () => "obviously"
}) && an.expression(), foo || an.expression(), !!foo && bar || an.expression(), some.complex({
expression: () => "obviously"
}) || an.expression();
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
if (foo.indexOf(bar) === -1) {
const thisIs = aStatement;
}
if (foo.indexOf(bar) !== -1) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) == -1) {
const thisIs = aStatement;
}
if (foo.indexOf(bar) != -1) {
const thisIs = aStatement;
}

if (fooBar === -1) {
const thisIs = aStatement;
}
if (fooBar !== -1) {
const thisIs = aStatement;
}

if (fooBar == -1) {
const thisIs = aStatement;
}
if (fooBar != -1) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) == 0) {
const thisIs = aStatement;
}
if (foo.indexOf(bar) != 0) {
const thisIs = aStatement;
}
if (foo.indexOf(bar) === 0) {
const thisIs = aStatement;
}
if (foo.indexOf(bar) !== 0) {
const thisIs = aStatement;
}

// --------------------------------------

foo.indexOf(bar) === -1 && an.expression()
foo.indexOf(bar) !== -1 && an.expression()

foo.indexOf(bar) == -1 && an.expression()
foo.indexOf(bar) != -1 && an.expression()

fooBar === -1 && an.expression()
fooBar !== -1 && an.expression()

fooBar == -1 && an.expression()
fooBar != -1 && an.expression()

foo.indexOf(bar) == 0 && an.expression()
foo.indexOf(bar) != 0 && an.expression()
foo.indexOf(bar) === 0 && an.expression()
foo.indexOf(bar) !== 0 && an.expression()
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
if (!~foo.indexOf(bar)) {
const thisIs = aStatement;
}

if (~foo.indexOf(bar)) {
const thisIs = aStatement;
}

if (!~foo.indexOf(bar)) {
const thisIs = aStatement;
}

if (~foo.indexOf(bar)) {
const thisIs = aStatement;
}

if (!~fooBar) {
const thisIs = aStatement;
}

if (~fooBar) {
const thisIs = aStatement;
}

if (!~fooBar) {
const thisIs = aStatement;
}

if (~fooBar) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) == 0) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) ^ 0) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) === 0) {
const thisIs = aStatement;
}

if (foo.indexOf(bar) !== 0) {
const thisIs = aStatement;
} // --------------------------------------


!~foo.indexOf(bar) && an.expression(), ~foo.indexOf(bar) && an.expression(), !~foo.indexOf(bar) && an.expression(), ~foo.indexOf(bar) && an.expression(), !~fooBar && an.expression(), ~fooBar && an.expression(), !~fooBar && an.expression(), ~fooBar && an.expression(), foo.indexOf(bar) == 0 && an.expression(), foo.indexOf(bar) ^ 0 && an.expression(), foo.indexOf(bar) === 0 && an.expression(), foo.indexOf(bar) !== 0 && an.expression();
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
if (!(foo === 2)) {
const thisIs = aStatement;
}
if (!(foo !== 2)) {
const thisIs = aStatement;
}

if (!(foo == 2)) {
const thisIs = aStatement;
}
if (!(foo != 2)) {
const thisIs = aStatement;
}

if (!(myFoo({ bar: { baz: { quux() {} } } }) === 2)) {
const thisIs = aStatement;
}
if (!(myFoo({ bar: { baz: { quux() {} } } }) !== 2)) {
const thisIs = aStatement;
}

if (!(myFoo({ bar: { baz: { quux() {} } } }) == 2)) {
const thisIs = aStatement;
}
if (!(myFoo({ bar: { baz: { quux() {} } } }) != 2)) {
const thisIs = aStatement;
}

// --------------------------------------

!(foo === 2) && an.expression()
!(foo !== 2) && an.expression()

!(foo == 2) && an.expression()
!(foo != 2) && an.expression()

!(myFoo({ bar: { baz: { quux() {} } } }) === 2) && an.expression()
!(myFoo({ bar: { baz: { quux() {} } } }) !== 2) && an.expression()

!(myFoo({ bar: { baz: { quux() {} } } }) == 2) && an.expression()
!(myFoo({ bar: { baz: { quux() {} } } }) != 2) && an.expression()
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
if (foo !== 2) {
const thisIs = aStatement;
}

if (foo === 2) {
const thisIs = aStatement;
}

if (foo ^ 2) {
const thisIs = aStatement;
}

if (foo == 2) {
const thisIs = aStatement;
}

if (myFoo({
bar: {
baz: {
quux() {}

}
}
}) !== 2) {
const thisIs = aStatement;
}

if (myFoo({
bar: {
baz: {
quux() {}

}
}
}) === 2) {
const thisIs = aStatement;
}

if (myFoo({
bar: {
baz: {
quux() {}

}
}
}) ^ 2) {
const thisIs = aStatement;
}

if (myFoo({
bar: {
baz: {
quux() {}

}
}
}) == 2) {
const thisIs = aStatement;
} // --------------------------------------


foo !== 2 && an.expression(), foo === 2 && an.expression(), foo ^ 2 && an.expression(), foo == 2 && an.expression(), myFoo({
bar: {
baz: {
quux() {}

}
}
}) !== 2 && an.expression(), myFoo({
bar: {
baz: {
quux() {}

}
}
}) === 2 && an.expression(), myFoo({
bar: {
baz: {
quux() {}

}
}
}) ^ 2 && an.expression(), myFoo({
bar: {
baz: {
quux() {}

}
}
}) == 2 && an.expression();
77 changes: 77 additions & 0 deletions packages/babel-plugin-minify-simplify/src/boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const isNumber = path =>
path.isNumericLiteral() ||
(path.isUnaryExpression({ operator: "-" }) &&
path.get("argument").isNumericLiteral());

const isSpecificNumber = (path, number) =>
path.isNumericLiteral({ value: number }) ||
(path.isUnaryExpression({ operator: "-" }) &&
path.get("argument").isNumericLiteral({ value: -number }));

module.exports = t => path => {
const isTripleEquals = (p = path) =>
p.isBinaryExpression({ operator: "!==" }) ||
p.isBinaryExpression({ operator: "===" });

const isDoubleEquals = (p = path) =>
p.isBinaryExpression({ operator: "!=" }) ||
p.isBinaryExpression({ operator: "==" });

const isEquals = (p = path) => isTripleEquals(p) || isDoubleEquals(p);

const isNegatedEqual = (p = path) =>
p.isBinaryExpression({ operator: "!==" }) ||
p.isBinaryExpression({ operator: "!=" });

// if (!!foo) {} >-> if (foo) {}
if (
path.isUnaryExpression({ operator: "!" }) &&
path.get("argument").isUnaryExpression({ operator: "!" })
) {
path.replaceWith(path.get("argument.argument"));
}

// if (!(foo === bar)) {} >-> if (foo !== bar) {}
// if (!(foo !== bar)) {} >-> if (foo === bar) {}
if (
path.isUnaryExpression({ operator: "!" }) &&
isEquals(path.get("argument"))
) {
const argument = path.get("argument");
if (isNegatedEqual(argument)) {
argument.node.operator = argument.node.operator.replace("!", "=");
} else {
// String#replace() only changes the first match
argument.node.operator = argument.node.operator.replace("=", "!");
}
path.replaceWith(argument);
}

// if (foo === -1) {} >-> if (!~foo) {}
// if (foo !== -1) {} >-> if (~foo) {}
if (isEquals()) {
let comparator = null;
if (isSpecificNumber(path.get("left"), -1)) {
comparator = path.node.right;
} else if (isSpecificNumber(path.get("right"), -1)) {
comparator = path.node.left;
}

if (comparator) {
const tilde = t.unaryExpression("~", comparator);
if (isNegatedEqual()) {
path.replaceWith(tilde);
} else {
path.replaceWith(t.unaryExpression("!", tilde));
}
}
}

// if (foo != 1) {} >-> if (foo^1) {}
if (
path.isBinaryExpression({ operator: "!=" }) &&
(isNumber(path.get("left")) || isNumber(path.get("right")))
) {
path.node.operator = "^";
}
};
Loading