@Override public void visitArrowFunction(ArrowFunctionTree tree) { scan(tree.parameters()); scan(tree.conciseBody()); }
private void checkBody(ArrowFunctionTree tree) { boolean hasBodyBraces = tree.body().is(Kind.BLOCK); if (bodyBraces && !hasBodyBraces) { addIssue(tree.body(), MESSAGE_ADD_BODY); } if (!bodyBraces && hasBodyBraces) { List<StatementTree> statements = ((BlockTree) tree.body()).statements(); if (statements.size() == 1) { StatementTree statement = statements.get(0); if (isRemovableReturn(statement)) { addIssue(tree.body(), MESSAGE_REMOVE_BODY); } } } }
private static Tree getTokenForIssueLocation(Tree tree) { if (tree.is(Kind.ARROW_FUNCTION)) { return ((ArrowFunctionTree) tree).doubleArrowToken(); } else { return tree.firstToken(); } }
@Override public void visitArrowFunction(ArrowFunctionTree lambdaExpressionTree) { // skip arrow function if body is an assignement if (!(lambdaExpressionTree.conciseBody() instanceof AssignmentExpressionTree)) { super.visitArrowFunction(lambdaExpressionTree); } }
private void addParametersToScope(AstNode functionNode, Set<String> currentScope) { if (functionNode.is(Kind.ARROW_FUNCTION)) { addArrowParametersToScope(((ArrowFunctionTree) functionNode).parameters(), currentScope); } else { addFormalParametersToScope((ParameterListTreeImpl) functionNode.getFirstChild(Kind.FORMAL_PARAMETER_LIST), currentScope); } }
private void checkParameterClause(ArrowFunctionTree tree) { boolean hasParameterParens = tree.parameterClause().is(Kind.PARAMETER_LIST); if (parameterParens && !hasParameterParens) { addIssue(tree.parameterClause(), MESSAGE_ADD_PARAMETER); } if (!parameterParens && hasParameterParens) { ParameterListTree parameterListTree = (ParameterListTree) tree.parameterClause(); SeparatedList<BindingElementTree> parameters = parameterListTree.parameters(); if (parameters.size() == 1 && parameters.get(0).is(Kind.BINDING_IDENTIFIER) && !hasCommentInside(parameterListTree)) { addIssue(tree.parameterClause(), MESSAGE_REMOVE_PARAMETER); } } }
/** * Ignoring function declared in function body block */ @Override public void visitArrowFunction(ArrowFunctionTree tree) { if (tree.conciseBody().is(Kind.BLOCK)) { scan(((BlockTree) tree.conciseBody()).statements()); } else { super.visitArrowFunction(tree); } }
@Override public void visitArrowFunction(ArrowFunctionTree tree) { Tree parameterClause = tree.parameterClause(); if (parameterClause.is(Kind.PARAMETER_LIST)) { checkNumberOfParameters((ParameterListTree) parameterClause); } super.visitArrowFunction(tree); }
private void raiseIssue(CognitiveComplexity.ComplexityData complexityData, Tree function) { String message = String.format(MESSAGE, complexityData.complexity(), threshold); SyntaxToken primaryLocation = function.firstToken(); if (function.is(ARROW_FUNCTION)) { primaryLocation = ((ArrowFunctionTree) function).doubleArrowToken(); } PreciseIssue issue = addIssue(primaryLocation, message).cost(complexityData.complexity() - (double)threshold); complexityData.secondaryLocations().forEach(issue::secondary); }
@Override public void visitArrowFunction(ArrowFunctionTree tree) { checkFunction(tree, tree.body()); super.visitArrowFunction(tree); }
private static SyntaxToken functionToken(FunctionTree functionTree) { SyntaxToken token; if (functionTree.is(Kind.FUNCTION_DECLARATION, Kind.GENERATOR_DECLARATION)) { token = ((FunctionDeclarationTree) functionTree).name().identifierToken(); } else if (functionTree.is(Kind.FUNCTION_EXPRESSION, Kind.GENERATOR_FUNCTION_EXPRESSION)) { token = ((FunctionExpressionTree) functionTree).functionKeyword(); } else { token = ((ArrowFunctionTree) functionTree).doubleArrowToken(); } return token; }
private void checkFunction(Tree parent, SyntaxToken openCurly) { if (parent.is(Kind.FUNCTION_DECLARATION, Kind.METHOD, Kind.GENERATOR_DECLARATION, Kind.FUNCTION_EXPRESSION, Kind.GENERATOR_FUNCTION_EXPRESSION)) { issueIfLineMismatch(openCurly, getParameterList((FunctionTree) parent).closeParenthesisToken()); } if (parent.is(Kind.ARROW_FUNCTION)) { issueIfLineMismatch(openCurly, ((ArrowFunctionTree) parent).doubleArrowToken()); } }
private void raiseIssue(Tree functionTree, FunctionReturns functionReturns, BlockTree body) { SyntaxToken tokenToRaiseIssue = functionTree.firstToken(); if (functionTree.is(Kind.ARROW_FUNCTION)) { tokenToRaiseIssue = ((ArrowFunctionTree) functionTree).doubleArrowToken(); } if (functionTree.is(Kind.GENERATOR_METHOD)) { tokenToRaiseIssue = ((MethodDeclarationTree) functionTree).name().firstToken(); } PreciseIssue issue = addIssue(tokenToRaiseIssue, MESSAGE); for (ReturnStatementTree returnStatement : functionReturns.returnStatements()) { issue.secondary(returnStatement.returnKeyword(), returnStatement.expression() == null ? "Return without value" : "Return with value"); } if (functionReturns.containsImplicitReturn()) { issue.secondary(body.closeCurlyBraceToken(), "Implicit return without value"); } }