From 7bdc5bfb0541f9d6e56c4a44fee57222a18a6b68 Mon Sep 17 00:00:00 2001 From: Andrea Guarino Date: Tue, 21 Jan 2020 11:46:24 +0100 Subject: [PATCH] Fix column value of tokens of interpolated expressions --- .../src/test/resources/checks/undeclaredNameUsage.py | 1 + .../src/main/java/org/sonar/python/tree/PythonTreeMaker.java | 4 ++-- .../test/java/org/sonar/plugins/python/SymbolVisitorTest.java | 1 + .../org/sonar/plugins/python/sensor/symbolVisitor.py | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/python-checks/src/test/resources/checks/undeclaredNameUsage.py b/python-checks/src/test/resources/checks/undeclaredNameUsage.py index 5af4ef8a03..b1a9e83106 100644 --- a/python-checks/src/test/resources/checks/undeclaredNameUsage.py +++ b/python-checks/src/test/resources/checks/undeclaredNameUsage.py @@ -207,3 +207,4 @@ def foo(param): print(f'{foo(param=3)}') # OK, param is a keyword argument print(f'{foo(param)}') # Noncompliant +# ^^^^^ diff --git a/python-frontend/src/main/java/org/sonar/python/tree/PythonTreeMaker.java b/python-frontend/src/main/java/org/sonar/python/tree/PythonTreeMaker.java index a1b4067bc9..db8d862928 100644 --- a/python-frontend/src/main/java/org/sonar/python/tree/PythonTreeMaker.java +++ b/python-frontend/src/main/java/org/sonar/python/tree/PythonTreeMaker.java @@ -33,9 +33,9 @@ import javax.annotation.Nullable; import org.sonar.plugins.python.api.tree.AliasedName; import org.sonar.plugins.python.api.tree.AnnotatedAssignment; -import org.sonar.plugins.python.api.tree.Argument; import org.sonar.plugins.python.api.tree.AnyParameter; import org.sonar.plugins.python.api.tree.ArgList; +import org.sonar.plugins.python.api.tree.Argument; import org.sonar.plugins.python.api.tree.AssertStatement; import org.sonar.plugins.python.api.tree.AssignmentStatement; import org.sonar.plugins.python.api.tree.BreakStatement; @@ -1321,7 +1321,7 @@ private static void updateTokensLineAndColumn(Token token, int startOfLiteral, L } int newline = (line - 1) + token.line(); // update token line and column with offset of string element. - TreeUtils.tokens(exp).forEach(t -> ((TokenImpl) t).setLineColumn(newline, col)); + TreeUtils.tokens(exp).forEach(t -> ((TokenImpl) t).setLineColumn(newline, col + t.column())); if (exp.is(Tree.Kind.STRING_LITERAL)) { ((StringLiteral) exp).stringElements().forEach(PythonTreeMaker::adjustNestedInterpolations); } diff --git a/sonar-python-plugin/src/test/java/org/sonar/plugins/python/SymbolVisitorTest.java b/sonar-python-plugin/src/test/java/org/sonar/plugins/python/SymbolVisitorTest.java index b24565d433..2161fa98dd 100644 --- a/sonar-python-plugin/src/test/java/org/sonar/plugins/python/SymbolVisitorTest.java +++ b/sonar-python-plugin/src/test/java/org/sonar/plugins/python/SymbolVisitorTest.java @@ -77,6 +77,7 @@ public void symbol_visitor() { verifyUsages(30, 11, reference(30, 1, 30, 4)); verifyUsages(34, 0, reference(35, 42, 35, 43), reference(38, 1, 38, 2), reference(41, 10, 41, 11), reference(41, 36, 41, 37)); + verifyUsages(43, 61, reference(43, 54, 43, 55)); } private void verifyUsages(int line, int offset, TextRange... trs) { diff --git a/sonar-python-plugin/src/test/resources/org/sonar/plugins/python/sensor/symbolVisitor.py b/sonar-python-plugin/src/test/resources/org/sonar/plugins/python/sensor/symbolVisitor.py index d6e0ef383a..2f98c7430b 100644 --- a/sonar-python-plugin/src/test/resources/org/sonar/plugins/python/sensor/symbolVisitor.py +++ b/sonar-python-plugin/src/test/resources/org/sonar/plugins/python/sensor/symbolVisitor.py @@ -39,3 +39,5 @@ def other_func(self): nested = f'some: {f"nested interpolation:\ {u} and then some {f"more {u}"}"}' + +f"symbol created and used inside interpolation: {[len(x) for x in []]}"