8000 fix(eslint-plugin): [no-unnecessary-condition] handle noUncheckedIndexedAccess true by yeonjuan · Pull Request #10514 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [no-unnecessary-condition] handle noUncheckedIndexedAccess true #10514

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
merged 3 commits into from
Dec 23, 2024
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
18 changes: 13 additions & 5 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ export default createRule<Options, MessageId>({
compilerOptions,
'strictNullChecks',
);
const isNoUncheckedIndexedAccess = tsutils.isCompilerOptionEnabled(
compilerOptions,
'noUncheckedIndexedAccess',
);

if (
!isStrictNullChecks &&
Expand Down Expand Up @@ -756,11 +760,15 @@ export default createRule<Options, MessageId>({
}
const indexInfo = checker.getIndexInfosOfType(type);

return indexInfo.some(
info =>
getTypeName(checker, info.keyType) === 'string' &&
isNullableType(info.type),
);
return indexInfo.some(info => {
const isStringTypeName =
getTypeName(checker, info.keyType) === 'string';

return (
isStringTypeName &&
(isNoUncheckedIndexedAccess || isNullableType(info.type))
);
});
});
return !isOwnNullable && isNullableType(prevType);
}
Expand Down
68 changes: 53 additions & 15 deletions packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const optionsWithExactOptionalPropertyTypes = {
tsconfigRootDir: rootPath,
};

const optionsWithNoUncheckedIndexedAccess = {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
};

const necessaryConditionTest = (condition: string): string => `
declare const b1: ${condition};
declare const b2: boolean;
Expand Down Expand Up @@ -591,11 +597,7 @@ const key = '1' as BrandedKey;
foo?.[key]?.trim();
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
{
Expand Down Expand Up @@ -649,11 +651,7 @@ function Foo(outer: Outer, key: Foo): number | undefined {
}
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
{
Expand All @@ -666,11 +664,51 @@ declare const key: Key;
foo?.[key]?.trim();
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
{
code: `
type Foo = {
key?: Record<string, { key: string }>;
};
declare const foo: Foo;
foo.key?.someKey?.key;
`,
languageOptions: {
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
{
code: `
type Foo = {
key?: {
[key: string]: () => void;
};
};
declare const foo: Foo;
foo.key?.value?.();
`,
languageOptions: {
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
{
code: `
type A = {
[name in Lowercase<string>]?: {
[name in Lowercase<string>]: {
a: 1;
};
};
};

declare const a: A;

a.a?.a?.a;
`,
languageOptions: {
parserOptions: optionsWithNoUncheckedIndexedAccess,
},
},
`
Expand Down
Loading
0