Skip to content

Commit aaa3240

Browse files
committed
Improve class expression display
Only display `(local class)` if the class expression is named and has no alias. Other named classes use the normal `class C` format; anonymous classes display `(Anonymous class)`.
1 parent 71c990e commit aaa3240

10 files changed

Lines changed: 64 additions & 204 deletions

src/services/symbolDisplay.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,17 +311,21 @@ namespace ts.SymbolDisplay {
311311
}
312312
if (symbolFlags & SymbolFlags.Class && !hasAddedSymbolInfo && !isThisExpression) {
313313
addAliasPrefixIfNecessary();
314-
if (getDeclarationOfKind(symbol, SyntaxKind.ClassExpression)) {
315-
// Special case for class expressions because we would like to indicate that
314+
if (getDeclarationOfKind(symbol, SyntaxKind.ClassExpression) && !alias) {
315+
// Special case for named class expressions because we would like to indicate that
316316
// the class name is local to the class body (similar to function expression)
317317
// (local class) class <className>
318-
pushSymbolKind(ScriptElementKind.localClassElement);
318+
// anonymous class expressions should only print as "(Anonymous class)"
319+
if (symbol.name !== InternalSymbolName.Class) {
320+
pushSymbolKind(ScriptElementKind.localClassElement);
321+
displayParts.push(spacePart());
322+
}
319323
}
320324
else {
321325
// Class declaration has name which is not local.
322326
displayParts.push(keywordPart(SyntaxKind.ClassKeyword));
327+
displayParts.push(spacePart());
323328
}
324-
displayParts.push(spacePart());
325329
addFullSymbolName(symbol);
326330
writeTypeParametersOfSymbol(symbol, sourceFile);
327331
}

tests/baselines/reference/findAllRefsClassExpression0.baseline.jsonc

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"containerName": "",
7878
"fileName": "/b.ts",
7979
"kind": "alias",
80-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
80+
"name": "(alias) class A\nimport A = require(\"./a\")",
8181
"textSpan": {
8282
"start": 7,
8383
"length": 1
@@ -100,16 +100,8 @@
100100
"kind": "space"
101101
},
102102
{
103-
"text": "(",
104-
"kind": "punctuation"
105-
},
106-
{
107-
"text": "local class",
108-
"kind": "text"
109-
},
110-
{
111-
"text": ")",
112-
"kind": "punctuation"
103+
"text": "class",
104+
"kind": "keyword"
113105
},
114106
{
115107
"text": " ",
@@ -273,7 +265,7 @@
273265
"containerName": "",
274266
"fileName": "/b.ts",
275267
"kind": "alias",
276-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
268+
"name": "(alias) class A\nimport A = require(\"./a\")",
277269
"textSpan": {
278270
"start": 7,
279271
"length": 1
@@ -296,16 +288,8 @@
296288
"kind": "space"
297289
},
298290
{
299-
"text": "(",
300-
"kind": "punctuation"
301-
},
302-
{
303-
"text": "local class",
304-
"kind": "text"
305-
},
306-
{
307-
"text": ")",
308-
"kind": "punctuation"
291+
"text": "class",
292+
"kind": "keyword"
309293
},
310294
{
311295
"text": " ",
@@ -406,7 +390,7 @@
406390
"containerName": "",
407391
"fileName": "/b.ts",
408392
"kind": "alias",
409-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
393+
"name": "(alias) class A\nimport A = require(\"./a\")",
410394
"textSpan": {
411395
"start": 7,
412396
"length": 1
@@ -429,16 +413,8 @@
429413
"kind": "space"
430414
},
431415
{
432-
"text": "(",
433-
"kind": "punctuation"
434-
},
435-
{
436-
"text": "local class",
437-
"kind": "text"
438-
},
439-
{
440-
"text": ")",
441-
"kind": "punctuation"
416+
"text": "class",
417+
"kind": "keyword"
442418
},
443419
{
444420
"text": " ",
@@ -604,7 +580,7 @@
604580
"containerName": "",
605581
"fileName": "/b.ts",
606582
"kind": "alias",
607-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
583+
"name": "(alias) class A\nimport A = require(\"./a\")",
608584
"textSpan": {
609585
"start": 7,
610586
"length": 1
@@ -627,16 +603,8 @@
627603
"kind": "space"
628604
},
629605
{
630-
"text": "(",
631-
"kind": "punctuation"
632-
},
633-
{
634-
"text": "local class",
635-
"kind": "text"
636-
},
637-
{
638-
"text": ")",
639-
"kind": "punctuation"
606+
"text": "class",
607+
"kind": "keyword"
640608
},
641609
{
642610
"text": " ",

tests/baselines/reference/findAllRefsClassExpression1.baseline.jsonc

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"containerName": "",
6767
"fileName": "/b.js",
6868
"kind": "alias",
69-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
69+
"name": "(alias) class A\nimport A = require(\"./a\")",
7070
"textSpan": {
7171
"start": 7,
7272
"length": 1
@@ -89,16 +89,8 @@
8989
"kind": "space"
9090
},
9191
{
92-
"text": "(",
93-
"kind": "punctuation"
94-
},
95-
{
96-
"text": "local class",
97-
"kind": "text"
98-
},
99-
{
100-
"text": ")",
101-
"kind": "punctuation"
92+
"text": "class",
93+
"kind": "keyword"
10294
},
10395
{
10496
"text": " ",
@@ -199,7 +191,7 @@
199191
"containerName": "",
200192
"fileName": "/b.js",
201193
"kind": "alias",
202-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
194+
"name": "(alias) class A\nimport A = require(\"./a\")",
203195
"textSpan": {
204196
"start": 7,
205197
"length": 1
@@ -222,16 +214,8 @@
222214
"kind": "space"
223215
},
224216
{
225-
"text": "(",
226-
"kind": "punctuation"
227-
},
228-
{
229-
"text": "local class",
230-
"kind": "text"
231-
},
232-
{
233-
"text": ")",
234-
"kind": "punctuation"
217+
"text": "class",
218+
"kind": "keyword"
235219
},
236220
{
237221
"text": " ",
@@ -386,7 +370,7 @@
386370
"containerName": "",
387371
"fileName": "/b.js",
388372
"kind": "alias",
389-
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
373+
"name": "(alias) class A\nimport A = require(\"./a\")",
390374
"textSpan": {
391375
"start": 7,
392376
"length": 1
@@ -409,16 +393,8 @@
409393
"kind": "space"
410394
},
411395
{
412-
"text": "(",
413-
"kind": "punctuation"
414-
},
415-
{
416-
"text": "local class",
417-
"kind": "text"
418-
},
419-
{
420-
"text": ")",
421-
"kind": "punctuation"
396+
"text": "class",
397+
"kind": "keyword"
422398
},
423399
{
424400
"text": " ",

tests/baselines/reference/findAllRefsClassExpression2.baseline.jsonc

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"containerName": "",
1313
"fileName": "/a.js",
1414
"kind": "alias",
15-
"name": "(alias) (local class) A\nimport A",
15+
"name": "(alias) class A\nimport A",
1616
"textSpan": {
1717
"start": 8,
1818
"length": 1
@@ -35,16 +35,8 @@
3535
"kind": "space"
3636
},
3737
{
38-
"text": "(",
39-
"kind": "punctuation"
40-
},
41-
{
42-
"text": "local class",
43-
"kind": "text"
44-
},
45-
{
46-
"text": ")",
47-
"kind": "punctuation"
38+
"text": "class",
39+
"kind": "keyword"
4840
},
4941
{
5042
"text": " ",
@@ -98,7 +90,7 @@
9890
"containerName": "",
9991
"fileName": "/b.js",
10092
"kind": "alias",
101-
"name": "(alias) (local class) A\nimport A",
93+
"name": "(alias) class A\nimport A",
10294
"textSpan": {
10395
"start": 9,
10496
"length": 1
@@ -121,16 +113,8 @@
121113
"kind": "space"
122114
},
123115
{
124-
"text": "(",
125-
"kind": "punctuation"
126-
},
127-
{
128-
"text": "local class",
129-
"kind": "text"
130-
},
131-
{
132-
"text": ")",
133-
"kind": "punctuation"
116+
"text": "class",
117+
"kind": "keyword"
134118
},
135119
{
136120
"text": " ",
@@ -203,7 +187,7 @@
203187
"containerName": "",
204188
"fileName": "/b.js",
205189
"kind": "alias",
206-
"name": "(alias) (local class) A\nimport A",
190+
"name": "(alias) class A\nimport A",
207191
"textSpan": {
208192
"start": 9,
209193
"length": 1
@@ -226,16 +210,8 @@
226210
"kind": "space"
227211
},
228212
{
229-
"text": "(",
230-
"kind": "punctuation"
231-
},
232-
{
233-
"text": "local class",
234-
"kind": "text"
235-
},
236-
{
237-
"text": ")",
238-
"kind": "punctuation"
213+
"text": "class",
214+
"kind": "keyword"
239215
},
240216
{
241217
"text": " ",
@@ -298,7 +274,7 @@
298274
"containerName": "",
299275
"fileName": "/a.js",
300276
"kind": "alias",
301-
"name": "(alias) (local class) A\nimport A",
277+
"name": "(alias) class A\nimport A",
302278
"textSpan": {
303279
"start": 8,
304280
"length": 1
@@ -321,16 +297,8 @@
321297
"kind": "space"
322298
},
323299
{
324-
"text": "(",
325-
"kind": "punctuation"
326-
},
327-
{
328-
"text": "local class",
329-
"kind": "text"
330-
},
331-
{
332-
"text": ")",
333-
"kind": "punctuation"
300+
"text": "class",
301+
"kind": "keyword"
334302
},
335303
{
336304
"text": " ",
@@ -394,7 +362,7 @@
394362
"containerName": "",
395363
"fileName": "/b.js",
396364
"kind": "alias",
397-
"name": "(alias) (local class) A\nimport A",
365+
"name": "(alias) class A\nimport A",
398366
"textSpan": {
399367
"start": 9,
400368
"length": 1
@@ -417,16 +385,8 @@
417385
"kind": "space"
418386
},
419387
{
420-
"text": "(",
421-
"kind": "punctuation"
422-
},
423-
{
424-
"text": "local class",
425-
"kind": "text"
426-
},
427-
{
428-
"text": ")",
429-
"kind": "punctuation"
388+
"text": "class",
389+
"kind": "keyword"
430390
},
431391
{
432392
"text": " ",
@@ -487,7 +447,7 @@
487447
"containerName": "",
488448
"fileName": "/a.js",
489449
"kind": "alias",
490-
"name": "(alias) (local class) A\nimport A",
450+
"name": "(alias) class A\nimport A",
491451
"textSpan": {
492452
"start": 8,
493453
"length": 1
@@ -510,16 +470,8 @@
510470
"kind": "space"
511471
},
512472
{
513-
"text": "(",
514-
"kind": "punctuation"
515-
},
516-
{
517-
"text": "local class",
518-
"kind": "text"
519-
},
520-
{
521-
"text": ")",
522-
"kind": "punctuation"
473+
"text": "class",
474+
"kind": "keyword"
523475
},
524476
{
525477
"text": " ",

0 commit comments

Comments
 (0)