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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations