Skip to content

Commit 16c181f

Browse files
committed
Add basic test for current behavior
1 parent 2c458c0 commit 16c181f

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"unittests/tsserver/formatSettings.ts",
129129
"unittests/tsserver/getApplicableRefactors.ts",
130130
"unittests/tsserver/getEditsForFileRename.ts",
131+
"unittests/tsserver/getExportReferences.ts",
131132
"unittests/tsserver/importHelpers.ts",
132133
"unittests/tsserver/inferredProjects.ts",
133134
"unittests/tsserver/languageService.ts",

src/testRunner/unittests/tsserver/declarationFileMaps.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
namespace ts.projectSystem {
2-
interface DocumentSpanFromSubstring {
3-
file: File;
4-
text: string;
5-
options?: SpanFromSubstringOptions;
6-
contextText?: string;
7-
contextOptions?: SpanFromSubstringOptions;
8-
}
92
function documentSpanFromSubstring({ file, text, contextText, options, contextOptions }: DocumentSpanFromSubstring): DocumentSpan {
103
const contextSpan = contextText !== undefined ? documentSpanFromSubstring({ file, text: contextText, options: contextOptions }) : undefined;
114
return {
@@ -19,19 +12,6 @@ namespace ts.projectSystem {
1912
return documentSpanFromSubstring(input);
2013
}
2114

22-
interface MakeReferenceItem extends DocumentSpanFromSubstring {
23-
isDefinition: boolean;
24-
lineText: string;
25-
}
26-
function makeReferenceItem({ isDefinition, lineText, ...rest }: MakeReferenceItem): protocol.ReferencesResponseItem {
27-
return {
28-
...protocolFileSpanWithContextFromSubstring(rest),
29-
isDefinition,
30-
isWriteAccess: isDefinition,
31-
lineText,
32-
};
33-
}
34-
3515
interface MakeReferenceEntry extends DocumentSpanFromSubstring {
3616
isDefinition: boolean;
3717
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace ts.projectSystem {
2+
describe("unittests:: tsserver:: getExportReferences", () => {
3+
it("should get const variable decralation references", () => {
4+
const mainTs: File = {
5+
path: "/main.ts",
6+
content: 'import { value } from "./mod";',
7+
};
8+
const modTs: File = {
9+
path: "/mod.ts",
10+
content: "export const value = 0;",
11+
};
12+
const tsconfig: File = {
13+
path: "/tsconfig.json",
14+
content: "{}",
15+
};
16+
17+
const host = createServerHost([mainTs, modTs, tsconfig]);
18+
const session = createSession(host);
19+
openFilesForSession([mainTs, modTs], session);
20+
21+
const response = executeSessionRequest<protocol.ReferencesRequest, protocol.ReferencesResponse>(
22+
session,
23+
protocol.CommandTypes.References,
24+
protocolFileLocationFromSubstring(modTs, "value"),
25+
);
26+
27+
const referenceModTs = (modTs: File): protocol.ReferencesResponseItem =>
28+
makeReferenceItem({
29+
file: modTs,
30+
isDefinition: true,
31+
lineText: "export const value = 0;",
32+
contextText: "export const value = 0;",
33+
text: "value",
34+
});
35+
const referenceMainTs = (mainTs: File): protocol.ReferencesResponseItem =>
36+
makeReferenceItem({
37+
file: mainTs,
38+
isDefinition: true,
39+
lineText: 'import { value } from "./mod";',
40+
contextText: 'import { value } from "./mod";',
41+
text: "value",
42+
});
43+
44+
const expectResponse = {
45+
refs: [referenceModTs(modTs), referenceMainTs(mainTs)],
46+
symbolDisplayString: "const value: 0",
47+
symbolName: "value",
48+
symbolStartOffset: protocolLocationFromSubstring(modTs.content, "value").offset,
49+
};
50+
51+
assert.deepEqual(response, expectResponse);
52+
});
53+
});
54+
}

src/testRunner/unittests/tsserver/helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ namespace ts.projectSystem {
516516
file: File;
517517
text: string;
518518
options?: SpanFromSubstringOptions;
519+
contextText?: string;
520+
contextOptions?: SpanFromSubstringOptions;
519521
}
520522
export function protocolFileSpanFromSubstring({ file, text, options }: DocumentSpanFromSubstring): protocol.FileSpan {
521523
return { file: file.path, ...protocolTextSpanFromSubstring(file.content, text, options) };
@@ -724,4 +726,18 @@ namespace ts.projectSystem {
724726
assert.strictEqual(outputs.length, index + 1, JSON.stringify(outputs));
725727
}
726728
}
729+
730+
interface MakeReferenceItem extends DocumentSpanFromSubstring {
731+
isDefinition: boolean;
732+
lineText: string;
733+
}
734+
735+
export function makeReferenceItem({ isDefinition, lineText, ...rest }: MakeReferenceItem): protocol.ReferencesResponseItem {
736+
return {
737+
...protocolFileSpanWithContextFromSubstring(rest),
738+
isDefinition,
739+
isWriteAccess: isDefinition,
740+
lineText,
741+
};
742+
}
727743
}

0 commit comments

Comments
 (0)