Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-duplicate-type-constituents] prevent unnecessary| undefined for optional parameters#9479
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for the PR,@abrahamguo! 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. |
netlifybot commentedJul 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview fortypescript-eslint ready!
To edit notification comments on pull requests, go to yourNetlify site configuration. |
nx-cloudbot commentedJul 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
codecovbot commentedJul 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@## main #9479 +/- ##==========================================+ Coverage 88.05% 88.06% +0.01%========================================== Files 407 407 Lines 13947 13961 +14 Branches 4071 4077 +6 ==========================================+ Hits 12281 12295 +14 Misses 1354 1354 Partials 312 312
Flags with carried forward coverage won't be shown.Click here to find out more.
|
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
| TSUnionType(node):void{ | ||
| checkDuplicate(node); | ||
| constmaybeTypeAnnotation=node.parent; | ||
| if(maybeTypeAnnotation.type===AST_NODE_TYPES.TSTypeAnnotation){ | ||
| constmaybeIdentifier=maybeTypeAnnotation.parent; | ||
| if( | ||
| maybeIdentifier.type===AST_NODE_TYPES.Identifier&& | ||
| maybeIdentifier.optional | ||
| ){ | ||
| constmaybeFunction=maybeIdentifier.parent; | ||
| const{ type}=maybeFunction; | ||
| if( | ||
| (type===AST_NODE_TYPES.ArrowFunctionExpression|| | ||
| type===AST_NODE_TYPES.FunctionDeclaration|| | ||
| type===AST_NODE_TYPES.FunctionExpression)&& | ||
| maybeFunction.params.includes(maybeIdentifier) | ||
| ){ | ||
| constexplicitUndefined=node.types.find( | ||
| ({ type})=>type===AST_NODE_TYPES.TSUndefinedKeyword, | ||
| ); | ||
| if(explicitUndefined){ | ||
| report('unnecessary',explicitUndefined); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is not currently handled even in the existing rule functionality — we'd need to implement some recursion handling for this. I've gone ahead and opened#9496 for further discussion
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
…nnecessary-undefined
abrahamguo commentedJul 31, 2024
Work done:
Ready for review! |
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
…nnecessary-undefined
| type===AST_NODE_TYPES.FunctionDeclaration|| | ||
| type===AST_NODE_TYPES.FunctionExpression)&& | ||
| (isFunctionOrFunctionType(maybeFunction)|| | ||
| maybeFunction.type===AST_NODE_TYPES.TSDeclareFunction)&& |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Created#9788 to addTSDeclareFunction inutils.
auvred commentedAug 24, 2024
Let's wait for#9788 to get merged |
abrahamguo commentedAug 27, 2024
Updated now that#9788 has been merged ✅ |
auvred left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
💯
jswalden commentedSep 5, 2024
Hi, maybe I'm missing something here, but...why does this lint make sense to have for For But for Maybe I'm just too TypeScript n00b. But it seems like if you're intersecting type sets not with intention of asserting non-overlap, the only way you get any value from doing so is if thereis duplication. (This was, in fact, exactly what I was intentionally doing when I just ran into this in my own code.) |
abrahamguo commentedSep 6, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@jswalden One place I like to use it is to combine two object types, like so: Here, we're using |
jswalden commentedSep 6, 2024
@abrahamguo Interesting! I probably should have realized that possibility. 🙂 I guess I don't have good ideas about |
abrahamguo commentedSep 6, 2024
Can you show the example from your code, where you ended up needing to use a disable comment? |
jswalden commentedSep 6, 2024
@abrahamguo The lines are here:https://github.com/bitfocus/companion-module-allenheath-sq/blob/50255dd03c4d2c2efc9a1e84e2a97483570c29f2/src/presets.ts#L17 I'll try to briefly summarize the enclosing use, as the line alone is probably obscure. Companion is software you can use to define connections to control devices (or more-abstract stuff like a Slack connection or a Google spreadsheet), by programming buttons on various hardware devices (such as theStream Deck). My code defines a module implementing a connection to control an Allen & Heath sound mixer. Companion modules in part define a set of "actions" Companion users can invoke to control the underlying device, and a set of "feedbacks" that expose device status changes. Users can assign series of actions to a hardware button, to be performed when it's pressed/released. A modulealso can define "presets": predefined buttons for simple, common use cases. Actions and feedbacks are identified using separate string ID sets. In this module, I represent "mute this input or output" action and feedback IDs using the This module has presets that show whether a particular mixer input/output is muted, that will toggle muting when pressed. When I define these presets, I use the keys of those enums to specify the particular kind of mute button being defined. Iintersect those keys to get a key that identifies both a mute action and a mute feedback. I can then use I hope that's reasonably clear without being excessively detailed! |
abrahamguo commentedSep 7, 2024
In this case, shouldn't If the two |
jswalden commentedSep 8, 2024
@abrahamguo They're type-identical, but they are notsemantically identical. Feedback IDs aren't the same as action IDs. |

PR Checklist
| undefinedfor optional parameters #9203