babel-plugin-transform-inline-consecutive-adds
Advanced tools
Comparing version 0.5.0-alpha.55276695 to 0.5.0-alpha.01eac1c3
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
const Collapser = require("./collapser"); | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
class ArrayCollapser extends Collapser { | ||
isInitTypeValid(init) { | ||
return init.isArrayExpression(); | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var Collapser = require("./collapser"); | ||
var ArrayCollapser = | ||
/*#__PURE__*/ | ||
function (_Collapser) { | ||
_inherits(ArrayCollapser, _Collapser); | ||
function ArrayCollapser() { | ||
_classCallCheck(this, ArrayCollapser); | ||
return _possibleConstructorReturn(this, (ArrayCollapser.__proto__ || Object.getPrototypeOf(ArrayCollapser)).apply(this, arguments)); | ||
isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
} | ||
_createClass(ArrayCollapser, [{ | ||
key: "isInitTypeValid", | ||
value: function isInitTypeValid(init) { | ||
return init.isArrayExpression(); | ||
} | ||
}, { | ||
key: "isExpressionTypeValid", | ||
value: function isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
} | ||
}, { | ||
key: "getExpressionChecker", | ||
value: function getExpressionChecker(objName, checkReference) { | ||
return function (expr) { | ||
// checks expr is of form: | ||
// foo.push(rval1, ...nrvals) | ||
var callee = expr.get("callee"); | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.push(rval1, ...nrvals) | ||
const callee = expr.get("callee"); | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
var obj = callee.get("object"), | ||
const obj = callee.get("object"), | ||
prop = callee.get("property"); | ||
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "push") { | ||
return false; | ||
} | ||
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "push") { | ||
return false; | ||
} | ||
var args = expr.get("arguments"); | ||
const args = expr.get("arguments"); | ||
if (args.some(checkReference)) { | ||
return false; | ||
} | ||
if (args.some(checkReference)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
}, { | ||
key: "extractAssignment", | ||
value: function extractAssignment(expr) { | ||
return expr.node.arguments; | ||
} | ||
}, { | ||
key: "addSuccessfully", | ||
value: function addSuccessfully(t, args, init) { | ||
args.map(function (a) { | ||
return init.elements.push(a); | ||
}); | ||
return true; | ||
} | ||
}]); | ||
}; | ||
} | ||
return ArrayCollapser; | ||
}(Collapser); | ||
extractAssignment(expr) { | ||
return expr.node.arguments; | ||
} | ||
addSuccessfully(t, args, init) { | ||
args.map(a => init.elements.push(a)); | ||
return true; | ||
} | ||
} | ||
module.exports = ArrayCollapser; |
"use strict"; | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
const Collapser = require("./collapser"); | ||
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } | ||
class ArrayPropertyCollapser extends Collapser { | ||
isInitTypeValid(init) { | ||
return init.isArrayExpression(); | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var Collapser = require("./collapser"); | ||
var ArrayPropertyCollapser = | ||
/*#__PURE__*/ | ||
function (_Collapser) { | ||
_inherits(ArrayPropertyCollapser, _Collapser); | ||
function ArrayPropertyCollapser() { | ||
_classCallCheck(this, ArrayPropertyCollapser); | ||
return _possibleConstructorReturn(this, (ArrayPropertyCollapser.__proto__ || Object.getPrototypeOf(ArrayPropertyCollapser)).apply(this, arguments)); | ||
isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
} | ||
_createClass(ArrayPropertyCollapser, [{ | ||
key: "isInitTypeValid", | ||
value: function isInitTypeValid(init) { | ||
return init.isArrayExpression(); | ||
} | ||
}, { | ||
key: "isExpressionTypeValid", | ||
value: function isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
} | ||
}, { | ||
key: "getExpressionChecker", | ||
value: function getExpressionChecker(objName, checkReference) { | ||
return function (expr) { | ||
// checks expr is of form: | ||
// foo[num] = rval | ||
var left = expr.get("left"); | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo[num] = rval | ||
const left = expr.get("left"); | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
var obj = left.get("object"), | ||
const obj = left.get("object"), | ||
prop = left.get("property"); | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
var checkIndex = function checkIndex(num) { | ||
return Number.isInteger(num) && num >= 0; | ||
}; | ||
const checkIndex = num => Number.isInteger(num) && num >= 0; | ||
if (!(prop.isNumericLiteral() || prop.isStringLiteral()) || !checkIndex(Number(prop.node.value))) { | ||
return false; | ||
} | ||
if (!(prop.isNumericLiteral() || prop.isStringLiteral()) || !checkIndex(Number(prop.node.value))) { | ||
return false; | ||
} | ||
var right = expr.get("right"); | ||
const right = expr.get("right"); | ||
if (checkReference(right)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
}, { | ||
key: "extractAssignment", | ||
value: function extractAssignment(expr) { | ||
return [expr.node.left.property.value, expr.get("right")]; | ||
} | ||
}, { | ||
key: "addSuccessfully", | ||
value: function addSuccessfully(t, _ref, init) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
index = _ref2[0], | ||
rval = _ref2[1]; | ||
var elements = init.elements; | ||
for (var i = elements.length; i <= index; i++) { | ||
elements.push(null); | ||
} | ||
if (elements[index] !== null) { | ||
if (checkReference(right)) { | ||
return false; | ||
} | ||
elements[index] = rval.node; | ||
return true; | ||
} | ||
}, { | ||
key: "isSizeSmaller", | ||
value: function isSizeSmaller(_ref3) { | ||
var newInit = _ref3.newInit, | ||
oldInit = _ref3.oldInit, | ||
varDecl = _ref3.varDecl, | ||
assignments = _ref3.assignments, | ||
statements = _ref3.statements; | ||
}; | ||
} | ||
var anyUndefined = function anyUndefined(args) { | ||
return args.some(function (a) { | ||
return a === undefined; | ||
}); | ||
}; // We make an inexact calculation of how much space we save. | ||
// It's inexact because we don't know how whitespaces will get minimized, | ||
// and other factors. | ||
extractAssignment(expr) { | ||
return [expr.node.left.property.value, expr.get("right")]; | ||
} | ||
addSuccessfully(t, [index, rval], init) { | ||
const elements = init.elements; | ||
if (anyUndefined([statements[statements.length - 1].node.end, varDecl.node.end])) { | ||
return false; | ||
} | ||
for (let i = elements.length; i <= index; i++) { | ||
elements.push(null); | ||
} | ||
var statementsLength = statements[statements.length - 1].node.end - varDecl.node.end; // Approx. formula of the change in `init`'s length = | ||
// (# commas added) + (size of all the new rvals added), where | ||
// # commas added = (difference between the lengths of the old and new arrays) | ||
if (elements[index] !== null) { | ||
return false; | ||
} | ||
var numCommaAdded = newInit.elements.length - oldInit.elements.length; | ||
elements[index] = rval.node; | ||
return true; | ||
} | ||
if (anyUndefined(assignments.map(function (_ref4) { | ||
var _ref5 = _slicedToArray(_ref4, 2), | ||
rval = _ref5[1]; | ||
isSizeSmaller({ | ||
newInit, | ||
oldInit, | ||
varDecl, | ||
assignments, | ||
statements | ||
}) { | ||
const anyUndefined = args => args.some(a => a === undefined); // We make an inexact calculation of how much space we save. | ||
// It's inexact because we don't know how whitespaces will get minimized, | ||
// and other factors. | ||
return rval.node.end; | ||
})) || anyUndefined(assignments.map(function (_ref6) { | ||
var _ref7 = _slicedToArray(_ref6, 2), | ||
rval = _ref7[1]; | ||
return rval.node.start; | ||
}))) { | ||
return false; | ||
} | ||
if (anyUndefined([statements[statements.length - 1].node.end, varDecl.node.end])) { | ||
return false; | ||
} | ||
var sizeOfRvals = assignments.map(function (_ref8) { | ||
var _ref9 = _slicedToArray(_ref8, 2), | ||
rval = _ref9[1]; | ||
const statementsLength = statements[statements.length - 1].node.end - varDecl.node.end; // Approx. formula of the change in `init`'s length = | ||
// (# commas added) + (size of all the new rvals added), where | ||
// # commas added = (difference between the lengths of the old and new arrays) | ||
return rval.node.end - rval.node.start + 1; | ||
}).reduce(function (a, b) { | ||
return a + b; | ||
}, 0); // add 1 for space in front // sum | ||
const numCommaAdded = newInit.elements.length - oldInit.elements.length; | ||
return numCommaAdded + sizeOfRvals < statementsLength; | ||
if (anyUndefined(assignments.map(([, rval]) => rval.node.end)) || anyUndefined(assignments.map(([, rval]) => rval.node.start))) { | ||
return false; | ||
} | ||
}]); | ||
return ArrayPropertyCollapser; | ||
}(Collapser); | ||
const sizeOfRvals = assignments.map(([, rval]) => rval.node.end - rval.node.start + 1).reduce((a, b) => a + b, 0); // add 1 for space in front // sum | ||
return numCommaAdded + sizeOfRvals < statementsLength; | ||
} | ||
} | ||
module.exports = ArrayPropertyCollapser; |
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
const NotImplementedError = Error("NotImplementedError"); | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
class Collapser { | ||
isInitTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
isExpressionTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
var NotImplementedError = Error("NotImplementedError"); | ||
getExpressionChecker() { | ||
throw NotImplementedError; | ||
} | ||
var Collapser = | ||
/*#__PURE__*/ | ||
function () { | ||
function Collapser() { | ||
_classCallCheck(this, Collapser); | ||
extractAssignment() { | ||
throw NotImplementedError; | ||
} | ||
_createClass(Collapser, [{ | ||
key: "isInitTypeValid", | ||
value: function isInitTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
}, { | ||
key: "isExpressionTypeValid", | ||
value: function isExpressionTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
}, { | ||
key: "getExpressionChecker", | ||
value: function getExpressionChecker() { | ||
throw NotImplementedError; | ||
} | ||
}, { | ||
key: "extractAssignment", | ||
value: function extractAssignment() { | ||
throw NotImplementedError; | ||
} | ||
}, { | ||
key: "addSuccessfully", | ||
value: function addSuccessfully() { | ||
throw NotImplementedError; | ||
} | ||
}, { | ||
key: "isSizeSmaller", | ||
value: function isSizeSmaller() { | ||
return true; | ||
} | ||
}]); | ||
addSuccessfully() { | ||
throw NotImplementedError; | ||
} | ||
return Collapser; | ||
}(); | ||
isSizeSmaller() { | ||
return true; | ||
} | ||
} | ||
module.exports = Collapser; |
129
lib/index.js
"use strict"; | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
var COLLAPSERS = [require("./object-collapser"), require("./array-collapser"), require("./array-property-collapser"), require("./set-collapser")].map(function (Collapser) { | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
const COLLAPSERS = [require("./object-collapser"), require("./array-collapser"), require("./array-property-collapser"), require("./set-collapser")].map(Collapser => { | ||
return new Collapser(); | ||
@@ -12,5 +16,3 @@ }); | ||
function getFunctionParent(path, scopeParent) { | ||
var parent = path.findParent(function (p) { | ||
return p.isFunction(); | ||
}); // don"t traverse higher than the function the var is defined in. | ||
const parent = path.findParent(p => p.isFunction()); // don"t traverse higher than the function the var is defined in. | ||
@@ -20,9 +22,7 @@ return parent === scopeParent ? null : parent; | ||
function getFunctionReferences(path, scopeParent) { | ||
var references = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set(); | ||
function getFunctionReferences(path, scopeParent, references = new Set()) { | ||
for (let func = getFunctionParent(path, scopeParent); func; func = getFunctionParent(func, scopeParent)) { | ||
const id = func.node.id; | ||
const binding = id && func.scope.getBinding(id.name); | ||
for (var func = getFunctionParent(path, scopeParent); func; func = getFunctionParent(func, scopeParent)) { | ||
var id = func.node.id; | ||
var binding = id && func.scope.getBinding(id.name); | ||
if (!binding) { | ||
@@ -32,3 +32,3 @@ continue; | ||
binding.referencePaths.forEach(function (path) { | ||
binding.referencePaths.forEach(path => { | ||
if (!references.has(path)) { | ||
@@ -46,3 +46,3 @@ references.add(path); | ||
// Returns false if there's an error. Otherwise returns a list of references. | ||
var binding = parent.scope.getBinding(name); | ||
const binding = parent.scope.getBinding(name); | ||
@@ -53,3 +53,3 @@ if (!binding) { | ||
var references = binding.referencePaths.reduce(function (references, ref) { | ||
const references = binding.referencePaths.reduce((references, ref) => { | ||
references.add(ref); | ||
@@ -71,3 +71,3 @@ getFunctionReferences(ref, parent, references); | ||
// otherwise returns [foo as string, ?rval, index of the variable declaration] | ||
var declarations = path.get("declarations"); | ||
const declarations = path.get("declarations"); | ||
@@ -78,5 +78,5 @@ if (declarations.length !== 1) { | ||
var declaration = declarations[0]; | ||
var id = declaration.get("id"), | ||
init = declaration.get("init"); | ||
const declaration = declarations[0]; | ||
const id = declaration.get("id"), | ||
init = declaration.get("init"); | ||
@@ -87,3 +87,3 @@ if (!id.isIdentifier()) { | ||
var parent = path.parentPath; | ||
const parent = path.parentPath; | ||
@@ -94,3 +94,3 @@ if (!parent.isBlockParent() || !parent.isScopable()) { | ||
var body = parent.get("body"); | ||
const body = parent.get("body"); | ||
@@ -101,3 +101,3 @@ if (!Array.isArray(body)) { | ||
var startIndex = body.indexOf(path); | ||
const startIndex = body.indexOf(path); | ||
@@ -117,3 +117,3 @@ if (startIndex === -1) { | ||
if (path.isExpressionStatement()) { | ||
var exprs = collectExpressions(path.get("expression"), isExprTypeValid); | ||
const exprs = collectExpressions(path.get("expression"), isExprTypeValid); | ||
return exprs !== null ? exprs : null; | ||
@@ -123,14 +123,8 @@ } | ||
if (path.isSequenceExpression()) { | ||
var _exprs = path.get("expressions").map(function (p) { | ||
return collectExpressions(p, isExprTypeValid); | ||
}); | ||
const exprs = path.get("expressions").map(p => collectExpressions(p, isExprTypeValid)); | ||
if (_exprs.some(function (e) { | ||
return e === null; | ||
})) { | ||
if (exprs.some(e => e === null)) { | ||
return null; | ||
} else { | ||
return _exprs.reduce(function (s, n) { | ||
return s.concat(n); | ||
}, []); // === Array.flatten | ||
return exprs.reduce((s, n) => s.concat(n), []); // === Array.flatten | ||
} | ||
@@ -147,11 +141,9 @@ } | ||
function getContiguousStatementsAndExpressions(body, start, end, isExprTypeValid, checkExpr) { | ||
var statements = []; | ||
var allExprs = []; | ||
const statements = []; | ||
let allExprs = []; | ||
for (var i = start; i < end; i++) { | ||
var exprs = collectExpressions(body[i], isExprTypeValid); | ||
for (let i = start; i < end; i++) { | ||
const exprs = collectExpressions(body[i], isExprTypeValid); | ||
if (exprs === null || !exprs.every(function (e) { | ||
return checkExpr(e); | ||
})) { | ||
if (exprs === null || !exprs.every(e => checkExpr(e))) { | ||
break; | ||
@@ -169,7 +161,3 @@ } | ||
// returns a function s.t. given an expr, returns true iff expr is an ancestor of a reference | ||
return function (expr) { | ||
return references.some(function (r) { | ||
return r === expr || r.isDescendant(expr); | ||
}); | ||
}; | ||
return expr => references.some(r => r === expr || r.isDescendant(expr)); | ||
} | ||
@@ -179,6 +167,6 @@ | ||
// Returns true iff successfully used the collapser. Otherwise returns undefined. | ||
var _topLevel = _slicedToArray(topLevel, 3), | ||
name = _topLevel[0], | ||
init = _topLevel[1], | ||
startIndex = _topLevel[2]; | ||
const _topLevel = _slicedToArray(topLevel, 3), | ||
name = _topLevel[0], | ||
init = _topLevel[1], | ||
startIndex = _topLevel[2]; | ||
@@ -189,8 +177,8 @@ if (!collapser.isInitTypeValid(init)) { | ||
var body = varDecl.parentPath.get("body"); | ||
const body = varDecl.parentPath.get("body"); | ||
var _getContiguousStateme = getContiguousStatementsAndExpressions(body, startIndex + 1, body.length, collapser.isExpressionTypeValid, collapser.getExpressionChecker(name, checkReference)), | ||
_getContiguousStateme2 = _slicedToArray(_getContiguousStateme, 2), | ||
statements = _getContiguousStateme2[0], | ||
exprs = _getContiguousStateme2[1]; | ||
const _getContiguousStateme = getContiguousStatementsAndExpressions(body, startIndex + 1, body.length, collapser.isExpressionTypeValid, collapser.getExpressionChecker(name, checkReference)), | ||
_getContiguousStateme2 = _slicedToArray(_getContiguousStateme, 2), | ||
statements = _getContiguousStateme2[0], | ||
exprs = _getContiguousStateme2[1]; | ||
@@ -201,11 +189,7 @@ if (statements.length === 0) { | ||
var assignments = exprs.map(function (e) { | ||
return collapser.extractAssignment(e); | ||
}); | ||
var oldInit = init.node; | ||
var newInit = t.cloneDeep(oldInit); | ||
const assignments = exprs.map(e => collapser.extractAssignment(e)); | ||
const oldInit = init.node; | ||
const newInit = t.cloneDeep(oldInit); | ||
if (!assignments.every(function (assignment) { | ||
return collapser.addSuccessfully(t, assignment, newInit); | ||
})) { | ||
if (!assignments.every(assignment => collapser.addSuccessfully(t, assignment, newInit))) { | ||
return; | ||
@@ -226,10 +210,9 @@ } // some collapses may increase the size | ||
init.replaceWith(newInit); | ||
statements.forEach(function (s) { | ||
return s.remove(); | ||
}); | ||
statements.forEach(s => s.remove()); | ||
return true; | ||
} | ||
module.exports = function (_ref) { | ||
var t = _ref.types; | ||
module.exports = function ({ | ||
types: t | ||
}) { | ||
return { | ||
@@ -239,3 +222,3 @@ name: "transform-inline-consecutive-adds", | ||
VariableDeclaration(varDecl) { | ||
var topLevel = validateTopLevel(varDecl); | ||
const topLevel = validateTopLevel(varDecl); | ||
@@ -246,6 +229,6 @@ if (!topLevel) { | ||
var _topLevel2 = _slicedToArray(topLevel, 1), | ||
name = _topLevel2[0]; | ||
const _topLevel2 = _slicedToArray(topLevel, 1), | ||
name = _topLevel2[0]; | ||
var references = getIdAndFunctionReferences(name, varDecl.parentPath); | ||
const references = getIdAndFunctionReferences(name, varDecl.parentPath); | ||
@@ -256,7 +239,5 @@ if (references === false) { | ||
var checkReference = getReferenceChecker(references); | ||
const checkReference = getReferenceChecker(references); | ||
if (COLLAPSERS.some(function (c) { | ||
return tryUseCollapser(t, c, varDecl, topLevel, checkReference); | ||
})) { | ||
if (COLLAPSERS.some(c => tryUseCollapser(t, c, varDecl, topLevel, checkReference))) { | ||
return; | ||
@@ -263,0 +244,0 @@ } |
"use strict"; | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
const Collapser = require("./collapser"); | ||
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } | ||
class ObjectCollapser extends Collapser { | ||
isInitTypeValid(init) { | ||
return init.isObjectExpression(); | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var Collapser = require("./collapser"); | ||
var ObjectCollapser = | ||
/*#__PURE__*/ | ||
function (_Collapser) { | ||
_inherits(ObjectCollapser, _Collapser); | ||
function ObjectCollapser() { | ||
_classCallCheck(this, ObjectCollapser); | ||
return _possibleConstructorReturn(this, (ObjectCollapser.__proto__ || Object.getPrototypeOf(ObjectCollapser)).apply(this, arguments)); | ||
isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
} | ||
_createClass(ObjectCollapser, [{ | ||
key: "isInitTypeValid", | ||
value: function isInitTypeValid(init) { | ||
return init.isObjectExpression(); | ||
} | ||
}, { | ||
key: "isExpressionTypeValid", | ||
value: function isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
} | ||
}, { | ||
key: "getExpressionChecker", | ||
value: function getExpressionChecker(objName, checkReference) { | ||
return function (expr) { | ||
// checks expr is of form: | ||
// foo.a = rval | foo[a] = rval | ||
var left = expr.get("left"); | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.a = rval | foo[a] = rval | ||
const left = expr.get("left"); | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
var obj = left.get("object"), | ||
const obj = left.get("object"), | ||
prop = left.get("property"); | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
if (!prop.isIdentifier() && checkReference(prop)) { | ||
return false; | ||
} | ||
if (!prop.isIdentifier() && checkReference(prop)) { | ||
return false; | ||
} | ||
if (left.node.computed && !(prop.isStringLiteral() || prop.isNumericLiteral())) { | ||
return false; | ||
} | ||
if (left.node.computed && !(prop.isStringLiteral() || prop.isNumericLiteral())) { | ||
return false; | ||
} | ||
var right = expr.get("right"); | ||
const right = expr.get("right"); | ||
if (checkReference(right)) { | ||
return false; | ||
} | ||
if (checkReference(right)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
}, { | ||
key: "extractAssignment", | ||
value: function extractAssignment(expr) { | ||
return [expr.node.left.property, expr.node.right]; | ||
} | ||
}, { | ||
key: "addSuccessfully", | ||
value: function addSuccessfully(t, _ref, init) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
left = _ref2[0], | ||
right = _ref2[1]; | ||
init.properties.push(t.objectProperty(left, right)); | ||
return true; | ||
} | ||
}]); | ||
}; | ||
} | ||
return ObjectCollapser; | ||
}(Collapser); | ||
extractAssignment(expr) { | ||
return [expr.node.left.property, expr.node.right]; | ||
} | ||
addSuccessfully(t, [left, right], init) { | ||
init.properties.push(t.objectProperty(left, right)); | ||
return true; | ||
} | ||
} | ||
module.exports = ObjectCollapser; |
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
const Collapser = require("./collapser"); | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
class SetCollapser extends Collapser { | ||
isInitTypeValid(init) { | ||
return init.isNewExpression() && init.get("callee").isIdentifier() && init.node.callee.name === "Set" && ( // other iterables might not be append-able | ||
init.node.arguments.length === 0 || init.node.arguments.length === 1 && init.get("arguments")[0].isArrayExpression()); | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var Collapser = require("./collapser"); | ||
var SetCollapser = | ||
/*#__PURE__*/ | ||
function (_Collapser) { | ||
_inherits(SetCollapser, _Collapser); | ||
function SetCollapser() { | ||
_classCallCheck(this, SetCollapser); | ||
return _possibleConstructorReturn(this, (SetCollapser.__proto__ || Object.getPrototypeOf(SetCollapser)).apply(this, arguments)); | ||
isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
} | ||
_createClass(SetCollapser, [{ | ||
key: "isInitTypeValid", | ||
value: function isInitTypeValid(init) { | ||
return init.isNewExpression() && init.get("callee").isIdentifier() && init.node.callee.name === "Set" && ( // other iterables might not be append-able | ||
init.node.arguments.length === 0 || init.node.arguments.length === 1 && init.get("arguments")[0].isArrayExpression()); | ||
} | ||
}, { | ||
key: "isExpressionTypeValid", | ||
value: function isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
} | ||
}, { | ||
key: "getExpressionChecker", | ||
value: function getExpressionChecker(objName, checkReference) { | ||
return function (expr) { | ||
// checks expr is of form: | ||
// foo.add(rval) | ||
var callee = expr.get("callee"); | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.add(rval) | ||
const callee = expr.get("callee"); | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
var obj = callee.get("object"), | ||
const obj = callee.get("object"), | ||
prop = callee.get("property"); | ||
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "add") { | ||
return false; | ||
} | ||
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "add") { | ||
return false; | ||
} | ||
var args = expr.get("arguments"); | ||
const args = expr.get("arguments"); | ||
if (args.length !== 1) { | ||
return false; | ||
} | ||
if (args.length !== 1) { | ||
return false; | ||
} | ||
if (checkReference(args[0])) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
} | ||
}, { | ||
key: "extractAssignment", | ||
value: function extractAssignment(expr) { | ||
return expr.node.arguments[0]; | ||
} | ||
}, { | ||
key: "addSuccessfully", | ||
value: function addSuccessfully(t, arg, init) { | ||
if (init.arguments.length === 0) { | ||
init.arguments.push(t.arrayExpression()); | ||
if (checkReference(args[0])) { | ||
return false; | ||
} | ||
init.arguments[0].elements.push(arg); | ||
return true; | ||
}; | ||
} | ||
extractAssignment(expr) { | ||
return expr.node.arguments[0]; | ||
} | ||
addSuccessfully(t, arg, init) { | ||
if (init.arguments.length === 0) { | ||
init.arguments.push(t.arrayExpression()); | ||
} | ||
}]); | ||
return SetCollapser; | ||
}(Collapser); | ||
init.arguments[0].elements.push(arg); | ||
return true; | ||
} | ||
} | ||
module.exports = SetCollapser; |
{ | ||
"name": "babel-plugin-transform-inline-consecutive-adds", | ||
"version": "0.5.0-alpha.55276695", | ||
"version": "0.5.0-alpha.01eac1c3", | ||
"description": "This plugin inlines consecutive property assignments, array pushes, etc.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -39,3 +39,3 @@ # babel-plugin-transform-inline-consecutive-adds | ||
```sh | ||
npm install babel-plugin-transform-inline-consecutive-adds | ||
npm install babel-plugin-transform-inline-consecutive-adds --save-dev | ||
``` | ||
@@ -42,0 +42,0 @@ |
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
14686
391