Skip to content
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
20 changes: 12 additions & 8 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export default createRule<Options, MessageId>({
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (isArrayIndexExpression(expression)) {
if (!isNoUncheckedIndexedAccess && isArrayIndexExpression(expression)) {
return;
}

Expand Down Expand Up @@ -424,12 +424,13 @@ export default createRule<Options, MessageId>({
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (
!isArrayIndexExpression(node) &&
!(
node.type === AST_NODE_TYPES.ChainExpression &&
node.expression.type !== AST_NODE_TYPES.TSNonNullExpression &&
optionChainContainsOptionArrayIndex(node.expression)
)
isNoUncheckedIndexedAccess ||
(!isArrayIndexExpression(node) &&
!(
node.type === AST_NODE_TYPES.ChainExpression &&
node.expression.type !== AST_NODE_TYPES.TSNonNullExpression &&
optionChainContainsOptionArrayIndex(node.expression)
))
) {
messageId = 'neverNullish';
}
Expand Down Expand Up @@ -835,7 +836,10 @@ export default createRule<Options, MessageId>({
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (optionChainContainsOptionArrayIndex(node)) {
if (
!isNoUncheckedIndexedAccess &&
optionChainContainsOptionArrayIndex(node)
) {
return;
}

Expand Down
146 changes: 146 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ const tuple = ['foo'] as const;
declare const n: number;
tuple[n]?.toUpperCase();
`,
{
code: `
declare const arr: Array<{ value: string } & (() => void)>;
if (arr[42]?.value) {
}
arr[41]?.();
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
},
`
if (arr?.[42]) {
}
Expand All @@ -417,6 +432,23 @@ declare const foo: TupleA | TupleB;
declare const index: number;
foo[index]?.toString();
`,
{
code: `
type TupleA = [string, number];
type TupleB = [string, number];

declare const foo: TupleA | TupleB;
declare const index: number;
foo[index]?.toString();
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
},
`
declare const returnsArr: undefined | (() => string[]);
if (returnsArr?.()[42]) {
Expand Down Expand Up @@ -3272,5 +3304,119 @@ declare const t: T;
t.a.a.a.value;
`,
},
{
code: `
declare const test: Array<{ a?: string }>;

if (test[0]?.a) {
test[0]?.a;
}
`,
errors: [
{
column: 10,
endColumn: 12,
endLine: 5,
line: 5,
messageId: 'neverOptionalChain',
},
],
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
output: `
declare const test: Array<{ a?: string }>;

if (test[0]?.a) {
test[0].a;
}
`,
},
{
code: `
declare const arr2: Array<{ x: { y: { z: object } } }>;
arr2[42]?.x?.y?.z;
`,
errors: [
{
column: 12,
endColumn: 14,
endLine: 3,
line: 3,
messageId: 'neverOptionalChain',
},
{
column: 15,
endColumn: 17,
endLine: 3,
line: 3,
messageId: 'neverOptionalChain',
},
],
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
output: `
declare const arr2: Array<{ x: { y: { z: object } } }>;
arr2[42]?.x.y.z;
`,
},
{
code: `
declare const arr: string[];

if (arr[0]) {
arr[0] ?? 'foo';
}
`,
errors: [
{
column: 3,
endColumn: 9,
endLine: 5,
line: 5,
messageId: 'neverNullish',
},
],
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
},
{
code: `
declare const arr: object[];

if (arr[42] && arr[42]) {
}
`,
errors: [
{
column: 16,
endColumn: 23,
endLine: 4,
line: 4,
messageId: 'alwaysTruthy',
},
],
languageOptions: {
parserOptions: {
project: './tsconfig.noUncheckedIndexedAccess.json',
projectService: false,
tsconfigRootDir: getFixturesRootDir(),
},
},
},
],
});
Loading