Search in sources :

Example 36 with JSType

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

the class JsDocInfoParserTest method testTypedefType4.

// https://github.com/google/closure-compiler/issues/2543
public void testTypedefType4() {
    JSDocInfo info = parse(LINE_JOINER.join("@typedef {{", " *  boo: ?,", " *  goo: ?", " * }}", " */"));
    assertThat(info.hasTypedefType()).isTrue();
    JSType recordType = createRecordTypeBuilder().addProperty("boo", UNKNOWN_TYPE, null).addProperty("goo", UNKNOWN_TYPE, null).build();
    assertTypeEquals(recordType, info.getTypedefType());
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 37 with JSType

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

the class TypedScopeCreator method createScopeInternal.

private TypedScope createScopeInternal(Node root, TypedScope typedParent) {
    // Constructing the global scope is very different than constructing
    // inner scopes, because only global scopes can contain named classes that
    // show up in the type registry.
    TypedScope newScope = null;
    AbstractScopeBuilder scopeBuilder = null;
    if (typedParent == null) {
        JSType globalThis = typeRegistry.getNativeObjectType(JSTypeNative.GLOBAL_THIS);
        // Mark the main root, the externs root, and the src root
        // with the global this type.
        root.setJSType(globalThis);
        root.getFirstChild().setJSType(globalThis);
        root.getLastChild().setJSType(globalThis);
        // Run a first-order analysis over the syntax tree.
        new FirstOrderFunctionAnalyzer().process(root.getFirstChild(), root.getLastChild());
        // Find all the classes in the global scope.
        newScope = createInitialScope(root);
        scopeBuilder = new GlobalScopeBuilder(newScope);
    } else {
        newScope = new TypedScope(typedParent, root);
        scopeBuilder = new LocalScopeBuilder(newScope);
    }
    scopeBuilder.build();
    scopeBuilder.resolveStubDeclarations();
    if (typedParent == null) {
        List<NominalTypeBuilder> delegateProxies = new ArrayList<>();
        for (FunctionType delegateProxyCtor : delegateProxyCtors) {
            delegateProxies.add(new NominalTypeBuilderOti(delegateProxyCtor, delegateProxyCtor.getInstanceType()));
        }
        codingConvention.defineDelegateProxyPrototypeProperties(typeRegistry, delegateProxies, delegateCallingConventions);
    }
    newScope.setTypeResolver(scopeBuilder);
    return newScope;
}
Also used : NominalTypeBuilderOti(com.google.javascript.rhino.jstype.NominalTypeBuilderOti) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) ArrayList(java.util.ArrayList) NominalTypeBuilder(com.google.javascript.rhino.NominalTypeBuilder)

Example 38 with JSType

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

the class TypeValidator method getFunctionType.

/**
 * Utility function for getting a function type from a var.
 */
static FunctionType getFunctionType(@Nullable TypedVar v) {
    JSType t = v == null ? null : v.getType();
    ObjectType o = t == null ? null : t.dereference();
    return JSType.toMaybeFunctionType(o);
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType)

Example 39 with JSType

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

the class Matchers method matchesPrototypeInstanceVar.

/**
 * Checks to see if the node represents an access of an instance variable
 * on an object given a prototype declaration of an object. For instance,
 * {@code ns.AppContext.prototype.get} will match {@code appContext.get}
 * or {@code this.get} when accessed from within the AppContext object.
 */
private static boolean matchesPrototypeInstanceVar(Node node, NodeMetadata metadata, String name) {
    String[] parts = name.split(".prototype.");
    String className = parts[0];
    String propertyName = parts[1];
    JSType providedJsType = getJsType(metadata, className);
    if (providedJsType == null) {
        return false;
    }
    JSType jsType = null;
    if (node.hasChildren()) {
        jsType = node.getFirstChild().getJSType();
    }
    if (jsType == null) {
        return false;
    }
    jsType = jsType.restrictByNotNullOrUndefined();
    if (!jsType.isUnknownType() && !jsType.isAllType() && jsType.isSubtypeOf(providedJsType)) {
        if (node.isName() && propertyName.equals(node.getString())) {
            return true;
        } else if (node.isGetProp() && propertyName.equals(node.getLastChild().getString())) {
            return true;
        }
    }
    return false;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Example 40 with JSType

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

the class JSTypeExpression method evaluate.

/**
 * Evaluates the type expression into a {@code JSType} object.
 */
public JSType evaluate(StaticTypedScope<JSType> scope, TypeIRegistry registry) {
    if (registry instanceof JSTypeRegistry) {
        JSType type = ((JSTypeRegistry) registry).createTypeFromCommentNode(root, sourceName, scope);
        root.setJSType(type);
        return type;
    }
    return null;
}
Also used : JSTypeRegistry(com.google.javascript.rhino.jstype.JSTypeRegistry) JSType(com.google.javascript.rhino.jstype.JSType)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)189 Node (com.google.javascript.rhino.Node)60 FunctionType (com.google.javascript.rhino.jstype.FunctionType)38 ObjectType (com.google.javascript.rhino.jstype.ObjectType)37 JSDocInfo (com.google.javascript.rhino.JSDocInfo)9 FlowScope (com.google.javascript.jscomp.type.FlowScope)7 TemplateType (com.google.javascript.rhino.jstype.TemplateType)6 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)5 UnionType (com.google.javascript.rhino.jstype.UnionType)4 ArrayList (java.util.ArrayList)4 EnumType (com.google.javascript.rhino.jstype.EnumType)3 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)3 TemplateTypeMapReplacer (com.google.javascript.rhino.jstype.TemplateTypeMapReplacer)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)2 Scope (com.google.javascript.jscomp.Scope)2 Token (com.google.javascript.rhino.Token)2 FunctionBuilder (com.google.javascript.rhino.jstype.FunctionBuilder)2 TemplatizedType (com.google.javascript.rhino.jstype.TemplatizedType)2 HashMap (java.util.HashMap)2