mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
wip-Emit import call expression for commonjs
This commit is contained in:
@@ -1644,6 +1644,7 @@ namespace ts {
|
||||
if (node.resolvedTypeReferenceDirectiveNames !== undefined) updated.resolvedTypeReferenceDirectiveNames = node.resolvedTypeReferenceDirectiveNames;
|
||||
if (node.imports !== undefined) updated.imports = node.imports;
|
||||
if (node.moduleAugmentations !== undefined) updated.moduleAugmentations = node.moduleAugmentations;
|
||||
if (node.containsDynamicImport !== undefined) updated.containsDynamicImport = node.containsDynamicImport;
|
||||
return updateNode(updated, node);
|
||||
}
|
||||
|
||||
|
||||
@@ -461,7 +461,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
export function isExternalModule(file: SourceFile): boolean {
|
||||
return file.externalModuleIndicator !== undefined;
|
||||
return file.externalModuleIndicator !== undefined || file.containsDynamicImport;
|
||||
}
|
||||
|
||||
// Produces a new SourceFile for the 'newText' provided. The 'textChangeRange' parameter
|
||||
@@ -2778,6 +2778,7 @@ namespace ts {
|
||||
case SyntaxKind.SlashToken:
|
||||
case SyntaxKind.SlashEqualsToken:
|
||||
case SyntaxKind.Identifier:
|
||||
case SyntaxKind.ImportKeyword:
|
||||
return true;
|
||||
default:
|
||||
return isIdentifier();
|
||||
@@ -3698,6 +3699,7 @@ namespace ts {
|
||||
if (importCall.specifier.kind === SyntaxKind.StringLiteral) {
|
||||
(sourceFile.imports || (sourceFile.imports = [])).push(importCall.specifier as StringLiteral);
|
||||
}
|
||||
sourceFile.containsDynamicImport = true;
|
||||
return importCall;
|
||||
}
|
||||
const expression = token() === SyntaxKind.SuperKeyword ? parseSuperExpression() : parseMemberExpressionOrHigher();
|
||||
|
||||
@@ -482,12 +482,35 @@ namespace ts {
|
||||
return visitEndOfDeclarationMarker(<EndOfDeclarationMarker>node);
|
||||
|
||||
default:
|
||||
// This visitor does not descend into the tree, as export/import statements
|
||||
// are only transformed at the top level of a file.
|
||||
return node;
|
||||
return visitEachChild(node, visitor, context);
|
||||
}
|
||||
}
|
||||
|
||||
function visitor(node: Node): VisitResult<Node> {
|
||||
// This visitor does not need to descend into the tree if there is no dynamic import,
|
||||
// as export/import statements are only transformed at the top level of a file.
|
||||
if (!currentSourceFile.containsDynamicImport) {
|
||||
return node;
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ImportCallExpression:
|
||||
return visitImportCallExpression(<ImportCallExpression>node);
|
||||
default:
|
||||
return visitEachChild(node, visitor, context);
|
||||
}
|
||||
}
|
||||
|
||||
function visitImportCallExpression(node: ImportCallExpression): Expression{
|
||||
return createCall(
|
||||
createPropertyAccess(
|
||||
createCall(/*expression*/ createPropertyAccess(createIdentifier("Promise"), "resolve"), /*typeArguments*/ undefined, /*argumentsArray*/ []),
|
||||
"then"),
|
||||
/*typeArguments*/ undefined,
|
||||
[ createArrowFunction(/*modifiers*/ undefined, /*typeParameters*/ undefined, /*parameters*/ undefined, /*type*/ undefined, createToken(SyntaxKind.EqualsGreaterThanToken), createCall(createIdentifier("require"), /*typeArguments*/ undefined, [node.specifier]))]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits an ImportDeclaration node.
|
||||
*
|
||||
@@ -780,7 +803,7 @@ namespace ts {
|
||||
node.asteriskToken,
|
||||
getDeclarationName(node, /*allowComments*/ true, /*allowSourceMaps*/ true),
|
||||
/*typeParameters*/ undefined,
|
||||
node.parameters,
|
||||
visitNodes(node.parameters, visitor),
|
||||
/*type*/ undefined,
|
||||
node.body
|
||||
),
|
||||
@@ -822,7 +845,7 @@ namespace ts {
|
||||
visitNodes(node.modifiers, modifierVisitor, isModifier),
|
||||
getDeclarationName(node, /*allowComments*/ true, /*allowSourceMaps*/ true),
|
||||
/*typeParameters*/ undefined,
|
||||
node.heritageClauses,
|
||||
visitNodes(node.heritageClauses, visitor),
|
||||
node.members
|
||||
),
|
||||
node
|
||||
@@ -884,7 +907,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
else {
|
||||
statements = append(statements, node);
|
||||
statements = append(statements, visitEachChild(node, visitor, context));
|
||||
}
|
||||
|
||||
if (hasAssociatedEndOfDeclarationMarker(node)) {
|
||||
@@ -907,7 +930,7 @@ namespace ts {
|
||||
function transformInitializedVariable(node: VariableDeclaration): Expression {
|
||||
if (isBindingPattern(node.name)) {
|
||||
return flattenDestructuringAssignment(
|
||||
node,
|
||||
visitNode(node, visitor),
|
||||
/*visitor*/ undefined,
|
||||
context,
|
||||
FlattenLevel.All,
|
||||
@@ -924,7 +947,7 @@ namespace ts {
|
||||
),
|
||||
/*location*/ node.name
|
||||
),
|
||||
node.initializer
|
||||
visitNode(node.initializer, visitor)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2282,6 +2282,7 @@ namespace ts {
|
||||
/* @internal */ moduleAugmentations: LiteralExpression[];
|
||||
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
|
||||
/* @internal */ ambientModuleNames: string[];
|
||||
/* @internal */ containsDynamicImport?: boolean;
|
||||
}
|
||||
|
||||
export interface Bundle extends Node {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// @module: commonjs
|
||||
// @target: esnext
|
||||
// @filename: 0.ts
|
||||
export function foo() { return "foo"; }
|
||||
|
||||
// @filename: 1.ts
|
||||
import("./0");
|
||||
var p1 = import("./0");
|
||||
p1.then(zero => {
|
||||
return zero.foo();
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// @module: commonjs
|
||||
// @target: esnext
|
||||
// @filename: 0.ts
|
||||
export function foo() { return "foo"; }
|
||||
|
||||
// @filename: 1.ts
|
||||
export function backup() { return "backup"; }
|
||||
|
||||
// @filename: 2.ts
|
||||
async function compute(promise: Promise<any>) {
|
||||
let j = await promise;
|
||||
if (!j) {
|
||||
j = await import("./1");
|
||||
return j.backup();
|
||||
}
|
||||
return j.foo();
|
||||
}
|
||||
|
||||
compute(import("./0"));
|
||||
Reference in New Issue
Block a user