Skip to content

Commit 5bf6e30

Browse files
authored
Use jsdoc aliases if visible when printing types (#24153)
* Use jsdoc aliases if visible when printing types * Modify implementation a bit, add test that aughta change in the new future * Accept baselines- hold off on typedef template lookup change
1 parent 15c09ef commit 5bf6e30

9 files changed

+243
-157
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,6 +4024,10 @@ namespace ts {
40244024

40254025
function determineIfDeclarationIsVisible() {
40264026
switch (node.kind) {
4027+
case SyntaxKind.JSDocTypedefTag:
4028+
// Top-level jsdoc typedefs are considered exported
4029+
// First parent is comment node, second is hosting declaration or token; we only care about those tokens or declarations whose parent is a source file
4030+
return !!(node.parent && node.parent.parent && node.parent.parent.parent && isSourceFile(node.parent.parent.parent));
40274031
case SyntaxKind.BindingElement:
40284032
return isDeclarationVisible(node.parent.parent);
40294033
case SyntaxKind.VariableDeclaration:
@@ -5163,8 +5167,8 @@ namespace ts {
51635167
let result: TypeParameter[];
51645168
for (const node of symbol.declarations) {
51655169
if (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.ClassDeclaration ||
5166-
node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.TypeAliasDeclaration) {
5167-
const declaration = <InterfaceDeclaration | TypeAliasDeclaration>node;
5170+
node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.TypeAliasDeclaration || node.kind === SyntaxKind.JSDocTypedefTag) {
5171+
const declaration = <InterfaceDeclaration | TypeAliasDeclaration | JSDocTypedefTag>node;
51685172
const typeParameters = getEffectiveTypeParameterDeclarations(declaration);
51695173
if (typeParameters) {
51705174
result = appendTypeParameters(result, typeParameters);
@@ -9046,7 +9050,7 @@ namespace ts {
90469050
}
90479051

90489052
function getAliasSymbolForTypeNode(node: TypeNode) {
9049-
return node.parent.kind === SyntaxKind.TypeAliasDeclaration ? getSymbolOfNode(node.parent) : undefined;
9053+
return (node.parent.kind === SyntaxKind.TypeAliasDeclaration || node.parent.kind === SyntaxKind.JSDocTypedefTag) ? getSymbolOfNode(node.parent) : undefined;
90509054
}
90519055

90529056
function getAliasTypeArgumentsForTypeNode(node: TypeNode) {

src/compiler/utilities.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,11 +3093,13 @@ namespace ts {
30933093
* Gets the effective type parameters. If the node was parsed in a
30943094
* JavaScript file, gets the type parameters from the `@template` tag from JSDoc.
30953095
*/
3096-
export function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters) {
3097-
return node.typeParameters || (isInJavaScriptFile(node) ? getJSDocTypeParameterDeclarations(node) : undefined);
3096+
export function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters | JSDocTypedefTag) {
3097+
return isJSDocTypedefTag(node)
3098+
? getJSDocTypeParameterDeclarations(node)
3099+
: node.typeParameters || (isInJavaScriptFile(node) ? getJSDocTypeParameterDeclarations(node) : undefined);
30983100
}
30993101

3100-
export function getJSDocTypeParameterDeclarations(node: DeclarationWithTypeParameters) {
3102+
export function getJSDocTypeParameterDeclarations(node: DeclarationWithTypeParameters | JSDocTypedefTag) {
31013103
const templateTag = getJSDocTemplateTag(node);
31023104
return templateTag && templateTag.typeParameters;
31033105
}

tests/baselines/reference/checkJsdocTypedefInParamTag1.types

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
* @param {Opts} opts
1111
*/
1212
function foo(opts) {
13-
>foo : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
14-
>opts : { x: string; y?: string; z?: string; w?: string; }
13+
>foo : (opts: Opts) => void
14+
>opts : Opts
1515

1616
opts.x;
1717
>opts.x : string
18-
>opts : { x: string; y?: string; z?: string; w?: string; }
18+
>opts : Opts
1919
>x : string
2020
}
2121

2222
foo({x: 'abc'});
2323
>foo({x: 'abc'}) : void
24-
>foo : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
24+
>foo : (opts: Opts) => void
2525
>{x: 'abc'} : { x: string; }
2626
>x : string
2727
>'abc' : "abc"
@@ -34,18 +34,18 @@ foo({x: 'abc'});
3434
* @param {AnotherOpts} opts
3535
*/
3636
function foo1(opts) {
37-
>foo1 : (opts: { anotherX: string; anotherY?: string; }) => void
38-
>opts : { anotherX: string; anotherY?: string; }
37+
>foo1 : (opts: AnotherOpts) => void
38+
>opts : AnotherOpts
3939

4040
opts.anotherX;
4141
>opts.anotherX : string
42-
>opts : { anotherX: string; anotherY?: string; }
42+
>opts : AnotherOpts
4343
>anotherX : string
4444
}
4545

4646
foo1({anotherX: "world"});
4747
>foo1({anotherX: "world"}) : void
48-
>foo1 : (opts: { anotherX: string; anotherY?: string; }) => void
48+
>foo1 : (opts: AnotherOpts) => void
4949
>{anotherX: "world"} : { anotherX: string; }
5050
>anotherX : string
5151
>"world" : "world"
@@ -60,17 +60,17 @@ foo1({anotherX: "world"});
6060
* @param {Opts1} opts
6161
*/
6262
function foo2(opts) {
63-
>foo2 : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
64-
>opts : { x: string; y?: string; z?: string; w?: string; }
63+
>foo2 : (opts: Opts1) => void
64+
>opts : Opts1
6565

6666
opts.x;
6767
>opts.x : string
68-
>opts : { x: string; y?: string; z?: string; w?: string; }
68+
>opts : Opts1
6969
>x : string
7070
}
7171
foo2({x: 'abc'});
7272
>foo2({x: 'abc'}) : void
73-
>foo2 : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
73+
>foo2 : (opts: Opts1) => void
7474
>{x: 'abc'} : { x: string; }
7575
>x : string
7676
>'abc' : "abc"

tests/baselines/reference/jsDocTypedef1.js

Lines changed: 2 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -41,140 +41,8 @@
4141
"kind": "space"
4242
},
4343
{
44-
"text": "{",
45-
"kind": "punctuation"
46-
},
47-
{
48-
"text": "\n",
49-
"kind": "lineBreak"
50-
},
51-
{
52-
"text": " ",
53-
"kind": "space"
54-
},
55-
{
56-
"text": "x",
57-
"kind": "propertyName"
58-
},
59-
{
60-
"text": ":",
61-
"kind": "punctuation"
62-
},
63-
{
64-
"text": " ",
65-
"kind": "space"
66-
},
67-
{
68-
"text": "string",
69-
"kind": "keyword"
70-
},
71-
{
72-
"text": ";",
73-
"kind": "punctuation"
74-
},
75-
{
76-
"text": "\n",
77-
"kind": "lineBreak"
78-
},
79-
{
80-
"text": " ",
81-
"kind": "space"
82-
},
83-
{
84-
"text": "y",
85-
"kind": "propertyName"
86-
},
87-
{
88-
"text": "?",
89-
"kind": "punctuation"
90-
},
91-
{
92-
"text": ":",
93-
"kind": "punctuation"
94-
},
95-
{
96-
"text": " ",
97-
"kind": "space"
98-
},
99-
{
100-
"text": "string",
101-
"kind": "keyword"
102-
},
103-
{
104-
"text": ";",
105-
"kind": "punctuation"
106-
},
107-
{
108-
"text": "\n",
109-
"kind": "lineBreak"
110-
},
111-
{
112-
"text": " ",
113-
"kind": "space"
114-
},
115-
{
116-
"text": "z",
117-
"kind": "propertyName"
118-
},
119-
{
120-
"text": "?",
121-
"kind": "punctuation"
122-
},
123-
{
124-
"text": ":",
125-
"kind": "punctuation"
126-
},
127-
{
128-
"text": " ",
129-
"kind": "space"
130-
},
131-
{
132-
"text": "string",
133-
"kind": "keyword"
134-
},
135-
{
136-
"text": ";",
137-
"kind": "punctuation"
138-
},
139-
{
140-
"text": "\n",
141-
"kind": "lineBreak"
142-
},
143-
{
144-
"text": " ",
145-
"kind": "space"
146-
},
147-
{
148-
"text": "w",
149-
"kind": "propertyName"
150-
},
151-
{
152-
"text": "?",
153-
"kind": "punctuation"
154-
},
155-
{
156-
"text": ":",
157-
"kind": "punctuation"
158-
},
159-
{
160-
"text": " ",
161-
"kind": "space"
162-
},
163-
{
164-
"text": "string",
165-
"kind": "keyword"
166-
},
167-
{
168-
"text": ";",
169-
"kind": "punctuation"
170-
},
171-
{
172-
"text": "\n",
173-
"kind": "lineBreak"
174-
},
175-
{
176-
"text": "}",
177-
"kind": "punctuation"
44+
"text": "Opts",
45+
"kind": "aliasName"
17846
}
17947
],
18048
"documentation": [],

tests/baselines/reference/jsdocTemplateConstructorFunction2.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ z.u = false
7979
*/
8080
/** @type {A} */
8181
const options = { value: null };
82-
>options : { value: any; }
82+
>options : A
8383
>{ value: null } : { value: null; }
8484
>value : null
8585
>null : null

tests/baselines/reference/jsdocTypedefMissingType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const t = 0;
1414

1515
/** @type Person */
1616
const person = { name: "" };
17-
>person : { name: string; }
17+
>person : Person
1818
>{ name: "" } : { name: string; }
1919
>name : string
2020
>"" : ""

tests/baselines/reference/jsdocTypedef_propertyWithNoType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/** @type {Foo} */
88
const x = { foo: 0 };
9-
>x : { foo: any; }
9+
>x : Foo
1010
>{ foo: 0 } : { foo: number; }
1111
>foo : number
1212
>0 : 0

tests/baselines/reference/typedefTagNested.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var ex;
1010

1111
/** @type {App} */
1212
const app = {
13-
>app : { name: string; icons: { image32: string; image64: string; }; }
13+
>app : App
1414
>{ name: 'name', icons: { image32: 'x.png', image64: 'y.png', }} : { name: string; icons: { image32: string; image64: string; }; }
1515

1616
name: 'name',
@@ -40,5 +40,5 @@ const app = {
4040

4141
/** @type {Opp} */
4242
var mistake;
43-
>mistake : { name: string; oops: { horrible: string; }; }
43+
>mistake : Opp
4444

0 commit comments

Comments
 (0)