Ensure moduleType is structured during cloneTypeAsModuleType (#51136)

This commit is contained in:
Jake Bailey 2023-03-15 12:56:11 -07:00 committed by GitHub
parent 074bf34633
commit dfa30bbe2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 161 additions and 2 deletions

View File

@ -5156,7 +5156,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true) ||
isEsmCjsRef
) {
const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference);
const moduleType = type.flags & TypeFlags.StructuredType
? getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference)
: createDefaultPropertyWrapperForModule(symbol, symbol.parent);
return cloneTypeAsModuleType(symbol, moduleType, referenceParent);
}
}
@ -34177,7 +34179,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return createPromiseReturnType(node, anyType);
}
function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol, anonymousSymbol?: Symbol | undefined) {
function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol | undefined, anonymousSymbol?: Symbol | undefined) {
const memberTable = createSymbolTable();
const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default);
newSymbol.parent = originalSymbol;

View File

@ -0,0 +1,37 @@
//// [tests/cases/compiler/moduleExportNonStructured.ts] ////
//// [package.json]
{
"name": "test",
"version": "1.0.0",
"description": "",
"type": "module",
"module": "index.mjs"
}
//// [index.mts]
import * as exportAny from "./exportAny.cjs";
import * as exportUnknown from "./exportUnknown.cjs";
import * as exportSymbol from "./exportSymbol.cjs";
import type * as exportAnyType from "./exportAny.cjs";
import type * as exportUnknownType from "./exportUnknown.cjs";
import type * as exportSymbolType from "./exportSymbol.cjs";
//// [exportAny.d.cts]
declare const __: any;
export = __;
//// [exportUnknown.d.cts]
declare const __: unknown;
export = __;
//// [exportSymbol.d.cts]
declare const __: symbol;
export = __;
//// [index.mjs]
export {};

View File

@ -0,0 +1,42 @@
=== tests/cases/compiler/index.mts ===
import * as exportAny from "./exportAny.cjs";
>exportAny : Symbol(exportAny, Decl(index.mts, 0, 6))
import * as exportUnknown from "./exportUnknown.cjs";
>exportUnknown : Symbol(exportUnknown, Decl(index.mts, 1, 6))
import * as exportSymbol from "./exportSymbol.cjs";
>exportSymbol : Symbol(exportSymbol, Decl(index.mts, 2, 6))
import type * as exportAnyType from "./exportAny.cjs";
>exportAnyType : Symbol(exportAnyType, Decl(index.mts, 4, 11))
import type * as exportUnknownType from "./exportUnknown.cjs";
>exportUnknownType : Symbol(exportUnknownType, Decl(index.mts, 5, 11))
import type * as exportSymbolType from "./exportSymbol.cjs";
>exportSymbolType : Symbol(exportSymbolType, Decl(index.mts, 6, 11))
=== tests/cases/compiler/exportAny.d.cts ===
declare const __: any;
>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13))
export = __;
>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13))
=== tests/cases/compiler/exportUnknown.d.cts ===
declare const __: unknown;
>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13))
export = __;
>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13))
=== tests/cases/compiler/exportSymbol.d.cts ===
declare const __: symbol;
>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13))
export = __;
>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13))

View File

@ -0,0 +1,42 @@
=== tests/cases/compiler/index.mts ===
import * as exportAny from "./exportAny.cjs";
>exportAny : { default: any; }
import * as exportUnknown from "./exportUnknown.cjs";
>exportUnknown : { default: unknown; }
import * as exportSymbol from "./exportSymbol.cjs";
>exportSymbol : { default: symbol; }
import type * as exportAnyType from "./exportAny.cjs";
>exportAnyType : { default: any; }
import type * as exportUnknownType from "./exportUnknown.cjs";
>exportUnknownType : { default: unknown; }
import type * as exportSymbolType from "./exportSymbol.cjs";
>exportSymbolType : { default: symbol; }
=== tests/cases/compiler/exportAny.d.cts ===
declare const __: any;
>__ : any
export = __;
>__ : any
=== tests/cases/compiler/exportUnknown.d.cts ===
declare const __: unknown;
>__ : unknown
export = __;
>__ : unknown
=== tests/cases/compiler/exportSymbol.d.cts ===
declare const __: symbol;
>__ : symbol
export = __;
>__ : symbol

View File

@ -0,0 +1,36 @@
// @target: ESNext
// @module: ESNext
// @moduleResolution: NodeNext
// @strict: true
// @filename: package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"type": "module",
"module": "index.mjs"
}
// @filename: index.mts
import * as exportAny from "./exportAny.cjs";
import * as exportUnknown from "./exportUnknown.cjs";
import * as exportSymbol from "./exportSymbol.cjs";
import type * as exportAnyType from "./exportAny.cjs";
import type * as exportUnknownType from "./exportUnknown.cjs";
import type * as exportSymbolType from "./exportSymbol.cjs";
// @filename: exportAny.d.cts
declare const __: any;
export = __;
// @filename: exportUnknown.d.cts
declare const __: unknown;
export = __;
// @filename: exportSymbol.d.cts
declare const __: symbol;
export = __;