From 5cd5976650ec3b137707b474b304963c26b983b4 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 12 Apr 2016 11:45:53 -0700 Subject: [PATCH] Emit module names when --out is specified for system and amd modules --- src/compiler/transformer.ts | 1 + src/compiler/transformers/module/module.ts | 25 ++++++++++++++++++--- src/compiler/transformers/module/system.ts | 26 +++++++++++++++++++--- src/compiler/types.ts | 1 + 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index ebacacf6c5f..1f4764da688 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -68,6 +68,7 @@ namespace ts { const context: TransformationContext = { getCompilerOptions: () => host.getCompilerOptions(), getEmitResolver: () => resolver, + getEmitHost: () => host, getNodeEmitFlags, setNodeEmitFlags, hoistVariableDeclaration, diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 440aec62e3b..ee4722b8708 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -21,6 +21,7 @@ namespace ts { const compilerOptions = context.getCompilerOptions(); const resolver = context.getEmitResolver(); + const host = context.getEmitHost(); const languageVersion = getEmitScriptTarget(compilerOptions); const moduleKind = getEmitModuleKind(compilerOptions); const previousExpressionSubstitution = context.expressionSubstitution; @@ -91,7 +92,7 @@ namespace ts { */ function transformAMDModule(node: SourceFile) { const define = createIdentifier("define"); - const moduleName = node.moduleName ? createLiteral(node.moduleName) : undefined; + const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions); return transformAsynchronousModule(node, define, moduleName, /*includeNonAmdDependencies*/ true); } @@ -771,8 +772,9 @@ namespace ts { function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) { const moduleName = getExternalModuleName(importNode); if (moduleName.kind === SyntaxKind.StringLiteral) { - return tryRenameExternalModule(moduleName) - || createLiteral(moduleName); + return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions) + || tryRenameExternalModule(moduleName) + || getSynthesizedClone(moduleName); } return undefined; @@ -802,6 +804,23 @@ namespace ts { } } + function tryGetModuleNameFromFile(file: SourceFile, host: EmitHost, options: CompilerOptions): StringLiteral { + if (!file) { + return undefined; + } + if (file.moduleName) { + return createLiteral(file.moduleName); + } + if (!isDeclarationFile(file) && (options.out || options.outFile)) { + return createLiteral(getExternalModuleNameFromPath(host, file.fileName)); + } + return undefined; + } + + function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) { + return tryGetModuleNameFromFile(resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions); + } + function createRequireCall(importNode: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration) { const moduleName = getExternalModuleNameLiteral(importNode); const args: Expression[] = []; diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index be9390beba8..5712b01f513 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -20,6 +20,7 @@ namespace ts { const compilerOptions = context.getCompilerOptions(); const resolver = context.getEmitResolver(); + const host = context.getEmitHost(); const languageVersion = getEmitScriptTarget(compilerOptions); const previousExpressionSubstitution = context.expressionSubstitution; context.enableExpressionSubstitution(SyntaxKind.Identifier); @@ -106,6 +107,7 @@ namespace ts { // Add the body of the module. addSystemModuleBody(statements, node, dependencyGroups); + const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions); const dependencies = createArrayLiteral(map(dependencyGroups, getNameOfDependencyGroup)); const body = createFunctionExpression( /*asteriskToken*/ undefined, @@ -127,8 +129,8 @@ namespace ts { createStatement( createCall( createPropertyAccess(createIdentifier("System"), "register"), - node.moduleName - ? [createLiteral(node.moduleName), dependencies, body] + moduleName + ? [moduleName, dependencies, body] : [dependencies, body] ) ) @@ -1155,7 +1157,8 @@ namespace ts { function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) { const moduleName = getExternalModuleName(importNode); if (moduleName.kind === SyntaxKind.StringLiteral) { - return tryRenameExternalModule(moduleName) + return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions) + || tryRenameExternalModule(moduleName) || getSynthesizedClone(moduleName); } @@ -1187,6 +1190,23 @@ namespace ts { } } + function tryGetModuleNameFromFile(file: SourceFile, host: EmitHost, options: CompilerOptions): StringLiteral { + if (!file) { + return undefined; + } + if (file.moduleName) { + return createLiteral(file.moduleName); + } + if (!isDeclarationFile(file) && (options.out || options.outFile)) { + return createLiteral(getExternalModuleNameFromPath(host, file.fileName)); + } + return undefined; + } + + function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) { + return tryGetModuleNameFromFile(resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions); + } + /** * Gets a name to use for a DeclarationStatement. * @param node The declaration statement. diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7723abfe270..2238cd4f22b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2892,6 +2892,7 @@ namespace ts { export interface TransformationContext extends LexicalEnvironment { getCompilerOptions(): CompilerOptions; getEmitResolver(): EmitResolver; + getEmitHost(): EmitHost; getNodeEmitFlags(node: Node): NodeEmitFlags; setNodeEmitFlags(node: T, flags: NodeEmitFlags): T; hoistFunctionDeclaration(node: FunctionDeclaration): void;