diff --git a/lib/6to5/transformation/file.js b/lib/6to5/transformation/file.js index 6d6d19fecc..aafe1399ad 100644 --- a/lib/6to5/transformation/file.js +++ b/lib/6to5/transformation/file.js @@ -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; -}; diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index 20479e3c10..f61be9b5a2 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -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()) ])); diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index 9a44d57791..93353752cd 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -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; diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index dc9c8162a9..d05567668c 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -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) { diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 73d2aeac31..d5c3d5d94e 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -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) ])); diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index 0c5db72d55..192fff7799 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -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", [ diff --git a/lib/6to5/transformation/transformers/playground/method-binding.js b/lib/6to5/transformation/transformers/playground/method-binding.js index 085496a46b..3835d04543 100644 --- a/lib/6to5/transformation/transformers/playground/method-binding.js +++ b/lib/6to5/transformation/transformers/playground/method-binding.js @@ -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)), diff --git a/lib/6to5/traversal/scope.js b/lib/6to5/traversal/scope.js index d84bf38f36..d408fd5c22 100644 --- a/lib/6to5/traversal/scope.js +++ b/lib/6to5/traversal/scope.js @@ -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 }; }