WIP - add extractor, generate snippet_data

This commit is contained in:
Stefan Fejes
2019-08-20 15:52:05 +02:00
parent 88084d3d30
commit cc8f1d8a7a
37396 changed files with 4588842 additions and 133 deletions

View File

@ -0,0 +1,76 @@
"use strict";
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; }; }();
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 ArrayCollapser = function (_Collapser) {
_inherits(ArrayCollapser, _Collapser);
function ArrayCollapser() {
_classCallCheck(this, ArrayCollapser);
return _possibleConstructorReturn(this, (ArrayCollapser.__proto__ || Object.getPrototypeOf(ArrayCollapser)).apply(this, arguments));
}
_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");
if (!callee.isMemberExpression()) {
return false;
}
var obj = callee.get("object"),
prop = callee.get("property");
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "push") {
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;
}
}]);
return ArrayCollapser;
}(Collapser);
module.exports = ArrayCollapser;

View File

@ -0,0 +1,148 @@
"use strict";
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"); } }; }();
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; }; }();
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));
}
_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");
if (!left.isMemberExpression()) {
return false;
}
var obj = left.get("object"),
prop = left.get("property");
if (!obj.isIdentifier() || obj.node.name !== objName) {
return false;
}
var checkIndex = function checkIndex(num) {
return Number.isInteger(num) && num >= 0;
};
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;
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;
// 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)
var numCommaAdded = newInit.elements.length - oldInit.elements.length;
if (anyUndefined(assignments.map(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 2),
rval = _ref5[1];
return rval.node.end;
})) || anyUndefined(assignments.map(function (_ref6) {
var _ref7 = _slicedToArray(_ref6, 2),
rval = _ref7[1];
return rval.node.start;
}))) {
return false;
}
var sizeOfRvals = assignments.map(function (_ref8) {
var _ref9 = _slicedToArray(_ref8, 2),
rval = _ref9[1];
return rval.node.end - rval.node.start + 1;
}).reduce(function (a, b) {
return a + b;
}, 0); // add 1 for space in front // sum
return numCommaAdded + sizeOfRvals < statementsLength;
}
}]);
return ArrayPropertyCollapser;
}(Collapser);
module.exports = ArrayPropertyCollapser;

View File

@ -0,0 +1,49 @@
"use strict";
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var NotImplementedError = Error("NotImplementedError");
var Collapser = function () {
function Collapser() {
_classCallCheck(this, Collapser);
}
_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;
}
}]);
return Collapser;
}();
module.exports = Collapser;

View File

@ -0,0 +1,230 @@
"use strict";
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"); } }; }();
var COLLAPSERS = [require("./object-collapser"), require("./array-collapser"), require("./array-property-collapser"), require("./set-collapser")].map(function (Collapser) {
return new Collapser();
});
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.
return parent === scopeParent ? null : parent;
}
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) {
continue;
}
binding.referencePaths.forEach(function (path) {
if (!references.has(path)) {
references.add(path);
getFunctionReferences(path, scopeParent, references);
}
});
}
return references;
}
function getIdAndFunctionReferences(name, parent) {
// Returns false if there's an error. Otherwise returns a list of references.
var binding = parent.scope.getBinding(name);
if (!binding) {
return false;
}
var references = binding.referencePaths.reduce(function (references, ref) {
references.add(ref);
getFunctionReferences(ref, parent, references);
return references;
}, new Set());
return Array.from(references);
}
function validateTopLevel(path) {
// Ensures the structure is of the form (roughly):
// {
// ...
// var foo = expr;
// ...
// }
// returns null if not of this form
// otherwise returns [foo as string, ?rval, index of the variable declaration]
var declarations = path.get("declarations");
if (declarations.length !== 1) {
return;
}
var declaration = declarations[0];
var id = declaration.get("id"),
init = declaration.get("init");
if (!id.isIdentifier()) {
return;
}
var parent = path.parentPath;
if (!parent.isBlockParent() || !parent.isScopable()) {
return;
}
var body = parent.get("body");
if (!Array.isArray(body)) {
return;
}
var startIndex = body.indexOf(path);
if (startIndex === -1) {
return;
}
return [id.node.name, init, startIndex];
}
function collectExpressions(path, isExprTypeValid) {
// input: ExprStatement => 'a | SequenceExpression
// SequenceExpression => 'a list
// Validates 'a is of the right type
// returns null if found inconsistency, else returns Array<"a>
if (path.isExpressionStatement()) {
var exprs = collectExpressions(path.get("expression"), isExprTypeValid);
return exprs !== null ? exprs : null;
}
if (path.isSequenceExpression()) {
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(function (s, n) {
return s.concat(n);
}, []); // === Array.flatten
}
}
if (isExprTypeValid(path)) {
return [path];
}
return null;
}
function getContiguousStatementsAndExpressions(body, start, end, isExprTypeValid, checkExpr) {
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;
}
statements.push(body[i]);
allExprs = allExprs.concat(exprs);
}
return [statements, allExprs];
}
function getReferenceChecker(references) {
// 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);
});
};
}
function tryUseCollapser(t, collapser, varDecl, topLevel, checkReference) {
// Returns true iff successfully used the collapser. Otherwise returns undefined.
var _topLevel = _slicedToArray(topLevel, 3),
name = _topLevel[0],
init = _topLevel[1],
startIndex = _topLevel[2];
if (!collapser.isInitTypeValid(init)) {
return;
}
var 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];
if (statements.length === 0) {
return;
}
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;
}
// some collapses may increase the size
if (!collapser.isSizeSmaller({
newInit,
oldInit,
varDecl,
assignments,
statements
})) {
return;
}
init.replaceWith(newInit);
statements.forEach(function (s) {
return s.remove();
});
return true;
}
module.exports = function (_ref) {
var t = _ref.types;
return {
name: "transform-inline-consecutive-adds",
visitor: {
VariableDeclaration(varDecl) {
var topLevel = validateTopLevel(varDecl);
if (!topLevel) {
return;
}
var _topLevel2 = _slicedToArray(topLevel, 1),
name = _topLevel2[0];
var references = getIdAndFunctionReferences(name, varDecl.parentPath);
if (references === false) {
return;
}
var checkReference = getReferenceChecker(references);
if (COLLAPSERS.some(function (c) {
return tryUseCollapser(t, c, varDecl, topLevel, checkReference);
})) {
return;
}
}
}
};
};

View File

@ -0,0 +1,86 @@
"use strict";
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"); } }; }();
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; }; }();
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));
}
_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");
if (!left.isMemberExpression()) {
return false;
}
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;
}
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;
}
}]);
return ObjectCollapser;
}(Collapser);
module.exports = ObjectCollapser;

View File

@ -0,0 +1,82 @@
"use strict";
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; }; }();
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 SetCollapser = function (_Collapser) {
_inherits(SetCollapser, _Collapser);
function SetCollapser() {
_classCallCheck(this, SetCollapser);
return _possibleConstructorReturn(this, (SetCollapser.__proto__ || Object.getPrototypeOf(SetCollapser)).apply(this, arguments));
}
_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");
if (!callee.isMemberExpression()) {
return false;
}
var obj = callee.get("object"),
prop = callee.get("property");
if (!obj.isIdentifier() || obj.node.name !== objName || !prop.isIdentifier() || prop.node.name !== "add") {
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());
}
init.arguments[0].elements.push(arg);
return true;
}
}]);
return SetCollapser;
}(Collapser);
module.exports = SetCollapser;