@@ -55,18 +55,16 @@ public class FunctionSymbolImpl extends SymbolImpl implements FunctionSymbol {
5555 setKind (Kind .FUNCTION );
5656 isInstanceMethod = isInstanceMethod (functionDef );
5757 hasDecorators = !functionDef .decorators ().isEmpty ();
58+ String fileId = null ;
59+ if (!SymbolUtils .isTypeShedFile (pythonFile )) {
60+ Path path = pathOf (pythonFile );
61+ fileId = path != null ? path .toString () : pythonFile .toString ();
62+ }
5863 ParameterList parametersList = functionDef .parameters ();
5964 if (parametersList != null ) {
60- createParameterNames (parametersList .all ());
61- }
62- if (SymbolUtils .isTypeShedFile (pythonFile )) {
63- functionDefinitionLocation = null ;
64- } else {
65- TokenLocation functionName = new TokenLocation (functionDef .name ().firstToken ());
66- Path path = pathOf (pythonFile );
67- String fileId = path != null ? path .toString () : pythonFile .toString ();
68- functionDefinitionLocation = new LocationInFile (fileId , functionName .startLine (), functionName .startLineOffset (), functionName .endLine (), functionName .endLineOffset ());
65+ createParameterNames (parametersList .all (), fileId );
6966 }
67+ functionDefinitionLocation = locationInFile (functionDef .name (), fileId );
7068 }
7169
7270 FunctionSymbolImpl (String name , FunctionSymbol functionSymbol ) {
@@ -93,6 +91,16 @@ public FunctionSymbolImpl(String name, @Nullable String fullyQualifiedName, bool
9391 this .isStub = true ;
9492 }
9593
94+ @ CheckForNull
95+ private static LocationInFile locationInFile (Tree tree , @ Nullable String fileId ) {
96+ if (fileId == null ) {
97+ return null ;
98+ }
99+ TokenLocation firstToken = new TokenLocation (tree .firstToken ());
100+ TokenLocation lastToken = new TokenLocation (tree .lastToken ());
101+ return new LocationInFile (fileId , firstToken .startLine (), firstToken .startLineOffset (), lastToken .endLine (), lastToken .endLineOffset ());
102+ }
103+
96104 @ Override
97105 FunctionSymbolImpl copyWithoutUsages () {
98106 FunctionSymbolImpl copy = new FunctionSymbolImpl (name (), this );
@@ -109,23 +117,23 @@ private static boolean isInstanceMethod(FunctionDef functionDef) {
109117 .noneMatch (decorator -> decorator .equals ("staticmethod" ) || decorator .equals ("classmethod" ));
110118 }
111119
112- private void createParameterNames (List <AnyParameter > parameterTrees ) {
120+ private void createParameterNames (List <AnyParameter > parameterTrees , @ Nullable String fileId ) {
113121 boolean keywordOnly = false ;
114122 for (AnyParameter anyParameter : parameterTrees ) {
115123 if (anyParameter .is (Tree .Kind .PARAMETER )) {
116124 org .sonar .plugins .python .api .tree .Parameter parameter = (org .sonar .plugins .python .api .tree .Parameter ) anyParameter ;
117125 Name parameterName = parameter .name ();
118126 Token starToken = parameter .starToken ();
119127 if (parameterName != null ) {
120- this .parameters .add (new ParameterImpl (parameterName .name (), parameter .defaultValue () != null , keywordOnly ));
128+ this .parameters .add (new ParameterImpl (parameterName .name (), parameter .defaultValue () != null , keywordOnly , locationInFile ( anyParameter , fileId ) ));
121129 if (starToken != null ) {
122130 hasVariadicParameter = true ;
123131 }
124132 } else if (starToken != null && "*" .equals (starToken .value ())) {
125133 keywordOnly = true ;
126134 }
127135 } else {
128- parameters .add (new ParameterImpl (null , false , false ));
136+ parameters .add (new ParameterImpl (null , false , false , locationInFile ( anyParameter , fileId ) ));
129137 }
130138 }
131139 }
@@ -181,11 +189,13 @@ private static class ParameterImpl implements Parameter {
181189 private final String name ;
182190 private final boolean hasDefaultValue ;
183191 private final boolean isKeywordOnly ;
192+ private final LocationInFile location ;
184193
185- ParameterImpl (@ Nullable String name , boolean hasDefaultValue , boolean isKeywordOnly ) {
194+ ParameterImpl (@ Nullable String name , boolean hasDefaultValue , boolean isKeywordOnly , @ Nullable LocationInFile location ) {
186195 this .name = name ;
187196 this .hasDefaultValue = hasDefaultValue ;
188197 this .isKeywordOnly = isKeywordOnly ;
198+ this .location = location ;
189199 }
190200
191201 @ Override
@@ -203,5 +213,11 @@ public boolean hasDefaultValue() {
203213 public boolean isKeywordOnly () {
204214 return isKeywordOnly ;
205215 }
216+
217+ @ CheckForNull
218+ @ Override
219+ public LocationInFile location () {
220+ return location ;
221+ }
206222 }
207223}
0 commit comments