Skip to content
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

fix(eslint-plugin): [non-nullable-type-assertion-style] fix false positive when asserting to a generic type that might be nullish #4509

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,7 +17,7 @@ export default util.createRule({
fixable: 'code',
messages: {
preferNonNullAssertion:
'Use a ! assertion to more succintly remove null and undefined from the type.',
'Use a ! assertion to more succinctly remove null and undefined from the type.',
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
},
schema: [],
type: 'suggestion',
Expand All @@ -43,22 +43,42 @@ export default util.createRule({
return tsutils.unionTypeParts(type);
};

const couldBeNullish = (type: ts.Type): boolean => {
if (type.flags & ts.TypeFlags.TypeParameter) {
const constraint = type.getConstraint();
return constraint == null || couldBeNullish(constraint);
} else if (tsutils.isUnionType(type)) {
djcsdy marked this conversation as resolved.
Show resolved Hide resolved
for (const part of type.types) {
if (couldBeNullish(part)) {
return true;
}
}
return false;
} else {
return (
(type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0
);
}
};

const sameTypeWithoutNullish = (
assertedTypes: ts.Type[],
originalTypes: ts.Type[],
): boolean => {
const nonNullishOriginalTypes = originalTypes.filter(
type =>
type.flags !== ts.TypeFlags.Null &&
type.flags !== ts.TypeFlags.Undefined,
(type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) === 0,
);

if (nonNullishOriginalTypes.length === originalTypes.length) {
return false;
}

for (const assertedType of assertedTypes) {
if (!nonNullishOriginalTypes.includes(assertedType)) {
if (
couldBeNullish(assertedType) ||
!nonNullishOriginalTypes.includes(assertedType)
) {
return false;
}
}
Expand Down
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noUncheckedIndexedAccess": true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice find here. I shudder to think what other subtle edge cases have popped up because of that 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw this issue can arise even without noUncheckedIndexAccess, but off the top of my head I can't think of an example that isn't totally contrived :-).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh? I was playing around with this for a good bit to try to find one but couldn't find anything. Is there a code snippet you have in mind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find one either to be honest, but I didn't try all that hard. Maybe it really is dependent on the flag. But even if it isn't I think this fix should suffice. I'll do some experimentation.

}
}
Expand Up @@ -7,7 +7,7 @@ const ruleTester = new RuleTester({
parserOptions: {
sourceType: 'module',
tsconfigRootDir: rootDir,
project: './tsconfig.json',
project: './tsconfig.noUncheckedIndexedAccess.json',
},
parser: '@typescript-eslint/parser',
});
Expand Down Expand Up @@ -61,6 +61,16 @@ const x = 1 as 1;
declare function foo<T = any>(): T;
const bar = foo() as number;
`,
`
function first<T>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
`
function first<T extends string | null>(array: ArrayLike<T>): T | null {
djcsdy marked this conversation as resolved.
Show resolved Hide resolved
return array.length > 0 ? (array[0] as T) : null;
}
`,
],

invalid: [
Expand Down Expand Up @@ -199,5 +209,26 @@ declare const x: T;
const y = x!;
`,
},
{
code: `
function first<T extends string | number>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0] as T) : null;
}
`,
errors: [
{
column: 30,
line: 3,
messageId: 'preferNonNullAssertion',
},
],
// Output is not expected to match required formatting due to excess parentheses
// eslint-disable-next-line @typescript-eslint/internal/plugin-test-formatting
output: `
function first<T extends string | number>(array: ArrayLike<T>): T | null {
return array.length > 0 ? (array[0]!) : null;
}
`,
},
],
});