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

Add quick fix for S4621 ('no-duplicate-in-composite') #3061

Merged
merged 4 commits into from
Mar 25, 2022
Merged

Conversation

yassin-kammoun-sonarsource
Copy link
Contributor

Fixes #3027

Copy link
Contributor

@vilchik-elena vilchik-elena left a comment

Choose a reason for hiding this comment

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

TBH I am not big fan of such solution based on full text replacement (and not on removal of duplicates), but indeed it's pretty straight-forward.

@@ -54,6 +55,7 @@ export const rule: Rule.RuleModule = {

groupedTypes.forEach(duplicates => {
if (duplicates.length > 1) {
const suggest = getSuggestions(compositeType, duplicates.slice(1), context);
Copy link
Contributor

Choose a reason for hiding this comment

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

minor but I would move slicing logic inside of the function

const firstToken = context.getSourceCode().getFirstToken(compositeNode);
const lastToken = context.getSourceCode().getLastToken(compositeNode);
let [prefix, suffix] = ['', ''];
if (firstToken?.value === '(' && lastToken?.value === ')') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see test for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

code: `type T = (number | string) & (number | string)`,
errors: [
{
suggestions: [
{
output: `type T = (number | string)`,
},
],
},
],
},
{
code: `type T = (number) | string | number`,
errors: [
{
suggestions: [
{
output: `type T = number | string`,
},
],
},
],
},
{
code: `type T = number | string | (number)`,
errors: [
{
suggestions: [
{
output: `type T = number | string`,
},
],
},
],
},

Copy link
Contributor

Choose a reason for hiding this comment

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

hm... here first and last parentheses are not a pair, that's why I didn't get that it covers the condition
Then for (number | string) & (number | string) & Foo we gonna get number | string & Foo which is wrong.

const firstToken = context.getSourceCode().getFirstToken(compositeNode);
const lastToken = context.getSourceCode().getLastToken(compositeNode);
let [prefix, suffix] = ['', ''];
if (firstToken?.value === '(' && lastToken?.value === ')') {
Copy link
Contributor

Choose a reason for hiding this comment

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

hm... here first and last parentheses are not a pair, that's why I didn't get that it covers the condition
Then for (number | string) & (number | string) & Foo we gonna get number | string & Foo which is wrong.

@vilchik-elena vilchik-elena enabled auto-merge (squash) March 25, 2022 12:57
@sonarsource-next
Copy link

sonarsource-next bot commented Mar 25, 2022

@vilchik-elena vilchik-elena merged commit 65a8ee6 into master Mar 25, 2022
@vilchik-elena vilchik-elena deleted the issue-3027 branch March 25, 2022 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add quick fix for S4621: Union and intersection types should not be defined with duplicated elements
2 participants