-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(eslint-plugin): add no-duplicate-type-constituents
rule
#5728
feat(eslint-plugin): add no-duplicate-type-constituents
rule
#5728
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 2bae913. 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 10 targets
Sent with 💌 from NxCloud. |
Thanks for the PR, @sajikix! 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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
A great start, thanks for sending this in! 💓
Left some requests for changes - lmk if any of that doesn't make sense!
packages/eslint-plugin/src/rules/no-duplicate-type-union-intersection-members.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-union-intersection-members.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-union-intersection-members.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/no-duplicate-type-union-intersection-members.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-union-intersection-members.ts
Outdated
Show resolved
Hide resolved
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #5728 +/- ##
==========================================
+ Coverage 87.24% 87.29% +0.05%
==========================================
Files 383 384 +1
Lines 13080 13139 +59
Branches 3845 3859 +14
==========================================
+ Hits 11411 11470 +59
Misses 1302 1302
Partials 367 367
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Sorry for the wait since I got your review! There have been two major changes since previous review.
1. use recursive ASTNode comparisonAs you reviewed, the previous implementation compared pure code strings, so there was a bug that prevented comparison if there were comments, etc. in the union/intersction type. 2. use removeRange()As you reviewed, we are using removeRange instead of replaceText in the fix function. Specifically, I set the removeRange as follows. type T = A | A | B;
// ^ ^
// start end
// start : end loc of the member before the duplicated member
// end : end loc of the duplicated member
// ↓ Deleting this range will result in ....
type T = A | B; This is a relatively large change, but I would appreciate your review. 🙏 |
no-duplicate-type-union-intersection-members
ruleno-duplicate-type-constituents
rule
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.
Progress! I think this is getting closer - but nailing down edge cases (i.e. switching to ===
type equality) is going to be key.
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
Thank you so much for your detailed explanation! The problem with the current behavior is that there are multiple fixers (number of detected duplicates) that remove the exact same duplicate (the one at the end), right? Now that I am aware of the problem, I would like to implement it according to the second solution you suggested (= report / fixer only the first duplicates found in constituents). However, I am wondering if the above change (pointing out the first detected duplicate) should be applied to the report function as well, or only to the fixer.
type F = (A | B) | (A | B) | ((C | D) & (A | B)) | (A | B);
// reported ^^^^^
// fixer ^^^^^
type F = (A | B) | (A | B) | (C | D) & (A | B) | (A | B);
// reported ^^^^^ ^^^^^^^^^^^^^^^ ^^^^^
// fixer ^^^^^ |
I have addressed the issues regarding the range of the fixer function and the clarity of the error reporting scope! You can see these improvements in action by checking out the playground. (It should be fixed properly 🙏 ) Talking about implementation, I have cut the |
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 look great, thank you for your hard work
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 looking really great, nice work @sajikix! 👏
Requesting changes on performance tuning, non-generated tests, and some small touchups. Awesome! Looking forward this getting shipped!
packages/eslint-plugin/docs/rules/no-duplicate-type-constituents.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/no-duplicate-type-constituents.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-duplicate-type-constituents.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-duplicate-type-constituents.test.ts
Outdated
Show resolved
Hide resolved
…ount of calculation.
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.
Thank you again and again for your kind review! 🙏 Do I need to do any additional work on this PR? (Is it OK if I leave the rest to you?) |
none needed, was just waiting for the build to pass then got distracted 😄 |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) | devDependencies | minor | [`5.56.0` -> `5.57.0`](https://renovatebot.com/diffs/npm/@typescript-eslint%2feslint-plugin/5.56.0/5.57.0) | | [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint) | devDependencies | minor | [`5.56.0` -> `5.57.0`](https://renovatebot.com/diffs/npm/@typescript-eslint%2fparser/5.56.0/5.57.0) | --- ### Release Notes <details> <summary>typescript-eslint/typescript-eslint (@​typescript-eslint/eslint-plugin)</summary> ### [`v5.57.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#​5570-httpsgithubcomtypescript-eslinttypescript-eslintcomparev5560v5570-2023-03-27) [Compare Source](typescript-eslint/typescript-eslint@v5.56.0...v5.57.0) ##### Bug Fixes - **eslint-plugin:** \[no-unnecessary-boolean-literal-compare] simplify fixer and add support for double negation ([#​6620](typescript-eslint/typescript-eslint#6620)) ([81c8519](typescript-eslint/typescript-eslint@81c8519)) - **eslint-plugin:** correct crashes with getTypeArguments for ts < 3.7 ([#​6767](typescript-eslint/typescript-eslint#6767)) ([59eab58](typescript-eslint/typescript-eslint@59eab58)) ##### Features - **eslint-plugin:** \[consistent-type-assertions] add suggestions for objectLiteralTypeAssertions ([#​6642](typescript-eslint/typescript-eslint#6642)) ([720e811](typescript-eslint/typescript-eslint@720e811)) - **eslint-plugin:** \[consistent-type-assertions] autofix angle bracket assertions to as ([#​6641](typescript-eslint/typescript-eslint#6641)) ([ad8ea64](typescript-eslint/typescript-eslint@ad8ea64)) - **eslint-plugin:** add `no-duplicate-type-constituents` rule ([#​5728](typescript-eslint/typescript-eslint#5728)) ([bc31078](typescript-eslint/typescript-eslint@bc31078)) </details> <details> <summary>typescript-eslint/typescript-eslint (@​typescript-eslint/parser)</summary> ### [`v5.57.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/parser/CHANGELOG.md#​5570-httpsgithubcomtypescript-eslinttypescript-eslintcomparev5560v5570-2023-03-27) [Compare Source](typescript-eslint/typescript-eslint@v5.56.0...v5.57.0) **Note:** Version bump only for package [@​typescript-eslint/parser](https://github.com/typescript-eslint/parser) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4yNC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMjQuMiJ9--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1831 Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
PR Checklist
Overview
Add new rule :
no-duplicate-type-union-intersection-members
.This rule disallows duplicate union or intersection type members. The autofixer will remove all duplicates, keeping the first one.
I implemented it so that only notational duplicates are checked.
It means that members with the same value but different names are not marked duplicates.
In the case of object types, it judged as duplicates only when the order of key and value are exactly the same.
What has considered a duplicate in
object type
is a matter of debate. I would love to hear the reviewers' opinions and would like to improve it with the reviewers' input.