/** * @deprecated in 1.17, use {@link #getDescendants(AstNodeType...)} instead, but don't forget that those methods behave differently due to bug SSLR-249 */ @Deprecated public List<AstNode> findChildren(AstNodeType... nodeTypes) { List<AstNode> result = Lists.newArrayList(); getDescendants(result, nodeTypes); return result; }
/** * @deprecated in 1.17, use {@link #getDescendants(AstNodeType...)} instead, but don't forget that those methods behave differently due to bug SSLR-249 */ @Deprecated public List<AstNode> findChildren(AstNodeType... nodeTypes) { List<AstNode> result = new ArrayList<>(); getDescendants(result, nodeTypes); return result; }
public AstSelect descendants(AstNodeType type) { List<AstNode> result = Lists.newArrayList(); for (AstNode node : list) { result.addAll(node.getDescendants(type)); } return AstSelectFactory.create(result); }
public AstSelect descendants(AstNodeType... types) { List<AstNode> result = Lists.newArrayList(); for (AstNode node : list) { result.addAll(node.getDescendants(types)); } return AstSelectFactory.create(result); }
@Override public AstSelect descendants(AstNodeType type) { List<AstNode> result = new ArrayList<>(); for (AstNode node : list) { result.addAll(node.getDescendants(type)); } return AstSelectFactory.create(result); }
@Override public AstSelect descendants(AstNodeType... types) { List<AstNode> result = new ArrayList<>(); for (AstNode node : list) { result.addAll(node.getDescendants(types)); } return AstSelectFactory.create(result); }
private List<AstNode> getTestsFromLongAssignmentExpression(AstNode expression) { List<AstNode> assignedExpressions = expression.getChildren(PythonGrammar.TESTLIST_STAR_EXPR); assignedExpressions.remove(assignedExpressions.size() - 1); List<AstNode> tests = new LinkedList<>(); for (AstNode assignedExpression : assignedExpressions) { tests.addAll(assignedExpression.getDescendants(PythonGrammar.TEST)); } return tests; }
private void createLambdaParameters(AstNode functionTree) { AstNode parameters = functionTree.getFirstChild(PythonGrammar.VARARGSLIST); if (parameters == null) { return; } parameters.getChildren(PythonGrammar.NAME).forEach(this::addWriteUsage); parameters.getDescendants(PythonGrammar.FPDEF).stream() .flatMap(paramDef -> paramDef.getChildren(PythonGrammar.NAME).stream()) .forEach(this::addWriteUsage); }
private void getDescendants(List<AstNode> result, AstNodeType... nodeTypes) { for (AstNodeType nodeType : nodeTypes) { if (is(nodeType)) { result.add(this); } } if (hasChildren()) { for (AstNode child : children) { child.getDescendants(result, nodeTypes); } } }
private void getDescendants(List<AstNode> result, AstNodeType... nodeTypes) { for (AstNodeType nodeType : nodeTypes) { if (is(nodeType)) { result.add(this); } } if (hasChildren()) { for (AstNode child : children) { child.getDescendants(result, nodeTypes); } } }
public FileMetrics(PythonVisitorContext context, boolean ignoreHeaderComments) { AstNode rootTree = context.rootTree(); numberOfStatements = rootTree.getDescendants(PythonGrammar.STATEMENT).size(); numberOfClasses = rootTree.getDescendants(PythonGrammar.CLASSDEF).size(); complexityVisitor.scanFile(context); cognitiveComplexityVisitor.scanFile(context); fileLinesVisitor = new FileLinesVisitor(ignoreHeaderComments); fileLinesVisitor.scanFile(context); for (AstNode functionDef : rootTree.getDescendants(PythonGrammar.FUNCDEF)) { functionComplexities.add(ComplexityVisitor.complexity(functionDef)); } }
private boolean isUsed(AstNode funcDef, String self) { List<AstNode> names = funcDef.getFirstChild(PythonGrammar.SUITE).getDescendants(PythonGrammar.NAME); for (AstNode name : names){ if (name.getTokenValue().equals(self)){ return true; } } return false; }
private static boolean isUsed(AstNode funcDef, String self) { List<AstNode> names = funcDef.getFirstChild(PythonGrammar.SUITE).getDescendants(PythonGrammar.NAME); for (AstNode name : names) { if (name.getTokenValue().equals(self)) { return true; } } return false; }
private static void saveClassComplexity(SensorContext context, InputFile inputFile, AstNode rootNode) { int complexityInClasses = 0; for (AstNode classDef : rootNode.getDescendants(FlexGrammar.CLASS_DEF, FlexGrammar.INTERFACE_DEF)) { int classComplexity = ComplexityVisitor.complexity(classDef); complexityInClasses += classComplexity; } saveMeasure(context, inputFile, CoreMetrics.COMPLEXITY_IN_CLASSES, complexityInClasses); }
public List<Token> getClassFields(AstNode classDef) { symbols = new LinkedList<>(); findFieldsInClassBody(classDef); List<AstNode> methods = classDef.getFirstChild(PythonGrammar.SUITE).getDescendants(PythonGrammar.FUNCDEF); for (AstNode method : methods) { addFieldsInMethod(method); } return symbols; }
private boolean isTextParsedAsCode(String text) { try { AstNode astNode = parser.parse(text); List<AstNode> expressions = astNode.getDescendants(PythonGrammar.EXPRESSION_STMT); return astNode.getNumberOfChildren() > 1 && !isSimpleExpression(expressions); } catch (Exception e) { return false; } }
private void addFieldsInMethod(AstNode method) { AstNode suite = method.getFirstChild(PythonGrammar.SUITE); List<AstNode> expressions = suite.getDescendants(PythonGrammar.EXPRESSION_STMT); for (AstNode expression : expressions) { if (CheckUtils.isAssignmentExpression(expression)) { addSelfDotIdentifiersFromLongAssignmentExpression(expression); } } }
public List<Token> getClassFields(AstNode classDef) { symbols = new LinkedList<>(); findFieldsInClassBody(classDef); List<AstNode> methods = classDef.getFirstChild(PythonGrammar.SUITE).getDescendants(PythonGrammar.FUNCDEF); for (AstNode method : methods) { // ignore any nested class if (belongToClass(classDef, method)) { addFieldsInMethod(method); } } return symbols; }
@Override public void visitNode(AstNode node) { if (!"__init__".equals(node.getFirstChild(PythonGrammar.FUNCNAME).getTokenValue())){ return; } List<AstNode> returnYieldStatements = node.getDescendants(PythonGrammar.YIELD_STMT, PythonGrammar.RETURN_STMT); for (AstNode returnYieldStatement : returnYieldStatements){ if (CheckUtils.insideFunction(returnYieldStatement, node) && !returnReturnNone(returnYieldStatement)){ raiseIssue(returnYieldStatement); } } }
@Override public void visitNode(AstNode node) { if (!"__init__".equals(node.getFirstChild(PythonGrammar.FUNCNAME).getTokenValue())){ return; } List<AstNode> returnYieldStatements = node.getDescendants(PythonGrammar.YIELD_STMT, PythonGrammar.RETURN_STMT); for (AstNode returnYieldStatement : returnYieldStatements){ if (CheckUtils.insideFunction(returnYieldStatement, node) && !returnReturnNone(returnYieldStatement)){ raiseIssue(returnYieldStatement); } } }