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): [no-unnecessary-condition] fix false positive for type variable #8235

Merged
merged 2 commits into from
Jan 19, 2024

Conversation

yeonjuan
Copy link
Contributor

PR Checklist

Overview

Fix the following false positive cases.

function foo<T extends object>(
  arg: T,
  key: keyof T
): void {
  const t1 = arg[key] == null;
  const t2 = null == arg[key];
  const t3 = arg[key] != null;
  const t4 = null != arg[key];
  const t5 = arg[key] == undefined;
  const t6 = undefined == arg[key];
  const t7 = arg[key] != undefined;
  const t8 = undefined != arg[key];
  const t9 = arg[key] === null;
  const t10 = null === arg[key];
  const t11 = arg[key] !== null;
  const t12 = null !== arg[key];
  const t13 = arg[key] === undefined;
  const t14 = undefined === arg[key];
  const t15 = arg[key] !== undefined;
  const t16 = undefined !== arg[key];
  arg[key] ?? 'default';
  arg[key] ??= 'default';
  arg[key] ||= 'default';
}

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @yeonjuan!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Jan 10, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit c2fbb1c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65aa5ac8fd342e0008166fcd
😎 Deploy Preview https://deploy-preview-8235--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 90 (🔴 down 7 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@yeonjuan yeonjuan force-pushed the fix/7850 branch 2 times, most recently from d7befe4 to b7741f8 Compare January 10, 2024 16:54
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

This looks great to me, thanks for the fix!

Just one note on testing, but I'm happy to merge as-is or make the cleanup myself given that the tests for this rule already do the "test a lot in one case" thing.

Lime yellow wacky inflatable arm tube thing dancing happily

@JoshuaKGoldberg JoshuaKGoldberg added 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge awaiting response Issues waiting for a reply from the OP or another party labels Jan 11, 2024
Comment on lines 257 to 259
function foo<T extends object>(arg: T, key: keyof T): void {
const t1 = arg[key] == null;
const t2 = null == arg[key];
const t3 = arg[key] != null;
const t4 = null != arg[key];
const t5 = arg[key] == undefined;
const t6 = undefined == arg[key];
const t7 = arg[key] != undefined;
const t8 = undefined != arg[key];
const t9 = arg[key] === null;
const t10 = null === arg[key];
const t11 = arg[key] !== null;
const t12 = null !== arg[key];
const t13 = arg[key] === undefined;
const t14 = undefined === arg[key];
const t15 = arg[key] !== undefined;
const t16 = undefined !== arg[key];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
function foo<T extends object>(arg: T, key: keyof T): void {
const t1 = arg[key] == null;
const t2 = null == arg[key];
const t3 = arg[key] != null;
const t4 = null != arg[key];
const t5 = arg[key] == undefined;
const t6 = undefined == arg[key];
const t7 = arg[key] != undefined;
const t8 = undefined != arg[key];
const t9 = arg[key] === null;
const t10 = null === arg[key];
const t11 = arg[key] !== null;
const t12 = null !== arg[key];
const t13 = arg[key] === undefined;
const t14 = undefined === arg[key];
const t15 = arg[key] !== undefined;
const t16 = undefined !== arg[key];
}
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] == null;
}

Comment on lines 770 to 773
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] ??= 'default';
arg[key] ||= 'default';
arg[key] &&= 'default';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] ??= 'default';
arg[key] ||= 'default';
arg[key] &&= 'default';
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] ??= 'default';

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jan 15, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

💯 looks great, thanks as always!

Animation of a happy blue animal with raised hands. The animal wipes and spins from left of view to right of view

@JoshuaKGoldberg JoshuaKGoldberg merged commit 920f909 into typescript-eslint:main Jan 19, 2024
57 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-condition] false positive on indexed access of a generic type
2 participants