Search in sources :

Example 31 with Color

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

the class NodeUtil method isConstructor.

static boolean isConstructor(Node fnNode) {
    if (fnNode == null || !fnNode.isFunction()) {
        return false;
    }
    JSType type = fnNode.getJSType();
    JSDocInfo jsDocInfo = getBestJSDocInfo(fnNode);
    Color color = fnNode.getColor();
    return (type != null && type.isConstructor()) || (jsDocInfo != null && jsDocInfo.isConstructor()) || (color != null && color.isConstructor()) || isEs6Constructor(fnNode);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Color(com.google.javascript.jscomp.colors.Color) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 32 with Color

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

the class Es6ConvertSuperConstructorCalls method getTypeOfThisForConstructor.

private AstFactory.Type getTypeOfThisForConstructor(Node constructor) {
    checkArgument(constructor.isFunction(), constructor);
    final Color constructorType = constructor.getColor();
    return constructorType != null && !constructorType.getInstanceColors().isEmpty() ? type(Color.createUnion(constructorType.getInstanceColors())) : type(StandardColors.UNKNOWN);
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

Example 33 with Color

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

the class Es6TemplateLiterals method createCookedStringArray.

private Node createCookedStringArray(Node n) {
    Color templateArrayType = astFactory.isAddingColors() ? compiler.getColorRegistry().get(StandardColors.I_TEMPLATE_ARRAY_ID) : null;
    Node array = astFactory.createArraylit().setColor(// tighten the type from Array to ITemplateArray
    templateArrayType);
    for (Node child = n.getFirstChild(); child != null; child = child.getNext()) {
        if (child.isTemplateLitString()) {
            if (child.getCookedString() != null) {
                array.addChildToBack(astFactory.createString(child.getCookedString()));
            } else {
                // undefined cooked string due to exception in template escapes
                array.addChildToBack(astFactory.createVoid(astFactory.createNumber(0)));
            }
        }
    }
    return array;
}
Also used : Color(com.google.javascript.jscomp.colors.Color) Node(com.google.javascript.rhino.Node)

Example 34 with Color

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

the class AstFactory method createName.

Node createName(StaticScope scope, String name) {
    Node result = IR.name(name);
    switch(this.typeMode) {
        case JSTYPE:
            JSType definitionType = getVarDefinitionNode(scope, name).getJSType();
            // TODO(b/149843534): crash instead of defaulting to unknown
            result.setJSType(definitionType != null ? definitionType : unknownType);
            break;
        case COLOR:
            Color definitionColor = getVarDefinitionNode(scope, name).getColor();
            // TODO(b/149843534): crash instead of defaulting to unknown
            result.setColor(definitionColor != null ? definitionColor : StandardColors.UNKNOWN);
            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 35 with Color

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

the class Es6RewriteClassTest method testComputedSuper.

@Test
public void testComputedSuper() {
    test(lines("/** @unrestricted */", "class Foo {", "  ['m']() { return 1; }", "}", "", "/** @unrestricted */", "class Bar extends Foo {", "  ['m']() {", "    RETURN: return super['m']() + 1;", "  }", "}"), lines("/** @constructor */", "let Foo = function() {};", "Foo.prototype['m'] = function() { return 1; };", "/** @constructor */", "let Bar = function() { Foo.apply(this, arguments); };", "$jscomp.inherits(Bar, Foo);", "Bar.prototype['m'] = function () {", "  RETURN: return Foo.prototype['m'].call(this) + 1;", "};"));
    Color fooType = getNodeWithName(getLastCompiler().getJsRoot(), "Foo").getColor();
    Color barType = getNodeWithName(getLastCompiler().getJsRoot(), "Bar").getColor();
    // Foo.prototype['m'].call(this)
    Node callNode = getNodeMatchingLabel(getLastCompiler().getJsRoot(), "RETURN").getFirstFirstChild();
    assertNode(callNode).hasToken(Token.CALL);
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // Foo.prototype['m'].call
    Node callee = callNode.getFirstChild();
    assertNode(callee).hasToken(Token.GETPROP);
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // Foo.prototype['m']
    Node property = callee.getFirstChild();
    assertNode(property).hasToken(Token.GETELEM);
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // Foo.prototype
    Node prototype = property.getFirstChild();
    assertNode(prototype).matchesQualifiedName("Foo.prototype").hasOriginalName("super");
    assertThat(fooType.getPrototypes()).containsExactly(prototype.getColor());
    // Foo
    Node superDotGReplacement = prototype.getFirstChild();
    assertNode(superDotGReplacement).matchesQualifiedName("Foo");
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(fooType);
    // `this` node from `Foo.prototype['m'].call(this)`
    Node thisNode = callee.getNext();
    assertNode(thisNode).hasToken(Token.THIS);
    assertThat(barType.getInstanceColors()).containsExactly(thisNode.getColor());
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) 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