Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 3 commits intotypescript-eslint:mainfromyeonjuan:fixes/10509
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -273,6 +273,10 @@ export default createRule<Options, MessageId>({
compilerOptions,
'strictNullChecks',
);
const isNoUncheckedIndexedAccess = tsutils.isCompilerOptionEnabled(
compilerOptions,
'noUncheckedIndexedAccess',
);

if (
!isStrictNullChecks &&
Expand DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp