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