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(eslint-plugin): [no-unnecessary-condition] ignore optionally accessing index signatures#6762

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

Conversation

Josh-Cena
Copy link
Member

@Josh-CenaJosh-Cena commentedMar 24, 2023
edited
Loading

PR Checklist

Overview

hehex9 reacted with hooray emojikarlhorky, schoero, jtbandes, and hehex9 reacted with heart emoji
@typescript-eslint
Copy link
Contributor

Thanks for the PR,@Josh-Cena!

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.

@netlify
Copy link

netlifybot commentedMar 24, 2023
edited
Loading

Deploy Preview fortypescript-eslint ready!

NameLink
🔨 Latest commitfd1488a
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/641e13f5c2740d0008b88042
😎 Deploy Previewhttps://deploy-preview-6762--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to yourNetlify site settings.

@nx-cloud
Copy link

nx-cloudbot commentedMar 24, 2023
edited
Loading

☁️ Nx Cloud Report

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

📂 See all runs for this branch


✅ Successfully ran 25 targets

Sent with 💌 fromNxCloud.

Comment on lines -1673 to -1679
function getElem(dict: Record<string, { foo: string }>, key: string) {
if (dict[key]) {
return dict[key].foo;
} else {
return '';
}
}
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

This was arguably a bug fix in the existing test case, because it shouldn't be reported either.

armano2 reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

This actually should be reported because the fixture config doesn't usenoUncheckedIndexAccess!

I think we should probably guard the new code around whether or not the flag is on.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

@bradzacher This code is the exact same principle asdictionary[key] ??= defaultVal. If we want to ignore that, we have to ignore others as well.

Copy link
Member

Choose a reason for hiding this comment

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

They are and they aren't. You're correct in that they're both record index access in a logical position - so they're semantically the same case. However TS reports the types differently for the two cases.

In this instance TS will report the read type, meaning that ifnoUncheckedIndexAccess is turned on then TS will report the type asT | undefimed to us.

However inx ?? y, TS will report the write type, meaning it reports justT - no matter whether or notnoUncheckedIndexAccess is on or not.

This is the fundamental difference between the two cases and why we are false positiving on the assignment operators, butnot on this case.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

However, we already ignore array index access, even withoutnoUncheckedIndexAccess. See this test case:

declareconstarr:{foo:number}[];constbar=arr[42]?.foo??0;

I find it quite natural to extend the scope to all index signatures, since there isn't something special about array indices.

Copy link
Member

Choose a reason for hiding this comment

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

The reason we did it for arrays and not objects is because defining arrays as(T | undefined)[] is similar toRecord<string, T | undefined>, but far, far less ergonomic syntactically and is arguably worse given how common it is to iterate arrays directly using an iterator method or for/of.

For objects it's pretty natural to union inundefined because most usecases for reading is an index signature - the direct iteration methods (Object.entries /Object.values) are both rarer usecases so theundefined doesn't get in the way as much.

Now that we havenoUncheckedIndexAccess - we don't really need the logic at all, but I understand that some people think the compiler option goes too far and can get in the way (which is true to a degree).

The motivation behind this change isn't bringing the two sets up to parity (that's a much larger change that we should discuss as a group to decide if we think we should treat objects the same as arrays in this regard) - instead it's just fixing the current false positives we have due to the logical operator handling we added in#6594

@codecov
Copy link

codecovbot commentedMar 24, 2023
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (main@bc31078).Click here to learn what that means.
Report is 873 commits behind head on main.

❗ Current head4ac41b6 differs from pull request most recent headfd1488a. Consider uploading reports for the commitfd1488a to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@##             main    #6762   +/-   ##=======================================  Coverage        ?   87.30%           =======================================  Files           ?      384             Lines           ?    13148             Branches        ?     3863           =======================================  Hits            ?    11479             Misses          ?     1302             Partials        ?      367
FlagCoverage Δ
unittest87.30% <100.00%> (?)

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

FilesCoverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts98.45% <100.00%> (ø)

@bradzacherbradzacher changed the titlefix(eslint-plugin): [no-unnec-cond] ignore optionally accessing index signaturesfix(eslint-plugin): [no-unnecessary-condition] ignore optionally accessing index signaturesMar 26, 2023
Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

Thanks for getting straight on this!
Almost there I think!

Comment on lines -1673 to -1679
function getElem(dict: Record<string, { foo: string }>, key: string) {
if (dict[key]) {
return dict[key].foo;
} else {
return '';
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This actually should be reported because the fixture config doesn't usenoUncheckedIndexAccess!

I think we should probably guard the new code around whether or not the flag is on.

@bradzacherbradzacher added bugSomething isn't working awaiting responseIssues waiting for a reply from the OP or another party labelsMar 27, 2023
karlhorky added a commit to upleveled/ical-move-events that referenced this pull requestApr 22, 2023
karlhorky added a commit to upleveled/ical-move-events that referenced this pull requestApr 22, 2023
* Update dependency upgrades - non-major* Require valid disables with explanations* Temporarily disable typescript-eslint ruleRef:typescript-eslint/typescript-eslint#6762---------Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>Co-authored-by: Karl Horky <karl.horky@gmail.com>
@JoshuaKGoldberg
Copy link
Member

👋@Josh-Cena just checking in, is this still something you have time for?

@Josh-Cena
Copy link
MemberAuthor

Yes! I'll get to it. Sorry for the wait. I still don't know what the best way to fix this is, because I feel like accounting for index signature optionality, even without the compiler option, should be acceptable.

JoshuaKGoldberg reacted with thumbs up emojiJoshuaKGoldberg reacted with rocket emoji

@hbiede
Copy link

Hoping this will be available in the near-term future. I'd love to use this rule without having to use dozens of casts

@karlhorky
Copy link

karlhorky commentedAug 16, 2023
edited
Loading

It seems like the TypeScript PRmicrosoft/TypeScript#54777 was merged, which closed the issue in the TypeScript repo:

Does that mean that this PR is now obsolete, once the TS PR changes are released in a version of TypeScript (eg. maybe 5.2)?

@Josh-Cena
Copy link
MemberAuthor

@karlhorky That PR changes the quick info of the write type so it excludesundefined (and the programmatic API always excludes it), which makes this PR even more necessary :)

karlhorky reacted with heart emojikarlhorky reacted with eyes emoji

@Josh-Cena
Copy link
MemberAuthor

@bradzacher Sorry it's been a really long time and I think neither of us are up to speed on this now. Let's recap—what (potential) false-positives should we fix and what should we continue erroring on? Currently we ignore allobject[index] whereobject has index signatures, because that would always be potentially undefined.

@github-actionsgithub-actionsbot removed the awaiting responseIssues waiting for a reply from the OP or another party labelOct 18, 2023
Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

originally I was against us bringing arrays and objects into parity.
however given the statenoUncheckedIndexAccess is in esp in regards to??= - I shall acquiesce here and we can loosen the rule to just ignore really anything with an index signature.

A few comments but otherwise looking good.

Comment on lines +192 to +195
if (checker.isTupleType(objType)) {
// If indexing a tuple with a literal, the type will be sound
return node.property.type !== AST_NODE_TYPES.Literal;
}
Copy link
Member

Choose a reason for hiding this comment

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

This check is actually no longer sound with modern TS.

Tuples can be variadic now - (eg[T1, ...T2[]],[...T1[], T2],[T1, ...T2[], T3],[...T1[]]) - so they're no more safe than an array.

I.e. forconst foo: [T1, ...T2[]] -foo[1] is possiblyundefined

playground

}
if (node.computed) {
const indexType = getNodeType(node.property);
if (isTypeAnyType(indexType)) {
Copy link
Member

Choose a reason for hiding this comment

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

question: do we want to handle thenever case here?
I think it's okay not to just figured I'd flag it for completeness sake

// No need to check anything about any
return true;
}
if (isTypeFlagSet(indexType, ts.TypeFlags.NumberLike)) {
Copy link
Member

Choose a reason for hiding this comment

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

This doens't handle "nominal" number types here.
EGtype T = number & { __brand: unknown } would work in the signature but we would not match it here.

I think it's okay that we ignore it here - it's a super niche usecase.
Just commenting for completeness's sake.

Comment on lines +202 to +208
if (isTypeFlagSet(indexType, ts.TypeFlags.NumberLike)) {
return (
checker.getIndexTypeOfType(objType, ts.IndexKind.Number) !==
undefined
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

we should probably also handlesymbol indexes as well.
They're super rare but it should just be one extraif

declareconstobj:{[k:symbol]:string};declareconstkey:symbol;constres=obj[key];res?.length// should not report

@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelNov 10, 2023
@JoshuaKGoldberg
Copy link
Member

👋 ping@Josh-Cena, do you still have time for this one?

@bradzacherbradzacher added the stalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period labelJan 27, 2024
@JoshuaKGoldberg
Copy link
Member

Closing this PR as it's been stale for a while without activity. If anybody wants to drive it forward, please do post your own PR - and if you use this as a start, consider adding a co-author attribution at the end of your PR description. Thanks! 😊

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsFeb 6, 2024
@Josh-CenaJosh-Cena deleted the nuc-index-signature branchJune 2, 2024 09:11
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@bradzacherbradzacherbradzacher requested changes

Assignees
No one assigned
Labels
awaiting responseIssues waiting for a reply from the OP or another partybugSomething isn't workingstalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-condition] warns about conditional assignment to index signature
5 participants
@Josh-Cena@JoshuaKGoldberg@hbiede@karlhorky@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp