Create synthetic default exports for dynamic imports (#17492)

* Create synthetic default exports for dynamic imports

* Slightly better solution

* Actually accept baselines

* Slightly adjust synthetic type

* Cache synthetic type

* Inline variables, remove non-required calls

* Rename function
This commit is contained in:
Wesley Wigham
2017-08-08 17:01:18 -07:00
committed by GitHub
parent 847d7fe3c8
commit 43e758e1a9
15 changed files with 224 additions and 129 deletions

View File

@@ -1757,7 +1757,7 @@ namespace ts {
// An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
// and an external module with no 'export =' declaration resolves to the module itself.
function resolveExternalModuleSymbol(moduleSymbol: Symbol, dontResolveAlias?: boolean): Symbol {
return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports.get("export=" as __String), dontResolveAlias)) || moduleSymbol;
return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports.get(InternalSymbolName.ExportEquals), dontResolveAlias)) || moduleSymbol;
}
// An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export ='
@@ -1772,7 +1772,7 @@ namespace ts {
}
function hasExportAssignmentSymbol(moduleSymbol: Symbol): boolean {
return moduleSymbol.exports.get("export=" as __String) !== undefined;
return moduleSymbol.exports.get(InternalSymbolName.ExportEquals) !== undefined;
}
function getExportsOfModuleAsArray(moduleSymbol: Symbol): Symbol[] {
@@ -16402,12 +16402,35 @@ namespace ts {
if (moduleSymbol) {
const esModuleSymbol = resolveESModuleSymbol(moduleSymbol, specifier, /*dontRecursivelyResolve*/ true);
if (esModuleSymbol) {
return createPromiseReturnType(node, getTypeOfSymbol(esModuleSymbol));
return createPromiseReturnType(node, getTypeWithSyntheticDefaultImportType(getTypeOfSymbol(esModuleSymbol), esModuleSymbol));
}
}
return createPromiseReturnType(node, anyType);
}
function getTypeWithSyntheticDefaultImportType(type: Type, symbol: Symbol): Type {
if (allowSyntheticDefaultImports && type && type !== unknownType) {
const synthType = type as SyntheticDefaultModuleType;
if (!synthType.syntheticType) {
if (!getPropertyOfType(type, InternalSymbolName.Default)) {
const memberTable = createSymbolTable();
const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default);
newSymbol.target = resolveSymbol(symbol);
memberTable.set(InternalSymbolName.Default, newSymbol);
const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type);
const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
anonymousSymbol.type = defaultContainingObject;
synthType.syntheticType = getIntersectionType([type, defaultContainingObject]);
}
else {
synthType.syntheticType = type;
}
}
return synthType.syntheticType;
}
return type;
}
function isCommonJsRequire(node: Node) {
if (!isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) {
return false;

View File

@@ -3318,6 +3318,11 @@ namespace ts {
awaitedTypeOfType?: Type;
}
/* @internal */
export interface SyntheticDefaultModuleType extends Type {
syntheticType?: Type;
}
export interface TypeVariable extends Type {
/* @internal */
resolvedBaseConstraint: Type;