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();
}
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();
}
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);
}
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));
}
}
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.");
}
}
});
}
Aggregations