Search in sources :

Example 46 with Color

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

the class AstFactoryTest method testCreateArgumentsReference_colors.

@Test
public void testCreateArgumentsReference_colors() {
    Node root = parseAndAddColors(new TestExternsBuilder().addArguments().build(), "function f() { arguments; }");
    AstFactory astFactory = createTestAstFactoryWithColors();
    Node block = NodeUtil.getFunctionBody(root.getFirstFirstChild());
    Node argumentsReferenceNode = block.getFirstFirstChild();
    Color argumentsReferenceColor = argumentsReferenceNode.getColor();
    Node argumentsNode = astFactory.createArgumentsReference();
    assertNode(argumentsNode).matchesName("arguments");
    assertNode(argumentsNode).hasColorThat().isEqualTo(argumentsReferenceColor);
}
Also used : Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Color(com.google.javascript.jscomp.colors.Color) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 47 with Color

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

the class AstFactoryTest method testCreateMemberFunctionDef_colors.

@Test
public void testCreateMemberFunctionDef_colors() {
    AstFactory astFactory = createTestAstFactoryWithColors();
    // just a quick way to get a valid function type
    Node root = parseAndAddColors("function foo() {}");
    Color functionType = // script
    root.getFirstChild().getFirstChild().getColor();
    Node paramList = IR.paramList();
    Node body = IR.block();
    Node functionNode = IR.function(IR.name(""), paramList, body).setColor(functionType);
    Node memberFunctionDef = astFactory.createMemberFunctionDef("bar", functionNode);
    assertNode(memberFunctionDef).hasToken(Token.MEMBER_FUNCTION_DEF);
    assertThat(memberFunctionDef.getString()).isEqualTo("bar");
    assertNode(memberFunctionDef).hasColorThat().isEqualTo(functionType);
}
Also used : Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 48 with Color

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

the class ColorGraphNodeFactoryTest method unionTypes_whenFlattened_dropNullAndUndefined.

@Test
public void unionTypes_whenFlattened_dropNullAndUndefined() {
    // Given
    ColorGraphNodeFactory factory = ColorGraphNodeFactory.createFactory(this.colorRegistry);
    Color nullOrVoidOrNumberType = Color.createUnion(ImmutableSet.of(StandardColors.NULL_OR_VOID, StandardColors.NUMBER));
    ColorGraphNode flatNumber = factory.createNode(StandardColors.NUMBER);
    // Given
    ColorGraphNode flatNullOrVoidOrNumber = factory.createNode(nullOrVoidOrNumberType);
    // Then
    assertThat(flatNullOrVoidOrNumber).isSameInstanceAs(flatNumber);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 49 with Color

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

the class ColorSerializer method generateTypePool.

/**
 * Generate a `TypePool` proto built from the previously added `Color`s and the arguments supplied
 * to this method.
 *
 * @param getDisambiguationSupertypesFn Given a `Color` return a set of `Color`s it inherits from.
 * @param getMismatchSourceRefsFn May be `null` if this `serializationMode` is `SKIP_DEBUG_INFO`.
 *     Otherwise, this function must provide a set of all the source reference strings indicating
 *     code locations where the given `Color` encountered a type mismatch.
 * @return a new `TypePool` proto
 */
TypePool generateTypePool(Function<Color, ImmutableSet<Color>> getDisambiguationSupertypesFn, @Nullable Function<Color, ImmutableSet<String>> getMismatchSourceRefsFn) {
    final TypePool.Builder typePoolBuilder = TypePool.newBuilder();
    // what effect changing an iterable will have on an iteration that is in progress.
    for (int i = TypePointers.untrimOffset(0); i < colorsInSerializedOrder.size(); i++) {
        final Color color = colorsInSerializedOrder.get(i);
        final Integer typePointer = colorIdToTypePointer.get(color.getId());
        typePoolBuilder.addType(generateTypeProto(color));
        for (Color supertype : getDisambiguationSupertypesFn.apply(color)) {
            typePoolBuilder.addDisambiguationEdgesBuilder().setSubtype(typePointer).setSupertype(addColor(supertype));
        }
    }
    if (serializationMode != SerializationOptions.SKIP_DEBUG_INFO) {
        checkNotNull(getMismatchSourceRefsFn);
        final TypePool.DebugInfo.Builder debugInfoBuilder = typePoolBuilder.getDebugInfoBuilder();
        // Construct a map from source reference string to type pointers,
        // because that's the way the Mismatch protos work.
        // Construct entries only for those colors that we have actually serialized in order to save
        // space.
        final LinkedHashMap<String, ArrayList<Integer>> srcRefToTypePointerList = new LinkedHashMap<>();
        for (Color color : colorsInSerializedOrder) {
            final Integer typePointer = colorIdToTypePointer.get(color.getId());
            for (String srcRef : getMismatchSourceRefsFn.apply(color)) {
                final ArrayList<Integer> typePointerList = srcRefToTypePointerList.computeIfAbsent(srcRef, (key) -> new ArrayList<>());
                typePointerList.add(typePointer);
            }
        }
        // Now use the map to build the Mismatch protos and put them into the debug info.
        for (Entry<String, ArrayList<Integer>> entry : srcRefToTypePointerList.entrySet()) {
            debugInfoBuilder.addMismatchBuilder().setSourceRef(entry.getKey()).addAllInvolvedColor(entry.getValue());
        }
    }
    return typePoolBuilder.build();
}
Also used : Color(com.google.javascript.jscomp.colors.Color) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with Color

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

the class ColorGraphBuilder method addInternal.

/**
 * Insert {@code node} and all necessary related colors into the datastructures of this pass.
 */
private LinkedDiGraphNode<ColorGraphNode, Object> addInternal(ColorGraphNode node) {
    LinkedDiGraphNode<ColorGraphNode, Object> flatNode = this.colorHoldsInstanceGraph.getNode(node);
    if (flatNode != null) {
        return flatNode;
    }
    flatNode = this.colorHoldsInstanceGraph.createNode(node);
    if (node.getColor().isUnion()) {
        for (Color alt : node.getColor().getUnionElements()) {
            this.connectSourceToDest(flatNode, EdgeReason.ALGEBRAIC, this.addInternal(alt));
        }
        return flatNode;
    }
    Color color = node.getColor();
    ImmutableSet<Color> supertypes = this.registry.getDisambiguationSupertypes(color);
    if (supertypes.isEmpty()) {
        this.connectSourceToDest(topNode, EdgeReason.ALGEBRAIC, flatNode);
    } else {
        for (Color supertype : supertypes) {
            this.connectSourceToDest(this.addInternal(supertype), EdgeReason.CAN_HOLD, flatNode);
        }
    }
    /**
     * Add all instance and prototype colors when visiting a constructor. We won't necessarily see
     * all possible instance colors that exist at runtime during an AST traversal.
     *
     * <p>For example, a subclass constructor may never be explicitly initialized but instead passed
     * to some function expecting `function(new:Parent)`. See {@link
     * AmbiguatePropertiesTest#testImplementsAndExtends_respectsUndeclaredProperties()}
     */
    for (Color prototype : color.getPrototypes()) {
        this.addInternal(prototype);
    }
    for (Color instanceColor : color.getInstanceColors()) {
        this.addInternal(instanceColor);
    }
    return flatNode;
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

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