Skip to content

Commit

Permalink
fix(typescript-estree): pass extraFileExtensions to projectService
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredringstad committed May 6, 2024
1 parent f248e68 commit c21ca49
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
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 @@ -123,6 +123,11 @@ export interface ProjectServiceOptions {
* @default 8
*/
maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING?: number;

/**
* Additional file extensions which should be considered in the TypeScript Program compilation.
*/
extraFileExtensions?: string[];
}

interface ParseAndGenerateServicesOptions extends ParseOptions {
Expand Down
11 changes: 11 additions & 0 deletions packages/typescript-estree/src/useProgramFromProjectService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debug from 'debug';
import { minimatch } from 'minimatch';
import path from 'path';
import { ScriptKind } from 'typescript';

import { createProjectProgram } from './create-program/createProjectProgram';
import type { ProjectServiceSettings } from './create-program/createProjectService';
Expand Down Expand Up @@ -31,6 +32,16 @@ export function useProgramFromProjectService(
filePathAbsolute,
);

if (parseSettings.extraFileExtensions.length) {
service.setHostConfiguration({
extraFileExtensions: parseSettings.extraFileExtensions.map(extension => ({
extension,
isMixedContent: false,
scriptKind: ScriptKind.Deferred,
})),
});
}

const opened = service.openClientFile(
filePathAbsolute,
parseSettings.codeFullText,
Expand Down
63 changes: 63 additions & 0 deletions packages/typescript-estree/tests/lib/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ describe('parseAndGenerateServices', () => {
alignErrorPath(error as Error);
}
};
const testParseExperimentalProjectService =
(filePath: string, extraFileExtensions: string[] = ['.vue']) =>
(): void => {
const result = parser.parseAndGenerateServices(code, {
...config,
extraFileExtensions,
filePath: join(PROJECT_DIR, filePath),
project: true,
EXPERIMENTAL_useProjectService: true,
});
const compilerOptions = result.services.program?.getCompilerOptions();

if (!compilerOptions?.configFilePath) {
throw new Error('No config file found, using inferred project');
}
};

describe('project includes', () => {
it("doesn't error for matched files", () => {
Expand Down Expand Up @@ -477,6 +493,53 @@ describe('parseAndGenerateServices', () => {
`);
});
});

describe('"parserOptions.extraFileExtensions" is non-empty and EXPERIMENTAL_useProjectService is true', () => {
describe('the extension matches', () => {
it('the file is included', () => {
expect(
testParseExperimentalProjectService('other/included.vue'),
).not.toThrow();
});

it("the file isn't included", () => {
expect(
testParseExperimentalProjectService('other/notIncluded.vue'),
).toThrowErrorMatchingInlineSnapshot(
`"No config file found, using inferred project"`,
);
});

it('duplicate extension', () => {
expect(
testParseExperimentalProjectService('ts/notIncluded.ts', ['.ts']),
).toThrowErrorMatchingInlineSnapshot(
`"No config file found, using inferred project"`,
);
});
});

it('invalid extension', () => {
expect(
testParseExperimentalProjectService(
'other/unknownFileType.unknown',
['unknown'],
),
).toThrowErrorMatchingInlineSnapshot(
`"No config file found, using inferred project"`,
);
});

it('the extension does not match', () => {
expect(
testParseExperimentalProjectService(
'other/unknownFileType.unknown',
),
).toThrowErrorMatchingInlineSnapshot(
`"No config file found, using inferred project"`,
);
});
});
});

describe('invalid project error messages', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function createMockProjectService() {

const mockParseSettings = {
filePath: 'path/PascalCaseDirectory/camelCaseFile.ts',
extraFileExtensions: [] as readonly string[],
} as ParseSettings;

const createProjectServiceSettings = <
Expand Down

0 comments on commit c21ca49

Please sign in to comment.