Merge pull request #1529 from Microsoft/uninstantiatedModulesBeforeClasses

Allow uninstantiated modules before clodules
This commit is contained in:
Daniel Rosenwasser 2015-01-07 12:40:50 -08:00
commit f32683d231
9 changed files with 166 additions and 3 deletions

View file

@ -9000,7 +9000,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)) {
@ -9011,6 +9016,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);

View file

@ -3686,8 +3686,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);

View file

@ -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 && (<ImportDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference;
}

View file

@ -0,0 +1,22 @@
tests/cases/compiler/cloduleWithPriorInstantiatedModule.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/cloduleWithPriorInstantiatedModule.ts (1 errors) ====
// Non-ambient & instantiated 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;
}
var x = 10;
}
class Moclodule {
}
// Instantiated module.
module Moclodule {
export class Manager {
}
}

View file

@ -0,0 +1,39 @@
//// [cloduleWithPriorInstantiatedModule.ts]
// Non-ambient & instantiated module.
module Moclodule {
export interface Someinterface {
foo(): void;
}
var x = 10;
}
class Moclodule {
}
// Instantiated module.
module Moclodule {
export class Manager {
}
}
//// [cloduleWithPriorInstantiatedModule.js]
// Non-ambient & instantiated module.
var Moclodule;
(function (Moclodule) {
var x = 10;
})(Moclodule || (Moclodule = {}));
var Moclodule = (function () {
function Moclodule() {
}
return Moclodule;
})();
// Instantiated module.
var Moclodule;
(function (Moclodule) {
var Manager = (function () {
function Manager() {
}
return Manager;
})();
Moclodule.Manager = Manager;
})(Moclodule || (Moclodule = {}));

View file

@ -0,0 +1,33 @@
//// [cloduleWithPriorUninstantiatedModule.ts]
// Non-ambient & uninstantiated module.
module Moclodule {
export interface Someinterface {
foo(): void;
}
}
class Moclodule {
}
// Instantiated module.
module Moclodule {
export class Manager {
}
}
//// [cloduleWithPriorUninstantiatedModule.js]
var Moclodule = (function () {
function Moclodule() {
}
return Moclodule;
})();
// Instantiated module.
var Moclodule;
(function (Moclodule) {
var Manager = (function () {
function Manager() {
}
return Manager;
})();
Moclodule.Manager = Manager;
})(Moclodule || (Moclodule = {}));

View file

@ -0,0 +1,25 @@
=== tests/cases/compiler/cloduleWithPriorUninstantiatedModule.ts ===
// Non-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
}
}

View file

@ -0,0 +1,16 @@
// Non-ambient & instantiated module.
module Moclodule {
export interface Someinterface {
foo(): void;
}
var x = 10;
}
class Moclodule {
}
// Instantiated module.
module Moclodule {
export class Manager {
}
}

View file

@ -0,0 +1,15 @@
// Non-ambient & uninstantiated module.
module Moclodule {
export interface Someinterface {
foo(): void;
}
}
class Moclodule {
}
// Instantiated module.
module Moclodule {
export class Manager {
}
}