Ensuring local module names are unique in emit.

Fixes #41 and #42.
This commit is contained in:
Anders Hejlsberg
2014-07-18 17:04:06 -07:00
committed by unknown
parent 393be4687c
commit 175dba4977
3 changed files with 45 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ module ts {
var container: Declaration;
var lastContainer: Declaration;
var symbolCount = 0;
var lastLocals: Declaration;
var Symbol = objectAllocator.getSymbolConstructor();
if (!file.locals) {

View File

@@ -6049,6 +6049,49 @@ module ts {
Debug.fail("getLocalNameForSymbol failed");
}
function isNodeParentedBy(node: Node, parent: Node): boolean {
while (node) {
if (node === parent) return true;
node = node.parent;
}
return false;
}
function isUniqueLocalName(name: string, container: Node): boolean {
name = escapeIdentifier(name);
if (container.locals) {
for (var node = container; isNodeParentedBy(node, container); node = node.nextLocals) {
if (hasProperty(node.locals, name) && node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
return false;
}
}
}
return true;
}
function getLocalNameOfContainer(container: Declaration): string {
var links = getNodeLinks(container);
if (!links.localModuleName) {
var name = container.name.text ? unescapeIdentifier(container.name.text) : "M";
while (!isUniqueLocalName(name, container)) {
name = "_" + name;
}
links.localModuleName = name;
}
return links.localModuleName;
}
function getLocalNameForSymbol(symbol: Symbol, location: Node): string {
var node = location;
while (node) {
if ((node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.EnumDeclaration) && getSymbolOfNode(node) === symbol) {
return getLocalNameOfContainer(node);
}
node = node.parent;
}
Debug.fail("getLocalNameForSymbol failed");
}
function getExpressionNamePrefix(node: Identifier): string {
var symbol = getNodeLinks(node).resolvedSymbol;
if (symbol) {

View File

@@ -691,7 +691,7 @@ module ts {
ExportHasLocal = Function | Class | Enum | ValueModule,
HasLocals = Function | Module | Method | Constructor | Accessor | Signature,
HasLocals = Function | Enum | Module | Method | Constructor | Accessor | Signature,
HasExports = Class | Enum | Module,
HasMembers = Class | Interface | TypeLiteral | ObjectLiteral,