Search in sources :

Example 11 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class AstFactory method createOr.

Node createOr(Node left, Node right) {
    Node result = IR.or(left, right);
    switch(this.typeMode) {
        case JSTYPE:
            JSType leftType = checkNotNull(left.getJSType(), left);
            JSType rightType = checkNotNull(right.getJSType(), right);
            result.setJSType(this.registry.createUnionType(leftType, rightType));
            break;
        case COLOR:
            Color leftColor = checkNotNull(left.getColor(), left);
            Color rightColor = checkNotNull(right.getColor(), right);
            result.setColor(Color.createUnion(ImmutableSet.of(leftColor, rightColor)));
            break;
        case NONE:
            break;
    }
    return result;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color)

Example 12 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class AstFactory method createPrototypeAccess.

/**
 * Creates a Node representing <receiver>.prototype
 *
 * <p>For example, given the AST for `Foo`, returns `Foo.prototype`
 */
Node createPrototypeAccess(Node receiver) {
    Node result = IR.getprop(receiver, "prototype");
    switch(this.typeMode) {
        case JSTYPE:
            result.setJSType(getJsTypeForProperty(receiver, "prototype"));
            break;
        case COLOR:
            checkNotNull(receiver.getColor(), "Missing color on %s", receiver);
            ImmutableSet<Color> possiblePrototypes = receiver.getColor().getPrototypes();
            result.setColor(possiblePrototypes.isEmpty() ? StandardColors.UNKNOWN : Color.createUnion(possiblePrototypes));
            break;
        case NONE:
            break;
    }
    return result;
}
Also used : Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color)

Example 13 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class AstFactory method createAnd.

Node createAnd(Node left, Node right) {
    Node result = IR.and(left, right);
    switch(this.typeMode) {
        case JSTYPE:
            JSType leftType = checkNotNull(left.getJSType(), left);
            JSType rightType = checkNotNull(right.getJSType(), right);
            result.setJSType(this.registry.createUnionType(leftType, rightType));
            break;
        case COLOR:
            Color leftColor = checkNotNull(left.getColor(), left);
            Color rightColor = checkNotNull(right.getColor(), right);
            result.setColor(Color.createUnion(ImmutableSet.of(leftColor, rightColor)));
            break;
        case NONE:
            break;
    }
    return result;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color)

Example 14 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class NodeUtil method mayBeString.

/**
 * Return if the node is possibly a string.
 *
 * @param n The node.
 * @param useType If true and the node has a primitive type, return true if that type is string
 *     and false otherwise.
 * @return Whether the results is possibly a string.
 */
static boolean mayBeString(Node n, boolean useType) {
    if (useType) {
        Color color = n.getColor();
        if (color != null) {
            if (color.equals(StandardColors.STRING)) {
                return true;
            } else if (color.equals(StandardColors.NUMBER) || color.equals(StandardColors.BIGINT) || color.equals(StandardColors.BOOLEAN) || color.equals(StandardColors.NULL_OR_VOID)) {
                return false;
            }
        }
        JSType type = n.getJSType();
        if (type != null) {
            if (type.isStringValueType()) {
                return true;
            } else if (type.isNumberValueType() || type.isBigIntValueType() || type.isBooleanValueType() || type.isNullType() || type.isVoidType()) {
                return false;
            }
        }
    }
    return mayBeString(getKnownValueType(n));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Color(com.google.javascript.jscomp.colors.Color)

Example 15 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class Es6RewriteDestructuringTest method testObjectDestructuringRest_typesRestAsObject.

@Test
public void testObjectDestructuringRest_typesRestAsObject() {
    test(lines(// 
    "const obj = {a: 3, b: 'string', c: null};", "const {...rest} = obj;"), lines("const obj = {a: 3, b: 'string', c: null};", "/** @const */ var $jscomp$destructuring$var0=obj;", "var $jscomp$destructuring$var1 = Object.assign({}, $jscomp$destructuring$var0);", "const rest = $jscomp$destructuring$var1;"));
    Node jsRoot = getLastCompiler().getJsRoot();
    Color objType = getNodeMatchingQName(jsRoot, "obj").getColor();
    // `$jscomp$destructuring$var0` has the same type as `obj`
    Node jscompDestructuringVar0Name = getNodeMatchingQName(jsRoot, "$jscomp$destructuring$var0");
    assertNode(jscompDestructuringVar0Name).hasColorThat().isEqualTo(objType);
    // `$jscomp$destructuring$var1` has the same type as `obj`
    Node jscompDestructuringVar1Name = getNodeMatchingQName(jsRoot, "$jscomp$destructuring$var1");
    assertNode(jscompDestructuringVar1Name).hasColorThat().isEqualTo(objType);
    // TODO(b/128355893) Do better inferrence. For now we just consider `rest` an `Object` rather
    // than trying to figure out what properties it gets.
    Node restName = getNodeMatchingQName(jsRoot, "rest");
    assertNode(restName).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Aggregations

Color (com.google.javascript.jscomp.colors.Color)57 Node (com.google.javascript.rhino.Node)37 Test (org.junit.Test)37 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)29 JSType (com.google.javascript.rhino.jstype.JSType)6 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)5 ImmutableSet (com.google.common.collect.ImmutableSet)2 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CompilerPass (com.google.javascript.jscomp.CompilerPass)1 DefaultNameGenerator (com.google.javascript.jscomp.DefaultNameGenerator)1 GatherGetterAndSetterProperties (com.google.javascript.jscomp.GatherGetterAndSetterProperties)1 NameGenerator (com.google.javascript.jscomp.NameGenerator)1 NodeTraversal (com.google.javascript.jscomp.NodeTraversal)1 AbstractPostOrderCallback (com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback)1