Search in sources :

Example 1 with LocationInFile

use of org.sonar.plugins.python.api.LocationInFile in project sonar-python by SonarSource.

the class InferredTypesTest method test_typeLocation.

@Test
public void test_typeLocation() {
    assertThat(InferredTypes.typeClassLocation(STR)).isNull();
    LocationInFile locationA = new LocationInFile("foo.py", 1, 1, 1, 1);
    RuntimeType aType = new RuntimeType(new ClassSymbolImpl("A", "mod.A", locationA));
    assertThat(InferredTypes.typeClassLocation(aType)).isEqualTo(locationA);
    LocationInFile locationB = new LocationInFile("foo.py", 1, 2, 1, 2);
    RuntimeType bType = new RuntimeType(new ClassSymbolImpl("B", "mod.B", locationB));
    assertThat(InferredTypes.typeClassLocation(or(aType, bType))).isNull();
    assertThat(InferredTypes.typeClassLocation(InferredTypes.anyType())).isNull();
}
Also used : ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) LocationInFile(org.sonar.plugins.python.api.LocationInFile) Test(org.junit.Test)

Example 2 with LocationInFile

use of org.sonar.plugins.python.api.LocationInFile in project sonar-python by SonarSource.

the class FunctionSymbolTest method locations.

@Test
public void locations() {
    PythonFile foo = PythonTestUtils.pythonFile("foo");
    FunctionSymbol functionSymbol = PythonTestUtils.functionSymbol(foo, "def foo(param1, param2): ...");
    assertThat(functionSymbol.parameters().get(0).location()).isEqualToComparingFieldByField(new LocationInFile(pathOf(foo).toString(), 1, 8, 1, 14));
    assertThat(functionSymbol.parameters().get(1).location()).isEqualToComparingFieldByField(new LocationInFile(pathOf(foo).toString(), 1, 16, 1, 22));
    assertThat(functionSymbol.definitionLocation()).isEqualToComparingFieldByField(new LocationInFile(pathOf(foo).toString(), 1, 4, 1, 7));
    functionSymbol = PythonTestUtils.functionSymbol(foo, "def foo(*param1): ...");
    assertThat(functionSymbol.parameters().get(0).location()).isEqualToComparingFieldByField(new LocationInFile(pathOf(foo).toString(), 1, 8, 1, 15));
    functionSymbol = PythonTestUtils.functionSymbol(foo, "def foo((a, b)): ...");
    assertThat(functionSymbol.parameters().get(0).location()).isEqualToComparingFieldByField(new LocationInFile(pathOf(foo).toString(), 1, 8, 1, 14));
    FileInput fileInput = parse(new SymbolTableBuilder(foo), "all([1,2,3])");
    CallExpression callExpression = (CallExpression) PythonTestUtils.getAllDescendant(fileInput, t -> t.is(Tree.Kind.CALL_EXPR)).get(0);
    FunctionSymbol builtinFunctionSymbol = (FunctionSymbol) callExpression.calleeSymbol();
    assertThat(builtinFunctionSymbol.definitionLocation()).isNull();
    assertThat(builtinFunctionSymbol.parameters().get(0).location()).isNull();
}
Also used : FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) LocationInFile(org.sonar.plugins.python.api.LocationInFile) FileInput(org.sonar.plugins.python.api.tree.FileInput) PythonFile(org.sonar.plugins.python.api.PythonFile) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Test(org.junit.Test)

Example 3 with LocationInFile

use of org.sonar.plugins.python.api.LocationInFile in project sonar-python by SonarSource.

the class PythonAnalyzerRegexSourceTest method assertLocation.

private static void assertLocation(int line, int startLineOffset, int endLineOffset, RegexTree tree) {
    LocationInFile location = ((PythonAnalyzerRegexSource) tree.getSource()).locationInFileFor(tree.getRange());
    assertLocation(line, startLineOffset, endLineOffset, location);
}
Also used : LocationInFile(org.sonar.plugins.python.api.LocationInFile)

Example 4 with LocationInFile

use of org.sonar.plugins.python.api.LocationInFile in project sonar-python by SonarSource.

the class IterationOnNonIterable method checkYieldStatement.

private void checkYieldStatement(SubscriptionContext ctx) {
    YieldStatement yieldStatement = (YieldStatement) ctx.syntaxNode();
    YieldExpression yieldExpression = yieldStatement.yieldExpression();
    if (yieldExpression.fromKeyword() == null) {
        return;
    }
    Expression expression = yieldExpression.expressions().get(0);
    Map<LocationInFile, String> secondaries = new HashMap<>();
    if (!isValidIterable(expression, secondaries)) {
        reportIssue(ctx, expression, secondaries, message(expression, false));
    }
}
Also used : YieldStatement(org.sonar.plugins.python.api.tree.YieldStatement) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) Expression(org.sonar.plugins.python.api.tree.Expression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) HashMap(java.util.HashMap) LocationInFile(org.sonar.plugins.python.api.LocationInFile)

Example 5 with LocationInFile

use of org.sonar.plugins.python.api.LocationInFile in project sonar-python by SonarSource.

the class NonCallableCalled method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> {
        CallExpression callExpression = (CallExpression) ctx.syntaxNode();
        Expression callee = callExpression.callee();
        InferredType calleeType = callee.type();
        if (isNonCallableType(calleeType)) {
            String name = nameFromExpression(callee);
            PreciseIssue preciseIssue = ctx.addIssue(callee, message(calleeType, name));
            LocationInFile location = typeClassLocation(calleeType);
            if (location != null) {
                preciseIssue.secondary(location, "Definition.");
            }
        }
    });
}
Also used : InferredType(org.sonar.plugins.python.api.types.InferredType) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Expression(org.sonar.plugins.python.api.tree.Expression) TreeUtils.nameFromExpression(org.sonar.python.tree.TreeUtils.nameFromExpression) LocationInFile(org.sonar.plugins.python.api.LocationInFile) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Aggregations

LocationInFile (org.sonar.plugins.python.api.LocationInFile)18 HashMap (java.util.HashMap)6 Expression (org.sonar.plugins.python.api.tree.Expression)6 Test (org.junit.Test)4 UnpackingExpression (org.sonar.plugins.python.api.tree.UnpackingExpression)4 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)4 PythonFile (org.sonar.plugins.python.api.PythonFile)3 Path (java.nio.file.Path)2 CheckForNull (javax.annotation.CheckForNull)2 IssueLocation (org.sonar.plugins.python.api.IssueLocation)2 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)2 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)2 InferredType (org.sonar.plugins.python.api.types.InferredType)2 TokenLocation (org.sonar.python.TokenLocation)2 TreeUtils.nameFromExpression (org.sonar.python.tree.TreeUtils.nameFromExpression)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1