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) { if (tree.name() != null){ getContext().addIssue(this, tree, "Make this function anonymous by removing its name: 'function() {...}'."); } super.visitFunctionExpression(tree); }
@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 visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.name()); scan(tree.parameters()); scan(tree.body()); }
@Override public void visitFunctionExpression(FunctionExpressionTree tree) { scan(tree.name()); scan(tree.parameters()); // Ignoring empty function scan(tree.body().statements()); }
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(); }