Search in sources :

Example 71 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class ProcessClosurePrimitives method createUnknownTypeJsDocInfo.

private JSDocInfo createUnknownTypeJsDocInfo() {
    JSDocInfoBuilder castToUnknownBuilder = new JSDocInfoBuilder(true);
    castToUnknownBuilder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("?"), "<ProcessClosurePrimitives.java>"));
    return castToUnknownBuilder.build();
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 72 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class JSTypeCreatorFromJSDoc method getInterfacesHelper.

private ImmutableSet<NominalType> getInterfacesHelper(JSDocInfo jsdoc, DeclaredTypeRegistry registry, ImmutableList<String> typeParameters, boolean implementedIntfs) {
    ImmutableSet.Builder<NominalType> builder = ImmutableSet.builder();
    for (JSTypeExpression texp : (implementedIntfs ? jsdoc.getImplementedInterfaces() : jsdoc.getExtendedInterfaces())) {
        Node expRoot = texp.getRoot();
        JSType interfaceType = getTypeFromComment(expRoot, registry, typeParameters);
        NominalType nt = interfaceType.getNominalTypeIfSingletonObj();
        if (nt != null && nt.isInterface()) {
            builder.add(nt);
        } else if (implementedIntfs) {
            warnings.add(JSError.make(expRoot, IMPLEMENTS_NON_INTERFACE, interfaceType.toString()));
        } else {
            warnings.add(JSError.make(expRoot, EXTENDS_NON_INTERFACE, interfaceType.toString()));
        }
    }
    return builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 73 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class PolymerClassRewriter method makeReadOnlySetter.

/**
 * Adds the generated setter for a readonly property.
 *
 * @see https://www.polymer-project.org/0.8/docs/devguide/properties.html#read-only
 */
private Node makeReadOnlySetter(MemberDefinition prop, String qualifiedPath) {
    String propName = prop.name.getString();
    String setterName = "_set" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1);
    Node fnNode = IR.function(IR.name(""), IR.paramList(IR.name(propName)), IR.block());
    compiler.reportChangeToChangeScope(fnNode);
    Node exprResNode = IR.exprResult(IR.assign(NodeUtil.newQName(compiler, qualifiedPath + setterName), fnNode));
    JSDocInfo.Builder info = JSDocInfo.builder().parseDocumentation();
    // This is overriding a generated function which was added to the interface in
    // {@code createExportsAndExterns}.
    info.recordOverride();
    JSTypeExpression propType = PolymerPassStaticUtils.getTypeFromProperty(prop, compiler);
    info.recordParameter(propName, propType);
    exprResNode.getFirstChild().setJSDocInfo(info.build());
    return exprResNode;
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 74 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class PolymerClassRewriter method createNewTypeExpressionForExtern.

/**
 * Creates a new type expression for JSDoc like @type {{ propertyName : ? }}
 */
private static JSTypeExpression createNewTypeExpressionForExtern(String propName, String sourceName, MemberDefinition prop) {
    Node leftCurly = new Node(Token.LC);
    Node leftBracket = new Node(Token.LB);
    Node colon = new Node(Token.COLON);
    Node propertyName = Node.newString(propName);
    propertyName.setToken(Token.STRING_KEY);
    Node unknown = new Node(Token.QMARK);
    colon.addChildToBack(propertyName);
    colon.addChildToBack(unknown);
    leftBracket.addChildToBack(colon);
    leftCurly.addChildToBack(leftBracket);
    leftCurly.srcrefTreeIfMissing(prop.name);
    return new JSTypeExpression(leftCurly, sourceName);
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 75 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class PolymerClassRewriter method replaceMethodStringWithReflectedCalls.

/**
 * Given a Polymer method call string such as: <code>"methodName(path.item, other.property.path)"
 * </code> parses the string into a method name and arguments and builds up a new string of
 * property reflection calls so that the properties can be renamed consistently.
 *
 * <p>Returns a list of property sink statements to guard against dead code elimination.
 */
private List<Node> replaceMethodStringWithReflectedCalls(Node className, Node methodSignature) {
    checkArgument(methodSignature.isStringLit());
    List<Node> propertySinkStatements = new ArrayList<>();
    String methodSignatureString = methodSignature.getString().trim();
    int openParenIndex = methodSignatureString.indexOf('(');
    if (methodSignatureString.charAt(methodSignatureString.length() - 1) != ')' || openParenIndex < 1) {
        compiler.report(JSError.make(methodSignature, PolymerPassErrors.POLYMER_UNPARSABLE_STRING));
        return propertySinkStatements;
    }
    // Reflect property calls require an instance of a type. Since we don't have one,
    // just cast an object literal to be that type. While not generally safe, it is
    // safe for property reflection.
    JSDocInfo.Builder classTypeDoc = JSDocInfo.builder();
    JSTypeExpression classType = new JSTypeExpression(new Node(Token.BANG, IR.string(className.getQualifiedName())).srcrefTree(methodSignature), className.getSourceFileName());
    classTypeDoc.recordType(classType);
    Node classTypeExpression = IR.cast(IR.objectlit(), classTypeDoc.build());
    // Add reflect and property sinks for the method name which will be a property on the class
    String methodName = methodSignatureString.substring(0, openParenIndex).trim();
    propertySinkStatements.add(IR.getprop(className.cloneTree(), "prototype", methodName).srcrefTree(methodSignature));
    Node reflectedMethodName = IR.call(IR.getprop(IR.name("$jscomp"), "reflectProperty"), IR.string(methodName), classTypeExpression.cloneTree());
    Node reflectedSignature = reflectedMethodName;
    // Process any parameters in the method call
    String nextParamDelimeter = "(";
    if (openParenIndex < methodSignatureString.length() - 2) {
        String methodParamsString = methodSignatureString.substring(openParenIndex + 1, methodSignatureString.length() - 1).trim();
        List<String> methodParams = parseMethodParams(methodParamsString, methodSignature);
        // Add property reflection for each parameter
        for (String methodParam : methodParams) {
            Node reflectedTypeReference = classTypeExpression;
            if (methodParam.length() == 0) {
                continue;
            }
            if (isParamLiteral(methodParam)) {
                Node term = IR.string(methodParam);
                reflectedSignature = IR.add(IR.add(reflectedSignature, IR.string(nextParamDelimeter)), term);
            } else {
                // Arguments in conmplex observer or computed property strings are property paths.
                // We need to rename each path segment.
                List<String> paramParts = Splitter.on('.').splitToList(methodParam);
                String nextPropertyTermDelimiter = nextParamDelimeter;
                for (int i = 0; i < paramParts.size(); i++) {
                    // These terms are not renamable and are left as is
                    if (i > 0 && i == paramParts.size() - 1 && (paramParts.get(i).equals("*") || paramParts.get(i).equals("splices"))) {
                        reflectedSignature = IR.add(reflectedSignature, IR.string(nextPropertyTermDelimiter + paramParts.get(i)));
                    } else {
                        if (i == 0) {
                            // The root of the parameter will be a property reference on the class
                            // Create both a property sink and a reflection call
                            propertySinkStatements.add(IR.getprop(className.cloneTree(), "prototype", paramParts.get(i)).srcrefTree(methodSignature));
                        }
                        Node reflectedParamPart = IR.call(IR.getprop(IR.name("$jscomp"), "reflectProperty"), IR.string(paramParts.get(i)), reflectedTypeReference.cloneTree());
                        reflectedSignature = IR.add(IR.add(reflectedSignature, IR.string(nextPropertyTermDelimiter)), reflectedParamPart);
                        reflectedTypeReference = IR.getprop(reflectedTypeReference.cloneTree(), paramParts.get(i));
                    }
                    nextPropertyTermDelimiter = ".";
                }
            }
            nextParamDelimeter = ",";
        }
        if (methodParams.isEmpty()) {
            reflectedSignature = IR.add(reflectedSignature, IR.string("()"));
        } else {
            reflectedSignature = IR.add(reflectedSignature, IR.string(")"));
        }
    } else {
        reflectedSignature = IR.add(reflectedSignature, IR.string("()"));
    }
    methodSignature.replaceWith(reflectedSignature.srcrefTree(methodSignature));
    compiler.reportChangeToEnclosingScope(reflectedSignature);
    return propertySinkStatements;
}
Also used : Node(com.google.javascript.rhino.Node) ArrayList(java.util.ArrayList) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Aggregations

JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)101 Node (com.google.javascript.rhino.Node)67 JSDocInfo (com.google.javascript.rhino.JSDocInfo)58 Test (org.junit.Test)26 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)18 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)9 JSType (com.google.javascript.rhino.jstype.JSType)9 ArrayList (java.util.ArrayList)8 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)7 Map (java.util.Map)6 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)2 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)2