Skip to content

Commit

Permalink
SONARPY-1727: S5905: Make sure the quick fix removes trailing commas (#…
Browse files Browse the repository at this point in the history
…2020)

* SONARPY-1727 remove trailing comma of singelton tuple

* SONARPY-1727 fix after review

* SONARPY-1727 fix review nitpick
  • Loading branch information
Seppli11 authored Sep 30, 2024
1 parent 0478d39 commit cacf1e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.quickfix.PythonQuickFix;
import org.sonar.plugins.python.api.tree.AssertStatement;
import org.sonar.plugins.python.api.tree.Token;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.tree.Tuple;
import org.sonar.plugins.python.api.quickfix.PythonQuickFix;
import org.sonar.python.quickfix.TextEditUtils;

@Rule(key = "S5905")
Expand All @@ -43,17 +44,22 @@ public void initialize(Context context) {

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

if (tuple.leftParenthesis() != null && tuple.rightParenthesis() != null) {
// defensive condition
issue.addQuickFix(PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
if (isSingletonTupleWithParenthesis(tuple)) {
Token comma = tuple.commas().get(0);
var quickfixBuilder = PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
.addTextEdit(TextEditUtils.remove(tuple.leftParenthesis()))
.addTextEdit(TextEditUtils.remove(tuple.rightParenthesis()))
.build());
.addTextEdit(TextEditUtils.replaceRange(comma, tuple.rightParenthesis(), ""));

issue.addQuickFix(quickfixBuilder.build());
}
}
});
}

private static boolean isSingletonTupleWithParenthesis(Tuple tuple) {
return tuple.leftParenthesis() != null && tuple.rightParenthesis() != null && tuple.elements().size() == 1;
}

@Override
public CheckScope scope() {
return CheckScope.ALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ void test() {

@Test
void quickFixTest() {
var before = "def foo():\n" +
" assert (a, b)";
var after = "def foo():\n" +
" assert a, b";
PythonQuickFixVerifier.verify(new AssertOnTupleLiteralCheck(), before, after);
PythonQuickFixVerifier.verifyQuickFixMessages(new AssertOnTupleLiteralCheck(), before, AssertOnTupleLiteralCheck.QUICK_FIX_MESSAGE);
PythonQuickFixVerifier.verifyQuickFixMessages(new AssertOnTupleLiteralCheck(), "assert (foo(),)", "Remove parentheses");
PythonQuickFixVerifier.verify(new AssertOnTupleLiteralCheck(), "assert (foo(),)", "assert foo()");
PythonQuickFixVerifier.verifyNoQuickFixes(new AssertOnTupleLiteralCheck(), "assert (foo(),b,)");
PythonQuickFixVerifier.verifyNoQuickFixes(new AssertOnTupleLiteralCheck(), "assert (foo(),b)");
PythonQuickFixVerifier.verifyNoQuickFixes(new AssertOnTupleLiteralCheck(), "assert ()");
}

@Test
Expand Down

0 comments on commit cacf1e6

Please sign in to comment.