Skip to content

fix(eslint-plugin): [no-unnecessary-condition] handle left-hand optional with exactOptionalPropertyTypes option #8249

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 2 commits into from
Feb 1, 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
30 changes: 29 additions & 1 deletion packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ export default createRule<Options, MessageId>({
);
}

function isNullableMemberExpression(
node: TSESTree.MemberExpression,
): boolean {
const objectType = services.getTypeAtLocation(node.object);
if (node.computed) {
const propertyType = services.getTypeAtLocation(node.property);
return isNullablePropertyType(objectType, propertyType);
}
const property = node.property;

if (property.type === AST_NODE_TYPES.Identifier) {
const propertyType = objectType.getProperty(property.name);
if (
propertyType &&
tsutils.isSymbolFlagSet(propertyType, ts.SymbolFlags.Optional)
) {
return true;
}
}
return false;
}

/**
* Checks if a conditional node is necessary:
* if the type of the node is always true or always false, it's not necessary.
Expand Down Expand Up @@ -283,7 +305,13 @@ export default createRule<Options, MessageId>({
let messageId: MessageId | null = null;
if (isTypeFlagSet(type, ts.TypeFlags.Never)) {
messageId = 'never';
} else if (!isPossiblyNullish(type)) {
} else if (
!isPossiblyNullish(type) &&
!(
node.type === AST_NODE_TYPES.MemberExpression &&
isNullableMemberExpression(node)
)
) {
// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const ruleTester = new RuleTester({
},
});

const optionsWithExactOptionalPropertyTypes = {
tsconfigRootDir: rootPath,
project: './tsconfig.exactOptionalPropertyTypes.json',
};

const ruleError = (
line: number,
column: number,
Expand Down Expand Up @@ -748,6 +753,48 @@ declare let foo: number;
foo ||= 1;
`,
`
declare const foo: { bar: { baz?: number; qux: number } };
type Key = 'baz' | 'qux';
declare const key: Key;
foo.bar[key] ??= 1;
`,
`
enum Keys {
A = 'A',
B = 'B',
}
type Foo = {
[Keys.A]: number | null;
[Keys.B]: number;
};
declare const foo: Foo;
declare const key: Keys;
foo[key] ??= 1;
`,
{
code: `
declare const foo: { bar?: number };
foo.bar ??= 1;
`,
parserOptions: optionsWithExactOptionalPropertyTypes,
},
{
code: `
declare const foo: { bar: { baz?: number } };
foo['bar'].baz ??= 1;
`,
parserOptions: optionsWithExactOptionalPropertyTypes,
},
{
code: `
declare const foo: { bar: { baz?: number; qux: number } };
type Key = 'baz' | 'qux';
declare const key: Key;
foo.bar[key] ??= 1;
`,
parserOptions: optionsWithExactOptionalPropertyTypes,
},
`
declare let foo: number;
foo &&= 1;
`,
Expand Down Expand Up @@ -2035,6 +2082,22 @@ foo &&= null;
},
],
},
{
code: `
declare const foo: { bar: number };
foo.bar ??= 1;
`,
parserOptions: optionsWithExactOptionalPropertyTypes,
errors: [
{
messageId: 'neverNullish',
line: 3,
endLine: 3,
column: 1,
endColumn: 8,
},
],
},
{
code: noFormat`
type Foo = { bar: () => number } | null;
Expand Down