Skip to content

Commit dba5ce0

Browse files
committed
Add basic test for current behavior
1 parent 70b7bd5 commit dba5ce0

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 declaration 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
@@ -519,6 +519,8 @@ namespace ts.projectSystem {
519519
file: File;
520520
text: string;
521521
options?: SpanFromSubstringOptions;
522+
contextText?: string;
523+
contextOptions?: SpanFromSubstringOptions;
522524
}
523525
export function protocolFileSpanFromSubstring({ file, text, options }: DocumentSpanFromSubstring): protocol.FileSpan {
524526
return { file: file.path, ...protocolTextSpanFromSubstring(file.content, text, options) };
@@ -727,4 +729,18 @@ namespace ts.projectSystem {
727729
assert.strictEqual(outputs.length, index + 1, JSON.stringify(outputs));
728730
}
729731
}
732+
733+
interface MakeReferenceItem extends DocumentSpanFromSubstring {
734+
isDefinition: boolean;
735+
lineText: string;
736+
}
737+
738+
export function makeReferenceItem({ isDefinition, lineText, ...rest }: MakeReferenceItem): protocol.ReferencesResponseItem {
739+
return {
740+
...protocolFileSpanWithContextFromSubstring(rest),
741+
isDefinition,
742+
isWriteAccess: isDefinition,
743+
lineText,
744+
};
745+
}
730746
}

0 commit comments

Comments
 (0)