Search in sources :

Example 1 with AliasedName

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

the class PythonTreeMakerTest method importStatement.

@Test
public void importStatement() {
    setRootRule(PythonGrammar.IMPORT_STMT);
    AstNode astNode = p.parse("import foo");
    StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null);
    ImportName importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.firstToken().value()).isEqualTo("import");
    assertThat(importStatement.lastToken().value()).isEqualTo("foo");
    assertThat(importStatement).isNotNull();
    assertThat(importStatement.importKeyword().value()).isEqualTo("import");
    assertThat(importStatement.modules()).hasSize(1);
    AliasedName importedName1 = importStatement.modules().get(0);
    assertThat(importedName1.dottedName().names()).hasSize(1);
    assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
    assertThat(importStatement.children()).hasSize(2);
    astNode = p.parse("import foo as f");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement).isNotNull();
    assertThat(importStatement.firstToken().value()).isEqualTo("import");
    assertThat(importStatement.lastToken().value()).isEqualTo("f");
    assertThat(importStatement.importKeyword().value()).isEqualTo("import");
    assertThat(importStatement.modules()).hasSize(1);
    importedName1 = importStatement.modules().get(0);
    assertThat(importedName1.firstToken().value()).isEqualTo("foo");
    assertThat(importedName1.lastToken().value()).isEqualTo("f");
    assertThat(importedName1.dottedName().names()).hasSize(1);
    assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
    assertThat(importedName1.asKeyword().value()).isEqualTo("as");
    assertThat(importedName1.alias().name()).isEqualTo("f");
    assertThat(importStatement.children()).hasSize(2);
    astNode = p.parse("import foo.bar");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement).isNotNull();
    assertThat(importStatement.importKeyword().value()).isEqualTo("import");
    assertThat(importStatement.modules()).hasSize(1);
    importedName1 = importStatement.modules().get(0);
    assertThat(importedName1.dottedName().names()).hasSize(2);
    assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
    assertThat(importedName1.dottedName().names().get(1).name()).isEqualTo("bar");
    assertThat(importStatement.children()).hasSize(2);
    astNode = p.parse("import foo, bar");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement).isNotNull();
    assertThat(importStatement.importKeyword().value()).isEqualTo("import");
    assertThat(importStatement.modules()).hasSize(2);
    importedName1 = importStatement.modules().get(0);
    assertThat(importedName1.dottedName().names()).hasSize(1);
    assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
    AliasedName importedName2 = importStatement.modules().get(1);
    assertThat(importedName2.dottedName().names()).hasSize(1);
    assertThat(importedName2.dottedName().names().get(0).name()).isEqualTo("bar");
    assertThat(importStatement.children()).hasSize(3);
}
Also used : AliasedName(org.sonar.plugins.python.api.tree.AliasedName) ImportName(org.sonar.plugins.python.api.tree.ImportName) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 2 with AliasedName

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

the class PythonTreeMakerTest method importFromStatement.

@Test
public void importFromStatement() {
    setRootRule(PythonGrammar.IMPORT_STMT);
    AstNode astNode = p.parse("from foo import f");
    StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null);
    ImportFrom importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement).isNotNull();
    assertThat(importStatement.firstToken().value()).isEqualTo("from");
    assertThat(importStatement.lastToken().value()).isEqualTo("f");
    assertThat(importStatement.importKeyword().value()).isEqualTo("import");
    assertThat(importStatement.dottedPrefixForModule()).isEmpty();
    assertThat(importStatement.fromKeyword().value()).isEqualTo("from");
    assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo");
    assertThat(importStatement.isWildcardImport()).isFalse();
    assertThat(importStatement.wildcard()).isNull();
    assertThat(importStatement.importedNames()).hasSize(1);
    AliasedName aliasedNameTree = importStatement.importedNames().get(0);
    assertThat(aliasedNameTree.asKeyword()).isNull();
    assertThat(aliasedNameTree.alias()).isNull();
    assertThat(aliasedNameTree.dottedName().names().get(0).name()).isEqualTo("f");
    assertThat(importStatement.children()).hasSize(4);
    assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.module(), importStatement.importKeyword(), aliasedNameTree);
    astNode = p.parse("from .foo import f");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.dottedPrefixForModule()).hasSize(1);
    assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo(".");
    assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo");
    aliasedNameTree = importStatement.importedNames().get(0);
    assertThat(importStatement.children()).hasSize(5);
    assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.dottedPrefixForModule().get(0), importStatement.module(), importStatement.importKeyword(), aliasedNameTree);
    astNode = p.parse("from ..foo import f");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.dottedPrefixForModule()).hasSize(2);
    assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo(".");
    assertThat(importStatement.dottedPrefixForModule().get(1).value()).isEqualTo(".");
    assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo");
    assertThat(importStatement.children()).hasSize(6);
    astNode = p.parse("from . import f");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.dottedPrefixForModule()).hasSize(1);
    assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo(".");
    assertThat(importStatement.module()).isNull();
    assertThat(importStatement.children()).hasSize(4);
    astNode = p.parse("from foo import f as g");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.importedNames()).hasSize(1);
    aliasedNameTree = importStatement.importedNames().get(0);
    assertThat(aliasedNameTree.asKeyword().value()).isEqualTo("as");
    assertThat(aliasedNameTree.alias().name()).isEqualTo("g");
    assertThat(aliasedNameTree.dottedName().names().get(0).name()).isEqualTo("f");
    assertThat(importStatement.children()).hasSize(4);
    astNode = p.parse("from foo import f as g, h");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.importedNames()).hasSize(2);
    AliasedName aliasedNameTree1 = importStatement.importedNames().get(0);
    assertThat(aliasedNameTree1.asKeyword().value()).isEqualTo("as");
    assertThat(aliasedNameTree1.alias().name()).isEqualTo("g");
    assertThat(aliasedNameTree1.dottedName().names().get(0).name()).isEqualTo("f");
    assertThat(importStatement.children()).hasSize(5);
    List<Tree> aliasedNameChildren = aliasedNameTree1.children();
    assertThat(aliasedNameChildren).hasSize(3);
    assertThat(aliasedNameChildren).containsExactly(aliasedNameTree1.dottedName(), aliasedNameTree1.asKeyword(), aliasedNameTree1.alias());
    AliasedName aliasedNameTree2 = importStatement.importedNames().get(1);
    assertThat(aliasedNameTree2.asKeyword()).isNull();
    assertThat(aliasedNameTree2.alias()).isNull();
    assertThat(aliasedNameTree2.dottedName().names().get(0).name()).isEqualTo("h");
    astNode = p.parse("from foo import *");
    statementWithSeparator = new StatementWithSeparator(astNode, null);
    importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator);
    assertThat(importStatement.importedNames()).isEmpty();
    assertThat(importStatement.isWildcardImport()).isTrue();
    assertThat(importStatement.wildcard().value()).isEqualTo("*");
    assertThat(importStatement.children()).hasSize(4);
    assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.module(), importStatement.importKeyword(), importStatement.wildcard());
}
Also used : AliasedName(org.sonar.plugins.python.api.tree.AliasedName) ImportFrom(org.sonar.plugins.python.api.tree.ImportFrom) Tree(org.sonar.plugins.python.api.tree.Tree) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 3 with AliasedName

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

the class FullyQualifiedNameTest method relative_import_symbols.

@Test
public void relative_import_symbols() {
    FileInput tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py")), "from . import b");
    Name b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("my_package.b");
    tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py")), "from . import b as b");
    AliasedName aliasedName = getFirstChild(tree, t -> t.is(Tree.Kind.ALIASED_NAME));
    assertThat(aliasedName.alias().symbol().fullyQualifiedName()).isEqualTo("my_package.b");
    tree = parse(new SymbolTableBuilder("my_package", pythonFile("__init__.py")), "from . import b as b");
    aliasedName = getFirstChild(tree, t -> t.is(Tree.Kind.ALIASED_NAME));
    assertThat(aliasedName.alias().symbol().fullyQualifiedName()).isEqualTo("my_package.b");
    tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py")), "from .other_module import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME) && ((Name) t).name().equals("b"));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("my_package.other_module.b");
    // no package
    tree = parse(new SymbolTableBuilder("", pythonFile("my_module.py")), "from . import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("b");
    // no package, init file
    tree = parse(new SymbolTableBuilder("", pythonFile("__init__.py")), "from . import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("b");
    // two levels up
    tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py")), "from ..my_package import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME) && ((Name) t).name().equals("b"));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("my_package.b");
    tree = parse(new SymbolTableBuilder("my_package1.my_package2", pythonFile("my_module.py")), "from ..other import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME) && ((Name) t).name().equals("b"));
    assertThat(b.symbol().fullyQualifiedName()).isEqualTo("my_package1.other.b");
    // overflow packages hierarchy
    tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py")), "from ...my_package import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME) && ((Name) t).name().equals("b"));
    assertThat(b.symbol().fullyQualifiedName()).isNull();
    // no fully qualified module name
    tree = parse(new SymbolTableBuilder(pythonFile("")), "from . import b");
    b = getFirstChild(tree, t -> t.is(Tree.Kind.NAME));
    assertThat(b.symbol().fullyQualifiedName()).isNull();
}
Also used : PythonFile(org.sonar.plugins.python.api.PythonFile) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) PythonParser(org.sonar.python.parser.PythonParser) PythonTestUtils.getAllDescendant(org.sonar.python.PythonTestUtils.getAllDescendant) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) PythonTreeMaker(org.sonar.python.tree.PythonTreeMaker) Name(org.sonar.plugins.python.api.tree.Name) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) PythonTestUtils.getFirstChild(org.sonar.python.PythonTestUtils.getFirstChild) URI(java.net.URI) Nullable(javax.annotation.Nullable) Usage(org.sonar.plugins.python.api.symbols.Usage) PythonTestUtils.parse(org.sonar.python.PythonTestUtils.parse) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) PythonTestUtils.pythonFile(org.sonar.python.PythonTestUtils.pythonFile) Mockito(org.mockito.Mockito) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) FileInput(org.sonar.plugins.python.api.tree.FileInput) Tree(org.sonar.plugins.python.api.tree.Tree) Symbol(org.sonar.plugins.python.api.symbols.Symbol) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) FileInput(org.sonar.plugins.python.api.tree.FileInput) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 4 with AliasedName

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

the class SelfAssignmentCheck method addImportedName.

private void addImportedName(AliasedName aliasedName) {
    Name alias = aliasedName.alias();
    if (alias != null) {
        importedNames.add(alias.name());
    } else {
        List<Name> names = aliasedName.dottedName().names();
        importedNames.add(names.get(names.size() - 1).name());
    }
}
Also used : AliasedName(org.sonar.plugins.python.api.tree.AliasedName) ImportName(org.sonar.plugins.python.api.tree.ImportName) Name(org.sonar.plugins.python.api.tree.Name)

Example 5 with AliasedName

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

the class PythonTreeMaker method importFromStatement.

private static ImportFrom importFromStatement(AstNode astNode, Separators separators) {
    Token importKeyword = toPyToken(astNode.getFirstChild(PythonKeyword.IMPORT).getToken());
    Token fromKeyword = toPyToken(astNode.getFirstChild(PythonKeyword.FROM).getToken());
    List<Token> dottedPrefixForModule = punctuators(astNode, PythonPunctuator.DOT);
    AstNode moduleNode = astNode.getFirstChild(PythonGrammar.DOTTED_NAME);
    DottedName moduleName = null;
    if (moduleNode != null) {
        moduleName = dottedName(moduleNode);
    }
    AstNode importAsnames = astNode.getFirstChild(PythonGrammar.IMPORT_AS_NAMES);
    List<AliasedName> aliasedImportNames = null;
    boolean isWildcardImport = true;
    if (importAsnames != null) {
        aliasedImportNames = importAsnames.getChildren(PythonGrammar.IMPORT_AS_NAME).stream().map(PythonTreeMaker::aliasedName).collect(Collectors.toList());
        isWildcardImport = false;
    }
    Token wildcard = null;
    if (isWildcardImport) {
        wildcard = toPyToken(astNode.getFirstChild(PythonPunctuator.MUL).getToken());
    }
    return new ImportFromImpl(fromKeyword, dottedPrefixForModule, moduleName, importKeyword, aliasedImportNames, wildcard, separators);
}
Also used : AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Token(org.sonar.plugins.python.api.tree.Token) DottedName(org.sonar.plugins.python.api.tree.DottedName) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AliasedName (org.sonar.plugins.python.api.tree.AliasedName)6 AstNode (com.sonar.sslr.api.AstNode)3 Test (org.junit.Test)3 ImportName (org.sonar.plugins.python.api.tree.ImportName)2 Name (org.sonar.plugins.python.api.tree.Name)2 Token (org.sonar.plugins.python.api.tree.Token)2 Tree (org.sonar.plugins.python.api.tree.Tree)2 RuleTest (org.sonar.python.parser.RuleTest)2 URI (java.net.URI)1 Nullable (javax.annotation.Nullable)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Mockito (org.mockito.Mockito)1 Mockito.when (org.mockito.Mockito.when)1 PythonFile (org.sonar.plugins.python.api.PythonFile)1 PythonVisitorContext (org.sonar.plugins.python.api.PythonVisitorContext)1 Symbol (org.sonar.plugins.python.api.symbols.Symbol)1 Usage (org.sonar.plugins.python.api.symbols.Usage)1 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)1 ClassDef (org.sonar.plugins.python.api.tree.ClassDef)1 DottedName (org.sonar.plugins.python.api.tree.DottedName)1