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

fix(typescript-estree): don't consider a cached program unless it's specified in the current parserOptions.project config #5999

Merged
merged 3 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,14 +1,11 @@
import path from 'path';
Copy link
Member Author

Choose a reason for hiding this comment

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

the changes here were extracted from #5916 as an easy test for this behaviour.
could add a regression test against this, but I didn't bother because ultimately it's such a rare case.


import rule from '../../src/rules/non-nullable-type-assertion-style';
import { RuleTester } from '../RuleTester';
import { getFixturesRootDir, RuleTester } from '../RuleTester';

const rootDir = path.resolve(__dirname, '../fixtures/');
const ruleTester = new RuleTester({
parserOptions: {
sourceType: 'module',
tsconfigRootDir: rootDir,
project: './tsconfig.noUncheckedIndexedAccess.json',
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.json',
},
parser: '@typescript-eslint/parser',
});
Expand Down Expand Up @@ -62,35 +59,6 @@ const x = 1 as 1;
declare function foo<T = any>(): T;
const bar = foo() as number;
`,
`
function first<T>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | null>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | undefined>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | null | undefined>(
array: ArrayLike<T>,
): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
type A = 'a' | 'A';
type B = 'b' | 'B';
function first<T extends A | B | null>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
],

invalid: [
Expand Down Expand Up @@ -229,24 +197,76 @@ declare const x: T;
const y = x!;
`,
},
{
code: `
function first<T extends string | number>(array: ArrayLike<T>): T | null {
],
});

const ruleTesterWithNoUncheckedIndexAccess = new RuleTester({
parserOptions: {
sourceType: 'module',
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
parser: '@typescript-eslint/parser',
dependencyConstraints: {
typescript: '4.1',
},
});

ruleTesterWithNoUncheckedIndexAccess.run(
'non-nullable-type-assertion-style - noUncheckedIndexedAccess',
rule,
{
valid: [
`
function first<T>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
errors: [
{
column: 30,
line: 3,
messageId: 'preferNonNullAssertion',
},
],
output: `
`
function first<T extends string | null>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | undefined>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | null | undefined>(
array: ArrayLike<T>,
): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
type A = 'a' | 'A';
type B = 'b' | 'B';
function first<T extends A | B | null>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
],
invalid: [
{
code: `
function first<T extends string | number>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
errors: [
{
column: 30,
line: 3,
messageId: 'preferNonNullAssertion',
},
],
output: `
function first<T extends string | number>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0]!) : null;
}
`,
},
],
});
`,
},
],
},
);
Expand Up @@ -159,11 +159,21 @@ function getProgramsForProjects(parseSettings: ParseSettings): ts.Program[] {
);
}

const currentProjectsFromSettings = new Set(parseSettings.projects);

/*
* before we go into the process of attempting to find and update every program
* see if we know of a program that contains this file
*/
for (const [tsconfigPath, existingWatch] of knownWatchProgramMap.entries()) {
if (!currentProjectsFromSettings.has(tsconfigPath)) {
// the current parser run doesn't specify this tsconfig in parserOptions.project
// so we don't want to consider it for caching purposes.
//
// if we did consider it we might return a program for a project
// that wasn't specified in the current parser run (which is obv bad!).
Copy link
Member

Choose a reason for hiding this comment

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

To you, maybe 😛

Suggested change
// that wasn't specified in the current parser run (which is obv bad!).
// that wasn't specified in the current parser run (which is bad!).

continue;
}
let fileList = programFileListCache.get(tsconfigPath);
let updatedProgram: ts.Program | null = null;
if (!fileList) {
Expand Down