Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-minify-constant-folding

Package Overview
Dependencies
Maintainers
5
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-minify-constant-folding - npm Package Compare versions

Comparing version 0.3.0-alpha.033d9ab6 to 0.3.0-alpha.169f8743

71

lib/index.js
"use strict";
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
const evaluate = require("babel-helper-evaluate-path");
var evaluate = require("babel-helper-evaluate-path");
var _require = require("./replacements");
var _require = require("./replacements"),
FALLBACK_HANDLER = _require.FALLBACK_HANDLER;
const FALLBACK_HANDLER = _require.FALLBACK_HANDLER;
function getName(member) {

@@ -24,7 +24,7 @@ if (member.computed) {

function swap(path, member, handlers) {
var key = getName(member.node);
function swap(path, member, handlers, ...args) {
const key = getName(member.node);
if (key === void 0) return false;
var handler = void 0;
let handler;
if (hop(handlers, key) && typeof handlers[key] === "function") {

@@ -38,7 +38,3 @@ handler = handlers[key];

for (var _len = arguments.length, args = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
args[_key - 3] = arguments[_key];
}
var replacement = handler.apply(member.get("object"), args);
const replacement = handler.apply(member.get("object"), args);
if (replacement) {

@@ -51,7 +47,7 @@ path.replaceWith(replacement);

module.exports = function (babel) {
var replacements = require("./replacements.js")(babel);
var seen = Symbol("seen");
var t = babel.types,
traverse = babel.traverse;
module.exports = babel => {
const replacements = require("./replacements.js")(babel);
const seen = Symbol("seen");
const t = babel.types,
traverse = babel.traverse;

@@ -66,4 +62,3 @@

BinaryExpression(path) {
var literal = void 0,
bin = void 0;
let literal, bin;
if (path.get("right").isStringLiteral()) {

@@ -87,3 +82,3 @@ literal = path.get("right");

var relevant = getLeaf(bin, literal.key);
const relevant = getLeaf(bin, literal.key);

@@ -94,3 +89,3 @@ if (!relevant) {

var value = literal.key === "right" ? relevant.node.value + literal.node.value : literal.node.value + relevant.node.value;
const value = literal.key === "right" ? relevant.node.value + literal.node.value : literal.node.value + relevant.node.value;

@@ -110,8 +105,4 @@ relevant.replaceWith(t.stringLiteral(value));

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

@@ -153,3 +144,3 @@

var res = evaluate(path, { tdz });
const res = evaluate(path, { tdz });
if (res.confident) {

@@ -165,5 +156,5 @@ // Avoid fractions because they can be longer than the original expression.

if (1 / res.value === -Infinity) {
var _node2 = t.unaryExpression("-", t.numericLiteral(0), true);
_node2[seen] = true;
path.replaceWith(_node2);
const node = t.unaryExpression("-", t.numericLiteral(0), true);
node[seen] = true;
path.replaceWith(node);
return;

@@ -182,13 +173,13 @@ }

var _node = t.valueToNode(res.value);
_node[seen] = true;
path.replaceWith(_node);
const node = t.valueToNode(res.value);
node[seen] = true;
path.replaceWith(node);
}
},
CallExpression(path) {
var node = path.node;
const node = path.node;
var member = path.get("callee");
const member = path.get("callee");
if (t.isMemberExpression(member)) {
var helpers = replacements[member.node.object.type];
const helpers = replacements[member.node.object.type];
if (!helpers || !helpers.calls) return;

@@ -199,9 +190,9 @@ // find if the input can be constant folded

}
swap.apply(undefined, [path, member, helpers.calls].concat(_toConsumableArray(node.arguments)));
swap(path, member, helpers.calls, ...node.arguments);
}
},
MemberExpression(path) {
var node = path.node;
const node = path.node;
var helpers = replacements[node.object.type];
const helpers = replacements[node.object.type];
if (!helpers || !helpers.members) return;

@@ -208,0 +199,0 @@ swap(path, path, helpers.members);

"use strict";
var FALLBACK_HANDLER = Symbol("fallback handler");
const FALLBACK_HANDLER = Symbol("fallback handler");
module.exports = function (_ref) {
var t = _ref.types;
module.exports = ({ types: t }) => {
const undef = t.unaryExpression("void", t.numericLiteral(0));
var undef = t.unaryExpression("void", t.numericLiteral(0));
function isUndef(ob) {

@@ -15,11 +13,5 @@ return ob === undefined || t.isIdentifier(ob, { name: "undefined" }) || t.isUnaryExpression(ob, { operator: "void" });

function defaultZero(cb) {
return function () {
var i = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : t.numericLiteral(0);
return function (i = t.numericLiteral(0), ...args) {
if (t.isNumericLiteral(i)) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return cb.call.apply(cb, [this.node, this.node, i.value].concat(args));
return cb.call(this.node, this.node, i.value, ...args);
}

@@ -30,5 +22,3 @@ };

function hasSpread(node) {
return node.elements.some(function (el) {
return t.isSpreadElement(el);
});
return node.elements.some(el => t.isSpreadElement(el));
}

@@ -58,9 +48,7 @@

calls: {
join() {
var sep = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : t.stringLiteral(",");
join(sep = t.stringLiteral(",")) {
if (!t.isStringLiteral(sep)) return;
var bad = false;
var str = this.get("elements").map(function (el) {
var evaled = el.evaluate();
let bad = false;
const str = this.get("elements").map(el => {
const evaled = el.evaluate();
if (!evaled.confident) {

@@ -74,4 +62,4 @@ bad = true;

},
push() {
return t.numericLiteral(this.node.elements.length + arguments.length);
push(...args) {
return t.numericLiteral(this.node.elements.length + args.length);
},

@@ -84,6 +72,3 @@ shift() {

},
slice() {
var start = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : t.numericLiteral(0);
var end = arguments[1];
slice(start = t.numericLiteral(0), end) {
if (!t.isNumericLiteral(start) || end && !t.isNumericLiteral(end)) {

@@ -100,17 +85,10 @@ return;

},
splice(start, end) {
var _node$elements$slice;
splice(start, end, ...args) {
if (!t.isNumericLiteral(start) || end && !t.isNumericLiteral(end)) {
return;
}
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
if (end) {
args.unshift(end.value);
}
return t.arrayExpression((_node$elements$slice = this.node.elements.slice()).splice.apply(_node$elements$slice, [start.value].concat(args)));
return t.arrayExpression(this.node.elements.slice().splice(start.value, ...args));
}

@@ -126,3 +104,3 @@ }

if (typeof i === "number" || i.match(/^\d+$/)) {
var ch = this.node.value[i];
const ch = this.node.value[i];
return ch ? t.stringLiteral(ch) : undef;

@@ -133,6 +111,4 @@ }

calls: {
split() {
var sep = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undef;
var realSep = null;
split(sep = undef) {
let realSep = null;
if (t.isStringLiteral(sep)) {

@@ -145,19 +121,8 @@ realSep = sep.value;

if (realSep !== null) {
return t.arrayExpression(this.node.value.split(realSep).map(function (str) {
return t.stringLiteral(str);
}));
return t.arrayExpression(this.node.value.split(realSep).map(str => t.stringLiteral(str)));
}
},
charAt: defaultZero(function (_ref2, i) {
var value = _ref2.value;
return t.stringLiteral(value.charAt(i));
}),
charCodeAt: defaultZero(function (_ref3, i) {
var value = _ref3.value;
return t.numericLiteral(value.charCodeAt(i));
}),
codePointAt: defaultZero(function (_ref4, i) {
var value = _ref4.value;
return t.numericLiteral(value.codePointAt(i));
})
charAt: defaultZero(({ value }, i) => t.stringLiteral(value.charAt(i))),
charCodeAt: defaultZero(({ value }, i) => t.numericLiteral(value.charCodeAt(i))),
codePointAt: defaultZero(({ value }, i) => t.numericLiteral(value.codePointAt(i)))
}

@@ -164,0 +129,0 @@ }

{
"name": "babel-plugin-minify-constant-folding",
"version": "0.3.0-alpha.033d9ab6",
"version": "0.3.0-alpha.169f8743",
"description": "",

@@ -15,4 +15,4 @@ "keywords": [

"dependencies": {
"babel-helper-evaluate-path": "^0.3.0-alpha.033d9ab6"
"babel-helper-evaluate-path": "^0.3.0-alpha.169f8743"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc