Fixed AddMemberPass visit() to iterate of Script node children. - Fixed fields and methods that were not getting added due to no jsdoc in source code.
Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/bbd2df6a Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/bbd2df6a Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/bbd2df6a Branch: refs/heads/develop Commit: bbd2df6a4a2b55c6091d6ca7d7127eafdc73a64d Parents: 6a4b3a8 Author: Michael Schmalle <[email protected]> Authored: Sat Jun 13 17:47:39 2015 -0400 Committer: Michael Schmalle <[email protected]> Committed: Sat Jun 13 17:56:17 2015 -0400 ---------------------------------------------------------------------- .../codegen/externals/pass/AddMemberPass.java | 280 ++++++------------- .../externals/reference/ClassReference.java | 60 ++-- .../externals/reference/ReferenceModel.java | 4 +- 3 files changed, 129 insertions(+), 215 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bbd2df6a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/AddMemberPass.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/AddMemberPass.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/AddMemberPass.java index d08f9b1..88ac9e5 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/AddMemberPass.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/AddMemberPass.java @@ -23,10 +23,7 @@ import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceMo import com.google.javascript.jscomp.AbstractCompiler; import com.google.javascript.jscomp.NodeTraversal; -import com.google.javascript.rhino.JSDocInfo; -import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; -import com.google.javascript.rhino.jstype.JSType; public class AddMemberPass extends AbstractCompilerPass { @@ -37,110 +34,95 @@ public class AddMemberPass extends AbstractCompilerPass } @Override - public void visit(NodeTraversal t, Node n, Node parent) + public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, + Node parent) { - if (n.getParent() == null || n.isScript() || n.isSyntheticBlock()) - return; + return n.isBlock() || n.isScript(); + } - JSDocInfo jsDoc = n.getJSDocInfo(); //NodeUtil.getBestJSDocInfo(n); - if (jsDoc != null) + @Override + public void visit(NodeTraversal t, Node n, Node parent) + { + for (Node child : n.children()) { - if (n.isVar()) - { - visitVar(t, n); - } - else if (n.isFunction()) - { - visitFunction(t, n); - } - else if (n.isAssign()) + //log(child); + + if (child.isExprResult()) { - if (n.getFirstChild().isGetProp() - && n.getLastChild().isFunction()) + Node first = child.getFirstChild(); + + if (first.isVar()) { - // instance or static method - visitMethod(t, n); + // visitVar(t, n); } - else + else if (first.isFunction()) { - // DOMException.INDEX_SIZE_ERR = 1; - // The first child of the assign is the GetProp node, - // if later you need the value, either change this or check - // for a parent assign node when creating the FieldReference - // which the value would be n.getLastChild() - // XXX visitStaticField(t, n); - //System.err.println(n.toStringTree()); + // visitFunction(t, n); + } + else if (first.isAssign()) + { + if (first.getFirstChild().isGetProp() + && first.getLastChild().isFunction()) + { + // instance or static method + visitMethod(t, first); + } + else + { + // DOMException.INDEX_SIZE_ERR = 1; + // The first child of the assign is the GetProp node, + // if later you need the value, either change this or check + // for a parent assign node when creating the FieldReference + // which the value would be n.getLastChild() + // XXX visitStaticField(t, n); + //System.err.println(n.toStringTree()); + } + } + else if (first.isGetProp()) + { + visitGetProp(t, first); } } - else if (n.isGetProp()) - { - visitGetProp(t, n); - } - } - } - - /* - ASSIGN 48 [jsdoc_info: JSDocInfo] [source_file: [w3c_dom1]] [length: 38] - GETPROP 48 [source_file: [w3c_dom1]] [length: 34] - NAME DOMException 48 [source_file: [w3c_dom1]] [length: 12] - STRING HIERARCHY_REQUEST_ERR 48 [source_file: [w3c_dom1]] [length: 21] - NUMBER 3.0 48 [source_file: [w3c_dom1]] [length: 1] - */ - - // Instance Field (prototype) - // GETPROP 2026 [jsdoc_info: JSDocInfo] [source_file: [es3]] [length: 27] - // GETPROP 2026 [source_file: [es3]] [length: 16] - // NAME RegExp 2026 [source_file: [es3]] [length: 6] - // STRING prototype 2026 [source_file: [es3]] [length: 9] - // STRING ignoreCase 2026 [source_file: [es3]] [length: 10] - @SuppressWarnings("unused") - private void visitInstanceField(NodeTraversal t, Node n) - { - Node className = n.getFirstChild().getFirstChild(); - Node name = n.getLastChild(); - model.addField(n, className.getString(), name.getString()); - } - - // Static Field - // GETPROP 1994 [jsdoc_info: JSDocInfo] [source_file: [es3]] [length: 9] - // NAME RegExp 1994 [source_file: [es3]] [length: 6] - // STRING $6 1994 [source_file: [es3]] [length: 2] - - @SuppressWarnings("unused") - private void visitStaticField(NodeTraversal t, Node n) - { - Node className = n.getFirstChild(); - Node name = n.getLastChild(); - model.addStaticField(n, className.getString(), name.getString()); + } + // JSDocInfo jsDoc = n.getJSDocInfo(); + // if (jsDoc != null) + // { + // if (n.isVar()) + // { + // // visitVar(t, n); + // } + // else if (n.isFunction()) + // { + // // visitFunction(t, n); + // } + // else if (n.isAssign()) + // { + // if (n.getFirstChild().isGetProp() + // && n.getLastChild().isFunction()) + // { + // // instance or static method + // visitMethod(t, n); + // } + // else + // { + // // DOMException.INDEX_SIZE_ERR = 1; + // // The first child of the assign is the GetProp node, + // // if later you need the value, either change this or check + // // for a parent assign node when creating the FieldReference + // // which the value would be n.getLastChild() + // // XXX visitStaticField(t, n); + // //System.err.println(n.toStringTree()); + // } + // } + // else if (n.isGetProp()) + // { + // visitGetProp(t, n); + // } + // } } - /* - - Instance - - ASSIGN 194 [jsdoc_info: JSDocInfo] [source_file: [es3]] [length: 44] - GETPROP 194 [source_file: [es3]] [length: 28] - GETPROP 194 [source_file: [es3]] [length: 16] - NAME Object 194 [source_file: [es3]] [length: 6] - STRING prototype 194 [source_file: [es3]] [length: 9] - STRING constructor 194 [source_file: [es3]] [length: 11] - FUNCTION 194 [source_file: [es3]] [length: 13] - NAME 194 [source_file: [es3]] [length: 13] - PARAM_LIST 194 [source_file: [es3]] [length: 2] - BLOCK 194 [source_file: [es3]] [length: 2] - - Static - - ASSIGN 770 [jsdoc_info: JSDocInfo] [source_file: [es3]] [length: 52] - GETPROP 770 [source_file: [es3]] [length: 10] - NAME Array 770 [source_file: [es3]] [length: 5] - STRING some 770 [source_file: [es3]] [length: 4] - - - */ - // n == ASSIGN private void visitMethod(NodeTraversal t, Node n) { @@ -176,120 +158,30 @@ public class AddMemberPass extends AbstractCompilerPass private void visitGetProp(NodeTraversal t, Node n) { - //log(n.toStringTree()); - log(n.getQualifiedName()); + String qualifiedName = n.getQualifiedName(); - String qName = n.getQualifiedName(); - // Port.prototype.name + log("visitGetProp [" + qualifiedName + "]"); - // chrome.runtime.lastError.message - int protoType = qName.indexOf(".prototype"); + int protoType = qualifiedName.indexOf(".prototype"); if (protoType != -1) { - String className = qName.substring(0, protoType); - String memberName = qName.substring(protoType + 11, qName.length()); + String className = qualifiedName.substring(0, protoType); + String memberName = qualifiedName.substring(protoType + 11, + qualifiedName.length()); //log("Prototype:: className [" + className // + "] memberName [" + memberName + "]"); model.addField(n, className, memberName); } else { - String className = qName.substring(0, qName.lastIndexOf(".")); - String memberName = qName.substring(qName.lastIndexOf(".") + 1, - qName.length()); + String className = qualifiedName.substring(0, + qualifiedName.lastIndexOf(".")); + String memberName = qualifiedName.substring( + qualifiedName.lastIndexOf(".") + 1, qualifiedName.length()); //log("className [" + className + "] memberName [" // + memberName + "]"); model.addStaticField(n, className, memberName); } - - } - - /* - - FUNCTION Arguments 34 [jsdoc_info: JSDocInfo] [source_file: [test]] [length: 23] - NAME Arguments 34 [source_file: [test]] [length: 9] - PARAM_LIST 34 [source_file: [test]] [length: 2] - BLOCK 34 [source_file: [test]] [length: 2] - - */ - - @SuppressWarnings("unused") - private void visitFunction(NodeTraversal t, Node n) - { - - JSDocInfo jsDoc = n.getJSDocInfo(); - - if (jsDoc != null) - { - if (jsDoc.isConstructor()) - { - //System.out.println("---------------------------------------------"); - Node name = n.getChildAtIndex(0); - //System.out.println("Class " + name.getString()); - - Node paramList = n.getChildAtIndex(1); - - JSTypeExpression returnType = jsDoc.getReturnType(); - if (returnType != null) - { - JSType jsReturnType = returnType.evaluate(null, - compiler.getTypeRegistry()); - //System.out.println("Returns: " + jsReturnType); - } - - Node block = n.getChildAtIndex(2); - //System.out.println(">>>>>>--------------------------------------"); - //System.out.println(n.toStringTree()); - } - else - { - // XX Global function parseInt(num, base) - //System.out.println(n.toStringTree()); - } - } - } - - private void visitVar(NodeTraversal t, Node n) - { - JSDocInfo jsDoc = n.getJSDocInfo(); - - if (jsDoc != null) - { - if (jsDoc.isConstant()) - { - Node first = n.getFirstChild(); - if (first.isName()) - { - Node second = first.getFirstChild(); - if (second != null && second.isObjectLit()) - { - // * @const - // var Math = {}; - //System.out.println("Final Class " - // + n.getFirstChild().getString()); - //System.out.println(n.toStringTree()); - } - else - { - // * @const - // var Infinity; - //System.out.println("var " - // + n.getFirstChild().getString()); - //System.out.println(n.toStringTree()); - } - } - } - else if (jsDoc.isConstructor()) - { - } - } - } - - @Override - public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, - Node parent) - { - return true; } } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bbd2df6a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java index 880d2da..3196261 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java @@ -26,9 +26,11 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.flex.compiler.internal.codegen.externals.utils.DebugLogUtils; import org.apache.flex.compiler.internal.codegen.externals.utils.JSTypeUtils; import com.google.javascript.rhino.JSDocInfo; +import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; @@ -197,25 +199,6 @@ public class ClassReference extends BaseReference } - public FieldReference addField(Node node, String fieldName, - JSDocInfo comment, boolean isStatic) - { - if (hasField(fieldName)) - { - // XXX Warning - return null; - } - - if (isNamespace) - isStatic = false; - - FieldReference field = new FieldReference(getModel(), this, node, - fieldName, comment, isStatic); - - fields.put(fieldName, field); - return field; - } - public boolean hasSuperField(String fieldName) { List<ClassReference> list = getSuperClasses(); @@ -292,12 +275,51 @@ public class ClassReference extends BaseReference return methods.get(fieldName).isStatic(); } + public FieldReference addField(Node node, String fieldName, + JSDocInfo comment, boolean isStatic) + { + if (hasField(fieldName)) + { + // XXX Warning + return null; + } + + if (isNamespace) + isStatic = false; + + if (comment == null) + { + DebugLogUtils.err("Field comment null for; " + + node.getQualifiedName()); + //DebugLogUtils.err(node); + JSDocInfoBuilder b = new JSDocInfoBuilder(true); + b.recordBlockDescription("Generated doc for missing field JSDoc."); + comment = b.build(); + } + + FieldReference field = new FieldReference(getModel(), this, node, + fieldName, comment, isStatic); + + fields.put(fieldName, field); + return field; + } + public MethodReference addMethod(Node node, String functionName, JSDocInfo comment, boolean isStatic) { if (isNamespace) isStatic = false; + if (comment == null) + { + DebugLogUtils.err("Method comment null for; " + + node.getQualifiedName()); + //DebugLogUtils.err(node); + JSDocInfoBuilder b = new JSDocInfoBuilder(true); + b.recordBlockDescription("Generated doc for missing method JSDoc."); + comment = b.build(); + } + MethodReference method = new MethodReference(getModel(), this, node, functionName, comment, isStatic); methods.put(functionName, method); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bbd2df6a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java index 9e4aa1c..5c7de17 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java @@ -260,10 +260,10 @@ public class ReferenceModel public void addMethod(Node node, String className, String memberName) { + JSDocInfo comment = NodeUtil.getBestJSDocInfo(node); ClassReference classReference = getClassReference(className); if (classReference != null) - classReference.addMethod(node, memberName, node.getJSDocInfo(), - false); + classReference.addMethod(node, memberName, comment, false); } public void addStaticMethod(Node node, String className, String memberName)
