From fac52017653a97a2c95013b76c8d62a55bf228a7 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 17 Dec 2014 17:00:42 -0800 Subject: [PATCH] Only error on non-ambient instantiated modules preceding clodules. --- src/compiler/checker.ts | 9 ++++++- src/compiler/emitter.ts | 4 +-- src/compiler/utilities.ts | 6 +++++ ...leWithPriorUninstantiatedModule.errors.txt | 21 ---------------- ...cloduleWithPriorUninstantiatedModule.types | 25 +++++++++++++++++++ 5 files changed, 41 insertions(+), 24 deletions(-) delete mode 100644 tests/baselines/reference/cloduleWithPriorUninstantiatedModule.errors.txt create mode 100644 tests/baselines/reference/cloduleWithPriorUninstantiatedModule.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ccc67ba3881..e5334c454d3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9003,7 +9003,12 @@ module ts { checkCollisionWithRequireExportsInGeneratedCode(node, node.name); checkExportsOnMergedDeclarations(node); var symbol = getSymbolOfNode(node); - if (symbol.flags & SymbolFlags.ValueModule && symbol.declarations.length > 1 && !isInAmbientContext(node)) { + + // The following checks only apply on a non-ambient instantiated module declaration. + if (symbol.flags & SymbolFlags.ValueModule + && symbol.declarations.length > 1 + && !isInAmbientContext(node) + && isInstantiatedModule(node, compilerOptions.preserveConstEnums)) { var classOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (classOrFunc) { if (getSourceFileOfNode(node) !== getSourceFileOfNode(classOrFunc)) { @@ -9014,6 +9019,8 @@ module ts { } } } + + // Checks for ambient external modules. if (node.name.kind === SyntaxKind.StringLiteral) { if (!isGlobalSourceFile(node.parent)) { error(node.name, Diagnostics.Ambient_external_modules_cannot_be_nested_in_other_modules); diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index b713fddf676..005c7aa5f15 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3680,8 +3680,8 @@ module ts { } function emitModuleDeclaration(node: ModuleDeclaration) { - var shouldEmit = getModuleInstanceState(node) === ModuleInstanceState.Instantiated || - (getModuleInstanceState(node) === ModuleInstanceState.ConstEnumOnly && compilerOptions.preserveConstEnums); + // Emit only if this module is non-ambient. + var shouldEmit = isInstantiatedModule(node, compilerOptions.preserveConstEnums); if (!shouldEmit) { return emitPinnedOrTripleSlashComments(node); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index bbe4d7dbf35..114e1dae1b3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -525,6 +525,12 @@ module ts { return false; } + export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean) { + var moduleState = getModuleInstanceState(node) + return moduleState === ModuleInstanceState.Instantiated || + (preserveConstEnums && moduleState === ModuleInstanceState.ConstEnumOnly); + } + export function isExternalModuleImportDeclaration(node: Node) { return node.kind === SyntaxKind.ImportDeclaration && (node).moduleReference.kind === SyntaxKind.ExternalModuleReference; } diff --git a/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.errors.txt b/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.errors.txt deleted file mode 100644 index cae69212f2f..00000000000 --- a/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.errors.txt +++ /dev/null @@ -1,21 +0,0 @@ -tests/cases/compiler/cloduleWithPriorUninstantiatedModule.ts(2,8): error TS2434: A module declaration cannot be located prior to a class or function with which it is merged - - -==== tests/cases/compiler/cloduleWithPriorUninstantiatedModule.ts (1 errors) ==== - // Ambient/uninstantiated module. - module Moclodule { - ~~~~~~~~~ -!!! error TS2434: A module declaration cannot be located prior to a class or function with which it is merged - export interface Someinterface { - foo(): void; - } - } - - class Moclodule { - } - - // Instantiated module. - module Moclodule { - export class Manager { - } - } \ No newline at end of file diff --git a/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.types b/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.types new file mode 100644 index 00000000000..1ab286b5964 --- /dev/null +++ b/tests/baselines/reference/cloduleWithPriorUninstantiatedModule.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/cloduleWithPriorUninstantiatedModule.ts === +// Ambient/uninstantiated module. +module Moclodule { +>Moclodule : typeof Moclodule + + export interface Someinterface { +>Someinterface : Someinterface + + foo(): void; +>foo : () => void + } +} + +class Moclodule { +>Moclodule : Moclodule +} + +// Instantiated module. +module Moclodule { +>Moclodule : typeof Moclodule + + export class Manager { +>Manager : Manager + } +}