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

Use evaluate helper in removeUndefined transform #662

Merged
merged 1 commit into from
Aug 14, 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
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-remove-undefined/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ require("babel-core").transform("code", {
plugins: ["babel-plugin-transform-remove-undefined"]
});
```

## Options

+ `tdz` - Detect usages before declaration/initialization in let/const(throws) and var(void 0)
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"license": "MIT",
"author": "shinew",
"main": "lib/index.js",
"repository": "https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-remove-undefined"
"repository": "https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-remove-undefined",
"dependencies": {
"babel-helper-evaluate-path": "^0.1.0"
}
}
26 changes: 18 additions & 8 deletions packages/babel-plugin-transform-remove-undefined/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"use strict";

function isPureAndUndefined(rval, scope = { hasBinding: () => false }) {
const evaluate = require("babel-helper-evaluate-path");

function isPureAndUndefined(
rval,
{ tdz, scope = { hasBinding: () => false } } = {}
) {
if (rval.isIdentifier() && rval.node.name === "undefined") {
// deopt right away if undefined is a local binding
if (scope.hasBinding(rval.node.name, true /* no globals */)) {
Expand All @@ -12,7 +17,7 @@ function isPureAndUndefined(rval, scope = { hasBinding: () => false }) {
if (!rval.isPure()) {
return false;
}
const evaluation = rval.evaluate();
const evaluation = evaluate(rval, { tdz });
return evaluation.confident === true && evaluation.value === undefined;
}

Expand Down Expand Up @@ -96,12 +101,12 @@ module.exports = function() {
return {
name: "transform-remove-undefined",
visitor: {
SequenceExpression(path) {
SequenceExpression(path, { opts: { tdz } = {} }) {
const expressions = path.get("expressions");

for (let i = 0; i < expressions.length; i++) {
const expr = expressions[i];
if (!isPureAndUndefined(expr, path.scope)) continue;
if (!isPureAndUndefined(expr, { tdz, scope: path.scope })) continue;

// last value
if (i === expressions.length - 1) {
Expand All @@ -114,21 +119,26 @@ module.exports = function() {
}
},

ReturnStatement(path) {
ReturnStatement(path, { opts: { tdz } = {} }) {
if (path.node.argument !== null) {
if (isPureAndUndefined(path.get("argument"), path.scope)) {
if (
isPureAndUndefined(path.get("argument"), {
tdz,
scope: path.scope
})
) {
path.node.argument = null;
}
}
},

VariableDeclaration(path) {
VariableDeclaration(path, { opts: { tdz } = {} }) {
switch (path.node.kind) {
case "const":
break;
case "let":
for (const declarator of path.get("declarations")) {
if (isPureAndUndefined(declarator.get("init"))) {
if (isPureAndUndefined(declarator.get("init"), { tdz })) {
declarator.node.init = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +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]
tdz | Passed to [builtIns][builtIns], [evaluate][evaluate], [deadcode][deadcode], [removeUndefined][removeUndefined]

**Examples**

Expand Down
22 changes: 22 additions & 0 deletions packages/babel-preset-minify/__tests__/babili-es2015-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,26 @@ describe("preset along with es2015", () => {
key = obj.k, foo();
`
);

thePlugin(
"should fix issue#614",
`
function a() {
var c = 1
class B {}
return B
}
`,
`
"use strict";

function _classCallCheck(a, b) { if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); }

function a() {
return function a() {
_classCallCheck(this, a);
};
}
`
);
});
2 changes: 1 addition & 1 deletion packages/babel-preset-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const PLUGINS = [
const PROXIES = {
keepFnName: ["mangle", "deadcode"],
keepClassName: ["mangle", "deadcode"],
tdz: ["builtIns", "evaluate", "deadcode"]
tdz: ["builtIns", "evaluate", "deadcode", "removeUndefined"]
};

module.exports = preset;
Expand Down