Skip to content

Commit 29f7f59

Browse files
committed
Add instantiated symbol mapper into context mapper when reprinting nodes we instantiate
1 parent 0ba5d99 commit 29f7f59

13 files changed

Lines changed: 227 additions & 12 deletions

‎tsc/internal/checker/nodebuilderimpl.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,11 @@ func (b *NodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
23002300
var reportedInferenceFallback bool
23012301
// !!! expandable hover support
23022302
if !b.isActivelyExpanding() && tryReuse && b.ctx.enclosingDeclaration != nil && declaration != nil && (ast.IsAccessor(declaration) || (ast.HasInferredType(declaration) && !ast.NodeIsSynthesized(declaration) && (t.ObjectFlags()&ObjectFlagsRequiresWidening) == 0)) {
2303+
oldMapper := b.ctx.mapper
2304+
// The annotation belongs to the original declaration, not the instantiated symbol.
2305+
if symbol != nil && symbol.CheckFlags&ast.CheckFlagsInstantiated != 0 {
2306+
b.ctx.mapper = b.ch.combineTypeMappers(b.ch.valueSymbolLinks.Get(symbol).mapper, oldMapper)
2307+
}
23032308
var remove func()
23042309
if symbol != nil {
23052310
remove = b.addSymbolTypeToContext(symbol, t)
@@ -2350,6 +2355,7 @@ func (b *NodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
23502355
if remove != nil {
23512356
remove()
23522357
}
2358+
b.ctx.mapper = oldMapper
23532359
}
23542360
if result == nil {
23552361
if reportedInferenceFallback {

‎tsc/internal/checker/pseudotypenodebuilder.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (b *NodeBuilderImpl) pseudoTypeToNodeWithCheckerFallback(t *pseudochecker.P
3030
return result
3131
} else if t.Kind == pseudochecker.PseudoTypeKindDirect {
3232
existing := t.AsPseudoTypeDirect().TypeNode
33-
if !b.canReuseExistingJSTypeNode(existing, checkerType) {
33+
if b.getTypeFromTypeNode(existing, true /*noMappedTypes*/) == nil || !b.canReuseExistingJSTypeNode(existing, checkerType) {
3434
if !b.ctx.suppressReportInferenceFallback {
3535
b.ctx.tracker.ReportInferenceFallback(existing)
3636
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/declarationEmitUninstantiatedConditionalTypeParameters.ts] ////
2+
3+
//// [contracts.js]
4+
exports.schema = { parse: v => v };
5+
6+
//// [router.ts]
7+
import { State } from "./contracts";
8+
interface Builder<TOutputOut> {
9+
query<T>(resolver: () => T): { output: TOutputOut extends "unset" ? T : TOutputOut };
10+
}
11+
12+
declare const builder: Builder<"unset">;
13+
14+
export const query = builder.query((): State | null => null);
15+
export const anyQuery = builder.query((): any => null);
16+
export const typedQuery = builder.query((): string | null => null);
17+
18+
19+
20+
21+
//// [router.d.ts]
22+
export declare const query: {
23+
output: any;
24+
};
25+
export declare const anyQuery: {
26+
output: any;
27+
};
28+
export declare const typedQuery: {
29+
output: string | null;
30+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/declarationEmitUninstantiatedConditionalTypeParameters.ts] ////
2+
3+
=== router.ts ===
4+
import { State } from "./contracts";
5+
>State : Symbol(State, Decl(router.ts, 0, 8))
6+
7+
interface Builder<TOutputOut> {
8+
>Builder : Symbol(Builder, Decl(router.ts, 0, 36))
9+
>TOutputOut : Symbol(TOutputOut, Decl(router.ts, 1, 18))
10+
11+
query<T>(resolver: () => T): { output: TOutputOut extends "unset" ? T : TOutputOut };
12+
>query : Symbol(Builder.query, Decl(router.ts, 1, 31))
13+
>T : Symbol(T, Decl(router.ts, 2, 10))
14+
>resolver : Symbol(resolver, Decl(router.ts, 2, 13))
15+
>T : Symbol(T, Decl(router.ts, 2, 10))
16+
>output : Symbol(output, Decl(router.ts, 2, 34))
17+
>TOutputOut : Symbol(TOutputOut, Decl(router.ts, 1, 18))
18+
>T : Symbol(T, Decl(router.ts, 2, 10))
19+
>TOutputOut : Symbol(TOutputOut, Decl(router.ts, 1, 18))
20+
}
21+
22+
declare const builder: Builder<"unset">;
23+
>builder : Symbol(builder, Decl(router.ts, 5, 13))
24+
>Builder : Symbol(Builder, Decl(router.ts, 0, 36))
25+
26+
export const query = builder.query((): State | null => null);
27+
>query : Symbol(query, Decl(router.ts, 7, 12))
28+
>builder.query : Symbol(Builder.query, Decl(router.ts, 1, 31))
29+
>builder : Symbol(builder, Decl(router.ts, 5, 13))
30+
>query : Symbol(Builder.query, Decl(router.ts, 1, 31))
31+
>State : Symbol(State, Decl(router.ts, 0, 8))
32+
33+
export const anyQuery = builder.query((): any => null);
34+
>anyQuery : Symbol(anyQuery, Decl(router.ts, 8, 12))
35+
>builder.query : Symbol(Builder.query, Decl(router.ts, 1, 31))
36+
>builder : Symbol(builder, Decl(router.ts, 5, 13))
37+
>query : Symbol(Builder.query, Decl(router.ts, 1, 31))
38+
39+
export const typedQuery = builder.query((): string | null => null);
40+
>typedQuery : Symbol(typedQuery, Decl(router.ts, 9, 12))
41+
>builder.query : Symbol(Builder.query, Decl(router.ts, 1, 31))
42+
>builder : Symbol(builder, Decl(router.ts, 5, 13))
43+
>query : Symbol(Builder.query, Decl(router.ts, 1, 31))
44+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/declarationEmitUninstantiatedConditionalTypeParameters.ts] ////
2+
3+
=== router.ts ===
4+
import { State } from "./contracts";
5+
>State : any
6+
7+
interface Builder<TOutputOut> {
8+
query<T>(resolver: () => T): { output: TOutputOut extends "unset" ? T : TOutputOut };
9+
>query : <T>(resolver: () => T) => { output: TOutputOut extends "unset" ? T : TOutputOut; }
10+
>resolver : () => T
11+
>output : TOutputOut extends "unset" ? T : TOutputOut
12+
}
13+
14+
declare const builder: Builder<"unset">;
15+
>builder : Builder<"unset">
16+
17+
export const query = builder.query((): State | null => null);
18+
>query : { output: any; }
19+
>builder.query((): State | null => null) : { output: any; }
20+
>builder.query : <T>(resolver: () => T) => { output: T; }
21+
>builder : Builder<"unset">
22+
>query : <T>(resolver: () => T) => { output: T; }
23+
>(): State | null => null : () => State | null
24+
25+
export const anyQuery = builder.query((): any => null);
26+
>anyQuery : { output: any; }
27+
>builder.query((): any => null) : { output: any; }
28+
>builder.query : <T>(resolver: () => T) => { output: T; }
29+
>builder : Builder<"unset">
30+
>query : <T>(resolver: () => T) => { output: T; }
31+
>(): any => null : () => any
32+
33+
export const typedQuery = builder.query((): string | null => null);
34+
>typedQuery : { output: string | null; }
35+
>builder.query((): string | null => null) : { output: string | null; }
36+
>builder.query : <T>(resolver: () => T) => { output: T; }
37+
>builder : Builder<"unset">
38+
>query : <T>(resolver: () => T) => { output: T; }
39+
>(): string | null => null : () => string | null
40+

‎tsc/testdata/baselines/reference/compiler/declarationsWithRecursiveInternalTypesProduceUniqueTypeParams.types‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎tsc/testdata/baselines/reference/compiler/es2018ObjectAssign.types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare const p: Promise<number>;
1717

1818
p.finally();
1919
>p.finally() : Promise<number>
20-
>p.finally : (onfinally?: (() => void) | undefined | null) => Promise<number>
20+
>p.finally : (onfinally?: (() => void) | null | undefined) => Promise<number>
2121
>p : Promise<number>
22-
>finally : (onfinally?: (() => void) | undefined | null) => Promise<number>
22+
>finally : (onfinally?: (() => void) | null | undefined) => Promise<number>
2323

‎tsc/testdata/baselines/reference/conformance/parserRealSource14.types‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ namespace TypeScript {
172172

173173
this.asts.pop();
174174
>this.asts.pop() : TypeScript.AST
175-
>this.asts.pop : () => TypeScript.AST | undefined
175+
>this.asts.pop : () => TypeScript.AST
176176
>this.asts : TypeScript.AST[]
177177
>this : this
178178
>asts : TypeScript.AST[]
179-
>pop : () => TypeScript.AST | undefined
179+
>pop : () => TypeScript.AST
180180
}
181181
return head;
182182
>head : TypeScript.AST
@@ -201,11 +201,11 @@ namespace TypeScript {
201201

202202
this.asts.pop();
203203
>this.asts.pop() : TypeScript.AST
204-
>this.asts.pop : () => TypeScript.AST | undefined
204+
>this.asts.pop : () => TypeScript.AST
205205
>this.asts : TypeScript.AST[]
206206
>this : this
207207
>asts : TypeScript.AST[]
208-
>pop : () => TypeScript.AST | undefined
208+
>pop : () => TypeScript.AST
209209
}
210210
this.top = this.asts.length;
211211
>this.top = this.asts.length : number

‎tsc/testdata/baselines/reference/conformance/parserRealSource8.types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,11 +2178,11 @@ namespace TypeScript {
21782178

21792179
context.modDeclChain.pop();
21802180
>context.modDeclChain.pop() : any
2181-
>context.modDeclChain.pop : () => ModuleDeclaration | undefined
2181+
>context.modDeclChain.pop : () => any
21822182
>context.modDeclChain : ModuleDeclaration[]
21832183
>context : AssignScopeContext
21842184
>modDeclChain : ModuleDeclaration[]
2185-
>pop : () => ModuleDeclaration | undefined
2185+
>pop : () => any
21862186

21872187
if (context.modDeclChain.length >= 1) {
21882188
>context.modDeclChain.length >= 1 : boolean

‎tsc/testdata/baselines/reference/conformance/usePromiseFinally(target=es2015).types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
let promise1 = new Promise(function(resolve, reject) {})
55
>promise1 : Promise<unknown>
66
>new Promise(function(resolve, reject) {}) .finally(function() {}) : Promise<unknown>
7-
>new Promise(function(resolve, reject) {}) .finally : (onfinally?: (() => void) | undefined | null) => Promise<unknown>
7+
>new Promise(function(resolve, reject) {}) .finally : (onfinally?: (() => void) | null | undefined) => Promise<unknown>
88
>new Promise(function(resolve, reject) {}) : Promise<unknown>
99
>Promise : PromiseConstructor
1010
>function(resolve, reject) {} : (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void
1111
>resolve : (value: unknown) => void
1212
>reject : (reason?: any) => void
1313

1414
.finally(function() {});
15-
>finally : (onfinally?: (() => void) | undefined | null) => Promise<unknown>
15+
>finally : (onfinally?: (() => void) | null | undefined) => Promise<unknown>
1616
>function() {} : () => void
1717

0 commit comments

Comments
 (0)