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