8000 fix(typescript-estree): don't throw on missing tsconfig.json by default in project service by JoshuaKGoldberg · Pull Request #9989 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(typescript-estree): don't throw on missing tsconfig.json by default in project service #9989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export function createProjectService(
jsDocParsingMode: ts.JSDocParsingMode | undefined,
tsconfigRootDir: string | undefined,
): ProjectServiceSettings {
const optionsRawObject = typeof optionsRaw === 'object' ? optionsRaw : {};
const options = {
defaultProject: 'tsconfig.json',
...(typeof optionsRaw === 'object' && optionsRaw),
...optionsRawObject,
};
validateDefaultProjectForFilesGlob(options.allowDefaultProject);

Expand Down Expand Up @@ -126,7 +127,7 @@ export function createProjectService(
});

log('Enabling default project: %s', options.defaultProject);
let configFile: ts.ParsedCommandLine;
let configFile: ts.ParsedCommandLine | undefined;

try {
configFile = getParsedConfigFile(
Expand All @@ -135,18 +136,22 @@ export function cr 8000 eateProjectService(
tsconfigRootDir,
);
} catch (error) {
throw new Error(
`Could not read project service default project '${options.defaultProject}': ${(error as Error).message}`,
);
if (optionsRawObject.defaultProject) {
throw new Error(
`Could not read project service default project '${options.defaultProject}': ${(error as Error).message}`,
);
}
}

service.setCompilerOptionsForInferredProjects(
// NOTE: The inferred projects API is not intended for source files when a tsconfig
// exists. There is no API that generates an InferredProjectCompilerOptions suggesting
// it is meant for hard coded options passed in. Hard asserting as a work around.
// See https://github.com/microsoft/TypeScript/blob/27bcd4cb5a98bce46c9cdd749752703ead021a4b/src/server/protocol.ts#L1904
configFile.options as ts.server.protocol.InferredProjectCompilerOptions,
);
if (configFile) {
service.setCompilerOptionsForInferredProjects(
// NOTE: The inferred projects API is not intended for source files when a tsconfig
// exists. There is no API that generates an InferredProjectCompilerOptions suggesting
// it is meant for hard coded options passed in. Hard asserting as a work around.
// See https://github.com/microsoft/TypeScript/blob/27bcd4cb5a98bce46c9cdd749752703ead021a4b/src/server/protocol.ts#L1904
configFile.options as ts.server.protocol.InferredProjectCompilerOptions,
);
}

return {
allowDefaultProject: options.allowDefaultProject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('createProjectService', () => {
expect(settings.allowDefaultProject).toBeUndefined();
});

it('throws an error with a local path when options.defaultProject is not provided and getParsedConfigFile throws a diagnostic error', () => {
it('does not throw an error when options.defaultProject is not provided and getParsedConfigFile throws a diagnostic error', () => {
mockGetParsedConfigFile.mockImplementation(() => {
throw new Error('tsconfig.json(1,1): error TS1234: Oh no!');
});
Expand All @@ -78,9 +78,7 @@ describe('createProjectService', () => {
undefined,
undefined,
),
).toThrow(
/Could not read project service default project 'tsconfig.json': .+ error TS1234: Oh no!/,
);
).not.toThrow();
});

it('throws an error with a relative path when options.defaultProject is set to a relative path and getParsedConfigFile throws a diagnostic error', () => {
Expand Down Expand Up @@ -132,6 +130,7 @@ describe('createProjectService', () => {
createProjectService(
{
allowDefaultProject: ['file.js'],
defaultProject: 'tsconfig.json',
},
undefined,
undefined,
Expand Down
Loading
0