@@ -10387,16 +10387,30 @@ module ts {
1038710387 }
1038810388 }
1038910389
10390- function getFirstNonAmbientClassOrFunctionDeclaration (symbol: Symbol): Declaration {
10390+ function getFirstClassOrFunctionDeclaration (symbol: Symbol, nonAmbientOnly: boolean ): Declaration {
1039110391 let declarations = symbol.declarations;
1039210392 for (let declaration of declarations) {
10393- if ((declaration.kind === SyntaxKind.ClassDeclaration || (declaration.kind === SyntaxKind.FunctionDeclaration && nodeIsPresent((<FunctionLikeDeclaration>declaration).body))) && !isInAmbientContext(declaration)) {
10393+ if ((declaration.kind === SyntaxKind.ClassDeclaration || (declaration.kind === SyntaxKind.FunctionDeclaration && nodeIsPresent((<FunctionLikeDeclaration>declaration).body))) && !(nonAmbientOnly && isInAmbientContext(declaration) )) {
1039410394 return declaration;
1039510395 }
1039610396 }
1039710397 return undefined;
1039810398 }
1039910399
10400+ function inSameLexicalScope(node1: Node, node2: Node) {
10401+ let container1 = getEnclosingBlockScopeContainer(node1);
10402+ let container2 = getEnclosingBlockScopeContainer(node2);
10403+ if (isGlobalSourceFile(container1)) {
10404+ return isGlobalSourceFile(container2);
10405+ }
10406+ else if (isGlobalSourceFile(container2)) {
10407+ return false;
10408+ }
10409+ else {
10410+ return container1 === container2;
10411+ }
10412+ }
10413+
1040010414 function checkModuleDeclaration(node: ModuleDeclaration) {
1040110415 if (produceDiagnostics) {
1040210416 // Grammar checking
@@ -10416,15 +10430,22 @@ module ts {
1041610430 && symbol.declarations.length > 1
1041710431 && !isInAmbientContext(node)
1041810432 && isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.separateCompilation)) {
10419- let classOrFunc = getFirstNonAmbientClassOrFunctionDeclaration (symbol);
10420- if (classOrFunc ) {
10421- if (getSourceFileOfNode(node) !== getSourceFileOfNode(classOrFunc )) {
10433+ let nonAmbientClassOrFunc = getFirstClassOrFunctionDeclaration (symbol, /*nonAmbientOnly*/ true );
10434+ if (nonAmbientClassOrFunc ) {
10435+ if (getSourceFileOfNode(node) !== getSourceFileOfNode(nonAmbientClassOrFunc )) {
1042210436 error(node.name, Diagnostics.A_module_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged);
1042310437 }
10424- else if (node.pos < classOrFunc .pos) {
10438+ else if (node.pos < nonAmbientClassOrFunc .pos) {
1042510439 error(node.name, Diagnostics.A_module_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged);
1042610440 }
1042710441 }
10442+
10443+ // if the module merges with a class declaration in the same lexical scope, we need to track this
10444+ // to ensure the correct emit.
10445+ let anyClassOrFunc = getFirstClassOrFunctionDeclaration(symbol, /*nonAmbientOnly*/ false);
10446+ if (anyClassOrFunc && anyClassOrFunc.kind === SyntaxKind.ClassDeclaration && inSameLexicalScope(node, anyClassOrFunc)) {
10447+ getNodeLinks(node).flags |= NodeCheckFlags.LexicalModuleMergesWithClass;
10448+ }
1042810449 }
1042910450
1043010451 // Checks for ambient external modules.
0 commit comments