move File::generateUid to Scope and add return type inferrence

This commit is contained in:
Sebastian McKenzie 2015-02-13 18:21:28 +11:00
parent 1912d1b26a
commit c3206aa9a4
8 changed files with 44 additions and 42 deletions

View File

@ -443,30 +443,3 @@ File.prototype.generate = function () {
return result;
};
File.prototype.generateUid = function (name, scope) {
name = t.toIdentifier(name).replace(/^_+/, "");
scope = scope || this.scope;
var uid;
var i = 0;
do {
uid = this._generateUid(name, i);
i++;
} while (scope.hasBinding(uid) || scope.hasGlobal(uid));
return uid;
};
File.prototype.generateUidIdentifier = function (name, scope) {
scope = scope || this.scope;
var id = t.identifier(this.generateUid(name, scope));
scope.getFunctionParent().registerBinding("uid", id);
return id;
};
File.prototype._generateUid = function (name, i) {
var id = name;
if (i > 1) id += i;
return "_" + id;
};

View File

@ -143,7 +143,7 @@ ReplaceSupers.prototype.getThisReference = function () {
if (this.topLevelThisReference) {
return this.topLevelThisReference;
} else {
var ref = this.topLevelThisReference = this.file.generateUidIdentifier("this");
var ref = this.topLevelThisReference = this.scope.generateUidIdentifier("this");
this.methodNode.value.body.body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(this.topLevelThisReference, t.thisExpression())
]));

View File

@ -9,8 +9,9 @@ var util = require("../../util");
var t = require("../../types");
function DefaultFormatter(file) {
this.file = file;
this.ids = object();
this.scope = file.scope;
this.file = file;
this.ids = object();
this.hasNonDefaultExports = false;

View File

@ -70,7 +70,7 @@ AMDFormatter.prototype.getModuleName = function () {
};
AMDFormatter.prototype._getExternalReference = function (node) {
return this.file.generateUidIdentifier(node.source.value);
return this.scope.generateUidIdentifier(node.source.value);
};
AMDFormatter.prototype.importDeclaration = function (node) {

View File

@ -93,7 +93,7 @@ CommonJSFormatter.prototype._getExternalReference = function (node, nodes) {
var call = t.callExpression(t.identifier("require"), [node.source]);
if (this.localImportOccurences[source] > 1) {
var uid = this.file.generateUidIdentifier(source);
var uid = this.scope.generateUidIdentifier(source);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(uid, call)
]));

View File

@ -12,7 +12,7 @@ var each = require("lodash/collection/each");
var map = require("lodash/collection/map");
function SystemFormatter(file) {
this.exportIdentifier = file.generateUidIdentifier("export");
this.exportIdentifier = file.scope.generateUidIdentifier("export");
this.noInteropRequireExport = true;
this.noInteropRequireImport = true;
@ -29,7 +29,7 @@ SystemFormatter.prototype._addImportSource = function (node, exportNode) {
};
SystemFormatter.prototype.buildExportsWildcard = function (objectIdentifier, node) {
var leftIdentifier = this.file.generateUidIdentifier("key");
var leftIdentifier = this.scope.generateUidIdentifier("key");
var valIdentifier = t.memberExpression(objectIdentifier, leftIdentifier, true);
var left = t.variableDeclaration("var", [

View File

@ -34,7 +34,7 @@ exports.BindFunctionExpression = function (node, parent, scope, file) {
]));
};
var temp = scope.generateTemp(file, "args");
var temp = scope.generateTemp("args");
return t.sequenceExpression([
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),

View File

@ -50,12 +50,11 @@ Scope.prototype.traverse = function (node, opts, state) {
/**
* Description
*
* @param {File} file
* @param {String} [name="temp"]
*/
Scope.prototype.generateTemp = function (file, name) {
var id = file.generateUidIdentifier(name || "temp", this);
Scope.prototype.generateTemp = function (name) {
var id = this.generateUidIdentifier(name || "temp");
this.push({
key: id.name,
id: id
@ -70,9 +69,36 @@ Scope.prototype.generateTemp = function (file, name) {
*/
Scope.prototype.generateUidIdentifier = function (name) {
return this.file.generateUidIdentifier(name, this);
var id = t.identifier(this.generateUid(name));
this.getFunctionParent().registerBinding("uid", id);
return id;
};
/**
* Description
*
* @param {String} name
*/
Scope.prototype.generateUid = function (name) {
name = t.toIdentifier(name).replace(/^_+/, "");
var uid;
var i = 0;
do {
uid = this._generateUid(name, i);
i++;
} while (this.hasBinding(uid) || this.hasGlobal(uid));
return uid;
};
Scope.prototype._generateUid = function (name, i) {
var id = name;
if (i > 1) id += i;
return "_" + id;
};
/*
* Description
*
@ -111,7 +137,7 @@ Scope.prototype.generateUidBasedOnNode = function (parent) {
var id = parts.join("$");
id = id.replace(/^_/, "") || "ref";
return this.file.generateUidIdentifier(id, this);
return this.generateUidIdentifier(id);
};
/**
@ -197,8 +223,9 @@ Scope.prototype.inferType = function (node) {
return;
}
if (t.isCallExpression(target)) {
// todo: resolve this to a return type
if (t.isCallExpression(target) && t.isIdentifier(target.callee)) {
var funcInfo = this.getBindingInfo(target.callee.name);
if (funcInfo) return funcInfo.node.returnType;
}
if (t.isIdentifier(target)) {
@ -329,6 +356,7 @@ Scope.prototype.registerBinding = function (kind, node) {
reassigned: false,
identifier: id,
scope: this,
node: node,
kind: kind
};
}