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)`.
This commit is contained in:
Nathan Shively-Sanders
2022-10-04 09:15:39 -07:00
parent 71c990e67b
commit aaa32402f8
10 changed files with 64 additions and 204 deletions

View File

@@ -311,17 +311,21 @@ namespace ts.SymbolDisplay {
}
if (symbolFlags & SymbolFlags.Class && !hasAddedSymbolInfo && !isThisExpression) {
addAliasPrefixIfNecessary();
if (getDeclarationOfKind(symbol, SyntaxKind.ClassExpression)) {
// Special case for class expressions because we would like to indicate that
if (getDeclarationOfKind(symbol, SyntaxKind.ClassExpression) && !alias) {
// Special case for named class expressions because we would like to indicate that
// the class name is local to the class body (similar to function expression)
// (local class) class <className>
pushSymbolKind(ScriptElementKind.localClassElement);
// anonymous class expressions should only print as "(Anonymous class)"
if (symbol.name !== InternalSymbolName.Class) {
pushSymbolKind(ScriptElementKind.localClassElement);
displayParts.push(spacePart());
}
}
else {
// Class declaration has name which is not local.
displayParts.push(keywordPart(SyntaxKind.ClassKeyword));
displayParts.push(spacePart());
}
displayParts.push(spacePart());
addFullSymbolName(symbol);
writeTypeParametersOfSymbol(symbol, sourceFile);
}

View File

@@ -77,7 +77,7 @@
"containerName": "",
"fileName": "/b.ts",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -100,16 +100,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -273,7 +265,7 @@
"containerName": "",
"fileName": "/b.ts",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -296,16 +288,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -406,7 +390,7 @@
"containerName": "",
"fileName": "/b.ts",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -429,16 +413,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -604,7 +580,7 @@
"containerName": "",
"fileName": "/b.ts",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -627,16 +603,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -66,7 +66,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -89,16 +89,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -199,7 +191,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -222,16 +214,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -386,7 +370,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A = require(\"./a\")",
"name": "(alias) class A\nimport A = require(\"./a\")",
"textSpan": {
"start": 7,
"length": 1
@@ -409,16 +393,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -12,7 +12,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 8,
"length": 1
@@ -35,16 +35,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -98,7 +90,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 9,
"length": 1
@@ -121,16 +113,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -203,7 +187,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 9,
"length": 1
@@ -226,16 +210,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -298,7 +274,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 8,
"length": 1
@@ -321,16 +297,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -394,7 +362,7 @@
"containerName": "",
"fileName": "/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 9,
"length": 1
@@ -417,16 +385,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -487,7 +447,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 8,
"length": 1
@@ -510,16 +470,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -85,7 +85,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) export=\nimport export=",
"name": "(alias) class export=\nimport export=",
"textSpan": {
"start": 0,
"length": 27
@@ -108,16 +108,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -15,7 +15,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) D\nimport D",
"name": "(alias) class D\nimport D",
"textSpan": {
"start": 44,
"length": 1
@@ -38,16 +38,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -85,7 +85,7 @@
"containerName": "",
"fileName": "/a.js",
"kind": "alias",
"name": "(alias) (local class) D\nimport D",
"name": "(alias) class D\nimport D",
"textSpan": {
"start": 44,
"length": 1
@@ -108,16 +108,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -65,7 +65,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 6,
"length": 1
@@ -88,16 +88,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",
@@ -157,7 +149,7 @@
"containerName": "",
"fileName": "/tests/cases/fourslash/b.js",
"kind": "alias",
"name": "(alias) (local class) A\nimport A",
"name": "(alias) class A\nimport A",
"textSpan": {
"start": 6,
"length": 1
@@ -180,16 +172,8 @@
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "local class",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
"text": "class",
"kind": "keyword"
},
{
"text": " ",

View File

@@ -19,7 +19,7 @@
//// let x7: (new() => Foo) = [|class { hello () { /**constructorType*/} }|];
//// let x8: Foo[] = [|[{ hello () { /**arrayType*/} }]|];
//// let x9: { y: Foo } = [|{ y: { hello () { /**typeLiteral*/} } }|];
//// let x10 = [ [|{|"parts": ["(","local class",")"," ", "(Anonymous class)"], "kind": "local class"|}class implements Foo { hello() {} }|] ]
//// let x10 = [ [|{|"parts": ["(Anonymous class)"], "kind": "local class"|}class implements Foo { hello() {} }|] ]
//// let x11 = class [|{|"parts": ["(","local class",")"," ","C"], "kind": "local class"|}C|] implements Foo { hello() {} }
////
//// // Should not do anything for type predicates

View File

@@ -3,6 +3,6 @@
////[1].forEach(cla/*1*/ss {});
////[1].forEach(cla/*2*/ss OK{});
verify.quickInfoAt("1", "(local class) (Anonymous class)");
verify.quickInfoAt("1", "(Anonymous class)");
verify.quickInfoAt("2", "(local class) OK");