Skip to content

fix(typescript-estree): specific error for parserOptions.project not including a file #9584

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 @@ -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<ts.Program>,
{ 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,
Copy link
Member Author

Choose a reason for hiding this comment

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

This used to be tsconfigRootDir || process.cwd(), but I'm pretty sure that's unnecessary. createParseSettings already defaults tsconfigRootDir to process.cwd().

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'));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/typescript-estree/tests/lib/semanticInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/hooks/useClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function useClipboard(code: () => string): useClipboardResult {

const copy = useCallback(
() =>
void navigator.clipboard.writeText(code()).then(() => {
navigator.clipboard.writeText(code()).then(() => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is also in the v8 branch. GitHub just isn't updating...

setCopied(true);
}),
[setCopied, code],
Expand Down
Loading