move File::generateUid to Scope and add return type inferrence
This commit is contained in:
parent
1912d1b26a
commit
c3206aa9a4
@ -443,30 +443,3 @@ File.prototype.generate = function () {
|
|||||||
|
|
||||||
return result;
|
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;
|
|
||||||
};
|
|
||||||
|
|||||||
@ -143,7 +143,7 @@ ReplaceSupers.prototype.getThisReference = function () {
|
|||||||
if (this.topLevelThisReference) {
|
if (this.topLevelThisReference) {
|
||||||
return this.topLevelThisReference;
|
return this.topLevelThisReference;
|
||||||
} else {
|
} 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", [
|
this.methodNode.value.body.body.unshift(t.variableDeclaration("var", [
|
||||||
t.variableDeclarator(this.topLevelThisReference, t.thisExpression())
|
t.variableDeclarator(this.topLevelThisReference, t.thisExpression())
|
||||||
]));
|
]));
|
||||||
|
|||||||
@ -9,8 +9,9 @@ var util = require("../../util");
|
|||||||
var t = require("../../types");
|
var t = require("../../types");
|
||||||
|
|
||||||
function DefaultFormatter(file) {
|
function DefaultFormatter(file) {
|
||||||
this.file = file;
|
this.scope = file.scope;
|
||||||
this.ids = object();
|
this.file = file;
|
||||||
|
this.ids = object();
|
||||||
|
|
||||||
this.hasNonDefaultExports = false;
|
this.hasNonDefaultExports = false;
|
||||||
|
|
||||||
|
|||||||
@ -70,7 +70,7 @@ AMDFormatter.prototype.getModuleName = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
AMDFormatter.prototype._getExternalReference = function (node) {
|
AMDFormatter.prototype._getExternalReference = function (node) {
|
||||||
return this.file.generateUidIdentifier(node.source.value);
|
return this.scope.generateUidIdentifier(node.source.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
AMDFormatter.prototype.importDeclaration = function (node) {
|
AMDFormatter.prototype.importDeclaration = function (node) {
|
||||||
|
|||||||
@ -93,7 +93,7 @@ CommonJSFormatter.prototype._getExternalReference = function (node, nodes) {
|
|||||||
var call = t.callExpression(t.identifier("require"), [node.source]);
|
var call = t.callExpression(t.identifier("require"), [node.source]);
|
||||||
|
|
||||||
if (this.localImportOccurences[source] > 1) {
|
if (this.localImportOccurences[source] > 1) {
|
||||||
var uid = this.file.generateUidIdentifier(source);
|
var uid = this.scope.generateUidIdentifier(source);
|
||||||
nodes.push(t.variableDeclaration("var", [
|
nodes.push(t.variableDeclaration("var", [
|
||||||
t.variableDeclarator(uid, call)
|
t.variableDeclarator(uid, call)
|
||||||
]));
|
]));
|
||||||
|
|||||||
@ -12,7 +12,7 @@ var each = require("lodash/collection/each");
|
|||||||
var map = require("lodash/collection/map");
|
var map = require("lodash/collection/map");
|
||||||
|
|
||||||
function SystemFormatter(file) {
|
function SystemFormatter(file) {
|
||||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
this.exportIdentifier = file.scope.generateUidIdentifier("export");
|
||||||
this.noInteropRequireExport = true;
|
this.noInteropRequireExport = true;
|
||||||
this.noInteropRequireImport = true;
|
this.noInteropRequireImport = true;
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ SystemFormatter.prototype._addImportSource = function (node, exportNode) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
SystemFormatter.prototype.buildExportsWildcard = function (objectIdentifier, node) {
|
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 valIdentifier = t.memberExpression(objectIdentifier, leftIdentifier, true);
|
||||||
|
|
||||||
var left = t.variableDeclaration("var", [
|
var left = t.variableDeclaration("var", [
|
||||||
|
|||||||
@ -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([
|
return t.sequenceExpression([
|
||||||
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
|
t.assignmentExpression("=", temp, t.arrayExpression(node.arguments)),
|
||||||
|
|||||||
@ -50,12 +50,11 @@ Scope.prototype.traverse = function (node, opts, state) {
|
|||||||
/**
|
/**
|
||||||
* Description
|
* Description
|
||||||
*
|
*
|
||||||
* @param {File} file
|
|
||||||
* @param {String} [name="temp"]
|
* @param {String} [name="temp"]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Scope.prototype.generateTemp = function (file, name) {
|
Scope.prototype.generateTemp = function (name) {
|
||||||
var id = file.generateUidIdentifier(name || "temp", this);
|
var id = this.generateUidIdentifier(name || "temp");
|
||||||
this.push({
|
this.push({
|
||||||
key: id.name,
|
key: id.name,
|
||||||
id: id
|
id: id
|
||||||
@ -70,9 +69,36 @@ Scope.prototype.generateTemp = function (file, name) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Scope.prototype.generateUidIdentifier = function (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
|
* Description
|
||||||
*
|
*
|
||||||
@ -111,7 +137,7 @@ Scope.prototype.generateUidBasedOnNode = function (parent) {
|
|||||||
var id = parts.join("$");
|
var id = parts.join("$");
|
||||||
id = id.replace(/^_/, "") || "ref";
|
id = id.replace(/^_/, "") || "ref";
|
||||||
|
|
||||||
return this.file.generateUidIdentifier(id, this);
|
return this.generateUidIdentifier(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -197,8 +223,9 @@ Scope.prototype.inferType = function (node) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t.isCallExpression(target)) {
|
if (t.isCallExpression(target) && t.isIdentifier(target.callee)) {
|
||||||
// todo: resolve this to a return type
|
var funcInfo = this.getBindingInfo(target.callee.name);
|
||||||
|
if (funcInfo) return funcInfo.node.returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t.isIdentifier(target)) {
|
if (t.isIdentifier(target)) {
|
||||||
@ -329,6 +356,7 @@ Scope.prototype.registerBinding = function (kind, node) {
|
|||||||
reassigned: false,
|
reassigned: false,
|
||||||
identifier: id,
|
identifier: id,
|
||||||
scope: this,
|
scope: this,
|
||||||
|
node: node,
|
||||||
kind: kind
|
kind: kind
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user