@Override public void visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.name()); scan(tree.parameters()); scan(tree.body()); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { checkFunction(tree, tree.functionKeyword()); super.visitFunctionExpression(tree); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { checkNumberOfParameters(tree.parameterClause()); super.visitFunctionExpression(tree); }
private static IssueLocation getPrimaryLocation(Tree tree) { Tree firstTree; Tree lastTree; if (tree.is(Kind.GENERATOR_DECLARATION)) { FunctionDeclarationTree functionDeclarationTree = (FunctionDeclarationTree) tree; firstTree = functionDeclarationTree.functionKeyword(); lastTree = functionDeclarationTree.name(); } else if (tree.is(Kind.GENERATOR_METHOD)) { MethodDeclarationTree methodDeclarationTree = (MethodDeclarationTree) tree; firstTree = methodDeclarationTree.starToken(); lastTree = methodDeclarationTree.name(); } else { FunctionExpressionTree functionExpressionTree = (FunctionExpressionTree) tree; firstTree = functionExpressionTree.functionKeyword(); if (functionExpressionTree.name() != null) { lastTree = functionExpressionTree.name(); } else { lastTree = functionExpressionTree.starToken(); } } return new IssueLocation(firstTree, lastTree, MESSAGE); } }
/** * Detail about <a href="http://people.mozilla.org/~jorendorff/es6-draft.html#sec-function-definitions-runtime-semantics-evaluation">Function Expression scope</a> * <blockquote> * The BindingIdentifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody * to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the BindingIdentifier * in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression. * </blockquote> **/ @Override public void visitFunctionExpression(FunctionExpressionTree tree) { newScope(tree); IdentifierTree name = tree.name(); if (name != null) { // Not available in enclosing scope symbolModel.declareSymbol(name.name(), Symbol.Kind.FUNCTION, currentScope).addUsage(Usage.create(name, Usage.Kind.DECLARATION)); } declareParameters(((ParameterListTreeImpl) tree.parameters()).parameterIdentifiers()); addFunctionBuiltInSymbols(); super.visitFunctionExpression(tree); leaveScope(); }
private boolean isFunctionExpressionException(FunctionExpressionTree functionExpressionTree){ int line = ((JavaScriptTree) functionExpressionTree).getLine(); List<StatementTree> statements = functionExpressionTree.body().statements(); return statements.size() == 1 && ((JavaScriptTree)statements.get(0)).getLine() == line && statementsPerLine.containsKey(line); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { checkNumberOfParameters(tree.parameters()); super.visitFunctionExpression(tree); }
public IdentifierTree getFunctionName(Tree tree) { if (tree instanceof FunctionExpressionTree) { return ((FunctionExpressionTree) tree).name(); } else if (tree instanceof FunctionDeclarationTree) { return ((FunctionDeclarationTree) tree).name(); } else { ExpressionTree name = ((MethodDeclarationTree) tree).name(); return name instanceof IdentifierTree ? (IdentifierTree) name : null; } } }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.body().statements()); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.name()); scan(tree.parameters()); // Ignoring empty function scan(tree.body().statements()); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { if (tree.name() != null){ getContext().addIssue(this, tree, "Make this function anonymous by removing its name: 'function() {...}'."); } super.visitFunctionExpression(tree); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.body().statements()); }
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; }
@Override public void visitNode(AstNode astNode) { if (astNode.is(Kind.FUNCTION_DECLARATION, Kind.GENERATOR_DECLARATION)) { checkFunctionName(astNode, ((FunctionDeclarationTree) astNode).name()); } else if (astNode.is(Kind.FUNCTION_EXPRESSION, Kind.GENERATOR_FUNCTION_EXPRESSION)) { checkFunctionName(astNode, ((FunctionExpressionTree) astNode).name()); } else if (astNode.is(CONST_AND_VAR_NODES)) { for (IdentifierTree identifier : ((VariableDeclarationTreeImpl) astNode).variableIdentifiers()) { String variableName = identifier.name(); check((AstNode) identifier, functionsStack.peek(), variableName); variablesStack.peek().add(variableName); } } if (astNode.is(FUNCTION_NODES)) { variablesStack.add(new HashSet<String>()); functionsStack.add(new HashSet<String>()); } }
@Override public void visitNewExpression(NewExpressionTree tree) { ExpressionTree expression = tree.expression(); if (!expression.types().isEmpty() && !isConstructor(expression.types())) { Tree primaryLocationTree = expression; String expressionStr = CheckUtils.asString(expression); ExpressionTree unwrapped = CheckUtils.removeParenthesis(expression); if (unwrapped.is(Tree.Kind.FUNCTION_EXPRESSION)) { primaryLocationTree = ((FunctionExpressionTree) unwrapped).functionKeyword(); expressionStr = "this function"; } addIssue(primaryLocationTree, String.format(MESSAGE, expressionStr)) .secondary(tree.newKeyword()); } super.visitNewExpression(tree); }