Skip to content

Commit cbde861

Browse files
author
Andy
authored
Improve use of SemanticMeaning in symbol display (#26953)
1 parent b1430e5 commit cbde861

13 files changed

+44
-25
lines changed

src/services/symbolDisplay.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,18 @@ namespace ts.SymbolDisplay {
128128
let documentation: SymbolDisplayPart[] | undefined;
129129
let tags: JSDocTagInfo[] | undefined;
130130
const symbolFlags = getCombinedLocalAndExportSymbolFlags(symbol);
131-
let symbolKind = getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(typeChecker, symbol, location);
131+
let symbolKind = semanticMeaning & SemanticMeaning.Value ? getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(typeChecker, symbol, location) : ScriptElementKind.unknown;
132132
let hasAddedSymbolInfo = false;
133-
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isExpression(location);
133+
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isInExpressionContext(location);
134134
let type: Type | undefined;
135135
let printer: Printer;
136136
let documentationFromAlias: SymbolDisplayPart[] | undefined;
137137
let tagsFromAlias: JSDocTagInfo[] | undefined;
138138

139+
if (location.kind === SyntaxKind.ThisKeyword && !isThisExpression) {
140+
return { displayParts: [keywordPart(SyntaxKind.ThisKeyword)], documentation: [], symbolKind: ScriptElementKind.primitiveType, tags: undefined };
141+
}
142+
139143
// Class at constructor site need to be shown as constructor apart from property,method, vars
140144
if (symbolKind !== ScriptElementKind.unknown || symbolFlags & SymbolFlags.Class || symbolFlags & SymbolFlags.Alias) {
141145
// If it is accessor they are allowed only if location is at name of the accessor
@@ -285,7 +289,7 @@ namespace ts.SymbolDisplay {
285289
addFullSymbolName(symbol);
286290
writeTypeParametersOfSymbol(symbol, sourceFile);
287291
}
288-
if (symbolFlags & SymbolFlags.TypeAlias) {
292+
if ((symbolFlags & SymbolFlags.TypeAlias) && (semanticMeaning & SemanticMeaning.Type)) {
289293
prefixNextMeaning();
290294
displayParts.push(keywordPart(SyntaxKind.TypeKeyword));
291295
displayParts.push(spacePart());

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts {
8888
if (node.kind === SyntaxKind.SourceFile) {
8989
return SemanticMeaning.Value;
9090
}
91-
else if (node.parent.kind === SyntaxKind.ExportAssignment) {
91+
else if (node.parent.kind === SyntaxKind.ExportAssignment || node.parent.kind === SyntaxKind.ExternalModuleReference) {
9292
return SemanticMeaning.All;
9393
}
9494
else if (isInRightSideOfInternalImportEqualsDeclaration(node)) {

tests/cases/fourslash/findAllRefsForDefaultExport02.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
const ranges = test.ranges();
1616
const [r0, r1, r2, r3, r4] = ranges;
1717
const fnRanges = [r0, r1, r2, r3];
18-
const fn = "function DefaultExportedFunction(): () => typeof DefaultExportedFunction";
19-
verify.singleReferenceGroup(fn, fnRanges);
18+
verify.singleReferenceGroup("function DefaultExportedFunction(): () => typeof DefaultExportedFunction", fnRanges);
2019

2120
// The namespace and function do not merge,
2221
// so the namespace should be all alone.
23-
verify.singleReferenceGroup(`namespace DefaultExportedFunction\n${fn}`, [r4]);
22+
verify.singleReferenceGroup(`namespace DefaultExportedFunction`, [r4]);

tests/cases/fourslash/findAllRefs_importType_exportEquals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ verify.noErrors();
1515

1616
const [r0, r1, r2, r3, r4] = test.ranges();
1717
verify.referenceGroups(r0, [{ definition: "type T = number\nnamespace T", ranges: [r0, r2, r3] }]);
18-
verify.referenceGroups(r1, [{ definition: "type T = number\nnamespace T", ranges: [r1, r2] }]);
18+
verify.referenceGroups(r1, [{ definition: "namespace T", ranges: [r1, r2] }]);
1919
verify.referenceGroups(r2, [{ definition: "type T = number\nnamespace T", ranges: [r0, r1, r2, r3] }]);
2020
verify.referenceGroups([r3, r4], [
2121
{ definition: 'module "/a"', ranges: [r4] },

tests/cases/fourslash/findAllRefs_importType_meaningAtLocation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
////const x: typeof import("./a").[|T|] = 0;
1010

1111
const [r0, r1, r2, r3] = test.ranges();
12-
verify.singleReferenceGroup("type T = 0\nconst T: 0", [r0, r2]);
13-
verify.singleReferenceGroup("type T = 0\nconst T: 0", [r1, r3]);
12+
verify.singleReferenceGroup("type T = 0", [r0, r2]);
13+
verify.singleReferenceGroup("const T: 0", [r1, r3]);

tests/cases/fourslash/jsdocTypedefTagSemanticMeaning0.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
const [t0, v0, t1, v1] = test.ranges();
1414

15-
verify.singleReferenceGroup("type T = number\nconst T: 1", [t0, t1]);
16-
verify.singleReferenceGroup("type T = number\nconst T: 1", [v0, v1]);
15+
verify.singleReferenceGroup("type T = number", [t0, t1]);
16+
verify.singleReferenceGroup("const T: 1", [v0, v1]);

tests/cases/fourslash/quickInfoImportedTypesWithMergedMeanings.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
//// export { Original/*1*/ } from './quickInfoImportedTypesWithMergedMeanings';
1111

1212
// @Filename: importer.ts
13-
//// import { Original as Alias } from './quickInfoImportedTypesWithMergedMeanings';
14-
//// Alias/*2*/;
13+
//// import { Original as /*2*/Alias } from './quickInfoImportedTypesWithMergedMeanings';
14+
//// Alias/*3*/;
15+
//// let x: Alias/*4*/
1516

1617
verify.quickInfoAt("1", [
1718
"(alias) function Original(): void",
@@ -26,3 +27,15 @@ verify.quickInfoAt("2", [
2627
"(alias) namespace Alias",
2728
"import Alias",
2829
].join("\n"), "some docs");
30+
31+
verify.quickInfoAt("3", [
32+
"(alias) function Alias(): void",
33+
"(alias) namespace Alias",
34+
"import Alias",
35+
].join("\n"), "some docs");
36+
37+
verify.quickInfoAt("4", [
38+
"(alias) type Alias<T> = () => T",
39+
"(alias) namespace Alias",
40+
"import Alias",
41+
].join("\n"), "some docs");

tests/cases/fourslash/quickInfoJsdocEnum.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
//// A: 0,
1313
////}
1414
////
15-
/////** @type {/**/E} */
16-
////const x = E.A;
15+
/////** @type {/*type*/E} */
16+
////const x = /*value*/E.A;
1717

1818
verify.noErrors();
1919

20-
verify.quickInfoAt("",
20+
verify.quickInfoAt("type",
21+
`enum E`,
22+
"Doc");
23+
verify.quickInfoAt("value",
2124
`enum E
2225
const E: {
2326
A: number;

tests/cases/fourslash/quickInfoOnThis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
////}
2424

2525
verify.quickInfos({
26-
0: "this: this",
26+
0: "this",
2727
1: "this: void",
2828
2: "this: this",
2929
3: "(parameter) this: Restricted",

tests/cases/fourslash/referencesForMergedDeclarations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
////[|Foo|].bind(this);
1616

1717
const [type1, namespace1, value1, namespace2, type2, value2] = test.ranges();
18-
verify.singleReferenceGroup("interface Foo\nnamespace Foo\nfunction Foo(): void", [type1, type2]);
19-
verify.singleReferenceGroup("namespace Foo\nfunction Foo(): void", [namespace1, namespace2]);
18+
verify.singleReferenceGroup("interface Foo\nnamespace Foo", [type1, type2]);
19+
verify.singleReferenceGroup("namespace Foo", [namespace1, namespace2]);
2020
verify.singleReferenceGroup("namespace Foo\nfunction Foo(): void", [value1, value2]);

tests/cases/fourslash/referencesForMergedDeclarations5.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const ranges = test.ranges();
1010
const [r0, r1, r2, r3] = ranges;
11-
verify.referenceGroups(r0, [{ definition: "interface Foo\nnamespace Foo\nfunction Foo(): void", ranges: [r0, r3] }]);
12-
verify.referenceGroups(r1, [{ definition: "namespace Foo\nfunction Foo(): void", ranges: [r1, r3] }]);
11+
verify.referenceGroups(r0, [{ definition: "interface Foo\nnamespace Foo", ranges: [r0, r3] }]);
12+
verify.referenceGroups(r1, [{ definition: "namespace Foo", ranges: [r1, r3] }]);
1313
verify.referenceGroups(r2, [{ definition: "namespace Foo\nfunction Foo(): void", ranges: [r2, r3] }]);
1414
verify.referenceGroups(r3, [{ definition: "interface Foo\nnamespace Foo\nfunction Foo(): void", ranges }]);

tests/cases/fourslash/referencesForMergedDeclarations7.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
const ranges = test.ranges();
1414
const [r0, r1, r2, r3] = ranges;
15-
verify.referenceGroups(r0, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r0, r3] }]);
16-
verify.referenceGroups(r1, [{ definition: "namespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r1, r3] }]);
15+
verify.referenceGroups(r0, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar", ranges: [r0, r3] }]);
16+
verify.referenceGroups(r1, [{ definition: "namespace Foo.Bar", ranges: [r1, r3] }]);
1717
verify.referenceGroups(r2, [{ definition: "namespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r2, r3] }]);
1818
verify.referenceGroups(r3, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar\nfunction Foo.Bar(): void", ranges }]);

tests/cases/fourslash/referencesForMergedDeclarations8.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
////// module
1111
////import a3 = Foo.[|Bar|].Baz;
1212

13-
verify.singleReferenceGroup("namespace Foo.Bar\nfunction Foo.Bar(): void");
13+
verify.singleReferenceGroup("namespace Foo.Bar");

0 commit comments

Comments
 (0)