• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: with skipLibCheck", () => {
3        it("should be turned on for js-only inferred projects", () => {
4            const file1 = {
5                path: "/a/b/file1.js",
6                content: `
7                /// <reference path="file2.d.ts" />
8                var x = 1;`
9            };
10            const file2 = {
11                path: "/a/b/file2.d.ts",
12                content: `
13                interface T {
14                    name: string;
15                };
16                interface T {
17                    name: number;
18                };`
19            };
20            const host = createServerHost([file1, file2]);
21            const session = createSession(host);
22            openFilesForSession([file1, file2], session);
23
24            const file2GetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
25                CommandNames.SemanticDiagnosticsSync,
26                { file: file2.path }
27            );
28            let errorResult = session.executeCommand(file2GetErrRequest).response as protocol.Diagnostic[];
29            assert.isTrue(errorResult.length === 0);
30
31            const closeFileRequest = makeSessionRequest<protocol.FileRequestArgs>(CommandNames.Close, { file: file1.path });
32            session.executeCommand(closeFileRequest);
33            errorResult = session.executeCommand(file2GetErrRequest).response as protocol.Diagnostic[];
34            assert.isTrue(errorResult.length !== 0);
35
36            openFilesForSession([file1], session);
37            errorResult = session.executeCommand(file2GetErrRequest).response as protocol.Diagnostic[];
38            assert.isTrue(errorResult.length === 0);
39        });
40
41        it("should be turned on for js-only external projects", () => {
42            const jsFile = {
43                path: "/a/b/file1.js",
44                content: "let x =1;"
45            };
46            const dTsFile = {
47                path: "/a/b/file2.d.ts",
48                content: `
49                interface T {
50                    name: string;
51                };
52                interface T {
53                    name: number;
54                };`
55            };
56            const host = createServerHost([jsFile, dTsFile]);
57            const session = createSession(host);
58
59            const openExternalProjectRequest = makeSessionRequest<protocol.OpenExternalProjectArgs>(
60                CommandNames.OpenExternalProject,
61                {
62                    projectFileName: "project1",
63                    rootFiles: toExternalFiles([jsFile.path, dTsFile.path]),
64                    options: {}
65                }
66            );
67            session.executeCommand(openExternalProjectRequest);
68
69            const dTsFileGetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
70                CommandNames.SemanticDiagnosticsSync,
71                { file: dTsFile.path }
72            );
73            const errorResult = session.executeCommand(dTsFileGetErrRequest).response as protocol.Diagnostic[];
74            assert.isTrue(errorResult.length === 0);
75        });
76
77        it("should be turned on for js-only external projects with skipLibCheck=false", () => {
78            const jsFile = {
79                path: "/a/b/file1.js",
80                content: "let x =1;"
81            };
82            const dTsFile = {
83                path: "/a/b/file2.d.ts",
84                content: `
85                interface T {
86                    name: string;
87                };
88                interface T {
89                    name: number;
90                };`
91            };
92            const host = createServerHost([jsFile, dTsFile]);
93            const session = createSession(host);
94
95            const openExternalProjectRequest = makeSessionRequest<protocol.OpenExternalProjectArgs>(
96                CommandNames.OpenExternalProject,
97                {
98                    projectFileName: "project1",
99                    rootFiles: toExternalFiles([jsFile.path, dTsFile.path]),
100                    options: { skipLibCheck: false }
101                }
102            );
103            session.executeCommand(openExternalProjectRequest);
104
105            const dTsFileGetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
106                CommandNames.SemanticDiagnosticsSync,
107                { file: dTsFile.path }
108            );
109            const errorResult = session.executeCommand(dTsFileGetErrRequest).response as protocol.Diagnostic[];
110            assert.isTrue(errorResult.length === 0);
111        });
112
113        it("should not report bind errors for declaration files with skipLibCheck=true", () => {
114            const jsconfigFile = {
115                path: "/a/jsconfig.json",
116                content: "{}"
117            };
118            const jsFile = {
119                path: "/a/jsFile.js",
120                content: "let x = 1;"
121            };
122            const dTsFile1 = {
123                path: "/a/dTsFile1.d.ts",
124                content: `
125                declare var x: number;`
126            };
127            const dTsFile2 = {
128                path: "/a/dTsFile2.d.ts",
129                content: `
130                declare var x: string;`
131            };
132            const host = createServerHost([jsconfigFile, jsFile, dTsFile1, dTsFile2]);
133            const session = createSession(host);
134            openFilesForSession([jsFile], session);
135
136            const dTsFile1GetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
137                CommandNames.SemanticDiagnosticsSync,
138                { file: dTsFile1.path }
139            );
140            const error1Result = session.executeCommand(dTsFile1GetErrRequest).response as protocol.Diagnostic[];
141            assert.isTrue(error1Result.length === 0);
142
143            const dTsFile2GetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
144                CommandNames.SemanticDiagnosticsSync,
145                { file: dTsFile2.path }
146            );
147            const error2Result = session.executeCommand(dTsFile2GetErrRequest).response as protocol.Diagnostic[];
148            assert.isTrue(error2Result.length === 0);
149        });
150
151        it("should report semantic errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
152            const jsFile = {
153                path: "/a/jsFile.js",
154                content: `
155                // @ts-check
156                let x = 1;
157                x === "string";`
158            };
159
160            const host = createServerHost([jsFile]);
161            const session = createSession(host);
162            openFilesForSession([jsFile], session);
163
164            const getErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
165                CommandNames.SemanticDiagnosticsSync,
166                { file: jsFile.path }
167            );
168            const errorResult = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
169            assert.isTrue(errorResult.length === 1);
170            assert.equal(errorResult[0].code, Diagnostics.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.code);
171        });
172
173        it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
174            const jsconfigFile = {
175                path: "/a/jsconfig.json",
176                content: "{}"
177            };
178
179            const jsFile = {
180                path: "/a/jsFile.js",
181                content: `
182                // @ts-check
183                let x = 1;
184                x === "string";`
185            };
186
187            const host = createServerHost([jsconfigFile, jsFile]);
188            const session = createSession(host);
189            openFilesForSession([jsFile], session);
190
191            const getErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
192                CommandNames.SemanticDiagnosticsSync,
193                { file: jsFile.path }
194            );
195            const errorResult = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
196            assert.isTrue(errorResult.length === 1);
197            assert.equal(errorResult[0].code, Diagnostics.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.code);
198        });
199
200        it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
201            const jsconfigFile = {
202                path: "/a/jsconfig.json",
203                content: JSON.stringify({
204                    compilerOptions: {
205                        checkJs: true,
206                        skipLibCheck: true
207                    },
208                })
209            };
210            const jsFile = {
211                path: "/a/jsFile.js",
212                content: `let x = 1;
213                x === "string";`
214            };
215
216            const host = createServerHost([jsconfigFile, jsFile]);
217            const session = createSession(host);
218            openFilesForSession([jsFile], session);
219
220            const getErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
221                CommandNames.SemanticDiagnosticsSync,
222                { file: jsFile.path }
223            );
224            const errorResult = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
225            assert.isTrue(errorResult.length === 1);
226            assert.equal(errorResult[0].code, Diagnostics.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.code);
227        });
228    });
229}
230