use scope for uid registry instead of a global registry

This commit is contained in:
Sebastian McKenzie
2015-01-22 00:27:45 +11:00
parent 4ce7d5a5b4
commit ac373a9e1c
6 changed files with 63 additions and 47 deletions

View File

@@ -17,7 +17,6 @@ function File(opts) {
this.data = {};
this.opts = File.normaliseOptions(opts);
this.transformers = this.getTransformers();
this.uids = {};
this.ast = {};
}
@@ -315,8 +314,10 @@ File.prototype.generateUid = function (name, scope) {
scope = scope || this.scope;
var uid;
var i = 0;
do {
uid = this._generateUid(name);
uid = this._generateUid(name, i);
i++;
} while (scope.has(uid));
return uid;
};
@@ -328,12 +329,8 @@ File.prototype.generateUidIdentifier = function (name, scope) {
return id;
};
File.prototype._generateUid = function (name) {
var uids = this.uids;
var i = uids[name] || 1;
File.prototype._generateUid = function (name, i) {
var id = name;
if (i > 1) id += i;
uids[name] = i + 1;
return "_" + id;
};

View File

@@ -36,9 +36,20 @@ Scope.defaultDeclarations = _.flatten([
vars.reservedVars
].map(_.keys));
Scope.add = function (node, references, throwOnDuplicate) {
Scope.prototype._add = function (node, references, throwOnDuplicate) {
if (!node) return;
_.defaults(references, t.getIds(node, true));
var ids = t.getIds(node, true);
for (var key in ids) {
var id = ids[key];
if (throwOnDuplicate && references[key]) {
throw this.file.errorWithNode(id, "Duplicate declaration");
}
references[key] = id;
}
};
/**
@@ -156,18 +167,31 @@ var functionVariableTraverser = {
}
};
var blockVariableTraverser = {
enter: function (node, parent, scope, context, add) {
if (t.isScope(node)) {
return context.skip();
}
if (t.isBlockScoped(node)) {
add(node, false, true);
}
}
};
Scope.prototype.getInfo = function () {
var parent = this.parent;
var block = this.block;
var block = this.block;
var self = this;
if (block._scopeInfo) return block._scopeInfo;
var info = block._scopeInfo = {};
var references = info.references = {};
var declarations = info.declarations = {};
var add = function (node, reference) {
Scope.add(node, references);
if (!reference) Scope.add(node, declarations);
var add = function (node, reference, throwOnDuplicate) {
self._add(node, references);
if (!reference) self._add(node, declarations, throwOnDuplicate);
};
if (parent && t.isBlockStatement(block) && t.isFor(parent.block)) {
@@ -179,7 +203,7 @@ Scope.prototype.getInfo = function () {
if (t.isFor(block)) {
_.each(FOR_KEYS, function (key) {
var node = block[key];
if (t.isBlockScoped(node)) add(node);
if (t.isBlockScoped(node)) add(node, false, true);
});
if (t.isBlockStatement(block.body)) {
@@ -190,10 +214,7 @@ Scope.prototype.getInfo = function () {
// Program, BlockStatement - let variables
if (t.isBlockStatement(block) || t.isProgram(block)) {
_.each(block.body, function (node) {
// check for non-var `VariableDeclaration`s
if (t.isBlockScoped(node)) add(node);
});
traverse(block, blockVariableTraverser, this, add);
}
// CatchClause - param
@@ -211,12 +232,10 @@ Scope.prototype.getInfo = function () {
// Program, Function - var variables
if (t.isProgram(block) || t.isFunction(block)) {
var state = {
traverse(block, functionVariableTraverser, this, {
blockId: block.id,
add: add
};
traverse(block, functionVariableTraverser, this, state);
});
}
// Function - params, rest
@@ -264,7 +283,7 @@ Scope.prototype.push = function (opts) {
*/
Scope.prototype.add = function (node) {
Scope.add(node, this.references);
this._add(node, this.references);
};
/**

View File

@@ -10,15 +10,15 @@ function one() {
one(1, 2);
function two() {
var _arguments2 = arguments;
var _arguments = arguments;
var inner = function () {
return _arguments2;
return _arguments;
};
var another = function () {
var _arguments3 = arguments;
var _arguments2 = arguments;
var inner2 = function () {
return _arguments3;
return _arguments2;
};
};
@@ -27,18 +27,18 @@ function two() {
two(1, 2);
function three() {
var _arguments4 = arguments;
var _arguments = arguments;
var fn = function () {
return _arguments4[0] + "bar";
return _arguments[0] + "bar";
};
return fn();
}
three("foo");
function four() {
var _arguments5 = arguments;
var _arguments = arguments;
var fn = function () {
return _arguments5[0].foo + "bar";
return _arguments[0].foo + "bar";
};
return fn();
}

View File

@@ -23,25 +23,25 @@ function somethingAdvanced(_ref) {
var y2 = _ref.bottomRight.y;
}
function unpackObject(_ref2) {
var title = _ref2.title;
var author = _ref2.author;
function unpackObject(_ref) {
var title = _ref.title;
var author = _ref.author;
return title + " " + author;
}
console.log(unpackObject({ title: "title", author: "author" }));
var unpackArray = function (_ref3, _ref4) {
var unpackArray = function (_ref, _ref3) {
var _ref2 = _slicedToArray(_ref, 3);
var a = _ref2[0];
var b = _ref2[1];
var c = _ref2[2];
var _ref32 = _slicedToArray(_ref3, 3);
var a = _ref32[0];
var b = _ref32[1];
var c = _ref32[2];
var _ref42 = _slicedToArray(_ref4, 3);
var x = _ref42[0];
var y = _ref42[1];
var z = _ref42[2];
var x = _ref32[0];
var y = _ref32[1];
var z = _ref32[2];
return a + b + c;
};

View File

@@ -7,7 +7,7 @@ var t = function (f) {
};
function t(f) {
for (var _len2 = arguments.length, items = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
items[_key2 - 1] = arguments[_key2];
for (var _len = arguments.length, items = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
items[_key - 1] = arguments[_key];
}
}

View File

@@ -7,7 +7,7 @@ var t = function () {
};
function t() {
for (var _len2 = arguments.length, items = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
items[_key2] = arguments[_key2];
for (var _len = arguments.length, items = Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
}