From aaa32402f870526f2e65345ff897a0cd40acc036 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 4 Oct 2022 09:15:39 -0700 Subject: [PATCH] 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)`. --- src/services/symbolDisplay.ts | 12 ++- ...findAllRefsClassExpression0.baseline.jsonc | 56 +++---------- ...findAllRefsClassExpression1.baseline.jsonc | 42 ++-------- ...findAllRefsClassExpression2.baseline.jsonc | 84 ++++--------------- ...findAllRefs_importType_js.1.baseline.jsonc | 14 +--- ...findAllRefs_importType_js.2.baseline.jsonc | 14 +--- ...findAllRefs_importType_js.3.baseline.jsonc | 14 +--- .../renameJsExports02.baseline.jsonc | 28 ++----- .../goToImplementationInterface_07.ts | 2 +- .../cases/fourslash/quickInfoClassKeyword.ts | 2 +- 10 files changed, 64 insertions(+), 204 deletions(-) diff --git a/src/services/symbolDisplay.ts b/src/services/symbolDisplay.ts index 87b5e16f29d..2e179cd2dce 100644 --- a/src/services/symbolDisplay.ts +++ b/src/services/symbolDisplay.ts @@ -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 - 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); } diff --git a/tests/baselines/reference/findAllRefsClassExpression0.baseline.jsonc b/tests/baselines/reference/findAllRefsClassExpression0.baseline.jsonc index da95bf2df1a..f4ee0ab4ff2 100644 --- a/tests/baselines/reference/findAllRefsClassExpression0.baseline.jsonc +++ b/tests/baselines/reference/findAllRefsClassExpression0.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/findAllRefsClassExpression1.baseline.jsonc b/tests/baselines/reference/findAllRefsClassExpression1.baseline.jsonc index d38eae968ba..f316ca6ad20 100644 --- a/tests/baselines/reference/findAllRefsClassExpression1.baseline.jsonc +++ b/tests/baselines/reference/findAllRefsClassExpression1.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/findAllRefsClassExpression2.baseline.jsonc b/tests/baselines/reference/findAllRefsClassExpression2.baseline.jsonc index 9fd59673e70..bf332ebf688 100644 --- a/tests/baselines/reference/findAllRefsClassExpression2.baseline.jsonc +++ b/tests/baselines/reference/findAllRefsClassExpression2.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/findAllRefs_importType_js.1.baseline.jsonc b/tests/baselines/reference/findAllRefs_importType_js.1.baseline.jsonc index 194d24ed3ed..da70df34387 100644 --- a/tests/baselines/reference/findAllRefs_importType_js.1.baseline.jsonc +++ b/tests/baselines/reference/findAllRefs_importType_js.1.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/findAllRefs_importType_js.2.baseline.jsonc b/tests/baselines/reference/findAllRefs_importType_js.2.baseline.jsonc index 55e21a81063..d720838f126 100644 --- a/tests/baselines/reference/findAllRefs_importType_js.2.baseline.jsonc +++ b/tests/baselines/reference/findAllRefs_importType_js.2.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/findAllRefs_importType_js.3.baseline.jsonc b/tests/baselines/reference/findAllRefs_importType_js.3.baseline.jsonc index 26e86ead593..08ad2138390 100644 --- a/tests/baselines/reference/findAllRefs_importType_js.3.baseline.jsonc +++ b/tests/baselines/reference/findAllRefs_importType_js.3.baseline.jsonc @@ -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": " ", diff --git a/tests/baselines/reference/renameJsExports02.baseline.jsonc b/tests/baselines/reference/renameJsExports02.baseline.jsonc index 8fddd69fe46..209919b189f 100644 --- a/tests/baselines/reference/renameJsExports02.baseline.jsonc +++ b/tests/baselines/reference/renameJsExports02.baseline.jsonc @@ -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": " ", diff --git a/tests/cases/fourslash/goToImplementationInterface_07.ts b/tests/cases/fourslash/goToImplementationInterface_07.ts index d07d219683c..4161bf40ba4 100644 --- a/tests/cases/fourslash/goToImplementationInterface_07.ts +++ b/tests/cases/fourslash/goToImplementationInterface_07.ts @@ -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 diff --git a/tests/cases/fourslash/quickInfoClassKeyword.ts b/tests/cases/fourslash/quickInfoClassKeyword.ts index 8a132cf8be9..e13c4339c98 100644 --- a/tests/cases/fourslash/quickInfoClassKeyword.ts +++ b/tests/cases/fourslash/quickInfoClassKeyword.ts @@ -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");