Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/typescript-estree/src/useProgramFromProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,24 @@ function openClientFileFromProjectService(
}

if (!isDefaultProjectAllowed) {
const baseMessage = `${wasNotFound}. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`;
const allowDefaultProject =
parseSettings.projectService?.allowDefaultProject;

if (!allowDefaultProject) {
throw new Error(baseMessage);
}

const relativeFilePath = path.relative(
parseSettings.tsconfigRootDir,
filePathAbsolute,
);

throw new Error(
`${wasNotFound}. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`,
[
baseMessage,
`allowDefaultProject is set to ${JSON.stringify(allowDefaultProject)}, which does not match '${relativeFilePath}'.`,
].join('\n'),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe(useProgramFromProjectService, () => {
expect(service.reloadProjects).not.toHaveBeenCalled();
});

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', () => {
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', () => {
const { service } = createMockProjectService();

service.openClientFile.mockReturnValueOnce({}).mockReturnValueOnce({});
Expand All @@ -189,6 +189,40 @@ describe(useProgramFromProjectService, () => {
expect(service.reloadProjects).toHaveBeenCalledOnce();
});

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', () => {
const { service } = createMockProjectService();
const allowDefaultProject = ['a.js', 'b.js'];

service.openClientFile.mockReturnValueOnce({}).mockReturnValueOnce({});

expect(() =>
useProgramFromProjectService(
createProjectServiceSettings({
allowDefaultProject,
lastReloadTimestamp: 0,
service,
}),
{
...mockParseSettings,
projectService: {
allowDefaultProject,
lastReloadTimestamp: 0,
maximumDefaultProjectFileMatchCount: 8,
service,
},
},
true,
new Set(),
),
).toThrow(
[
`${mockParseSettings.filePath} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject.`,
`allowDefaultProject is set to ["a.js","b.js"], which does not match '${path.normalize('path/PascalCaseDirectory/camelCaseFile.ts')}'.`,
].join('\n'),
);
expect(service.reloadProjects).toHaveBeenCalledOnce();
});

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', () => {
const { service } = createMockProjectService();
const program = { getSourceFile: vi.fn() };
Expand Down