Skip to content

Commit b1c71c0

Browse files
SONARPY-1025 Fix Quickfix for functions with * parameter (#1148)
1 parent 5b77f87 commit b1c71c0

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

python-checks/src/main/java/org/sonar/python/checks/InstanceAndClassMethodsAtLeastOnePositionalCheck.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.sonar.python.quickfix.PythonQuickFix;
3838
import org.sonar.python.tree.TreeUtils;
3939

40-
import static org.sonar.python.quickfix.PythonTextEdit.insertBefore;
40+
import static org.sonar.python.quickfix.PythonTextEdit.insertAfter;
4141

4242
@Rule(key="S5719")
4343
public class InstanceAndClassMethodsAtLeastOnePositionalCheck extends PythonSubscriptionCheck {
@@ -99,10 +99,10 @@ private static void handleFunctionDef(SubscriptionContext ctx, ClassDef classDef
9999
private static void addIssue(SubscriptionContext ctx, FunctionDef functionDef, MethodIssueType type) {
100100
IssueWithQuickFix issue = (IssueWithQuickFix) ctx.addIssue(functionDef.defKeyword(), functionDef.rightPar(),
101101
type.message);
102-
102+
String separator = functionDef.parameters() == null ? "" : ", ";
103103
for (String insertion : type.insertions) {
104104
PythonQuickFix quickFix = PythonQuickFix.newQuickFix(String.format("Add '%s' as the first argument.", insertion))
105-
.addTextEdit(insertBefore(functionDef.rightPar(), insertion))
105+
.addTextEdit(insertAfter(functionDef.leftPar(), insertion + separator))
106106
.build();
107107
issue.addQuickFix(quickFix);
108108
}

python-checks/src/test/java/org/sonar/python/checks/InstanceAndClassMethodsAtLeastOnePositionalCheckTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ public void test() {
3434

3535
@Test
3636
public void class_method_quickfix() {
37-
String codeWithIssue = "class Foo():\n" +
37+
String codeWithIssue = "" +
38+
"class Foo():\n" +
3839
" @classmethod\n" +
3940
" def bar(): pass";
40-
String fixedCodeWithClassParameter = "class Foo():\n" +
41+
42+
String fixedCodeWithClassParameter = "" +
43+
"class Foo():\n" +
4144
" @classmethod\n" +
4245
" def bar(cls): pass";
4346

@@ -46,15 +49,35 @@ public void class_method_quickfix() {
4649

4750
@Test
4851
public void regular_method_quickfix() {
49-
String codeWithIssue = "class Foo():\n" +
52+
String codeWithIssue = "" +
53+
"class Foo():\n" +
5054
" def bar(): pass";
51-
String fixedCodeWithSelfParameter = "class Foo():\n" +
55+
56+
String fixedCodeWithSelfParameter = "" +
57+
"class Foo():\n" +
5258
" def bar(self): pass";
53-
String fixedCodeWithClassParameter = "class Foo():\n" +
59+
60+
String fixedCodeWithClassParameter = "" +
61+
"class Foo():\n" +
5462
" def bar(cls): pass";
5563

5664
PythonQuickFixVerifier.verify(check, codeWithIssue,
5765
fixedCodeWithSelfParameter,
5866
fixedCodeWithClassParameter);
5967
}
68+
69+
@Test
70+
public void no_pos_args_quickfix() {
71+
String codeWithIssue = "" +
72+
"class Foo():\n" +
73+
" @classmethod\n" +
74+
" def bar(*, param): pass";
75+
76+
String fixedCodeWithClassParameter = "" +
77+
"class Foo():\n" +
78+
" @classmethod\n" +
79+
" def bar(cls, *, param): pass";
80+
81+
PythonQuickFixVerifier.verify(check, codeWithIssue, fixedCodeWithClassParameter);
82+
}
6083
}

0 commit comments

Comments
 (0)