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

SONARPY-1727: S5905: Make sure the quick fix removes trailing commas #2020

Merged
merged 3 commits into from
Sep 30, 2024

Conversation

Seppli11
Copy link
Contributor

No description provided.

@Seppli11 Seppli11 marked this pull request as ready for review September 30, 2024 10:27
Copy link
Contributor

@joke1196 joke1196 left a comment

Choose a reason for hiding this comment

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

I have two questions. Not sure I get which should be handled in the scope of the ticket.

Comment on lines 51 to 55
if(tuple.elements().size() == 1 && !tuple.commas().isEmpty()) {
quickfixBuilder.addTextEdit(TextEditUtils.replaceRange(tuple.commas().get(0), tuple.rightParenthesis(), ""));
} else {
quickfixBuilder.addTextEdit(TextEditUtils.remove(tuple.rightParenthesis()));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure this would work with assert (a,b,c,) as this only check for single element tuples. Is that on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, ups. No it's not intentional. I wasn't aware that trailing commas are allowed in general 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I'm thinking about this, I'm not sure if the quick fix actually makes sense. For an assert like assert (a,b) there are two ways to correct this with different semantics. it could be assert a, b (where b is the error message), or it could be assert a and b. The current quick fix assumes that it is the first one.
However, given something like assert (a,b,c) the only sensible way to correct that is to generate assert a and b and c. But the current quick fix would generate assert a, b, c which isn't syntactically correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joke1196, what do you think? Would it make more sense to always join the different elements with and?

Copy link
Contributor

Choose a reason for hiding this comment

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

So as discussed we should provide the quick fix only for the single element tuple, as in other cases we risk breaking the code.

@@ -43,12 +43,18 @@ public void initialize(Context context) {

var issue = ctx.addIssue(tuple, MESSAGE);

if (tuple.leftParenthesis() != null && tuple.rightParenthesis() != null) {
if (tuple.leftParenthesis() != null && tuple.rightParenthesis() != null && !tuple.elements().isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we raise on this test assert () # Noncompliant when the tuple.elements is not empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking that it wouldn't make sense to offer a quick fix for assert () as this would result in just assert . Or is that a sensible quick fix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yes, this makes sense. I thought the check was at the level of the issue detection. And did not get why this would raise! Thanks for the clarification!

Copy link
Contributor

@joke1196 joke1196 left a comment

Choose a reason for hiding this comment

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

LGTM. I have just added a small nitpick

Comment on lines 48 to 54
var quickfixBuilder = PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
.addTextEdit(TextEditUtils.remove(tuple.leftParenthesis()));

Token lastComma = tuple.commas().get(tuple.commas().size() - 1);
quickfixBuilder.addTextEdit(TextEditUtils.replaceRange(lastComma, tuple.rightParenthesis(), ""));

issue.addQuickFix(quickfixBuilder.build());
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: maybe we can group the quickfixes statement together

Suggested change
var quickfixBuilder = PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
.addTextEdit(TextEditUtils.remove(tuple.leftParenthesis()));
Token lastComma = tuple.commas().get(tuple.commas().size() - 1);
quickfixBuilder.addTextEdit(TextEditUtils.replaceRange(lastComma, tuple.rightParenthesis(), ""));
issue.addQuickFix(quickfixBuilder.build());
Token lastComma = tuple.commas().get(tuple.commas().size() - 1);
var quickfixBuilder = PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
.addTextEdit(TextEditUtils.remove(tuple.leftParenthesis()));
.addTextEdit(TextEditUtils.replaceRange(lastComma, tuple.rightParenthesis(), ""));
issue.addQuickFix(quickfixBuilder.build());

@Seppli11 Seppli11 merged commit cacf1e6 into master Sep 30, 2024
10 checks passed
@Seppli11 Seppli11 deleted the SONARPY-1727 branch September 30, 2024 13:42
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.

2 participants