babel-plugin-transform-inline-consecutive-adds
Advanced tools
Comparing version 0.3.0-alpha.5ee3078e to 0.3.0-alpha.636ae276
"use strict"; | ||
const Collapser = require("./collapser"); | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
class ArrayCollapser 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"); } } | ||
isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } 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 = function (_Collapser) { | ||
_inherits(ArrayCollapser, _Collapser); | ||
function ArrayCollapser() { | ||
_classCallCheck(this, ArrayCollapser); | ||
return _possibleConstructorReturn(this, (ArrayCollapser.__proto__ || Object.getPrototypeOf(ArrayCollapser)).apply(this, arguments)); | ||
} | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.push(rval1, ...nrvals) | ||
_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) | ||
const callee = expr.get("callee"); | ||
var callee = expr.get("callee"); | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
const obj = callee.get("object"), | ||
var 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; | ||
} | ||
const args = expr.get("arguments"); | ||
if (args.some(checkReference)) { | ||
return false; | ||
} | ||
var args = expr.get("arguments"); | ||
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; | ||
}; | ||
} | ||
} | ||
}]); | ||
extractAssignment(expr) { | ||
return expr.node.arguments; | ||
} | ||
return ArrayCollapser; | ||
}(Collapser); | ||
addSuccessfully(t, args, init) { | ||
args.map(a => init.elements.push(a)); | ||
return true; | ||
} | ||
} | ||
module.exports = ArrayCollapser; |
"use strict"; | ||
const Collapser = require("./collapser"); | ||
var _slicedToArray = function () { 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"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (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(); | ||
} | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } 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 = function (_Collapser) { | ||
_inherits(ArrayPropertyCollapser, _Collapser); | ||
function ArrayPropertyCollapser() { | ||
_classCallCheck(this, ArrayPropertyCollapser); | ||
return _possibleConstructorReturn(this, (ArrayPropertyCollapser.__proto__ || Object.getPrototypeOf(ArrayPropertyCollapser)).apply(this, arguments)); | ||
} | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo[num] = rval | ||
_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 | ||
const left = expr.get("left"); | ||
var left = expr.get("left"); | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
const obj = left.get("object"), | ||
var 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; | ||
} | ||
const checkIndex = num => Number.isInteger(num) && num >= 0; | ||
var checkIndex = function checkIndex(num) { | ||
return Number.isInteger(num) && num >= 0; | ||
}; | ||
if (!(prop.isNumericLiteral() || prop.isStringLiteral()) || !checkIndex(Number(prop.node.value))) { | ||
if (!(prop.isNumericLiteral() || prop.isStringLiteral()) || !checkIndex(Number(prop.node.value))) { | ||
return false; | ||
} | ||
var 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) { | ||
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; | ||
const right = expr.get("right"); | ||
if (checkReference(right)) { | ||
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. | ||
if (anyUndefined([statements[statements.length - 1].node.end, varDecl.node.end])) { | ||
return false; | ||
} | ||
var statementsLength = statements[statements.length - 1].node.end - varDecl.node.end; | ||
return true; | ||
}; | ||
} | ||
// 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) | ||
extractAssignment(expr) { | ||
return [expr.node.left.property.value, expr.get("right")]; | ||
} | ||
var numCommaAdded = newInit.elements.length - oldInit.elements.length; | ||
if (anyUndefined(assignments.map(function (_ref4) { | ||
var _ref5 = _slicedToArray(_ref4, 2), | ||
rval = _ref5[1]; | ||
addSuccessfully(t, [index, rval], init) { | ||
const elements = init.elements; | ||
for (let i = elements.length; i <= index; i++) { | ||
elements.push(null); | ||
} | ||
if (elements[index] !== null) { | ||
return false; | ||
} | ||
elements[index] = rval.node; | ||
return true; | ||
} | ||
return rval.node.end; | ||
})) || anyUndefined(assignments.map(function (_ref6) { | ||
var _ref7 = _slicedToArray(_ref6, 2), | ||
rval = _ref7[1]; | ||
isSizeSmaller({ newInit, oldInit, varDecl, assignments, statements }) { | ||
const anyUndefined = args => args.some(a => a === undefined); | ||
return rval.node.start; | ||
}))) { | ||
return false; | ||
} | ||
var sizeOfRvals = assignments.map(function (_ref8) { | ||
var _ref9 = _slicedToArray(_ref8, 2), | ||
rval = _ref9[1]; | ||
// 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. | ||
if (anyUndefined([statements[statements.length - 1].node.end, varDecl.node.end])) { | ||
return false; | ||
} | ||
const statementsLength = statements[statements.length - 1].node.end - varDecl.node.end; | ||
return rval.node.end - rval.node.start + 1; | ||
}).reduce(function (a, b) { | ||
return a + b; | ||
}, 0); // add 1 for space in front // sum | ||
// 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) | ||
const numCommaAdded = newInit.elements.length - oldInit.elements.length; | ||
if (anyUndefined(assignments.map(([, rval]) => rval.node.end)) || anyUndefined(assignments.map(([, rval]) => rval.node.start))) { | ||
return false; | ||
return numCommaAdded + sizeOfRvals < statementsLength; | ||
} | ||
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; | ||
} | ||
} | ||
return ArrayPropertyCollapser; | ||
}(Collapser); | ||
module.exports = ArrayPropertyCollapser; |
"use strict"; | ||
const NotImplementedError = Error("NotImplementedError"); | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
class Collapser { | ||
isInitTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
isExpressionTypeValid() { | ||
throw NotImplementedError; | ||
} | ||
var NotImplementedError = Error("NotImplementedError"); | ||
getExpressionChecker() { | ||
throw NotImplementedError; | ||
var Collapser = 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; |
122
lib/index.js
@@ -5,3 +5,3 @@ "use strict"; | ||
const COLLAPSERS = [require("./object-collapser"), require("./array-collapser"), require("./array-property-collapser"), require("./set-collapser")].map(Collapser => { | ||
var COLLAPSERS = [require("./object-collapser"), require("./array-collapser"), require("./array-property-collapser"), require("./set-collapser")].map(function (Collapser) { | ||
return new Collapser(); | ||
@@ -11,3 +11,5 @@ }); | ||
function getFunctionParent(path, scopeParent) { | ||
const parent = path.findParent(p => p.isFunction()); | ||
var parent = path.findParent(function (p) { | ||
return p.isFunction(); | ||
}); | ||
// don"t traverse higher than the function the var is defined in. | ||
@@ -17,7 +19,9 @@ return parent === scopeParent ? null : parent; | ||
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); | ||
function getFunctionReferences(path, scopeParent) { | ||
var references = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set(); | ||
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) { | ||
@@ -27,3 +31,3 @@ continue; | ||
binding.referencePaths.forEach(path => { | ||
binding.referencePaths.forEach(function (path) { | ||
if (!references.has(path)) { | ||
@@ -40,3 +44,3 @@ references.add(path); | ||
// Returns false if there's an error. Otherwise returns a list of references. | ||
const binding = parent.scope.getBinding(name); | ||
var binding = parent.scope.getBinding(name); | ||
if (!binding) { | ||
@@ -46,3 +50,3 @@ return false; | ||
const references = binding.referencePaths.reduce((references, ref) => { | ||
var references = binding.referencePaths.reduce(function (references, ref) { | ||
references.add(ref); | ||
@@ -66,3 +70,3 @@ getFunctionReferences(ref, parent, references); | ||
const declarations = path.get("declarations"); | ||
var declarations = path.get("declarations"); | ||
if (declarations.length !== 1) { | ||
@@ -72,5 +76,5 @@ return; | ||
const declaration = declarations[0]; | ||
const id = declaration.get("id"), | ||
init = declaration.get("init"); | ||
var declaration = declarations[0]; | ||
var id = declaration.get("id"), | ||
init = declaration.get("init"); | ||
if (!id.isIdentifier()) { | ||
@@ -80,3 +84,3 @@ return; | ||
const parent = path.parentPath; | ||
var parent = path.parentPath; | ||
if (!parent.isBlockParent() || !parent.isScopable()) { | ||
@@ -86,7 +90,7 @@ return; | ||
const body = parent.get("body"); | ||
var body = parent.get("body"); | ||
if (!Array.isArray(body)) { | ||
return; | ||
} | ||
const startIndex = body.indexOf(path); | ||
var startIndex = body.indexOf(path); | ||
if (startIndex === -1) { | ||
@@ -105,3 +109,3 @@ return; | ||
if (path.isExpressionStatement()) { | ||
const exprs = collectExpressions(path.get("expression"), isExprTypeValid); | ||
var exprs = collectExpressions(path.get("expression"), isExprTypeValid); | ||
return exprs !== null ? exprs : null; | ||
@@ -111,7 +115,13 @@ } | ||
if (path.isSequenceExpression()) { | ||
const exprs = path.get("expressions").map(p => collectExpressions(p, isExprTypeValid)); | ||
if (exprs.some(e => e === null)) { | ||
var _exprs = path.get("expressions").map(function (p) { | ||
return collectExpressions(p, isExprTypeValid); | ||
}); | ||
if (_exprs.some(function (e) { | ||
return e === null; | ||
})) { | ||
return null; | ||
} else { | ||
return exprs.reduce((s, n) => s.concat(n), []); // === Array.flatten | ||
return _exprs.reduce(function (s, n) { | ||
return s.concat(n); | ||
}, []); // === Array.flatten | ||
} | ||
@@ -128,7 +138,9 @@ } | ||
function getContiguousStatementsAndExpressions(body, start, end, isExprTypeValid, checkExpr) { | ||
const statements = []; | ||
let allExprs = []; | ||
for (let i = start; i < end; i++) { | ||
const exprs = collectExpressions(body[i], isExprTypeValid); | ||
if (exprs === null || !exprs.every(e => checkExpr(e))) { | ||
var statements = []; | ||
var allExprs = []; | ||
for (var i = start; i < end; i++) { | ||
var exprs = collectExpressions(body[i], isExprTypeValid); | ||
if (exprs === null || !exprs.every(function (e) { | ||
return checkExpr(e); | ||
})) { | ||
break; | ||
@@ -144,3 +156,7 @@ } | ||
// returns a function s.t. given an expr, returns true iff expr is an ancestor of a reference | ||
return expr => references.some(r => r === expr || r.isDescendant(expr)); | ||
return function (expr) { | ||
return references.some(function (r) { | ||
return r === expr || r.isDescendant(expr); | ||
}); | ||
}; | ||
} | ||
@@ -150,8 +166,7 @@ | ||
// Returns true iff successfully used the collapser. Otherwise returns undefined. | ||
var _topLevel = _slicedToArray(topLevel, 3); | ||
var _topLevel = _slicedToArray(topLevel, 3), | ||
name = _topLevel[0], | ||
init = _topLevel[1], | ||
startIndex = _topLevel[2]; | ||
const name = _topLevel[0], | ||
init = _topLevel[1], | ||
startIndex = _topLevel[2]; | ||
if (!collapser.isInitTypeValid(init)) { | ||
@@ -161,11 +176,9 @@ return; | ||
const body = varDecl.parentPath.get("body"); | ||
var body = varDecl.parentPath.get("body"); | ||
var _getContiguousStateme = getContiguousStatementsAndExpressions(body, startIndex + 1, body.length, collapser.isExpressionTypeValid, collapser.getExpressionChecker(name, checkReference)), | ||
_getContiguousStateme2 = _slicedToArray(_getContiguousStateme, 2); | ||
_getContiguousStateme2 = _slicedToArray(_getContiguousStateme, 2), | ||
statements = _getContiguousStateme2[0], | ||
exprs = _getContiguousStateme2[1]; | ||
const statements = _getContiguousStateme2[0], | ||
exprs = _getContiguousStateme2[1]; | ||
if (statements.length === 0) { | ||
@@ -175,6 +188,10 @@ return; | ||
const assignments = exprs.map(e => collapser.extractAssignment(e)); | ||
const oldInit = init.node; | ||
const newInit = t.cloneDeep(oldInit); | ||
if (!assignments.every(assignment => collapser.addSuccessfully(t, assignment, newInit))) { | ||
var assignments = exprs.map(function (e) { | ||
return collapser.extractAssignment(e); | ||
}); | ||
var oldInit = init.node; | ||
var newInit = t.cloneDeep(oldInit); | ||
if (!assignments.every(function (assignment) { | ||
return collapser.addSuccessfully(t, assignment, newInit); | ||
})) { | ||
return; | ||
@@ -195,7 +212,11 @@ } | ||
init.replaceWith(newInit); | ||
statements.forEach(s => s.remove()); | ||
statements.forEach(function (s) { | ||
return s.remove(); | ||
}); | ||
return true; | ||
} | ||
module.exports = function ({ types: t }) { | ||
module.exports = function (_ref) { | ||
var t = _ref.types; | ||
return { | ||
@@ -205,3 +226,3 @@ name: "transform-inline-consecutive-adds", | ||
VariableDeclaration(varDecl) { | ||
const topLevel = validateTopLevel(varDecl); | ||
var topLevel = validateTopLevel(varDecl); | ||
if (!topLevel) { | ||
@@ -211,13 +232,14 @@ return; | ||
var _topLevel2 = _slicedToArray(topLevel, 1); | ||
var _topLevel2 = _slicedToArray(topLevel, 1), | ||
name = _topLevel2[0]; | ||
const name = _topLevel2[0]; | ||
const references = getIdAndFunctionReferences(name, varDecl.parentPath); | ||
var references = getIdAndFunctionReferences(name, varDecl.parentPath); | ||
if (references === false) { | ||
return; | ||
} | ||
const checkReference = getReferenceChecker(references); | ||
var checkReference = getReferenceChecker(references); | ||
if (COLLAPSERS.some(c => tryUseCollapser(t, c, varDecl, topLevel, checkReference))) { | ||
if (COLLAPSERS.some(function (c) { | ||
return tryUseCollapser(t, c, varDecl, topLevel, checkReference); | ||
})) { | ||
return; | ||
@@ -224,0 +246,0 @@ } |
"use strict"; | ||
const Collapser = require("./collapser"); | ||
var _slicedToArray = function () { 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"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (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(); | ||
} | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
isExpressionTypeValid(expr) { | ||
return expr.isAssignmentExpression(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } 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 = function (_Collapser) { | ||
_inherits(ObjectCollapser, _Collapser); | ||
function ObjectCollapser() { | ||
_classCallCheck(this, ObjectCollapser); | ||
return _possibleConstructorReturn(this, (ObjectCollapser.__proto__ || Object.getPrototypeOf(ObjectCollapser)).apply(this, arguments)); | ||
} | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.a = rval | foo[a] = rval | ||
_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 | ||
const left = expr.get("left"); | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
var left = expr.get("left"); | ||
if (!left.isMemberExpression()) { | ||
return false; | ||
} | ||
const obj = left.get("object"), | ||
var obj = left.get("object"), | ||
prop = left.get("property"); | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
if (!prop.isIdentifier() && checkReference(prop)) { | ||
return false; | ||
} | ||
if (left.node.computed && !(prop.isStringLiteral() || prop.isNumericLiteral())) { | ||
return false; | ||
} | ||
if (!obj.isIdentifier() || obj.node.name !== objName) { | ||
return false; | ||
} | ||
if (!prop.isIdentifier() && checkReference(prop)) { | ||
return false; | ||
} | ||
if (left.node.computed && !(prop.isStringLiteral() || prop.isNumericLiteral())) { | ||
return false; | ||
} | ||
const right = expr.get("right"); | ||
if (checkReference(right)) { | ||
return false; | ||
} | ||
var right = expr.get("right"); | ||
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; | ||
}; | ||
} | ||
} | ||
}]); | ||
extractAssignment(expr) { | ||
return [expr.node.left.property, expr.node.right]; | ||
} | ||
return ObjectCollapser; | ||
}(Collapser); | ||
addSuccessfully(t, [left, right], init) { | ||
init.properties.push(t.objectProperty(left, right)); | ||
return true; | ||
} | ||
} | ||
module.exports = ObjectCollapser; |
"use strict"; | ||
const Collapser = require("./collapser"); | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
isExpressionTypeValid(expr) { | ||
return expr.isCallExpression(); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } 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 = function (_Collapser) { | ||
_inherits(SetCollapser, _Collapser); | ||
function SetCollapser() { | ||
_classCallCheck(this, SetCollapser); | ||
return _possibleConstructorReturn(this, (SetCollapser.__proto__ || Object.getPrototypeOf(SetCollapser)).apply(this, arguments)); | ||
} | ||
getExpressionChecker(objName, checkReference) { | ||
return expr => { | ||
// checks expr is of form: | ||
// foo.add(rval) | ||
_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) | ||
const callee = expr.get("callee"); | ||
var callee = expr.get("callee"); | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
if (!callee.isMemberExpression()) { | ||
return false; | ||
} | ||
const obj = callee.get("object"), | ||
var 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; | ||
} | ||
const args = expr.get("arguments"); | ||
if (args.length !== 1) { | ||
return false; | ||
var args = expr.get("arguments"); | ||
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]; | ||
} | ||
return SetCollapser; | ||
}(Collapser); | ||
addSuccessfully(t, arg, init) { | ||
if (init.arguments.length === 0) { | ||
init.arguments.push(t.arrayExpression()); | ||
} | ||
init.arguments[0].elements.push(arg); | ||
return true; | ||
} | ||
} | ||
module.exports = SetCollapser; |
{ | ||
"name": "babel-plugin-transform-inline-consecutive-adds", | ||
"version": "0.3.0-alpha.5ee3078e", | ||
"version": "0.3.0-alpha.636ae276", | ||
"description": "This plugin inlines consecutive property assignments, array pushes, etc.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
26969
563