Skip to content

Commit 175dba4

Browse files
ahejlsbergunknown
authored andcommitted
Ensuring local module names are unique in emit.
Fixes microsoft#41 and microsoft#42.
1 parent 393be46 commit 175dba4

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module ts {
3333
var container: Declaration;
3434
var lastContainer: Declaration;
3535
var symbolCount = 0;
36+
var lastLocals: Declaration;
3637
var Symbol = objectAllocator.getSymbolConstructor();
3738

3839
if (!file.locals) {

src/compiler/checker.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6049,6 +6049,49 @@ module ts {
60496049
Debug.fail("getLocalNameForSymbol failed");
60506050
}
60516051

6052+
function isNodeParentedBy(node: Node, parent: Node): boolean {
6053+
while (node) {
6054+
if (node === parent) return true;
6055+
node = node.parent;
6056+
}
6057+
return false;
6058+
}
6059+
6060+
function isUniqueLocalName(name: string, container: Node): boolean {
6061+
name = escapeIdentifier(name);
6062+
if (container.locals) {
6063+
for (var node = container; isNodeParentedBy(node, container); node = node.nextLocals) {
6064+
if (hasProperty(node.locals, name) && node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
6065+
return false;
6066+
}
6067+
}
6068+
}
6069+
return true;
6070+
}
6071+
6072+
function getLocalNameOfContainer(container: Declaration): string {
6073+
var links = getNodeLinks(container);
6074+
if (!links.localModuleName) {
6075+
var name = container.name.text ? unescapeIdentifier(container.name.text) : "M";
6076+
while (!isUniqueLocalName(name, container)) {
6077+
name = "_" + name;
6078+
}
6079+
links.localModuleName = name;
6080+
}
6081+
return links.localModuleName;
6082+
}
6083+
6084+
function getLocalNameForSymbol(symbol: Symbol, location: Node): string {
6085+
var node = location;
6086+
while (node) {
6087+
if ((node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.EnumDeclaration) && getSymbolOfNode(node) === symbol) {
6088+
return getLocalNameOfContainer(node);
6089+
}
6090+
node = node.parent;
6091+
}
6092+
Debug.fail("getLocalNameForSymbol failed");
6093+
}
6094+
60526095
function getExpressionNamePrefix(node: Identifier): string {
60536096
var symbol = getNodeLinks(node).resolvedSymbol;
60546097
if (symbol) {

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ module ts {
691691

692692
ExportHasLocal = Function | Class | Enum | ValueModule,
693693

694-
HasLocals = Function | Module | Method | Constructor | Accessor | Signature,
694+
HasLocals = Function | Enum | Module | Method | Constructor | Accessor | Signature,
695695
HasExports = Class | Enum | Module,
696696
HasMembers = Class | Interface | TypeLiteral | ObjectLiteral,
697697

0 commit comments

Comments
 (0)