Skip to content

Commit 5c50292

Browse files
feat(typescript-estree): mention file specifics in project service allowDefaultProject error (#11635)
* feat(typescript-estree): mention file specifics in project service allowDefaultProject error * test: normalize paths * Reset to main * Reset to main * Reset to main * Unnecessary .filter(Boolean)
1 parent 760d299 commit 5c50292

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

packages/typescript-estree/src/useProgramFromProjectService.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,24 @@ function openClientFileFromProjectService(
9696
}
9797

9898
if (!isDefaultProjectAllowed) {
99+
const baseMessage = `${wasNotFound}. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`;
100+
const allowDefaultProject =
101+
parseSettings.projectService?.allowDefaultProject;
102+
103+
if (!allowDefaultProject) {
104+
throw new Error(baseMessage);
105+
}
106+
107+
const relativeFilePath = path.relative(
108+
parseSettings.tsconfigRootDir,
109+
filePathAbsolute,
110+
);
111+
99112
throw new Error(
100-
`${wasNotFound}. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`,
113+
[
114+
baseMessage,
115+
`allowDefaultProject is set to ${JSON.stringify(allowDefaultProject)}, which does not match '${relativeFilePath}'.`,
116+
].join('\n'),
101117
);
102118
}
103119
}

packages/typescript-estree/tests/lib/useProgramFromProjectService.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe(useProgramFromProjectService, () => {
167167
expect(service.reloadProjects).not.toHaveBeenCalled();
168168
});
169169

170-
it('throws an error after reloading projects when hasFullTypeInformation is enabled, the file is neither in the project service nor allowDefaultProject, and the last reload was recent', () => {
170+
it('throws a non-file-specific error after reloading projects when hasFullTypeInformation is enabled, the file is neither in the project service nor an empty allowDefaultProject, and the last reload was recent', () => {
171171
const { service } = createMockProjectService();
172172

173173
service.openClientFile.mockReturnValueOnce({}).mockReturnValueOnce({});
@@ -189,6 +189,40 @@ describe(useProgramFromProjectService, () => {
189189
expect(service.reloadProjects).toHaveBeenCalledOnce();
190190
});
191191

192+
it('throws a file-specific error after reloading projects when hasFullTypeInformation is enabled, the file is neither in the project service nor a populated allowDefaultProject, and the last reload was recent', () => {
193+
const { service } = createMockProjectService();
194+
const allowDefaultProject = ['a.js', 'b.js'];
195+
196+
service.openClientFile.mockReturnValueOnce({}).mockReturnValueOnce({});
197+
198+
expect(() =>
199+
useProgramFromProjectService(
200+
createProjectServiceSettings({
201+
allowDefaultProject,
202+
lastReloadTimestamp: 0,
203+
service,
204+
}),
205+
{
206+
...mockParseSettings,
207+
projectService: {
208+
allowDefaultProject,
209+
lastReloadTimestamp: 0,
210+
maximumDefaultProjectFileMatchCount: 8,
211+
service,
212+
},
213+
},
214+
true,
215+
new Set(),
216+
),
217+
).toThrow(
218+
[
219+
`${mockParseSettings.filePath} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`,
220+
`allowDefaultProject is set to ["a.js","b.js"], which does not match '${path.normalize('path/PascalCaseDirectory/camelCaseFile.ts')}'.`,
221+
].join('\n'),
222+
);
223+
expect(service.reloadProjects).toHaveBeenCalledOnce();
224+
});
225+
192226
it('returns a created program after reloading projects when hasFullTypeInformation is enabled, the file is only in the project service after reload, and the last reload was recent', () => {
193227
const { service } = createMockProjectService();
194228
const program = { getSourceFile: vi.fn() };

0 commit comments

Comments
 (0)