Search in sources :

Example 16 with JSType

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

the class Matchers method jsDocType.

/**
 * Returns a Matcher that matches nodes that contain JS Doc that specify the
 * {@code @type} annotation equivalent to the provided type.
 */
public static Matcher jsDocType(final String type) {
    return new Matcher() {

        @Override
        public boolean matches(Node node, NodeMetadata metadata) {
            JSType providedJsType = getJsType(metadata, type);
            if (providedJsType == null) {
                return false;
            }
            providedJsType = providedJsType.restrictByNotNullOrUndefined();
            // The JSDoc for a var declaration is on the VAR node, but the type only
            // exists on the NAME node.
            // TODO(mknichel): Make NodeUtil.getBestJSDoc public and use that.
            JSDocInfo jsDoc = node.getParent().isVar() ? node.getParent().getJSDocInfo() : node.getJSDocInfo();
            JSType jsType = node.getJSType();
            return jsDoc != null && jsDoc.hasType() && jsType != null && providedJsType.isEquivalentTo(jsType.restrictByNotNullOrUndefined());
        }
    };
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 17 with JSType

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

the class Matchers method enumDefinitionOfType.

/**
 * Returns a Matcher that matches definitions of an enum of the given type.
 */
public static Matcher enumDefinitionOfType(final String type) {
    return new Matcher() {

        @Override
        public boolean matches(Node node, NodeMetadata metadata) {
            JSType providedJsType = getJsType(metadata, type);
            if (providedJsType == null) {
                return false;
            }
            providedJsType = providedJsType.restrictByNotNullOrUndefined();
            JSType jsType = node.getJSType();
            return jsType != null && jsType.isEnumType() && providedJsType.isEquivalentTo(jsType.toMaybeEnumType().getElementsType().getPrimitiveType());
        }
    };
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Example 18 with JSType

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

the class SemanticReverseAbstractInterpreter method caseAndOrNotShortCircuiting.

private FlowScope caseAndOrNotShortCircuiting(Node left, Node right, FlowScope blindScope, boolean outcome) {
    // left type
    JSType leftType = getTypeIfRefinable(left, blindScope);
    boolean leftIsRefineable;
    if (leftType != null) {
        leftIsRefineable = true;
    } else {
        leftIsRefineable = false;
        leftType = left.getJSType();
        blindScope = firstPreciserScopeKnowingConditionOutcome(left, blindScope, outcome);
    }
    // restricting left type
    JSType restrictedLeftType = (leftType == null) ? null : leftType.getRestrictedTypeGivenToBooleanOutcome(outcome);
    if (restrictedLeftType == null) {
        return firstPreciserScopeKnowingConditionOutcome(right, blindScope, outcome);
    }
    blindScope = maybeRestrictName(blindScope, left, leftType, leftIsRefineable ? restrictedLeftType : null);
    // right type
    JSType rightType = getTypeIfRefinable(right, blindScope);
    boolean rightIsRefineable;
    if (rightType != null) {
        rightIsRefineable = true;
    } else {
        rightIsRefineable = false;
        rightType = right.getJSType();
        blindScope = firstPreciserScopeKnowingConditionOutcome(right, blindScope, outcome);
    }
    if (outcome) {
        JSType restrictedRightType = (rightType == null) ? null : rightType.getRestrictedTypeGivenToBooleanOutcome(outcome);
        // creating new scope
        return maybeRestrictName(blindScope, right, rightType, rightIsRefineable ? restrictedRightType : null);
    }
    return blindScope;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Example 19 with JSType

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

the class SemanticReverseAbstractInterpreter method caseEquality.

private FlowScope caseEquality(Node left, Node right, FlowScope blindScope, Function<TypePair, TypePair> merging) {
    // left type
    JSType leftType = getTypeIfRefinable(left, blindScope);
    boolean leftIsRefineable;
    if (leftType != null) {
        leftIsRefineable = true;
    } else {
        leftIsRefineable = false;
        leftType = left.getJSType();
    }
    // right type
    JSType rightType = getTypeIfRefinable(right, blindScope);
    boolean rightIsRefineable;
    if (rightType != null) {
        rightIsRefineable = true;
    } else {
        rightIsRefineable = false;
        rightType = right.getJSType();
    }
    // merged types
    TypePair merged = merging.apply(new TypePair(leftType, rightType));
    // creating new scope
    if (merged != null) {
        return maybeRestrictTwoNames(blindScope, left, leftType, leftIsRefineable ? merged.typeA : null, right, rightType, rightIsRefineable ? merged.typeB : null);
    }
    return blindScope;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) TypePair(com.google.javascript.rhino.jstype.JSType.TypePair)

Example 20 with JSType

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

the class ClosureReverseAbstractInterpreterTest method testClosureFunction.

private void testClosureFunction(String function, JSType type, JSType trueType, JSType falseType) {
    // function(a) where a : type
    Node n = compiler.parseTestCode("var a; " + function + "(a)");
    Node call = n.getLastChild().getLastChild();
    Node name = call.getLastChild();
    TypedScope scope = (TypedScope) SyntacticScopeCreator.makeTyped(compiler).createScope(n, null);
    FlowScope flowScope = LinkedFlowScope.createEntryLattice(scope);
    assertEquals(Token.CALL, call.getToken());
    assertEquals(Token.NAME, name.getToken());
    flowScope.inferSlotType("a", type);
    ClosureReverseAbstractInterpreter rai = new ClosureReverseAbstractInterpreter(registry);
    // trueScope
    Asserts.assertTypeEquals(trueType, rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true).getSlot("a").getType());
    // falseScope
    JSType aType = rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false).getSlot("a").getType();
    if (falseType == null) {
        assertThat(aType).isNull();
    } else {
        Asserts.assertTypeEquals(falseType, aType);
    }
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter) FlowScope(com.google.javascript.jscomp.type.FlowScope)

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