diff --git a/packages/typescript-estree/src/create-program/useProvidedPrograms.ts b/packages/typescript-estree/src/create-program/useProvidedPrograms.ts index cae07bda5fa1..753dbd8a2b97 100644 --- a/packages/typescript-estree/src/create-program/useProvidedPrograms.ts +++ b/packages/typescript-estree/src/create-program/useProvidedPrograms.ts @@ -3,47 +3,51 @@ import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; +import type { ParseSettings } from '../parseSettings'; import type { ASTAndDefiniteProgram } from './shared'; import { CORE_COMPILER_OPTIONS, getAstFromProgram } from './shared'; const log = debug('typescript-eslint:typescript-estree:useProvidedProgram'); -export interface ProvidedProgramsSettings { - filePath: string; - tsconfigRootDir: string; -} - function useProvidedPrograms( programInstances: Iterable, - { filePath, tsconfigRootDir }: ProvidedProgramsSettings, + parseSettings: ParseSettings, ): ASTAndDefiniteProgram | undefined { - log('Retrieving ast for %s from provided program instance(s)', filePath); + log( + 'Retrieving ast for %s from provided program instance(s)', + parseSettings.filePath, + ); let astAndProgram: ASTAndDefiniteProgram | undefined; for (const programInstance of programInstances) { - astAndProgram = getAstFromProgram(programInstance, filePath); + astAndProgram = getAstFromProgram(programInstance, parseSettings.filePath); // Stop at the first applicable program instance if (astAndProgram) { break; } } - if (!astAndProgram) { - const relativeFilePath = path.relative( - tsconfigRootDir || process.cwd(), - filePath, - ); - const errorLines = [ - '"parserOptions.programs" has been provided for @typescript-eslint/parser.', - `The file was not found in any of the provided program instance(s): ${relativeFilePath}`, - ]; - - throw new Error(errorLines.join('\n')); + if (astAndProgram) { + astAndProgram.program.getTypeChecker(); // ensure parent pointers are set in source files + return astAndProgram; } - astAndProgram.program.getTypeChecker(); // ensure parent pointers are set in source files + const relativeFilePath = path.relative( + parseSettings.tsconfigRootDir, + parseSettings.filePath, + ); + + const [typeSource, typeSources] = + parseSettings.projects.size > 0 + ? ['project', 'project(s)'] + : ['programs', 'program instance(s)']; + + const errorLines = [ + `"parserOptions.${typeSource}" has been provided for @typescript-eslint/parser.`, + `The file was not found in any of the provided ${typeSources}: ${relativeFilePath}`, + ]; - return astAndProgram; + throw new Error(errorLines.join('\n')); } /** diff --git a/packages/typescript-estree/tests/lib/semanticInfo.test.ts b/packages/typescript-estree/tests/lib/semanticInfo.test.ts index f6818dc14076..eb0426399afb 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.test.ts @@ -41,6 +41,10 @@ function createOptions(fileName: string): TSESTreeOptions & { cwd?: string } { beforeEach(() => clearCaches()); describe('semanticInfo', () => { + beforeEach(() => { + process.env.TSESTREE_SINGLE_RUN = ''; + }); + // test all AST snapshots testFiles.forEach(filename => { const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8'); @@ -327,6 +331,24 @@ describe('semanticInfo', () => { const parseResult = parseAndGenerateServices(code, optionsProjectString); expect(parseResult.services.program).toBe(program1); }); + + it('file not in single provided project instance in single-run mode should throw', () => { + process.env.TSESTREE_SINGLE_RUN = 'true'; + const filename = 'non-existent-file.ts'; + const options = createOptions(filename); + const optionsWithProjectTrue = { + ...options, + project: true, + programs: undefined, + }; + expect(() => + parseAndGenerateServices('const foo = 5;', optionsWithProjectTrue), + ).toThrow( + process.env.TYPESCRIPT_ESLINT_PROJECT_SERVICE === 'true' + ? `${filename} was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject.` + : `The file was not found in any of the provided project(s): ${filename}`, + ); + }); } it('file not in single provided program instance should throw', () => { diff --git a/packages/website/src/hooks/useClipboard.ts b/packages/website/src/hooks/useClipboard.ts index 0c07beacf445..82bdd1135505 100644 --- a/packages/website/src/hooks/useClipboard.ts +++ b/packages/website/src/hooks/useClipboard.ts @@ -9,7 +9,7 @@ export function useClipboard(code: () => string): useClipboardResult { const copy = useCallback( () => - void navigator.clipboard.writeText(code()).then(() => { + navigator.clipboard.writeText(code()).then(() => { setCopied(true); }), [setCopied, code],