-
Notifications
You must be signed in to change notification settings - Fork 90
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
Conversation
f2748b1
to
a647ba8
Compare
a647ba8
to
283adfc
Compare
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.
I have two questions. Not sure I get which should be handled in the scope of the ticket.
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())); | ||
} |
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.
I am not sure this would work with assert (a,b,c,)
as this only check for single element tuples. Is that on purpose?
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.
Oh, ups. No it's not intentional. I wasn't aware that trailing commas are allowed in general 😓
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.
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.
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.
@joke1196, what do you think? Would it make more sense to always join the different elements with and
?
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.
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()) { |
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.
How do we raise on this test assert () # Noncompliant
when the tuple.elements
is not empty?
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.
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?
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.
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!
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.
LGTM. I have just added a small nitpick
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()); |
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.
Nitpick: maybe we can group the quickfixes statement together
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()); |
Quality Gate passedIssues Measures |
No description provided.