Skip to content

Commit 79a8514

Browse files
authored
Attempt to reuse type parameter constraint nodes (microsoft#58539)
1 parent e8274f7 commit 79a8514

File tree

705 files changed

+4654
-4609
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

705 files changed

+4654
-4609
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7459,8 +7459,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
74597459
return factory.createTypeParameterDeclaration(modifiers, name, constraintNode, defaultParameterNode);
74607460
}
74617461

7462+
function typeToTypeNodeHelperWithPossibleReusableTypeNode(type: Type, typeNode: TypeNode | undefined, context: NodeBuilderContext) {
7463+
return typeNode && tryReuseExistingNonParameterTypeNode(context, typeNode, type) || typeToTypeNodeHelper(type, context);
7464+
}
7465+
74627466
function typeParameterToDeclaration(type: TypeParameter, context: NodeBuilderContext, constraint = getConstraintOfTypeParameter(type)): TypeParameterDeclaration {
7463-
const constraintNode = constraint && typeToTypeNodeHelper(constraint, context);
7467+
const constraintNode = constraint && typeToTypeNodeHelperWithPossibleReusableTypeNode(constraint, getConstraintDeclaration(type), context);
74647468
return typeParameterToDeclarationWithConstraint(type, context, constraintNode);
74657469
}
74667470

@@ -8274,7 +8278,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
82748278
sym = resolveEntityName(leftmost, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
82758279
if (
82768280
context.enclosingDeclaration &&
8277-
(getNodeLinks(context.enclosingDeclaration).fakeScopeForSignatureDeclaration || !findAncestor(node, n => n === context.enclosingDeclaration)) &&
82788281
!(sym && sym.flags & SymbolFlags.TypeParameter)
82798282
) {
82808283
sym = getExportSymbolOfValueSymbolIfExported(sym);
@@ -8431,6 +8434,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84318434
}
84328435

84338436
function visitExistingNodeTreeSymbolsWorker(node: Node): Node | undefined {
8437+
if (isJSDocTypeExpression(node)) {
8438+
// Unwrap JSDocTypeExpressions
8439+
return visitNode(node.type, visitExistingNodeTreeSymbols, isTypeNode);
8440+
}
84348441
// We don't _actually_ support jsdoc namepath types, emit `any` instead
84358442
if (isJSDocAllType(node) || node.kind === SyntaxKind.JSDocNamepathType) {
84368443
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);

tests/baselines/reference/1.0lib-noErrors.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ interface Array<T> {
20812081
*/
20822082
concat<U extends T[]>(...items: U[]): T[];
20832083
>concat : { <U extends T[]>(...items: U[]): T[]; (...items: T[]): T[]; }
2084-
> : ^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^^ ^^^^^^ ^^ ^^^^^^^^^
2084+
> : ^^^ ^^^^^^^^^ ^^^^^ ^^ ^^^ ^^^^^^ ^^ ^^^^^^^^^
20852085
>items : U[]
20862086
> : ^^^
20872087

@@ -2091,7 +2091,7 @@ interface Array<T> {
20912091
*/
20922092
concat(...items: T[]): T[];
20932093
>concat : { <U extends T[]>(...items: U[]): T[]; (...items: T[]): T[]; }
2094-
> : ^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^ ^^ ^^^ ^^^
2094+
> : ^^^ ^^^^^^^^^ ^^^^^ ^^ ^^^^^^^^^^^^ ^^ ^^^ ^^^
20952095
>items : T[]
20962096
> : ^^^
20972097

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedAndNonExportedLocalVarsOfTheSameName.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module A {
2121

2222
export function mirror<T extends Point>(p: T) {
2323
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
24-
> : ^ ^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
>p : T
2626
> : ^
2727

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedLocalVarsOfTheSameName.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export module A {
2121

2222
export function mirror<T extends Point>(p: T) {
2323
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
24-
> : ^ ^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
>p : T
2626
> : ^
2727

tests/baselines/reference/TwoInternalModulesWithTheSameNameAndDifferentCommonRoot.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Root {
2525

2626
export function mirror<T extends Point>(p: T) {
2727
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
28-
> : ^ ^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929
>p : T
3030
> : ^
3131

tests/baselines/reference/TwoInternalModulesWithTheSameNameAndSameCommonRoot.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module A {
2121

2222
export function mirror<T extends Point>(p: T) {
2323
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
24-
> : ^ ^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
>p : T
2626
> : ^
2727

tests/baselines/reference/accessorsOverrideProperty8.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type AnyCtor<P extends object> = new (...a: any[]) => P
2222

2323
declare function classWithProperties<T extends { [key: string]: Types }, P extends object>(properties: T, klass: AnyCtor<P>): {
2424
>classWithProperties : <T extends { [key: string]: Types; }, P extends object>(properties: T, klass: AnyCtor<P>) => { new (): P & Properties<T>; prototype: P & Properties<T>; }
25-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^
25+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
2626
>key : string
2727
> : ^^^^^^
2828
>properties : T
@@ -43,7 +43,7 @@ const Base = classWithProperties({
4343
>classWithProperties({ get x() { return 'boolean' as const }, y: 'string',}, class Base {}) : { new (): Base & Properties<{ readonly x: "boolean"; y: "string"; }>; prototype: Base & Properties<{ readonly x: "boolean"; y: "string"; }>; }
4444
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545
>classWithProperties : <T extends { [key: string]: Types; }, P extends object>(properties: T, klass: AnyCtor<P>) => { new (): P & Properties<T>; prototype: P & Properties<T>; }
46-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
>{ get x() { return 'boolean' as const }, y: 'string',} : { readonly x: "boolean"; y: "string"; }
4848
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4949

tests/baselines/reference/accessorsOverrideProperty9.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface ApiItemContainerMixin extends ApiItem {
5050

5151
function ApiItemContainerMixin<TBaseClass extends IApiItemConstructor>(
5252
>ApiItemContainerMixin : <TBaseClass extends IApiItemConstructor>(baseClass: TBaseClass) => TBaseClass & (new (...args: any[]) => ApiItemContainerMixin)
53-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^
53+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
5454

5555
baseClass: TBaseClass
5656
>baseClass : TBaseClass
@@ -102,7 +102,7 @@ export class ApiEnum extends ApiItemContainerMixin(ApiItem) {
102102
>ApiItemContainerMixin(ApiItem) : ApiItem & ApiItemContainerMixin
103103
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104104
>ApiItemContainerMixin : <TBaseClass extends IApiItemConstructor>(baseClass: TBaseClass) => TBaseClass & (new (...args: any[]) => ApiItemContainerMixin)
105-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
106106
>ApiItem : typeof ApiItem
107107
> : ^^^^^^^^^^^^^^
108108

tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// test for #17069
55
function sum<T extends Record<K, number>, K extends string>(n: number, v: T, k: K) {
66
>sum : <T extends Record<K, number>, K extends string>(n: number, v: T, k: K) => void
7-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
7+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
88
>n : number
99
> : ^^^^^^
1010
>v : T
@@ -42,7 +42,7 @@ function sum<T extends Record<K, number>, K extends string>(n: number, v: T, k:
4242
}
4343
function realSum<T extends Record<K, number>, K extends string>(n: number, vs: T[], k: K) {
4444
>realSum : <T extends Record<K, number>, K extends string>(n: number, vs: T[], k: K) => void
45-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
45+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
4646
>n : number
4747
> : ^^^^^^
4848
>vs : T[]

tests/baselines/reference/aliasUsageInGenericFunction.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface IHasVisualizationModel {
2222
}
2323
function foo<T extends { a: IHasVisualizationModel }>(x: T) {
2424
>foo : <T extends { a: IHasVisualizationModel; }>(x: T) => T
25-
> : ^ ^^^^^^^^^^^^^^ ^^^^^ ^^ ^^^^^^
25+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^
2626
>a : IHasVisualizationModel
2727
> : ^^^^^^^^^^^^^^^^^^^^^^
2828
>x : T
@@ -38,7 +38,7 @@ var r = foo({ a: moduleA });
3838
>foo({ a: moduleA }) : { a: typeof moduleA; }
3939
> : ^^^^^^^^^^^^^^^^^^^^^^
4040
>foo : <T extends { a: IHasVisualizationModel; }>(x: T) => T
41-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^
41+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^
4242
>{ a: moduleA } : { a: typeof moduleA; }
4343
> : ^^^^^^^^^^^^^^^^^^^^^^
4444
>a : typeof moduleA
@@ -52,7 +52,7 @@ var r2 = foo({ a: <IHasVisualizationModel>null });
5252
>foo({ a: <IHasVisualizationModel>null }) : { a: IHasVisualizationModel; }
5353
> : ^^^^^ ^^^
5454
>foo : <T extends { a: IHasVisualizationModel; }>(x: T) => T
55-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^
55+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^
5656
>{ a: <IHasVisualizationModel>null } : { a: IHasVisualizationModel; }
5757
> : ^^^^^ ^^^
5858
>a : IHasVisualizationModel

0 commit comments

Comments
 (0)