8000 fix(typescript-estree): don't throw on missing tsconfig.json by defau… · ronami/typescript-eslint@ef3384e · GitHub
[go: up one dir, main page]

Skip to content

Commit ef3384e

Browse files
fix(typescript-estree): don't throw on missing tsconfig.json by default in project service (typescript-eslint#9989)
1 parent b353a86 commit ef3384e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

packages/typescript-estree/src/create-program/createProjectService.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ export function createProjectService(
4242
jsDocParsingMode: ts.JSDocParsingMode | undefined,
4343
tsconfigRootDir: string | undefined,
4444
): ProjectServiceSettings {
45+
const optionsRawObject = typeof optionsRaw === 'object' ? optionsRaw : {};
4546
const options = {
4647
defaultProject: 'tsconfig.json',
47-
...(typeof optionsRaw === 'object' && optionsRaw),
48+
...optionsRawObject,
4849
};
4950
validateDefaultProjectForFilesGlob(options.allowDefaultProject);
5051

@@ -126,7 +127,7 @@ export function createProjectService(
126127
});
127128

128129
log('Enabling default project: %s', options.defaultProject);
129-
let configFile: ts.ParsedCommandLine;
130+
let configFile: ts.ParsedCommandLine | undefined;
130131

131132
try {
132133
configFile = getParsedConfigFile(
@@ -135,18 +136,22 @@ export function createProjectService(
135136
tsconfigRootDir,
136137
);
137138
} catch (error) {
138-
throw new Error(
139-
`Could not read project service default project '${options.defaultProject}': ${(error as Error).message}`,
140-
);
139+
if (optionsRawObject.defaultProject) {
140+
throw new Error(
141+
`Could not read project service default project '${options.defaultProject}': ${(error as Error).message}`,
142+
);
143+
}
141144
}
142145

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

151156
return {
152157
allowDefaultProject: options.allowDefaultProject,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('createProjectService', () => {
6565
expect(settings.allowDefaultProject).toBeUndefined();
6666
});
6767

68-
C70E it('throws an error with a local path when options.defaultProject is not provided and getParsedConfigFile throws a diagnostic error', () => {
68+
it('does not throw an error when options.defaultProject is not provided and getParsedConfigFile throws a diagnostic error', () => {
6969
mockGetParsedConfigFile.mockImplementation(() => {
7070
throw new Error('tsconfig.json(1,1): error TS1234: Oh no!');
7171
});
@@ -78,9 +78,7 @@ describe('createProjectService', () => {
7878
undefined,
7979
undefined,
8080
),
81-
).toThrow(
82-
/Could not read project service default project 'tsconfig.json': .+ error TS1234: Oh no!/,
83-
);
81+
).not.toThrow();
8482
});
8583

8684
it('throws an error with a relative path when options.defaultProject is set to a relative path and getParsedConfigFile throws a diagnostic error', () => {
@@ -132,6 +130,7 @@ describe('createProjectService', () => {
132130
createProjectService(
133131
{
134132
allowDefaultProject: ['file.js'],
133+
defaultProject: 'tsconfig.json',
135134
},
136135
undefined,
137136
undefined,

0 commit comments

Comments
 (0)
0