mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 11:35:42 -06:00
Move helpers to factory.ts
This commit is contained in:
parent
5cd5976650
commit
9547d0de0d
@ -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 && (<ImportDeclaration>node).importClause) {
|
||||
return getGeneratedNameForNode(node);
|
||||
}
|
||||
if (node.kind === SyntaxKind.ExportDeclaration && (<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(<StringLiteral>moduleName, sourceFile)
|
||||
|| getSynthesizedClone(<StringLiteral>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);
|
||||
}
|
||||
}
|
||||
@ -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(<StringLiteral>moduleName)
|
||||
|| getSynthesizedClone(<StringLiteral>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 && (<ImportDeclaration>node).importClause) {
|
||||
return getGeneratedNameForNode(node);
|
||||
}
|
||||
if (node.kind === SyntaxKind.ExportDeclaration && (<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));
|
||||
|
||||
@ -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 (!(<ImportDeclaration>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(<StringLiteral>moduleName)
|
||||
|| getSynthesizedClone(<StringLiteral>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 && (<ImportDeclaration>node).importClause) {
|
||||
return getGeneratedNameForNode(node);
|
||||
}
|
||||
if (node.kind === SyntaxKind.ExportDeclaration && (<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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user