@Override public void init() { final FunctionNode root = parserResult.getLookup().lookup(FunctionNode.class); if (root != null) { root.accept(this); } }
private String debugInfo(Node node) { StringBuilder sb = new StringBuilder(); if (node instanceof FunctionNode) { FunctionNode fn = (FunctionNode)node; sb.append("FunctionNode name: ").append(fn.getName()); sb.append(", Ident: ").append(fn.getIdent()); if (fn.allVarsInScope()) sb.append(", allVarsInScope"); if (fn.isAnonymous()) sb.append(", isAnonymous"); if (fn.isDeclared()) sb.append(", isDeclared"); if (fn.isMethod()) sb.append(", isMethod"); if (fn.isNamedFunctionExpression()) sb.append(", isNamedFunctionExpression"); if (fn.isVarArg()) sb.append(", isVarArg"); if (fn.hasDeclaredFunctions()) sb.append(", hasDeclaredFunctions"); if (fn.hasDirectSuper()) sb.append(", hasDirectSuper"); // if (fn.hasScopeBlock()) sb.append(", hasScoprBlock"); } else if (node instanceof VarNode) { VarNode vn = (VarNode)node; sb.append("VarNode ").append(vn.getName()); if (vn.isBlockScoped()) sb.append(", isBlockScoped"); if (vn.isConst()) sb.append(", isConst"); if (vn.isFunctionDeclaration()) sb.append(", isFunctionDeclaration"); if (vn.isLet()) sb.append(", isLet"); } else { sb.append(node.getClass().getName()); } return sb.toString(); }
public static OffsetRange getOffsetRange(FunctionNode node) { return new OffsetRange(Token.descPosition(node.getFirstToken()), Token.descPosition(node.getLastToken()) + Token.descLength(node.getLastToken())); }
public String getFunctionName(FunctionNode node) { if (node.isAnonymous() ) { return globalObject.getName() + node.getName().replace(':', '#'); } else { if (node.isNamedFunctionExpression()) { return node.getName(); } return node.getIdent().getName(); } } }
private boolean isFunctionAnonymous(FunctionNode fn) { boolean result = false; if (fn.isAnonymous() ) { Node lastVisited = getPreviousFromPath(2); if (fn.getIdent().getName().startsWith("L:") && !(lastVisited instanceof PropertyNode)) { //NOI18N // XXX this depends on the implemenation of parser. Find the better way result = true; } else if (fn.getIdent().getStart() == fn.getIdent().getFinish()) { if (lastVisited instanceof CallNode) { result = true; } } } return result; }
@Override public boolean enterFunctionNode(FunctionNode functionNode) { if (functionNode.isClassConstructor() && !ModelUtils.CONSTRUCTOR.equals(functionNode.getName())) { if (functionNode.isProgram()) { fncScope = fncParent; if (this.parserResult.getSnapshot().getSource().getFileObject() != null) { if (property == null && functionNode.isStrict()) { property = ((DeclarationScopeImpl)modelBuilder.getCurrentDeclarationScope()).getProperty(modelBuilder.getFunctionName(functionNode)); LOGGER.log(Level.FINE, "FunctionNode: " + functionNode.toString() + " is not processed, because parent function " + fncParent.toString() + " doesn't contain such property."); //NOI18N return false; fncScope.setStrict(functionNode.isStrict()); if (!functionNode.isProgram() && !functionNode.isModule()) { correctNameAndOffsets(fncScope, functionNode); setParent(fncScope, functionNode); if (functionNode.isModule()) { List<ImportNode> imports = functionNode.getModule().getImports(); for (ImportNode moduleImport : imports) { moduleImport.accept(this); List<ExportNode> exports = functionNode.getModule().getExports(); for (ExportNode moduleExport : exports) { moduleExport.accept(this);
if (!lc.getParentFunction(fn).isProgram() && !(lastVisited instanceof PropertyNode || lastVisited instanceof BinaryNode)) { PropertyNode pNode = (PropertyNode)lastVisited; isStatic = pNode.isStatic(); if (fn.isClassConstructor() || fn.isSubclassConstructor()) { jsFunction.setJsKind(JsElement.Kind.CONSTRUCTOR); } else if (fn.isMethod()) { if (fn.equals(pNode.getGetter())) { jsFunction.setJsKind(JsElement.Kind.PROPERTY_GETTER); } else if (fn.equals(pNode.getSetter())) { jsFunction.setJsKind(JsElement.Kind.PROPERTY_SETTER); } else { if (fn.getKind() == FunctionNode.Kind.GENERATOR) {
private void handleDeclaredFunction(DeclarationScopeImpl inScope, JsObject parent, FunctionNode fnNode) { LOGGER.log(Level.FINEST, " function: " + debugInfo(fnNode)); // NOI18N String name = fnNode.isAnonymous() ? modelBuilder.getFunctionName(fnNode) : fnNode.getIdent().getName(); Identifier fnName = new Identifier(name, new OffsetRange(fnNode.getIdent().getStart(), fnNode.getIdent().getFinish())); if (fnNode.isClassConstructor() && !ModelUtils.CONSTRUCTOR.equals(fnName.getName())) { // skip artifical/ syntetic constructor nodes, that are created // when a class extends different class return; } // process parameters List<Identifier> parameters = new ArrayList(fnNode.getParameters().size()); for(IdentNode node: fnNode.getParameters()) { Identifier param = create(parserResult, node); if (param != null && !node.isDestructuredParameter()) { // can be null, if it's a generated embeding. parameters.add(param); } } // The parent can be changed in the later processing JsFunctionImpl declaredFn = new JsFunctionImpl(inScope, parent, fnName, parameters, getOffsetRange(fnNode), inScope.getMimeType(), inScope.getSourceLabel()); inScope.addProperty(modelBuilder.getFunctionName(fnNode), declaredFn); if (fnName.getOffsetRange().getLength() > 0 && !fnNode.isNamedFunctionExpression()) { declaredFn.addOccurrence(fnName.getOffsetRange()); } }
private void processDeclarations(final JsFunctionImpl parentFn, final FunctionNode inNode) { LOGGER.log(Level.FINEST, "in function: " + inNode.getName() + ", ident: " + inNode.getIdent()); final JsDocumentationHolder docHolder = JsDocumentationSupport.getDocumentationHolder(parserResult); Block block = inNode.getBody(); PathNodeVisitor visitor = new PathNodeVisitor(lc) { if (inNode.isModule() && inNode.getModule().getExports() != null) { for(ExportNode export :inNode.getModule().getExports()) { if (!export.isDefault()) { Expression expression = export.getExpression(); if ((expression instanceof ClassNode && ((ClassNode)expression).getIdent() != null) || (expression instanceof FunctionNode && ((FunctionNode)expression).getIdent() != null)) { export.accept(visitor);
int start = Token.descPosition(functionNode.getFirstToken()); int end = Token.descPosition(functionNode.getLastToken()) + Token.descLength(functionNode.getLastToken()); if (end <= start) { end = start + 1; assert false: "The end offset of a function is before the start offset: [" + start + ", " + end + "] in file: " + parserResult.getSnapshot().getSource().getFileObject().getPath(); //NOI18N List<Identifier> parameters = new ArrayList(functionNode.getParameters().size()); for(IdentNode node: functionNode.getParameters()) { Identifier param = create(parserResult, node); if (param != null) {
ident.getName(), getOffsetRange(ident))); } else if (node instanceof FunctionNode) { if (((FunctionNode) node).getKind() == FunctionNode.Kind.SCRIPT) { return Collections.<Identifier>emptyList(); IdentNode ident = ((FunctionNode) node).getIdent(); return Arrays.<Identifier>asList(new Identifier( ident.getName(), getOffsetRange(ident)));
@Override public boolean enterFunctionNode(FunctionNode functionNode) { String name = builder.getFunctionName(functionNode); exp.add(name); if (typeOffset == -1) { typeOffset = functionNode.getIdent().getStart(); } return false; }
isDeclaredInParent = ((FunctionNode) lastVisited).getKind() == FunctionNode.Kind.SCRIPT;
} else if (lastVisited instanceof VarNode) { VarNode varNode = (VarNode)lastVisited; if (fn.isNamedFunctionExpression()) { Identifier refName = new Identifier(fn.getIdent().getName(), new OffsetRange(fn.getIdent().getStart(), fn.getIdent().getFinish())); JsFunctionReference jsRef = new JsFunctionReference(jsFunction, refName, jsFunction, true, EnumSet.of(Modifier.PRIVATE)); jsRef.addOccurrence(jsRef.getDeclarationName().getOffsetRange()); } else if ((varNode.isFunctionDeclaration() || fn.isAnonymous())) { parent = jsObject.getParent(); if (fn.isNamedFunctionExpression()) { Identifier refName = new Identifier(fn.getIdent().getName(), new OffsetRange(fn.getIdent().getStart(), fn.getIdent().getFinish())); JsFunctionReference jsRef = new JsFunctionReference(jsFunction, refName, jsFunction, true, EnumSet.of(Modifier.PRIVATE)); jsRef.addOccurrence(jsRef.getDeclarationName().getOffsetRange()); if (fn.isNamedFunctionExpression() && fn.getName().equals(varName.getName())) {
private List<Identifier> getName(PropertyNode propertyNode) { List<Identifier> name = new ArrayList(1); if (propertyNode.getGetter() != null || propertyNode.getSetter() != null) { // check whether this is not defining getter or setter of a property. Node previousNode = getPreviousFromPath(1); if (previousNode instanceof FunctionNode) { FunctionNode fNode = (FunctionNode)previousNode; String fName = fNode.getIdent().getName(); if (fName.startsWith("get ") || fName.startsWith("set ")) { //NOI18N name.add(new Identifier(fName, new OffsetRange(fNode.getIdent().getStart(), fNode.getIdent().getFinish()))); return name; } } } return getName(propertyNode, parserResult); }
isDeclaredInParent = ((FunctionNode) lastVisited).getKind() == FunctionNode.Kind.SCRIPT;