babel-plugin-minify-constant-folding
Advanced tools
Comparing version 0.2.0 to 0.3.0-alpha.033d9ab6
@@ -25,11 +25,12 @@ "use strict"; | ||
function swap(path, member, handlers) { | ||
var key = getName(member); | ||
if (key === undefined) return; | ||
var handler = handlers[key]; | ||
if (typeof handler !== "function" || !Object.hasOwnProperty.call(handlers, key)) { | ||
if (typeof handlers[FALLBACK_HANDLER] === "function") { | ||
handler = handlers[FALLBACK_HANDLER].bind(member.object, key); | ||
} else { | ||
return false; | ||
} | ||
var key = getName(member.node); | ||
if (key === void 0) return false; | ||
var handler = void 0; | ||
if (hop(handlers, key) && typeof handlers[key] === "function") { | ||
handler = handlers[key]; | ||
} else if (typeof handlers[FALLBACK_HANDLER] === "function") { | ||
handler = handlers[FALLBACK_HANDLER].bind(member.get("object"), key); | ||
} else { | ||
return false; | ||
} | ||
@@ -41,3 +42,3 @@ | ||
var replacement = handler.apply(member.object, args); | ||
var replacement = handler.apply(member.get("object"), args); | ||
if (replacement) { | ||
@@ -165,2 +166,11 @@ path.replaceWith(replacement); | ||
// this will convert object to object but | ||
// t.valueToNode has other effects where property name | ||
// is not treated for the respective environment. | ||
// So we bail here for objects and let other plugins | ||
// take care of converting String literal to Identifier | ||
if (typeof res.value === "object") { | ||
return; | ||
} | ||
var _node = t.valueToNode(res.value); | ||
@@ -173,7 +183,11 @@ _node[seen] = true; | ||
var node = path.node; | ||
var member = node.callee; | ||
var member = path.get("callee"); | ||
if (t.isMemberExpression(member)) { | ||
var helpers = replacements[member.object.type]; | ||
var helpers = replacements[member.node.object.type]; | ||
if (!helpers || !helpers.calls) return; | ||
// find if the input can be constant folded | ||
if (typeof helpers.canReplace === "function" && !helpers.canReplace.call(member.get("object"))) { | ||
return; | ||
} | ||
swap.apply(undefined, [path, member, helpers.calls].concat(_toConsumableArray(node.arguments))); | ||
@@ -183,10 +197,14 @@ } | ||
MemberExpression(path) { | ||
var member = path.node; | ||
var node = path.node; | ||
var helpers = replacements[member.object.type]; | ||
var helpers = replacements[node.object.type]; | ||
if (!helpers || !helpers.members) return; | ||
swap(path, member, helpers.members); | ||
swap(path, path, helpers.members); | ||
} | ||
} | ||
}; | ||
}; | ||
}; | ||
function hop(o, key) { | ||
return Object.prototype.hasOwnProperty.call(o, key); | ||
} |
@@ -23,3 +23,3 @@ "use strict"; | ||
return cb.call.apply(cb, [this, this, i.value].concat(args)); | ||
return cb.call.apply(cb, [this.node, this.node, i.value].concat(args)); | ||
} | ||
@@ -29,21 +29,26 @@ }; | ||
function hasSpread(node) { | ||
return node.elements.some(function (el) { | ||
return t.isSpreadElement(el); | ||
}); | ||
} | ||
return { | ||
ArrayExpression: { | ||
canReplace() { | ||
return !hasSpread(this.node); | ||
}, | ||
members: { | ||
length() { | ||
if (this.elements.some(function (el) { | ||
return t.isSpreadElement(el); | ||
})) { | ||
if (hasSpread(this.node)) { | ||
return; | ||
} | ||
return t.numericLiteral(this.elements.length); | ||
return t.numericLiteral(this.node.elements.length); | ||
}, | ||
[FALLBACK_HANDLER](i) { | ||
if (this.elements.some(function (el) { | ||
return t.isSpreadElement(el); | ||
})) { | ||
if (hasSpread(this.node)) { | ||
return; | ||
} | ||
if (typeof i === "number" || i.match(/^\d+$/)) { | ||
return this.elements[i] || undef; | ||
return this.node.elements[i] || undef; | ||
} | ||
@@ -58,19 +63,20 @@ } | ||
var bad = false; | ||
var str = this.elements.map(function (el) { | ||
if (!t.isLiteral(el)) { | ||
var str = this.get("elements").map(function (el) { | ||
var evaled = el.evaluate(); | ||
if (!evaled.confident) { | ||
bad = true; | ||
return; | ||
} | ||
return el.value; | ||
return evaled.value; | ||
}).join(sep.value); | ||
return bad ? undefined : t.stringLiteral(str); | ||
return bad ? void 0 : t.stringLiteral(str); | ||
}, | ||
push() { | ||
return t.numericLiteral(this.elements.length + arguments.length); | ||
return t.numericLiteral(this.node.elements.length + arguments.length); | ||
}, | ||
shift() { | ||
if (this.elements.length === 0) { | ||
if (this.node.elements.length === 0) { | ||
return undef; | ||
} | ||
return t.numericLiteral(this.elements.length - 1); | ||
return t.numericLiteral(this.node.elements.length - 1); | ||
}, | ||
@@ -84,12 +90,12 @@ slice() { | ||
} | ||
return t.arrayExpression(this.elements.slice(start.value, end && end.value)); | ||
return t.arrayExpression(this.node.elements.slice(start.value, end && end.value)); | ||
}, | ||
pop() { | ||
return this.elements[this.elements.length - 1] || undef; | ||
return this.node.elements[this.node.elements.length - 1] || undef; | ||
}, | ||
reverse() { | ||
return t.arrayExpression(this.elements.reverse()); | ||
return t.arrayExpression(this.node.elements.reverse()); | ||
}, | ||
splice(start, end) { | ||
var _elements$slice; | ||
var _node$elements$slice; | ||
@@ -107,3 +113,3 @@ if (!t.isNumericLiteral(start) || end && !t.isNumericLiteral(end)) { | ||
} | ||
return t.arrayExpression((_elements$slice = this.elements.slice()).splice.apply(_elements$slice, [start.value].concat(args))); | ||
return t.arrayExpression((_node$elements$slice = this.node.elements.slice()).splice.apply(_node$elements$slice, [start.value].concat(args))); | ||
} | ||
@@ -115,7 +121,7 @@ } | ||
length() { | ||
return t.numericLiteral(this.value.length); | ||
return t.numericLiteral(this.node.value.length); | ||
}, | ||
[FALLBACK_HANDLER](i) { | ||
if (typeof i === "number" || i.match(/^\d+$/)) { | ||
var ch = this.value[i]; | ||
var ch = this.node.value[i]; | ||
return ch ? t.stringLiteral(ch) : undef; | ||
@@ -137,3 +143,3 @@ } | ||
if (realSep !== null) { | ||
return t.arrayExpression(this.value.split(realSep).map(function (str) { | ||
return t.arrayExpression(this.node.value.split(realSep).map(function (str) { | ||
return t.stringLiteral(str); | ||
@@ -140,0 +146,0 @@ })); |
{ | ||
"name": "babel-plugin-minify-constant-folding", | ||
"version": "0.2.0", | ||
"version": "0.3.0-alpha.033d9ab6", | ||
"description": "", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.2.0" | ||
"babel-helper-evaluate-path": "^0.3.0-alpha.033d9ab6" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12935
314
4
+ Addedbabel-helper-evaluate-path@0.3.0(transitive)
- Removedbabel-helper-evaluate-path@0.2.0(transitive)