• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: refactors", () => {
3        it("use formatting options", () => {
4            const file = {
5                path: "/a.ts",
6                content: "function f() {\n  1;\n}",
7            };
8            const host = createServerHost([file]);
9            const session = createSession(host);
10            openFilesForSession([file], session);
11
12            const response0 = session.executeCommandSeq<server.protocol.ConfigureRequest>({
13                command: server.protocol.CommandTypes.Configure,
14                arguments: {
15                    formatOptions: {
16                        indentSize: 2,
17                    },
18                },
19            }).response;
20            assert.deepEqual(response0, /*expected*/ undefined);
21
22            const response1 = session.executeCommandSeq<server.protocol.GetEditsForRefactorRequest>({
23                command: server.protocol.CommandTypes.GetEditsForRefactor,
24                arguments: {
25                    refactor: "Extract Symbol",
26                    action: "function_scope_1",
27                    file: "/a.ts",
28                    startLine: 2,
29                    startOffset: 3,
30                    endLine: 2,
31                    endOffset: 4,
32                },
33            }).response;
34            assert.deepEqual(response1, {
35                edits: [
36                    {
37                        fileName: "/a.ts",
38                        textChanges: [
39                            {
40                                start: { line: 2, offset: 3 },
41                                end: { line: 2, offset: 5 },
42                                newText: "newFunction();",
43                            },
44                            {
45                                start: { line: 3, offset: 2 },
46                                end: { line: 3, offset: 2 },
47                                newText: "\n\nfunction newFunction() {\n  1;\n}\n",
48                            },
49                        ]
50                    }
51                ],
52                renameFilename: "/a.ts",
53                renameLocation: { line: 2, offset: 3 },
54            });
55        });
56
57        it("handles text changes in tsconfig.json", () => {
58            const aTs = {
59                path: "/a.ts",
60                content: "export const a = 0;",
61            };
62            const tsconfig = {
63                path: "/tsconfig.json",
64                content: '{ "files": ["./a.ts"] }',
65            };
66
67            const session = createSession(createServerHost([aTs, tsconfig]));
68            openFilesForSession([aTs], session);
69
70            const response1 = session.executeCommandSeq<server.protocol.GetEditsForRefactorRequest>({
71                command: server.protocol.CommandTypes.GetEditsForRefactor,
72                arguments: {
73                    refactor: "Move to a new file",
74                    action: "Move to a new file",
75                    file: "/a.ts",
76                    startLine: 1,
77                    startOffset: 1,
78                    endLine: 1,
79                    endOffset: 20,
80                },
81            }).response;
82            assert.deepEqual(response1, {
83                edits: [
84                    {
85                        fileName: "/a.ts",
86                        textChanges: [
87                            {
88                                start: { line: 1, offset: 1 },
89                                end: { line: 1, offset: 20 },
90                                newText: "",
91                            },
92                        ],
93                    },
94                    {
95                        fileName: "/tsconfig.json",
96                        textChanges: [
97                            {
98                                start: { line: 1, offset: 21 },
99                                end: { line: 1, offset: 21 },
100                                newText: ", \"./a.1.ts\"",
101                            },
102                        ],
103                    },
104                    {
105                        fileName: "/a.1.ts",
106                        textChanges: [
107                            {
108                                start: { line: 0, offset: 0 },
109                                end: { line: 0, offset: 0 },
110                                newText: "export const a = 0;\n",
111                            },
112                        ],
113                    }
114                ],
115                renameFilename: undefined,
116                renameLocation: undefined,
117            });
118        });
119
120        it("handles canonicalization of tsconfig path", () => {
121            const aTs: File = { path: "/Foo/a.ts", content: "const x = 0;" };
122            const tsconfig: File = { path: "/Foo/tsconfig.json", content: '{ "files": ["./a.ts"] }' };
123            const session = createSession(createServerHost([aTs, tsconfig]));
124            openFilesForSession([aTs], session);
125
126            const result = executeSessionRequest<protocol.GetEditsForRefactorRequest, protocol.GetEditsForRefactorResponse>(session, protocol.CommandTypes.GetEditsForRefactor, {
127                file: aTs.path,
128                startLine: 1,
129                startOffset: 1,
130                endLine: 2,
131                endOffset: aTs.content.length,
132                refactor: "Move to a new file",
133                action: "Move to a new file",
134            });
135            assert.deepEqual<protocol.RefactorEditInfo | undefined>(result, {
136                edits: [
137                    {
138                        fileName: aTs.path,
139                        textChanges: [{ start: { line: 1, offset: 1 }, end: { line: 1, offset: aTs.content.length + 1 }, newText: "" }],
140                    },
141                    {
142                        fileName: tsconfig.path,
143                        textChanges: [{ start: { line: 1, offset: 21 }, end: { line: 1, offset: 21 }, newText: ', "./x.ts"' }],
144                    },
145                    {
146                        fileName: "/Foo/x.ts",
147                        textChanges: [{ start: { line: 0, offset: 0 }, end: { line: 0, offset: 0 }, newText: "const x = 0;\n" }],
148                    },
149                ],
150                renameFilename: undefined,
151                renameLocation: undefined,
152            });
153        });
154    });
155}
156