Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: [no-unnecessary-condition] use assignability APIs in no-unnecessary-condition (POC)#10378

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
kirkwaiblinger wants to merge6 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromkirkwaiblinger:nuc-assignability-3

Conversation

kirkwaiblinger
Copy link
Member

@kirkwaiblingerkirkwaiblinger commentedNov 23, 2024
edited
Loading

PR Checklist

Overview

This fundamentally changes the truthiness/falsiness algorithm used in no-unnecessary-condition. Up until now, the rule has checked various heuristics for possible falsiness: essentially whether the type is a known falsy literal and whether the type has TS's (inconsistent) PossiblyFalsy flag set. The proposed change is to use the assignability API to simply check whether one of JS's few falsy types is assignable to the type being investigated.

This has a few interesting consequences that improve correctness....

// used to flag, no longer flagsfunctionusesEmptyObject(x:{}){if(x){console.log('truthy case')}else{console.log('truthy case')}}// becauseusesEmptyObject(false);// used to flag, no longer flagsfunctionusesNumberyObject(x:{toFixed:()=>string}){if(x){console.log('truthy case')}else{console.log('truthy case')}}// becauseusesNumberyObject(0);

Annoyingly, this also has the drawback that the following no longer flags.

constneverNull={};if(neverNull){}

That's due to TS's (questionable?) decision to giveneverNull type{} instead of typeobject, even when initializing aconst variable.

The following do still flag

constneverNull:object={};if(neverNull){}// this is a TS error tooif({}){}declarefunctionassert(x:unknown): assertsx;// flagged by no-unnecessary-condition and not a TS error.assert({});

If that specific case is something we want to preserve, wecould do so with scope analysis.

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@kirkwaiblinger!

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 onhttps://opencollective.com/typescript-eslint.

@netlifyNetlify
Copy link

netlifybot commentedNov 23, 2024
edited
Loading

Deploy Preview fortypescript-eslint ready!

NameLink
🔨 Latest commit0b8d6b8
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/67423489b440f60008947bea
😎 Deploy Previewhttps://deploy-preview-10378--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: 99 (🟢 up 1 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 yourNetlify site configuration.

@nx-cloudNx Cloud
Copy link

nx-cloudbot commentedNov 23, 2024
edited
Loading

☁️ Nx Cloud Report

CI is running/has finished running commands for commit0b8d6b8. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 fromNxCloud.

@codecovCodecov
Copy link

codecovbot commentedNov 23, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.67%. Comparing base(88e4c66) to head(0b8d6b8).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@##             main   #10378      +/-   ##==========================================- Coverage   86.69%   86.67%   -0.03%==========================================  Files         434      434                Lines       15227    15226       -1       Branches     4445     4442       -3     ==========================================- Hits        13201    13197       -4- Misses       1673     1675       +2- Partials      353      354       +1
FlagCoverage Δ
unittest86.67% <100.00%> (-0.03%)⬇️

Flags with carried forward coverage won't be shown.Click here to find out more.

Files with missing linesCoverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts98.00% <100.00%> (-1.21%)⬇️

@kirkwaiblingerkirkwaiblinger changed the titlePOC - using assignability APIs in no-unnecessary-conditionfix: [no-unnecessary-condition] use assignability APIs in no-unnecessary-condition (POC)Nov 23, 2024
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldberg left a comment
edited
Loading

Choose a reason for hiding this comment

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

I like this direction! Nicely done 🙂

@@ -107,7 +107,7 @@ function assert(condition: unknown): asserts condition {

assert(false); // Unnecessary; condition is always falsy.

const neverNull = {};
const neverNull = { someProperty: 'someValue'};

Choose a reason for hiding this comment

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

That's due to TS's (questionable?) decision to giveneverNull type{} instead of typeobject, even when initializing aconst variable.

😩 this again... is there a TypeScript issue filed that we can reference? It feels like a bug to me.

I also don't mind it very much. It's not often that folks use{}. This docs change kind of makes it more realistic to me.

kirkwaiblinger reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

😩 this again... is there a TypeScript issue filed that we can reference? It feels like a bug to me.

That's a good question. I'll take an action item to investigate this.

I also don't mind it very much. It's not often that folks use{}. This docs change kind of makes it more realistic to me.

Agreed that the docs change is probably an improvement. But, the false negative here makes the rule a little less understandable to me, so I'd lean on the side of wanting it to be correct if we can get it to be.

JoshuaKGoldberg reacted with thumbs up emoji
Copy link
MemberAuthor

@kirkwaiblingerkirkwaiblingerNov 24, 2024
edited
Loading

Choose a reason for hiding this comment

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

Oh, dear, the moment I start playing around with this, I immediately find infuriating behavior around{}....

functiontakesObject(x:object){if(x){console.log('should always be true')}else{console.error('this should be unreachable');}}consto:{}=0;// no error here!?!?!?!?takesObject(o);

I hate the{} type so much.

JoshuaKGoldberg reacted with laugh emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Regardingconst o = {}; // has type {}, started discord conversation athttps://discord.com/channels/508357248330760243/508357638677856287/1310419206260654081.

Regarding{} ->object assignments, filedmicrosoft/TypeScript#60582... and immediately learned this is a duplicate ofmicrosoft/TypeScript#56205, in which this behavior is deemed intentional 🫤.

Reminder to self: I'm pretty sure we'll have some good info to add to the motivation forhttps://typescript-eslint.io/rules/no-empty-object-type/ as an outcome of these discussions 😆

JoshuaKGoldberg reacted with laugh emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
DO NOT MERGEPRs which should not be merged yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@kirkwaiblinger@JoshuaKGoldberg

[8]ページ先頭

©2009-2025 Movatter.jp