diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index cc3fb24bdb86d..3836c980b45c7 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -463,6 +463,10 @@ namespace ts { return file.externalModuleIndicator !== undefined; } + export function isInternalModule(file: SourceFile): boolean { + return file.internalModuleIndicator !== undefined; + } + // Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter // indicates what changed between the 'text' that this SourceFile has and the 'newText'. // The SourceFile will be created with the compiler attempting to reuse as many nodes from @@ -675,7 +679,7 @@ namespace ts { Debug.assert(token() === SyntaxKind.EndOfFileToken); sourceFile.endOfFileToken = parseTokenNode(); - setExternalModuleIndicator(sourceFile); + setInternalExternalModuleIndicators(sourceFile) sourceFile.nodeCount = nodeCount; sourceFile.identifierCount = identifierCount; @@ -5912,15 +5916,20 @@ namespace ts { sourceFile.checkJsDirective = checkJsDirective; } - function setExternalModuleIndicator(sourceFile: SourceFile) { - sourceFile.externalModuleIndicator = forEach(sourceFile.statements, node => - hasModifier(node, ModifierFlags.Export) + function setInternalExternalModuleIndicators(sourceFile: SourceFile) { + forEach(sourceFile.statements, node => { + if (hasModifier(node, ModifierFlags.Export) || node.kind === SyntaxKind.ImportEqualsDeclaration && (node).moduleReference.kind === SyntaxKind.ExternalModuleReference || node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ExportAssignment || node.kind === SyntaxKind.ExportDeclaration - ? node - : undefined); + ) { + sourceFile.externalModuleIndicator = node + } + else if (node.kind === SyntaxKind.ModuleDeclaration || node.kind === SyntaxKind.NamespaceImport) { + sourceFile.internalModuleIndicator = node + } + }) } const enum ParsingContext { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 7893fee651e2d..2b97c7f7e15df 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1754,7 +1754,7 @@ namespace ts { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher)); } - const firstNonExternalModuleSourceFile = forEach(files, f => !isExternalModule(f) && !isDeclarationFile(f) ? f : undefined); + const firstNonExternalModuleSourceFile = forEach(files, f => isInternalModule(f) && !isDeclarationFile(f) ? f : undefined); if (firstNonExternalModuleSourceFile) { const span = getErrorSpanForNode(firstNonExternalModuleSourceFile, firstNonExternalModuleSourceFile); programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided)); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 40b8ce3067861..c538e1aeb596e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2288,6 +2288,8 @@ namespace ts { // The first node that causes this file to be an external module /* @internal */ externalModuleIndicator: Node; + // The first node that causes this file to be an internal module + /* @internal */ internalModuleIndicator: Node; // The first node that causes this file to be a CommonJS module /* @internal */ commonJsModuleIndicator: Node;