Skip to content

fix(eslint-plugin): [prefer-nullish-coalescing] handle generic type #11019

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
48 changes: 34 additions & 14 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isNodeOfTypes,
isNullLiteral,
isNullableType,
isTypeFlagSet,
isUndefinedIdentifier,
nullThrows,
NullThrowsReasons,
Expand Down Expand Up @@ -211,7 +212,10 @@ export default createRule<Options, MessageIds>({
* a nullishness check, taking into account the rule's configuration.
*/
function isTypeEligibleForPreferNullish(type: ts.Type): boolean {
if (!isNullableType(type)) {
if (
!isNullableType(type) &&
!isTypeFlagSet(type, ts.TypeFlags.TypeParameter)
) {
return false;
}

Expand All @@ -235,16 +239,7 @@ export default createRule<Options, MessageIds>({
return true;
}

// if the type is `any` or `unknown` we can't make any assumptions
// about the value, so it could be any primitive, even though the flags
// won't be set.
//
// technically, this is true of `void` as well, however, it's a TS error
// to test `void` for truthiness, so we don't need to bother checking for
// it in valid code.
if (
tsutils.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)
) {
if (isIndeterminateType(type)) {
return false;
}

Expand All @@ -254,7 +249,7 @@ export default createRule<Options, MessageIds>({
.some(t =>
tsutils
.intersectionTypeParts(t)
.some(t => tsutils.isTypeFlagSet(t, ignorableFlags)),
.some(t => isTypeFlagSet(t, ignorableFlags)),
)
) {
return false;
Expand Down Expand Up @@ -447,12 +442,12 @@ export default createRule<Options, MessageIds>({
const type = parserServices.getTypeAtLocation(
nullishCoalescingLeftNode,
);
const flags = getTypeFlags(type);

if (flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
if (isIndeterminateType(type)) {
return false;
}

const flags = getTypeFlags(type);
const hasNullType = (flags & ts.TypeFlags.Null) !== 0;

// it is fixable if we check for undefined and the type is not nullable
Expand Down Expand Up @@ -932,3 +927,28 @@ function formatComments(
)
.join('');
}

/**
* Returns `true` if the provided type is `any`, `unknown`,
* or a union type that includes a type parameter.
*
* In these cases, no assumptions can be made about the value's structure or behavior,
* as it might represent `undefined`, `null`, or any primitive,
* even if the standard type flags are not set.
*
* Note: This could technically include `void` as well, but testing for `void` is unnecessary,
* since evaluating `void` for truthiness results in a TypeScript error.
*/
function isIndeterminateType(type: ts.Type): boolean {
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return true;
}

return tsutils
.typeParts(type)
.some(t =>
tsutils
.unionTypeParts(t)
.some(t => isTypeFlagSet(t, ts.TypeFlags.TypeParameter)),
);
}
Loading