Skip to content

Commit

Permalink
Improve use of SemanticMeaning in symbol display (microsoft#26953)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy authored Sep 7, 2018
1 parent b1430e5 commit cbde861
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 25 deletions.
10 changes: 7 additions & 3 deletions src/services/symbolDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,18 @@ namespace ts.SymbolDisplay {
let documentation: SymbolDisplayPart[] | undefined;
let tags: JSDocTagInfo[] | undefined;
const symbolFlags = getCombinedLocalAndExportSymbolFlags(symbol);
let symbolKind = getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(typeChecker, symbol, location);
let symbolKind = semanticMeaning & SemanticMeaning.Value ? getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(typeChecker, symbol, location) : ScriptElementKind.unknown;
let hasAddedSymbolInfo = false;
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isExpression(location);
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isInExpressionContext(location);
let type: Type | undefined;
let printer: Printer;
let documentationFromAlias: SymbolDisplayPart[] | undefined;
let tagsFromAlias: JSDocTagInfo[] | undefined;

if (location.kind === SyntaxKind.ThisKeyword && !isThisExpression) {
return { displayParts: [keywordPart(SyntaxKind.ThisKeyword)], documentation: [], symbolKind: ScriptElementKind.primitiveType, tags: undefined };
}

// Class at constructor site need to be shown as constructor apart from property,method, vars
if (symbolKind !== ScriptElementKind.unknown || symbolFlags & SymbolFlags.Class || symbolFlags & SymbolFlags.Alias) {
// If it is accessor they are allowed only if location is at name of the accessor
Expand Down Expand Up @@ -285,7 +289,7 @@ namespace ts.SymbolDisplay {
addFullSymbolName(symbol);
writeTypeParametersOfSymbol(symbol, sourceFile);
}
if (symbolFlags & SymbolFlags.TypeAlias) {
if ((symbolFlags & SymbolFlags.TypeAlias) && (semanticMeaning & SemanticMeaning.Type)) {
prefixNextMeaning();
displayParts.push(keywordPart(SyntaxKind.TypeKeyword));
displayParts.push(spacePart());
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace ts {
if (node.kind === SyntaxKind.SourceFile) {
return SemanticMeaning.Value;
}
else if (node.parent.kind === SyntaxKind.ExportAssignment) {
else if (node.parent.kind === SyntaxKind.ExportAssignment || node.parent.kind === SyntaxKind.ExternalModuleReference) {
return SemanticMeaning.All;
}
else if (isInRightSideOfInternalImportEqualsDeclaration(node)) {
Expand Down
5 changes: 2 additions & 3 deletions tests/cases/fourslash/findAllRefsForDefaultExport02.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
const ranges = test.ranges();
const [r0, r1, r2, r3, r4] = ranges;
const fnRanges = [r0, r1, r2, r3];
const fn = "function DefaultExportedFunction(): () => typeof DefaultExportedFunction";
verify.singleReferenceGroup(fn, fnRanges);
verify.singleReferenceGroup("function DefaultExportedFunction(): () => typeof DefaultExportedFunction", fnRanges);

// The namespace and function do not merge,
// so the namespace should be all alone.
verify.singleReferenceGroup(`namespace DefaultExportedFunction\n${fn}`, [r4]);
verify.singleReferenceGroup(`namespace DefaultExportedFunction`, [r4]);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ verify.noErrors();

const [r0, r1, r2, r3, r4] = test.ranges();
verify.referenceGroups(r0, [{ definition: "type T = number\nnamespace T", ranges: [r0, r2, r3] }]);
verify.referenceGroups(r1, [{ definition: "type T = number\nnamespace T", ranges: [r1, r2] }]);
verify.referenceGroups(r1, [{ definition: "namespace T", ranges: [r1, r2] }]);
verify.referenceGroups(r2, [{ definition: "type T = number\nnamespace T", ranges: [r0, r1, r2, r3] }]);
verify.referenceGroups([r3, r4], [
{ definition: 'module "/a"', ranges: [r4] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
////const x: typeof import("./a").[|T|] = 0;

const [r0, r1, r2, r3] = test.ranges();
verify.singleReferenceGroup("type T = 0\nconst T: 0", [r0, r2]);
verify.singleReferenceGroup("type T = 0\nconst T: 0", [r1, r3]);
verify.singleReferenceGroup("type T = 0", [r0, r2]);
verify.singleReferenceGroup("const T: 0", [r1, r3]);
4 changes: 2 additions & 2 deletions tests/cases/fourslash/jsdocTypedefTagSemanticMeaning0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

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

verify.singleReferenceGroup("type T = number\nconst T: 1", [t0, t1]);
verify.singleReferenceGroup("type T = number\nconst T: 1", [v0, v1]);
verify.singleReferenceGroup("type T = number", [t0, t1]);
verify.singleReferenceGroup("const T: 1", [v0, v1]);
17 changes: 15 additions & 2 deletions tests/cases/fourslash/quickInfoImportedTypesWithMergedMeanings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
//// export { Original/*1*/ } from './quickInfoImportedTypesWithMergedMeanings';

// @Filename: importer.ts
//// import { Original as Alias } from './quickInfoImportedTypesWithMergedMeanings';
//// Alias/*2*/;
//// import { Original as /*2*/Alias } from './quickInfoImportedTypesWithMergedMeanings';
//// Alias/*3*/;
//// let x: Alias/*4*/

verify.quickInfoAt("1", [
"(alias) function Original(): void",
Expand All @@ -26,3 +27,15 @@ verify.quickInfoAt("2", [
"(alias) namespace Alias",
"import Alias",
].join("\n"), "some docs");

verify.quickInfoAt("3", [
"(alias) function Alias(): void",
"(alias) namespace Alias",
"import Alias",
].join("\n"), "some docs");

verify.quickInfoAt("4", [
"(alias) type Alias<T> = () => T",
"(alias) namespace Alias",
"import Alias",
].join("\n"), "some docs");
9 changes: 6 additions & 3 deletions tests/cases/fourslash/quickInfoJsdocEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
//// A: 0,
////}
////
/////** @type {/**/E} */
////const x = E.A;
/////** @type {/*type*/E} */
////const x = /*value*/E.A;

verify.noErrors();

verify.quickInfoAt("",
verify.quickInfoAt("type",
`enum E`,
"Doc");
verify.quickInfoAt("value",
`enum E
const E: {
A: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/quickInfoOnThis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
////}

verify.quickInfos({
0: "this: this",
0: "this",
1: "this: void",
2: "this: this",
3: "(parameter) this: Restricted",
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/referencesForMergedDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
////[|Foo|].bind(this);

const [type1, namespace1, value1, namespace2, type2, value2] = test.ranges();
verify.singleReferenceGroup("interface Foo\nnamespace Foo\nfunction Foo(): void", [type1, type2]);
verify.singleReferenceGroup("namespace Foo\nfunction Foo(): void", [namespace1, namespace2]);
verify.singleReferenceGroup("interface Foo\nnamespace Foo", [type1, type2]);
verify.singleReferenceGroup("namespace Foo", [namespace1, namespace2]);
verify.singleReferenceGroup("namespace Foo\nfunction Foo(): void", [value1, value2]);
4 changes: 2 additions & 2 deletions tests/cases/fourslash/referencesForMergedDeclarations5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const ranges = test.ranges();
const [r0, r1, r2, r3] = ranges;
verify.referenceGroups(r0, [{ definition: "interface Foo\nnamespace Foo\nfunction Foo(): void", ranges: [r0, r3] }]);
verify.referenceGroups(r1, [{ definition: "namespace Foo\nfunction Foo(): void", ranges: [r1, r3] }]);
verify.referenceGroups(r0, [{ definition: "interface Foo\nnamespace Foo", ranges: [r0, r3] }]);
verify.referenceGroups(r1, [{ definition: "namespace Foo", ranges: [r1, r3] }]);
verify.referenceGroups(r2, [{ definition: "namespace Foo\nfunction Foo(): void", ranges: [r2, r3] }]);
verify.referenceGroups(r3, [{ definition: "interface Foo\nnamespace Foo\nfunction Foo(): void", ranges }]);
4 changes: 2 additions & 2 deletions tests/cases/fourslash/referencesForMergedDeclarations7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

const ranges = test.ranges();
const [r0, r1, r2, r3] = ranges;
verify.referenceGroups(r0, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r0, r3] }]);
verify.referenceGroups(r1, [{ definition: "namespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r1, r3] }]);
verify.referenceGroups(r0, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar", ranges: [r0, r3] }]);
verify.referenceGroups(r1, [{ definition: "namespace Foo.Bar", ranges: [r1, r3] }]);
verify.referenceGroups(r2, [{ definition: "namespace Foo.Bar\nfunction Foo.Bar(): void", ranges: [r2, r3] }]);
verify.referenceGroups(r3, [{ definition: "interface Foo.Bar\nnamespace Foo.Bar\nfunction Foo.Bar(): void", ranges }]);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/referencesForMergedDeclarations8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
////// module
////import a3 = Foo.[|Bar|].Baz;

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

0 comments on commit cbde861

Please sign in to comment.