Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Pick PR #61392 (Fix serialization of accessor types...) into release-5.8 #61423

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6136,10 +6136,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
serializeExistingTypeNode(context, typeNode, addUndefined) {
return serializeExistingTypeNode(context as NodeBuilderContext, typeNode, !!addUndefined);
},
serializeReturnTypeForSignature(syntacticContext, signatureDeclaration) {
serializeReturnTypeForSignature(syntacticContext, signatureDeclaration, symbol) {
const context = syntacticContext as NodeBuilderContext;
const signature = getSignatureFromDeclaration(signatureDeclaration);
const returnType = context.enclosingSymbolTypes.get(getSymbolId(getSymbolOfDeclaration(signatureDeclaration))) ?? instantiateType(getReturnTypeOfSignature(signature), context.mapper);
symbol ??= getSymbolOfDeclaration(signatureDeclaration);
const returnType = context.enclosingSymbolTypes.get(getSymbolId(symbol)) ?? instantiateType(getReturnTypeOfSignature(signature), context.mapper);
return serializeInferredReturnTypeForSignature(context, signature, returnType);
},
serializeTypeOfExpression(syntacticContext, expr) {
Expand All @@ -6153,7 +6154,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
symbol ??= getSymbolOfDeclaration(declaration);
let type = context.enclosingSymbolTypes?.get(getSymbolId(symbol));
if (type === undefined) {
type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
type = symbol.flags & SymbolFlags.Accessor && declaration.kind === SyntaxKind.SetAccessor ? instantiateType(getWriteTypeOfSymbol(symbol), context.mapper) :
symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
? instantiateType(getWidenedLiteralType(getTypeOfSymbol(symbol)), context.mapper)
: errorType;
}
Expand Down Expand Up @@ -7383,12 +7385,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (propertySymbol.flags & SymbolFlags.Accessor) {
const writeType = getWriteTypeOfSymbol(propertySymbol);
if (propertyType !== writeType && !isErrorType(propertyType) && !isErrorType(writeType)) {
const symbolMapper = getSymbolLinks(propertySymbol).mapper;
const getterDeclaration = getDeclarationOfKind<GetAccessorDeclaration>(propertySymbol, SyntaxKind.GetAccessor)!;
const getterSignature = getSignatureFromDeclaration(getterDeclaration);
typeElements.push(
setCommentRange(
context,
signatureToSignatureDeclarationHelper(getterSignature, SyntaxKind.GetAccessor, context, { name: propertyName }) as GetAccessorDeclaration,
signatureToSignatureDeclarationHelper(symbolMapper ? instantiateSignature(getterSignature, symbolMapper) : getterSignature, SyntaxKind.GetAccessor, context, { name: propertyName }) as GetAccessorDeclaration,
getterDeclaration,
),
);
Expand All @@ -7397,7 +7400,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
typeElements.push(
setCommentRange(
context,
signatureToSignatureDeclarationHelper(setterSignature, SyntaxKind.SetAccessor, context, { name: propertyName }) as SetAccessorDeclaration,
signatureToSignatureDeclarationHelper(symbolMapper ? instantiateSignature(setterSignature, symbolMapper) : setterSignature, SyntaxKind.SetAccessor, context, { name: propertyName }) as SetAccessorDeclaration,
setterDeclaration,
),
);
Expand Down Expand Up @@ -8662,6 +8665,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const addUndefinedForParameter = declaration && (isParameter(declaration) || isJSDocParameterTag(declaration)) && requiresAddingImplicitUndefined(declaration, context.enclosingDeclaration);
const decl = declaration ?? symbol.valueDeclaration ?? getDeclarationWithTypeAnnotation(symbol) ?? symbol.declarations?.[0];
if (decl) {
const restore = addSymbolTypeToContext(context, symbol, type);
if (isAccessor(decl)) {
result = syntacticNodeBuilder.serializeTypeOfAccessor(decl, symbol, context);
}
Expand All @@ -8670,10 +8674,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& !nodeIsSynthesized(decl)
&& !(getObjectFlags(type) & ObjectFlags.RequiresWidening)
) {
const restore = addSymbolTypeToContext(context, symbol, type);
result = syntacticNodeBuilder.serializeTypeOfDeclaration(decl, symbol, context);
restore();
}
restore();
}
if (!result) {
if (addUndefinedForParameter) {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ export function createSyntacticTypeNodeBuilder(
return withNewScope(context, node, () => serializeTypeAnnotationOfDeclaration(accessorType, context, node, symbol) ?? inferTypeOfDeclaration(node, symbol, context));
}
if (accessorDeclarations.getAccessor) {
return withNewScope(context, accessorDeclarations.getAccessor, () => createReturnFromSignature(accessorDeclarations.getAccessor!, /*symbol*/ undefined, context));
return withNewScope(context, accessorDeclarations.getAccessor, () => createReturnFromSignature(accessorDeclarations.getAccessor!, symbol, context));
}
return undefined;
}
Expand Down Expand Up @@ -855,14 +855,14 @@ export function createSyntacticTypeNodeBuilder(
return resolver.serializeTypeOfExpression(context, node) ?? factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
}

function inferReturnTypeOfSignatureSignature(node: SignatureDeclaration | JSDocSignature, context: SyntacticTypeNodeBuilderContext, reportFallback: boolean) {
function inferReturnTypeOfSignatureSignature(node: SignatureDeclaration | JSDocSignature, context: SyntacticTypeNodeBuilderContext, symbol: Symbol | undefined, reportFallback: boolean) {
if (reportFallback) {
context.tracker.reportInferenceFallback(node);
}
if (context.noInferenceFallback === true) {
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
return resolver.serializeReturnTypeForSignature(context, node) ?? factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
return resolver.serializeReturnTypeForSignature(context, node, symbol) ?? factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
}

function inferAccessorType(node: GetAccessorDeclaration | SetAccessorDeclaration, allAccessors: AllAccessorDeclarations, context: SyntacticTypeNodeBuilderContext, symbol: Symbol | undefined, reportFallback: boolean = true): TypeNode | undefined {
Expand Down Expand Up @@ -1276,7 +1276,7 @@ export function createSyntacticTypeNodeBuilder(
else if (isValueSignatureDeclaration(fn)) {
returnType = typeFromSingleReturnExpression(fn, context);
}
return returnType.type !== undefined ? returnType.type : inferReturnTypeOfSignatureSignature(fn, context, reportFallback && returnType.reportFallback && !returnTypeNode);
return returnType.type !== undefined ? returnType.type : inferReturnTypeOfSignatureSignature(fn, context, symbol, reportFallback && returnType.reportFallback && !returnTypeNode);
}

function typeFromSingleReturnExpression(declaration: FunctionLikeDeclaration | undefined, context: SyntacticTypeNodeBuilderContext): SyntacticResult {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10553,7 +10553,7 @@ export interface SyntacticTypeNodeBuilderResolver {
isDefinitelyReferenceToGlobalSymbolObject(node: Node): boolean;
isEntityNameVisible(context: SyntacticTypeNodeBuilderContext, entityName: EntityNameOrEntityNameExpression, shouldComputeAliasToMakeVisible?: boolean): SymbolVisibilityResult;
serializeExistingTypeNode(context: SyntacticTypeNodeBuilderContext, node: TypeNode, addUndefined?: boolean): TypeNode | undefined;
serializeReturnTypeForSignature(context: SyntacticTypeNodeBuilderContext, signatureDeclaration: SignatureDeclaration | JSDocSignature): TypeNode | undefined;
serializeReturnTypeForSignature(context: SyntacticTypeNodeBuilderContext, signatureDeclaration: SignatureDeclaration | JSDocSignature, symbol: Symbol | undefined): TypeNode | undefined;
serializeTypeOfExpression(context: SyntacticTypeNodeBuilderContext, expr: Expression): TypeNode;
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined): TypeNode | undefined;
serializeNameOfParameter(context: SyntacticTypeNodeBuilderContext, parameter: ParameterDeclaration): BindingName | string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitGenericTypeParamerSerialization.ts] ////

//// [declarationEmitGenericTypeParamerSerialization.ts]
function wrapper<T>(value: T) {
return {
m() { return value; },
get g() { return value; },
}
}

export const w = wrapper(0)


//// [declarationEmitGenericTypeParamerSerialization.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.w = void 0;
function wrapper(value) {
return {
m: function () { return value; },
get g() { return value; },
};
}
exports.w = wrapper(0);


//// [declarationEmitGenericTypeParamerSerialization.d.ts]
export declare const w: {
m(): number;
readonly g: number;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/declarationEmitGenericTypeParamerSerialization.ts] ////

=== declarationEmitGenericTypeParamerSerialization.ts ===
function wrapper<T>(value: T) {
>wrapper : Symbol(wrapper, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 0))
>T : Symbol(T, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 17))
>value : Symbol(value, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 20))
>T : Symbol(T, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 17))

return {
m() { return value; },
>m : Symbol(m, Decl(declarationEmitGenericTypeParamerSerialization.ts, 1, 10))
>value : Symbol(value, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 20))

get g() { return value; },
>g : Symbol(g, Decl(declarationEmitGenericTypeParamerSerialization.ts, 2, 28))
>value : Symbol(value, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 20))
}
}

export const w = wrapper(0)
>w : Symbol(w, Decl(declarationEmitGenericTypeParamerSerialization.ts, 7, 12))
>wrapper : Symbol(wrapper, Decl(declarationEmitGenericTypeParamerSerialization.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/compiler/declarationEmitGenericTypeParamerSerialization.ts] ////

=== declarationEmitGenericTypeParamerSerialization.ts ===
function wrapper<T>(value: T) {
>wrapper : <T>(value: T) => { m(): T; readonly g: T; }
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value : T
> : ^

return {
>{ m() { return value; }, get g() { return value; }, } : { m(): T; readonly g: T; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^

m() { return value; },
>m : () => T
> : ^^^^^^^
>value : T
> : ^

get g() { return value; },
>g : T
> : ^
>value : T
> : ^
}
}

export const w = wrapper(0)
>w : { m(): number; readonly g: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>wrapper(0) : { m(): number; readonly g: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>wrapper : <T>(value: T) => { m(): T; readonly g: T; }
> : ^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>0 : 0
> : ^

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//// [tests/cases/compiler/declarationEmitGenericTypeParamerSerialization2.ts] ////

//// [declarationEmitGenericTypeParamerSerialization2.ts]
type ExpandRecursively<T> = {} & {
[P in keyof T]: T[P]
}

type G<T = string> = {
get readonlyProperty(): T;
field: T;
method(p: T): T;
fnField: (p: T) => T;
set writeOnlyProperty(p: T);
get property(): T;
set property(p: T);
get divergentProperty(): string | T;
set divergentProperty(p: number | T);
};

export const x = (() => null! as ExpandRecursively<G>)();


function makeV() {
type X<T> = {
get readonlyProperty(): T;
field: T;
method(p: T): T;
fnField: (p: T) => T;
set writeOnlyProperty(p: T);
get property(): T;
set property(p: T);
get divergentProperty(): string | T;
set divergentProperty(p: number | T);
}
return null! as X<number>
}

export const v = makeV();


//// [declarationEmitGenericTypeParamerSerialization2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.v = exports.x = void 0;
exports.x = (function () { return null; })();
function makeV() {
return null;
}
exports.v = makeV();


//// [declarationEmitGenericTypeParamerSerialization2.d.ts]
export declare const x: {
readonly readonlyProperty: string;
field: string;
method: (p: string) => string;
fnField: (p: string) => string;
writeOnlyProperty: string;
property: string;
divergentProperty: string;
};
export declare const v: {
readonly readonlyProperty: number;
field: number;
method(p: number): number;
fnField: (p: number) => number;
writeOnlyProperty: number;
property: number;
get divergentProperty(): string | number;
set divergentProperty(p: number);
};
Loading