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

Make tdz optional in helper evaluate #658

Merged
merged 3 commits into from
Aug 11, 2017
Merged
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
12 changes: 10 additions & 2 deletions packages/babel-helper-evaluate-path/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"use strict";

module.exports = function evaluate(path) {
module.exports = function evaluate(path, { tdz = false } = {}) {
if (!tdz) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, would help shave off some time in the benchmarks

return baseEvaluate(path);
}

if (path.isReferencedIdentifier()) {
return evaluateIdentifier(path);
}
Expand Down Expand Up @@ -32,6 +36,10 @@ module.exports = function evaluate(path) {
return state;
}

return baseEvaluate(path);
};

function baseEvaluate(path) {
try {
return path.evaluate();
} catch (e) {
Expand All @@ -40,7 +48,7 @@ module.exports = function evaluate(path) {
error: e
};
}
};
}

// Original Source:
// https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/evaluation.js
Expand Down
13 changes: 10 additions & 3 deletions packages/babel-minify/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const plugins = [
"undefinedToVoid"
];

const proxies = ["keepFnName", "keepClassName"];
const proxies = ["keepFnName", "keepClassName", "tdz"];

const dceBooleanOpts = [
"deadcode.keepFnName",
Expand Down Expand Up @@ -72,9 +72,11 @@ function printHelpInfo({ exitCode = 0 } = {}) {
const msg = `
Usage: minify index.js [options]

Options:
IO Options:
--out-file, -o Output to a specific file
--out-dir, -d Output to a specific directory

Transform Options:
--mangle Context and scope aware variable renaming
--simplify Simplifies code for minification by reducing statements into
expressions
Expand Down Expand Up @@ -102,7 +104,12 @@ function printHelpInfo({ exitCode = 0 } = {}) {
to be the same
--typeConstructors Minify constructors to equivalent version
--undefinedToVoid Transforms undefined into void 0
--version, -V Prints the current version number

Other Options:
--keepFnName Preserve Function Name (useful for code depending on fn.name)
--keepClassName Preserve Class Name (useful for code depending on c.name)
--keepFnArgs Don't remove unused fn arguments (useful for code depending on fn.length)
--tdz Detect usages of variables in the Temporal Dead Zone

Nested Options:
To use nested options (plugin specfic options) simply use the pattern
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-plugin-minify-builtins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ require("babel-core").transform("code", {
plugins: ["minify-builtins"]
});
```

## Options

+ `tdz` - Account for TDZ (Temporal Dead Zone)
9 changes: 5 additions & 4 deletions packages/babel-plugin-minify-builtins/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const INVALID_METHODS = ["random"];

module.exports = function({ types: t }) {
class BuiltInReplacer {
constructor(program) {
constructor(program, { tdz }) {
this.program = program;
this.tdz = tdz;
// map<expr_name, path[]>;
this.pathsToUpdate = new Map();
}
Expand Down Expand Up @@ -48,7 +49,7 @@ module.exports = function({ types: t }) {
// computed property should be not optimized
// Math[max]() -> Math.max()
if (!isComputed(callee) && isBuiltin(callee)) {
const result = evaluate(path);
const result = evaluate(path, { tdz: context.tdz });
// deopt when we have side effecty evaluate-able arguments
// Math.max(foo(), 1) --> untouched
// Math.floor(1) --> 1
Expand Down Expand Up @@ -96,8 +97,8 @@ module.exports = function({ types: t }) {
return {
name: "minify-builtins",
visitor: {
Program(path) {
const builtInReplacer = new BuiltInReplacer(path);
Program(path, { opts: { tdz = false } = {} }) {
const builtInReplacer = new BuiltInReplacer(path, { tdz });
builtInReplacer.run();
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-plugin-minify-constant-folding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ require("babel-core").transform("code", {
plugins: ["minify-constant-folding"]
});
```

## Options

+ `tdz` - Account for TDZ (Temporal Dead Zone)
4 changes: 2 additions & 2 deletions packages/babel-plugin-minify-constant-folding/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = babel => {
},

// TODO: look into evaluating binding too (could result in more code, but gzip?)
Expression(path) {
Expression(path, { opts: { tdz = false } = {} }) {
const { node } = path;

if (node[seen]) {
Expand Down Expand Up @@ -143,7 +143,7 @@ module.exports = babel => {
return;
}

const res = evaluate(path);
const res = evaluate(path, { tdz });
if (res.confident) {
// Avoid fractions because they can be longer than the original expression.
// There is also issues with number percision?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ require("babel-core").transform("code", {

+ `keepFnName` - prevent plugin from removing function name. Useful for code depending on `fn.name`
+ `keepFnArgs` - prevent plugin from removing function args. Useful for code depending on `fn.length`
+ `keepClassName` - prevent mangler from altering class names.
+ `keepClassName` - prevent plugin from removing class name. Useful for code depending on `cls.name`
+ `tdz` - Account for TDZ (Temporal Dead Zone)
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@ describe("dce-plugin", () => {
if (a) return;
x();
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin(
Expand All @@ -536,7 +539,10 @@ describe("dce-plugin", () => {
if (a) return 1;
x();
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin(
Expand All @@ -552,7 +558,10 @@ describe("dce-plugin", () => {
y();
}
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin(
Expand Down Expand Up @@ -2452,7 +2461,10 @@ describe("dce-plugin", () => {
if (v) var w = 10;
if (w) console.log("hello", v);
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin.skip(
Expand All @@ -2462,7 +2474,10 @@ describe("dce-plugin", () => {
bar(a); // Should be a ReferenceError
let a = 1;
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);

thePlugin(
Expand All @@ -2483,6 +2498,9 @@ describe("dce-plugin", () => {
if (a) console.log(a);
};
}
`
`,
{
plugins: [[deadcode, { tdz: true }]]
}
);
});
20 changes: 11 additions & 9 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ module.exports = ({ types: t, traverse }) => {

SwitchStatement: {
exit(path) {
const evaluated = evaluate(path.get("discriminant"));
const evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz });

if (!evaluated.confident) return;

Expand All @@ -528,7 +528,7 @@ module.exports = ({ types: t, traverse }) => {
continue;
}

const testResult = evaluate(test);
const testResult = evaluate(test, { tdz: this.tdz });

// if we are not able to deternine a test during
// compile time, we terminate immediately
Expand Down Expand Up @@ -614,7 +614,7 @@ module.exports = ({ types: t, traverse }) => {

WhileStatement(path) {
const test = path.get("test");
const result = evaluate(test);
const result = evaluate(test, { tdz: this.tdz });
if (result.confident && test.isPure() && !result.value) {
path.remove();
}
Expand All @@ -624,7 +624,7 @@ module.exports = ({ types: t, traverse }) => {
const test = path.get("test");
if (!test.isPure()) return;

const result = evaluate(test);
const result = evaluate(test, { tdz: this.tdz });
if (result.confident) {
if (result.value) {
test.remove();
Expand All @@ -636,7 +636,7 @@ module.exports = ({ types: t, traverse }) => {

DoWhileStatement(path) {
const test = path.get("test");
const result = evaluate(test);
const result = evaluate(test, { tdz: this.tdz });
if (result.confident && test.isPure() && !result.value) {
const body = path.get("body");

Expand Down Expand Up @@ -767,12 +767,12 @@ module.exports = ({ types: t, traverse }) => {
}
},
IfStatement: {
exit(path) {
exit(path, { opts: { tdz = false } = {} }) {
const consequent = path.get("consequent");
const alternate = path.get("alternate");
const test = path.get("test");

const evalResult = evaluate(test);
const evalResult = evaluate(test, { tdz });
const isPure = test.isPure();

const replacements = [];
Expand Down Expand Up @@ -864,7 +864,8 @@ module.exports = ({ types: t, traverse }) => {
optimizeRawSize = false,
keepFnName = false,
keepClassName = false,
keepFnArgs = false
keepFnArgs = false,
tdz = false
} = {}
} = {}
) {
Expand All @@ -879,7 +880,8 @@ module.exports = ({ types: t, traverse }) => {
optimizeRawSize,
keepFnName,
keepClassName,
keepFnArgs
keepFnArgs,
tdz
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ OptionName | Plugins
---------- | -------
keepFnName | Passed to [mangle][mangle] & [deadcode][deadcode]
keepClassName | Passed to [mangle][mangle] & [deadcode][deadcode]
tdz | Passed to [builtIns][builtIns], [evaluate][evaluate] & [deadcode][deadcode]

**Examples**

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const PLUGINS = [

const PROXIES = {
keepFnName: ["mangle", "deadcode"],
keepClassName: ["mangle", "deadcode"]
keepClassName: ["mangle", "deadcode"],
tdz: ["builtIns", "evaluate", "deadcode"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the same option to preset README?

};

module.exports = preset;
Expand Down