Skip to content

Commit

Permalink
fix(typescript-estree): don't consider a cached program unless it's s…
Browse files Browse the repository at this point in the history
…pecified in the current `parserOptions.project` config (#5999)
  • Loading branch information
bradzacher committed Nov 16, 2022
1 parent 49f623f commit 530e0e6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
@@ -1,14 +1,11 @@
import path from 'path';

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

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!).
continue;
}
let fileList = programFileListCache.get(tsconfigPath);
let updatedProgram: ts.Program | null = null;
if (!fileList) {
Expand Down

0 comments on commit 530e0e6

Please sign in to comment.