always coerce leading computed property initialisers into the init object - fixes #1820

This commit is contained in:
Sebastian McKenzie 2015-06-25 04:10:56 +01:00
parent a6f04055c0
commit 0b1ce6c9a4
6 changed files with 77 additions and 26 deletions

View File

@ -1,8 +1,8 @@
import * as t from "../../../types";
function loose(node, body, objId) {
for (var i = 0; i < node.properties.length; i++) {
var prop = node.properties[i];
for (var prop of (node.properties: Array)) {
if (!prop) continue;
body.push(t.expressionStatement(
t.assignmentExpression(
@ -15,32 +15,18 @@ function loose(node, body, objId) {
}
function spec(node, body, objId, initProps, file) {
var props = node.properties;
// add all non-computed properties and `__proto__` properties to the initializer
var broken = false;
for (let i = 0; i < props.length; i++) {
let prop = props[i];
if (prop.computed) {
broken = true;
}
if (prop.kind !== "init" || !broken || t.isLiteral(t.toComputedKey(prop, prop.key), { value: "__proto__" })) {
initProps.push(prop);
props[i] = null;
}
}
// add a simple assignment for all Symbol member expressions due to symbol polyfill limitations
// otherwise use Object.defineProperty
for (let i = 0; i < props.length; i++) {
let prop = props[i];
for (let prop of (node.properties: Array)) {
if (!prop) continue;
// this wont work with Object.defineProperty
if (t.isLiteral(t.toComputedKey(prop), { value: "__proto__" })) {
initProps.push(prop);
continue;
}
let key = prop.key;
if (t.isIdentifier(key) && !prop.computed) {
key = t.literal(key.name);
@ -68,14 +54,33 @@ export var visitor = {
exit(node, parent, scope, file) {
var hasComputed = false;
for (var prop of (node.properties: Array)) {
for (let prop of (node.properties: Array)) {
hasComputed = t.isProperty(prop, { computed: true, kind: "init" });
if (hasComputed) break;
}
if (!hasComputed) return;
// put all getters/setters into the first object expression as well as all initialisers up
// to the first computed property
var initProps = [];
var stopInits = false;
for (var i = 0; i < node.properties.length; i++) {
let prop = node.properties[i];
if (prop.computed) {
stopInits = true;
}
if (prop.kind !== "init" || !stopInits) {
initProps.push(prop);
node.properties[i] = null;
}
}
//
var objId = scope.generateUidIdentifierBasedOnNode(parent);
//

36
test.js Normal file
View File

@ -0,0 +1,36 @@
var uglify = require("uglify-js");
var babel = require(".");
var zlib = require("zlib");
var vm = require("vm");
var fs = require("fs");
var code = fs.readFileSync("/Users/sebmck/Downloads/jquery-1.11.3 (1).js", "utf8");
function sizeof(name, fn) {
var start = Date.now();
var code = fn();
var end = Date.now();
console.log(name, "time:", (end - start) + "ms", "raw:", (code.length / 1000) + "KB", "gzipped:", (zlib.gzipSync(code).length / 1000) + "KB");
fs.writeFileSync("jquery." + name + ".js", code);
new Function(code);
}
sizeof("raw", function () {
return code;
});
sizeof("babel", function () {
return babel.transform(code, {
compact: true,
experimental: true,
blacklist: ["strict"],
plugins: ["minify-booleans", "merge-sibling-variables"]
}).code;
});
sizeof("uglify", function () {
return uglify.minify(code, {
mangle: false,
fromString: true
}).code;
});

View File

@ -0,0 +1,7 @@
"use strict";
var _obj;
var obj = (_obj = {
foo: "bar"
}, _obj[bar] = "foo", _obj);

View File

@ -1,3 +1,4 @@
{
"blacklist": ["es5.properties.mutators"],
"loose": ["es6.properties.computed"]
}

View File

@ -1,4 +1,4 @@
var obj = {
first: "first",
["second"]: "second",
[second]: "second",
};

View File

@ -2,4 +2,6 @@
var _obj;
var obj = (_obj = {}, _obj.first = "first", _obj["second"] = "second", _obj);
var obj = (_obj = {
first: "first"
}, _obj[second] = "second", _obj);