private static Tree getTokenForIssueLocation(Tree tree) { if (tree.is(Kind.ARROW_FUNCTION)) { return ((ArrowFunctionTree) tree).doubleArrowToken(); } else { return tree.firstToken(); } }
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); }
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"); } }