Skip to content
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

feat(typescript-estree): add defaultProject for project service #8815

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/packages/TypeScript_ESTree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,14 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
interface ProjectServiceOptions {
/**
* Globs of files to allow running with the default inferred project settings.
* Globs of files to allow running with the default project compiler options.
*/
allowDefaultProjectForFiles?: string[];

/**
* Path to a TSConfig to use instead of TypeScript's default project configuration.
*/
defaultProject?: string;
}

interface ParserServices {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function -- for TypeScript APIs*/
import os from 'node:os';

import type * as ts from 'typescript/lib/tsserverlibrary';

import type { ProjectServiceOptions } from '../parser-options';
Expand Down Expand Up @@ -58,6 +60,40 @@
jsDocParsingMode,
});

if (typeof options === 'object' && options.defaultProject) {
try {
const configRead = tsserver.readConfigFile(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: ts.readConfigFile has two unhappy paths:

  • If the config file is valid JSON but not right for TypeScript, it returns an object
  • If the config file is invalid JSON, it throws

🙌 error handling!

options.defaultProject,
system.readFile,
);

if (configRead.error) {
throw new Error(
`Could not read default project '${options.defaultProject}': ${tsserver.formatDiagnostic(
configRead.error,
{
getCurrentDirectory: system.getCurrentDirectory,
getCanonicalFileName: (fileName: string) => fileName,

Check warning on line 76 in packages/typescript-estree/src/create-program/createProjectService.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L76 was not covered by tests
getNewLine: () => os.EOL,
},
)}`,
);
}

type ProjectCompilerOptions =
ts.server.protocol.InferredProjectCompilerOptions;

service.setCompilerOptionsForInferredProjects(

Check warning on line 86 in packages/typescript-estree/src/create-program/createProjectService.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L86 was not covered by tests
(configRead.config as { compilerOptions: ProjectCompilerOptions })
.compilerOptions,
);
} catch (error) {
throw new Error(
`Could not parse default project '${options.defaultProject}': ${(error as Error).message}`,
);
}
}

return {
allowDefaultProjectForFiles:
typeof options === 'object'
Expand Down
5 changes: 5 additions & 0 deletions packages/typescript-estree/src/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export interface ProjectServiceOptions {
* Globs of files to allow running with the default inferred project settings.
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
*/
allowDefaultProjectForFiles?: string[];

/**
* Path to a TSConfig to use instead of TypeScript's default project configuration.
*/
defaultProject?: string;
}

interface ParseAndGenerateServicesOptions extends ParseOptions {
Expand Down
46 changes: 46 additions & 0 deletions packages/typescript-estree/tests/lib/createProjectService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import * as ts from 'typescript';

import { createProjectService } from '../../src/create-program/createProjectService';

const mockReadConfigFile = jest.fn();

jest.mock('typescript/lib/tsserverlibrary', () => ({
...jest.requireActual('typescript/lib/tsserverlibrary'),
readConfigFile: mockReadConfigFile,
}));

describe('createProjectService', () => {
it('sets allowDefaultProjectForFiles when options.allowDefaultProjectForFiles is defined', () => {
const allowDefaultProjectForFiles = ['./*.js'];
Expand All @@ -18,4 +27,41 @@ describe('createProjectService', () => {

expect(settings.allowDefaultProjectForFiles).toBeUndefined();
});

it('throws an error when options.defaultProject is set and the readConfigFile returns an error', () => {
mockReadConfigFile.mockReturnValue({
error: {
category: ts.DiagnosticCategory.Error,
code: 1000,
},
});

expect(() =>
createProjectService(
{
allowDefaultProjectForFiles: ['file.js'],
defaultProject: './tsconfig.json',
},
undefined,
),
).toThrow(
/Could not read default project '\.\/tsconfig.json': error TS1000/,
);
});

it('throws an error when options.defaultProject is set and the readConfigFile throws an error', () => {
mockReadConfigFile.mockImplementation(() => {
throw new Error('Oh no!');
});

expect(() =>
createProjectService(
{
allowDefaultProjectForFiles: ['file.js'],
defaultProject: './tsconfig.json',
},
undefined,
),
).toThrow("Could not parse default project './tsconfig.json': Oh no!");
});
});