From 9547d0de0df15e50ce777675d8736745219fa9ee Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 12 Apr 2016 16:14:35 -0700 Subject: [PATCH] Move helpers to factory.ts --- src/compiler/factory.ts | 52 ++++++++++++++++++ src/compiler/transformers/module/module.ts | 58 ++------------------ src/compiler/transformers/module/system.ts | 63 ++-------------------- 3 files changed, 60 insertions(+), 113 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 0b32988a6ee..b620be2ed5e 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -1707,4 +1707,56 @@ namespace ts { nodes.hasTrailingComma = hasTrailingComma; return nodes; } + + export function getLocalNameForExternalImport(node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration, sourceFile: SourceFile): Identifier { + const namespaceDeclaration = getNamespaceDeclarationNode(node); + if (namespaceDeclaration && !isDefaultImport(node)) { + return createIdentifier(getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name)); + } + if (node.kind === SyntaxKind.ImportDeclaration && (node).importClause) { + return getGeneratedNameForNode(node); + } + if (node.kind === SyntaxKind.ExportDeclaration && (node).moduleSpecifier) { + return getGeneratedNameForNode(node); + } + } + + export function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration, sourceFile: SourceFile, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) { + const moduleName = getExternalModuleName(importNode); + if (moduleName.kind === SyntaxKind.StringLiteral) { + return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions) + || tryRenameExternalModule(moduleName, sourceFile) + || getSynthesizedClone(moduleName); + } + + return undefined; + } + + /** + * Some bundlers (SystemJS builder) sometimes want to rename dependencies. + * Here we check if alternative name was provided for a given moduleName and return it if possible. + */ + function tryRenameExternalModule(moduleName: LiteralExpression, sourceFile: SourceFile) { + if (sourceFile.renamedDependencies && hasProperty(sourceFile.renamedDependencies, moduleName.text)) { + return createLiteral(sourceFile.renamedDependencies[moduleName.text]); + } + return undefined; + } + + export 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); + } } \ No newline at end of file diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index ee4722b8708..c0a021ec8de 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -769,60 +769,8 @@ namespace ts { ); } - function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) { - const moduleName = getExternalModuleName(importNode); - if (moduleName.kind === SyntaxKind.StringLiteral) { - return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions) - || tryRenameExternalModule(moduleName) - || getSynthesizedClone(moduleName); - } - - return undefined; - } - - /** - * Some bundlers (SystemJS builder) sometimes want to rename dependencies. - * Here we check if alternative name was provided for a given moduleName and return it if possible. - */ - function tryRenameExternalModule(moduleName: LiteralExpression) { - if (currentSourceFile.renamedDependencies && hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return createLiteral(currentSourceFile.renamedDependencies[moduleName.text]); - } - return undefined; - } - - function getLocalNameForExternalImport(node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration): Identifier { - const namespaceDeclaration = getNamespaceDeclarationNode(node); - if (namespaceDeclaration && !isDefaultImport(node)) { - return createIdentifier(getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name)); - } - if (node.kind === SyntaxKind.ImportDeclaration && (node).importClause) { - return getGeneratedNameForNode(node); - } - if (node.kind === SyntaxKind.ExportDeclaration && (node).moduleSpecifier) { - return getGeneratedNameForNode(node); - } - } - - 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 moduleName = getExternalModuleNameLiteral(importNode, currentSourceFile, host, resolver, compilerOptions); const args: Expression[] = []; if (isDefined(moduleName)) { args.push(moduleName); @@ -881,10 +829,10 @@ namespace ts { for (const importNode of externalImports) { // Find the name of the external module - const externalModuleName = getExternalModuleNameLiteral(importNode); + const externalModuleName = getExternalModuleNameLiteral(importNode, currentSourceFile, host, resolver, compilerOptions); // Find the name of the module alias, if there is one - const importAliasName = getLocalNameForExternalImport(importNode); + const importAliasName = getLocalNameForExternalImport(importNode, currentSourceFile); if (includeNonAmdDependencies && importAliasName) { aliasedModuleNames.push(externalModuleName); importAliasNames.push(createParameter(importAliasName)); diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 5712b01f513..ca2c9e2b70e 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -343,11 +343,11 @@ namespace ts { const setters: Expression[] = []; for (const group of dependencyGroups) { // derive a unique name for parameter from the first named entry in the group - const localName = forEach(group.externalImports, getLocalNameForExternalImport); + const localName = forEach(group.externalImports, i => getLocalNameForExternalImport(i, currentSourceFile)); const parameterName = localName ? getGeneratedNameForNode(localName) : createUniqueName(""); const statements: Statement[] = []; for (const entry of group.externalImports) { - const importVariableName = getLocalNameForExternalImport(entry); + const importVariableName = getLocalNameForExternalImport(entry, currentSourceFile); switch (entry.kind) { case SyntaxKind.ImportDeclaration: if (!(entry).importClause) { @@ -536,7 +536,7 @@ namespace ts { function visitImportDeclaration(node: ImportDeclaration): Node { if (node.importClause && contains(externalImports, node)) { - hoistVariableDeclaration(getLocalNameForExternalImport(node)); + hoistVariableDeclaration(getLocalNameForExternalImport(node, currentSourceFile)); } return undefined; @@ -544,7 +544,7 @@ namespace ts { function visitImportEqualsDeclaration(node: ImportEqualsDeclaration): Node { if (contains(externalImports, node)) { - hoistVariableDeclaration(getLocalNameForExternalImport(node)); + hoistVariableDeclaration(getLocalNameForExternalImport(node, currentSourceFile)); } // NOTE(rbuckton): Do we support export import = require('') in System? @@ -1154,59 +1154,6 @@ namespace ts { return node; } - function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) { - const moduleName = getExternalModuleName(importNode); - if (moduleName.kind === SyntaxKind.StringLiteral) { - return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions) - || tryRenameExternalModule(moduleName) - || getSynthesizedClone(moduleName); - } - - return undefined; - } - - /** - * Some bundlers (SystemJS builder) sometimes want to rename dependencies. - * Here we check if alternative name was provided for a given moduleName and return it if possible. - */ - function tryRenameExternalModule(moduleName: LiteralExpression) { - if (currentSourceFile.renamedDependencies && hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return createLiteral(currentSourceFile.renamedDependencies[moduleName.text]); - } - - return undefined; - } - - function getLocalNameForExternalImport(node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration): Identifier { - const namespaceDeclaration = getNamespaceDeclarationNode(node); - if (namespaceDeclaration && !isDefaultImport(node)) { - return createIdentifier(getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name)); - } - if (node.kind === SyntaxKind.ImportDeclaration && (node).importClause) { - return getGeneratedNameForNode(node); - } - if (node.kind === SyntaxKind.ExportDeclaration && (node).moduleSpecifier) { - return getGeneratedNameForNode(node); - } - } - - 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. @@ -1336,7 +1283,7 @@ namespace ts { const dependencyGroups: DependencyGroup[] = []; for (let i = 0; i < externalImports.length; i++) { const externalImport = externalImports[i]; - const externalModuleName = getExternalModuleNameLiteral(externalImport); + const externalModuleName = getExternalModuleNameLiteral(externalImport, currentSourceFile, host, resolver, compilerOptions); const text = externalModuleName.text; if (hasProperty(groupIndices, text)) { // deduplicate/group entries in dependency list by the dependency name