better uid names

This commit is contained in:
Sebastian McKenzie
2015-01-02 03:53:28 +11:00
parent b2bac04b72
commit 93ceafc732
25 changed files with 153 additions and 124 deletions

View File

@@ -21,7 +21,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasAny) return;
if (node.properties.length) {
var objId = util.getUid(parent, file);
var objId = t.getUid(parent, file);
return util.template("object-define-properties-closure", {
KEY: objId,

View File

@@ -87,11 +87,9 @@ Class.prototype.run = function () {
if (superName) {
this.closure = true;
// so we're only evaluating it once
var superRefName = "super";
if (className) superRefName = className.name + "Super";
var superRef = file.generateUidIdentifier(superRefName, this.scope);
// so we're only evaluating it once
var superRef = t.getUid(superName, this.file);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(superRef, superName)
]));

View File

@@ -14,7 +14,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasComputed) return;
var objId = util.getUid(parent, file);
var objId = t.getUid(parent, file);
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));

View File

@@ -1,6 +1,7 @@
// TODO: Clean up
var t = require("../../types");
var util = require("../../util");
var t = require("../../types");
var buildVariableAssign = function (opts, id, init) {
var op = opts.operator;
@@ -75,7 +76,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
var _parentId = t.getUid(parentId, opts.file, opts.scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(_parentId, toArray)
]));
@@ -113,7 +114,7 @@ var pushPattern = function (opts) {
var scope = opts.scope;
if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
var key = file.generateUidIdentifier("ref", scope);
var key = t.getUid(parentId, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, parentId)

View File

@@ -4,8 +4,8 @@ var t = require("../../types");
exports.experimental = true;
var build = function (node, file) {
var uid = file.generateUidIdentifier("arr");
var build = function (node, parent, file, scope) {
var uid = t.getUid(parent, file, scope);
var container = util.template("array-comprehension-container", {
KEY: uid
@@ -55,8 +55,8 @@ exports._build = function (node, buildBody) {
);
};
exports.ComprehensionExpression = function (node, parent, file) {
exports.ComprehensionExpression = function (node, parent, file, scope) {
if (node.generator) return;
return build(node, file);
return build(node, parent, file, scope);
};

View File

@@ -10,7 +10,7 @@ var getPropRef = function (nodes, prop, file, scope) {
if (t.isIdentifier(prop)) {
return t.literal(prop.name);
} else {
var temp = file.generateUidIdentifier("propKey", scope);
var temp = t.getUid(prop, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, prop)
]));
@@ -20,7 +20,7 @@ var getPropRef = function (nodes, prop, file, scope) {
var getObjRef = function (nodes, obj, file, scope) {
if (t.isDynamic(obj)) {
var temp = file.generateUidIdentifier("obj", scope);
var temp = t.getUid(obj, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));

View File

@@ -156,7 +156,7 @@ t.toIdentifier = function (name) {
if (t.isIdentifier(name)) return name.name;
// replace all non-valid identifiers with dashes
name = name.replace(/[^a-zA-Z0-9]/g, "-");
name = name.replace(/[^a-zA-Z0-9$_]/g, "-");
// remove all dashes and numbers from start of name
name = name.replace(/^[-0-9]+/, "");
@@ -166,6 +166,9 @@ t.toIdentifier = function (name) {
return c ? c.toUpperCase() : "";
});
// remove underscores from start of name
name = name.replace(/^\_/, "");
return name || '_';
};
@@ -231,6 +234,49 @@ t.toBlock = function (node, parent) {
return t.blockStatement(node);
};
t.getUid = function (parent, file, scope) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, scope);
};
t.getIds = function (node, map, ignoreTypes) {
ignoreTypes = ignoreTypes || [];

View File

@@ -52,22 +52,6 @@ exports.arrayify = function (val) {
throw new TypeError("illegal type for arrayify");
};
exports.getUid = function (parent, file) {
var node;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isIdentifier(node)) id = node.name;
return file.generateUidIdentifier(id);
};
exports.isAbsolute = function (loc) {
if (!loc) return false;
if (loc[0] === "/") return true; // unix

View File

@@ -14,32 +14,32 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
woops["super"].test();
_TestSuper.call(this);
_TestSuper.prototype.test.call(this);
foob(_TestSuper);
_Foo.call(this);
_Foo.prototype.test.call(this);
foob(_Foo);
_TestSuper.call.apply(_TestSuper, [this].concat(_slice.call(arguments)));
_TestSuper.call.apply(_TestSuper, [this, "test"].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this, "test"].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this, "test"].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype, [this, "test"].concat(_slice.call(arguments)));
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
Test.prototype.test = function () {
_TestSuper.prototype.test.call(this);
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this, "test"].concat(_slice.call(arguments)));
_Foo.prototype.test.call(this);
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
};
Test.foo = function () {
_TestSuper.foo.call(this);
_TestSuper.foo.call.apply(_TestSuper.foo, [this].concat(_slice.call(arguments)));
_TestSuper.foo.call.apply(_TestSuper.foo, [this, "test"].concat(_slice.call(arguments)));
_Foo.foo.call(this);
_Foo.foo.call.apply(_Foo.foo, [this].concat(_slice.call(arguments)));
_Foo.foo.call.apply(_Foo.foo, [this, "test"].concat(_slice.call(arguments)));
};
return Test;

View File

@@ -13,13 +13,13 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
_TestSuper.prototype.test;
_TestSuper.prototype.test.whatever;
_Foo.prototype.test;
_Foo.prototype.test.whatever;
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
return Test;
})();

View File

@@ -13,16 +13,16 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
_TestSuper.prototype.test.whatever();
_TestSuper.prototype.test.call(this);
_Foo.prototype.test.whatever();
_Foo.prototype.test.call(this);
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
Test.test = function () {
return _TestSuper.wow.call(this);
return _Foo.wow.call(this);
};
return Test;

View File

@@ -17,12 +17,12 @@ var Test = function Test() {
};
var Foo = (function () {
var _FooSuper = Bar;
var _Bar = Bar;
var Foo = function Foo() {
this.state = "test";
};
_inherits(Foo, _FooSuper);
_inherits(Foo, _Bar);
return Foo;
})();

View File

@@ -13,27 +13,27 @@ var _inherits = function (child, parent) {
};
var BaseController = (function () {
var _BaseControllerSuper = Chaplin.Controller;
var _Chaplin$Controller = Chaplin.Controller;
var BaseController = function BaseController() {
if (_BaseControllerSuper) {
_BaseControllerSuper.apply(this, arguments);
if (_Chaplin$Controller) {
_Chaplin$Controller.apply(this, arguments);
}
};
_inherits(BaseController, _BaseControllerSuper);
_inherits(BaseController, _Chaplin$Controller);
return BaseController;
})();
var BaseController2 = (function () {
var _BaseController2Super = Chaplin.Controller.Another;
var _Chaplin$Controller$Another = Chaplin.Controller.Another;
var BaseController2 = function BaseController2() {
if (_BaseController2Super) {
_BaseController2Super.apply(this, arguments);
if (_Chaplin$Controller$Another) {
_Chaplin$Controller$Another.apply(this, arguments);
}
};
_inherits(BaseController2, _BaseController2Super);
_inherits(BaseController2, _Chaplin$Controller$Another);
return BaseController2;
})();

View File

@@ -13,14 +13,14 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
if (_TestSuper) {
_TestSuper.apply(this, arguments);
if (_Foo) {
_Foo.apply(this, arguments);
}
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
return Test;
})();

View File

@@ -24,15 +24,15 @@ var a = _ref2[0];
var b = _ref2[1];
var _ref3 = [3, 4];
var _ref4 = _slicedToArray(_ref3, 2);
var _ref3 = _slicedToArray(_ref3, 2);
var c = _ref4[0];
var d = _ref4[1];
var _ref5 = { e: 5, f: 6 };
var c = _ref3[0];
var d = _ref3[1];
var _ref4 = { e: 5, f: 6 };
var e = _ref5.e;
var f = _ref5.f;
var _ref6 = { a: 7, b: 8 };
var e = _ref4.e;
var f = _ref4.f;
var _ref5 = { a: 7, b: 8 };
var g = _ref6.a;
var h = _ref6.b;
var g = _ref5.a;
var h = _ref5.b;

View File

@@ -21,10 +21,10 @@ var _ref = ["hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 4);
var a = _ref2[0];
var _ref3 = _slicedToArray(_ref2[1], 1);
var _ref2$1 = _slicedToArray(_ref2[1], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[2], 1);
var b = _ref2$1[0];
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var c = _ref4[0];
var c = _ref2$2[0];
var d = _ref2[3];

View File

@@ -1,6 +1,6 @@
"use strict";
var _temp, _ref;
var _temp, _temp2;
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
@@ -17,4 +17,4 @@ var _slicedToArray = function (arr, i) {
}
};
console.log((_temp = [123], _ref = _slicedToArray(_temp, 1), x = _ref[0], _temp));
console.log((_temp = [123], _temp2 = _slicedToArray(_temp, 1), x = _temp2[0], _temp));

View File

@@ -21,10 +21,10 @@ var _ref = ["foo", "hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 5);
var a = _ref2[1];
var _ref3 = _slicedToArray(_ref2[2], 1);
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[3], 1);
var b = _ref2$2[0];
var _ref2$3 = _slicedToArray(_ref2[3], 1);
var c = _ref4[0];
var c = _ref2$3[0];
var d = _ref2[4];

View File

@@ -16,11 +16,11 @@ var _slicedToArray = function (arr, i) {
}
};
var _ref = _slicedToArray(rect.topLeft, 2);
var _rect$topLeft = _slicedToArray(rect.topLeft, 2);
var x1 = _ref[0];
var y1 = _ref[1];
var _ref2 = _slicedToArray(rect.bottomRight, 2);
var x1 = _rect$topLeft[0];
var y1 = _rect$topLeft[1];
var _rect$bottomRight = _slicedToArray(rect.bottomRight, 2);
var x2 = _ref2[0];
var y2 = _ref2[1];
var x2 = _rect$bottomRight[0];
var y2 = _rect$bottomRight[1];

View File

@@ -31,17 +31,17 @@ function unpackObject(_ref2) {
console.log(unpackObject({ title: "title", author: "author" }));
var unpackArray = function (_ref3, _ref5) {
var _ref4 = _slicedToArray(_ref3, 3);
var unpackArray = function (_ref3, _ref4) {
var _ref3 = _slicedToArray(_ref3, 3);
var a = _ref4[0];
var b = _ref4[1];
var c = _ref4[2];
var _ref6 = _slicedToArray(_ref5, 3);
var a = _ref3[0];
var b = _ref3[1];
var c = _ref3[2];
var _ref4 = _slicedToArray(_ref4, 3);
var x = _ref6[0];
var y = _ref6[1];
var z = _ref6[2];
var x = _ref4[0];
var y = _ref4[1];
var z = _ref4[2];
return a + b + c;
};

View File

@@ -3,14 +3,14 @@
function add() {
var _arguments = arguments;
return (function () {
var _arr = [];
var _ref = [];
for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var i = _step.value;
_arr.push(i * _arguments[0]);
_ref.push(i * _arguments[0]);
}
return _arr;
return _ref;
})();
}

View File

@@ -1,17 +1,17 @@
"use strict";
var seattlers = (function () {
var _arr = [];
var _seattlers = [];
for (var _iterator = countries[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var customers = _step.value;
for (var _iterator2 = customers[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) {
var c = _step2.value;
if (c.city == "Seattle") {
_arr.push({ name: c.name, age: c.age });
_seattlers.push({ name: c.name, age: c.age });
}
}
}
return _arr;
return _seattlers;
})();

View File

@@ -3,14 +3,14 @@
function add() {
var _this = this;
return (function () {
var _arr = [];
var _ref = [];
for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var i = _step.value;
_arr.push(i * _this.multiplier);
_ref.push(i * _this.multiplier);
}
return _arr;
return _ref;
})();
}

View File

@@ -27,10 +27,10 @@ var _ref = ["hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 4);
var a = _ref2[0];
var _ref3 = _slicedToArray(_ref2[1], 1);
var _ref2$1 = _slicedToArray(_ref2[1], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[2], 1);
var b = _ref2$1[0];
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var c = _ref4[0];
var c = _ref2$2[0];
var d = _ref2[3];

View File

@@ -1,6 +1,6 @@
"use strict";
var _propKey2, _obj2, _propKey4;
var _ref2, _obj2, _ref4;
var _hasOwn = Object.prototype.hasOwnProperty;
var obj = {};
@@ -9,17 +9,17 @@ if (!_hasOwn.call(obj, "x")) obj.x = 2;
console.log((!_hasOwn.call(obj, "x") && (obj.x = 2), obj.x));
var _propKey = x();
var _ref = x();
if (!_hasOwn.call(obj, _propKey)) obj[_propKey] = 2;
if (!_hasOwn.call(obj, _ref)) obj[_ref] = 2;
console.log((_propKey2 = x(), !_hasOwn.call(obj, _propKey2) && (obj[_propKey2] = 2), obj[_propKey2]));
console.log((_ref2 = x(), !_hasOwn.call(obj, _ref2) && (obj[_ref2] = 2), obj[_ref2]));
var _obj = obj[y()];
var _propKey3 = x();
var _ref3 = x();
if (!_hasOwn.call(_obj, _propKey3)) _obj[_propKey3] = 2;
if (!_hasOwn.call(_obj, _ref3)) _obj[_ref3] = 2;
console.log((_obj2 = obj[y()], _propKey4 = x(), !_hasOwn.call(_obj2, _propKey4) && (_obj2[_propKey4] = 2), _obj2[_propKey4]));
console.log((_obj2 = obj[y()], _ref4 = x(), !_hasOwn.call(_obj2, _ref4) && (_obj2[_ref4] = 2), _obj2[_ref4]));