Skip to content

Commit 373d797

Browse files
ghislainpiotguillaume-dequenne
authored andcommitted
SONARPY-2122 Ensure function parameters have a definition location (#1958)
1 parent 3c9e977 commit 373d797

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

python-frontend/src/main/java/org/sonar/python/semantic/v2/FunctionTypeBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class FunctionTypeBuilder implements TypeBuilder<FunctionType> {
5656
private static final String CLASS_METHOD_DECORATOR = "classmethod";
5757
private static final String STATIC_METHOD_DECORATOR = "staticmethod";
5858

59-
public FunctionTypeBuilder fromFunctionDef(FunctionDef functionDef) {
59+
public FunctionTypeBuilder fromFunctionDef(FunctionDef functionDef, @Nullable String fileId) {
6060
this.name = functionDef.name().name();
6161
this.attributes = new ArrayList<>();
6262
this.parameters = new ArrayList<>();
@@ -65,7 +65,7 @@ public FunctionTypeBuilder fromFunctionDef(FunctionDef functionDef) {
6565
isInstanceMethod = isInstanceMethod(functionDef);
6666
ParameterList parameterList = functionDef.parameters();
6767
if (parameterList != null) {
68-
createParameterNames(parameterList.all(), null);
68+
createParameterNames(parameterList.all(), fileId);
6969
}
7070
return this;
7171
}

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/TrivialTypeInferenceVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void visitFunctionDef(FunctionDef functionDef) {
271271

272272
private FunctionType buildFunctionType(FunctionDef functionDef) {
273273
FunctionTypeBuilder functionTypeBuilder = new FunctionTypeBuilder()
274-
.fromFunctionDef(functionDef)
274+
.fromFunctionDef(functionDef, fileId)
275275
.withDefinitionLocation(locationInFile(functionDef.name(), fileId));
276276
ClassType owner = null;
277277
if (currentType() instanceof ClassType classType) {

python-frontend/src/test/java/org/sonar/python/types/v2/FunctionTypeTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void arity() {
101101
assertThat(functionType.parameters().get(0).isVariadic()).isFalse();
102102
assertThat(functionType.parameters().get(0).isKeywordVariadic()).isFalse();
103103
assertThat(functionType.parameters().get(0).isPositionalVariadic()).isFalse();
104-
assertThat(functionType.parameters().get(0).location()).isNull();
104+
assertThat(functionType.parameters().get(0).location()).isEqualTo(new LocationInFile(fileId, 1, 7, 1, 17));
105105

106106
functionType = functionType("def fn(**kwargs): pass");
107107
assertThat(functionType.parameters()).hasSize(1);
@@ -188,6 +188,15 @@ void owner() {
188188
assertThat(functionType.owner()).isNull();
189189
}
190190

191+
@Test
192+
void location() {
193+
FunctionType functionType = functionType("def fn(param: int, (a: str, b)): pass");
194+
String fileId = SymbolUtils.pathOf(pythonFile).toString();
195+
assertThat(functionType.definitionLocation()).contains(new LocationInFile(fileId, 1, 4, 1, 6));
196+
assertThat(functionType.parameters().get(0).location()).isEqualTo(new LocationInFile(fileId, 1, 7, 1, 17));
197+
assertThat(functionType.parameters().get(1).location()).isEqualTo(new LocationInFile(fileId, 1, 19, 1, 30));
198+
}
199+
191200
public static FunctionType functionType(String... code) {
192201
FileInput fileInput = parseWithoutSymbols(code);
193202
var symbolTable = new SymbolTableBuilderV2(fileInput)

0 commit comments

Comments
 (0)