本文整理汇总了Java中com.google.javascript.rhino.jstype.UnionType.getAlternates方法的典型用法代码示例。如果您正苦于以下问题:Java UnionType.getAlternates方法的具体用法?Java UnionType.getAlternates怎么用?Java UnionType.getAlternates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.jstype.UnionType
的用法示例。
在下文中一共展示了UnionType.getAlternates方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testUnresolvedUnions
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
public void testUnresolvedUnions() throws Exception {
assertPartialCompilationSucceeds("/** @type {some.thing.Foo|some.thing.Bar} */", "var x;");
TypedVar x = compiler.getTopScope().getSlot("x");
assertThat(x.getType().isUnionType()).named("type %s", x.getType()).isTrue();
UnionType unionType = (UnionType) x.getType();
Collection<JSType> alternatives = unionType.getAlternates();
assertThat(alternatives).hasSize(3);
int nullTypeCount = 0;
List<String> namedTypes = new ArrayList<>();
for (JSType alternative : alternatives) {
assertThat(alternative.isNamedType() || alternative.isNullType()).isTrue();
if (alternative.isNamedType()) {
assertThat(alternative.isNoResolvedType()).isTrue();
namedTypes.add(((NamedType) alternative).getReferenceName());
}
if (alternative.isNullType()) {
nullTypeCount++;
}
}
assertThat(nullTypeCount).isEqualTo(1);
assertThat(namedTypes).containsExactly("some.thing.Foo", "some.thing.Bar");
}
示例2: addEventize
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
private void addEventize(JSType thisType, JSType thatType) {
if (collectorFilterType(thisType) ||
collectorFilterType(thatType) ||
thisType.isEquivalentTo(thatType)) {
return;
}
String className = thisType.getDisplayName();
if (thatType.isUnionType()) {
UnionType ut = thatType.toMaybeUnionType();
for (JSType type : ut.getAlternates()) {
if (type.isObject()) {
addEventizeClass(className, type);
}
}
} else {
addEventizeClass(className, thatType);
}
}
示例3: caseUnionType
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
public JSType caseUnionType(UnionType type) {
JSType restricted = null;
for (JSType alternate : type.getAlternates()) {
JSType restrictedAlternate = alternate.visit(this);
if (restrictedAlternate != null) {
if (restricted == null) {
restricted = restrictedAlternate;
} else {
restricted = restrictedAlternate.getLeastSupertype(restricted);
}
}
}
return restricted;
}
示例4: caseUnionType
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
@Override
public JSType caseUnionType(UnionType type) {
JSType restricted = null;
for (JSType alternate : type.getAlternates()) {
JSType restrictedAlternate = alternate.visit(this);
if (restrictedAlternate != null) {
if (restricted == null) {
restricted = restrictedAlternate;
} else {
restricted = restrictedAlternate.getLeastSupertype(restricted);
}
}
}
return restricted;
}
示例5: caseUnionType
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
public Set<String> caseUnionType(UnionType type) {
// Visit the alternatives.
// T1|T2|T3
for (JSType alternateType : type.getAlternates()) {
visitOnce(alternateType);
}
return externProperties;
}
示例6: caseUnionType
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
private void caseUnionType(UnionType type, boolean filterVoid) {
com.github.jsdossier.proto.UnionType.Builder unionType =
currentExpression().getUnionTypeBuilder();
Set<TypeExpression> alternateTypes = new LinkedHashSet<>();
for (JSType alternate : type.getAlternates()) {
TypeExpression.Builder alternateType = TypeExpression.newBuilder();
expressions.addLast(alternateType);
alternate.visit(this);
expressions.removeLast();
if (TypeExpression.NodeTypeCase.UNION_TYPE.equals(alternateType.getNodeTypeCase())) {
alternateTypes.addAll(alternateType.getUnionType().getTypeList());
} else {
alternateTypes.add(alternateType.build());
}
}
if (filterVoid) {
alternateTypes.remove(VOID_TYPE);
if (alternateTypes.size() == 1) {
currentExpression().mergeFrom(alternateTypes.iterator().next());
}
return;
}
unionType.addAllType(UNION_ORDERING.sortedCopy(alternateTypes));
if (unionType.getTypeCount() == 1) {
currentExpression().clearUnionType();
currentExpression().mergeFrom(unionType.getType(0));
}
}
示例7: addAlternate
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
/**
* Adds an alternate to the union type under construction. Returns this
* for easy chaining.
*/
UnionTypeBuilder addAlternate(JSType alternate) {
// build() returns the bottom type by default, so we can
// just bail out early here.
if (alternate.isNoType()) {
return this;
}
isAllType = isAllType || alternate.isAllType();
boolean isAlternateUnknown = alternate instanceof UnknownType;
isNativeUnknownType = isNativeUnknownType || isAlternateUnknown;
if (isAlternateUnknown) {
areAllUnknownsChecked = areAllUnknownsChecked &&
alternate.isCheckedUnknownType();
}
if (!isAllType && !isNativeUnknownType) {
if (alternate instanceof UnionType) {
UnionType union = (UnionType) alternate;
for (JSType unionAlt : union.getAlternates()) {
addAlternate(unionAlt);
}
} else {
if (!alternate.isUnknownType()) {
Iterator<JSType> it = alternates.iterator();
while (it.hasNext()) {
JSType current = it.next();
if (!current.isUnknownType()) {
if (alternate.isSubtype(current)) {
// Alternate is unnecessary.
return this;
} else if (current.isSubtype(alternate)) {
// Alternate makes current obsolete
it.remove();
}
}
}
}
alternates.add(alternate);
result = null; // invalidate the memoized result
}
} else {
result = null;
}
return this;
}
示例8: addAlternate
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
/**
* Adds an alternate to the union type under construction. Returns this
* for easy chaining.
*/
UnionTypeBuilder addAlternate(JSType alternate) {
// build() returns the bottom type by default, so we can
// just bail out early here.
if (alternate.isNoType()) {
return this;
}
isAllType = isAllType || alternate.isAllType();
boolean isAlternateUnknown = alternate instanceof UnknownType;
isNativeUnknownType = isNativeUnknownType || isAlternateUnknown;
if (isAlternateUnknown) {
areAllUnknownsChecked = areAllUnknownsChecked &&
alternate.isCheckedUnknownType();
}
if (!isAllType && !isNativeUnknownType) {
if (alternate instanceof UnionType) {
UnionType union = (UnionType) alternate;
for (JSType unionAlt : union.getAlternates()) {
addAlternate(unionAlt);
}
} else {
if (alternates.size() > MAX_UNION_SIZE) {
return this;
}
// Look through the alternates we've got so far,
// and check if any of them are duplicates of
// one another.
Iterator<JSType> it = alternates.iterator();
while (it.hasNext()) {
JSType current = it.next();
if (alternate.isUnknownType() ||
current.isUnknownType()) {
if (alternate.isEquivalentTo(current)) {
// Alternate is unnecessary.
return this;
}
} else {
if (alternate.isSubtype(current)) {
// Alternate is unnecessary.
return this;
} else if (current.isSubtype(alternate)) {
// Alternate makes current obsolete
it.remove();
}
}
}
alternates.add(alternate);
result = null; // invalidate the memoized result
}
} else {
result = null;
}
return this;
}
示例9: generatePropTypesError
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
JSError generatePropTypesError(Node paramNode) {
JSType paramType = paramNode.getJSType();
if (paramType == null) {
return null;
}
JSType validatorFuncType = validatorFuncNode.getJSType();
if (!(validatorFuncType instanceof FunctionType)) {
return null;
}
JSType expectedType = ((FunctionType) validatorFuncType).getReturnType();
if (expectedType == null) {
return null;
}
if (paramType.isSubtype(expectedType)) {
return null;
}
// Unpack the union with null if elements can be created with no props.
if (canBeCreatedWithNoProps && expectedType instanceof UnionType) {
UnionType unionExpectedType = (UnionType) expectedType;
for (JSType expectedAlternateType : unionExpectedType.getAlternates()) {
if (!expectedAlternateType.isNullType()) {
expectedType = expectedAlternateType;
break;
}
}
}
if (expectedType instanceof NamedType) {
expectedType = ((NamedType) expectedType).getReferencedType();
}
if (!(paramType instanceof PrototypeObjectType)) {
return null;
}
if (!(expectedType instanceof PrototypeObjectType)) {
return null;
}
PrototypeObjectType expectedRecordType = (PrototypeObjectType) expectedType;
PrototypeObjectType paramRecordType = (PrototypeObjectType) paramType;
if (expectedRecordType == null || paramRecordType == null) {
return null;
}
List<String> errors = Lists.newArrayList();
for (String property : expectedRecordType.getPropertyNames()) {
JSType expectedPropertyType =
expectedRecordType.getPropertyType(property);
if (!paramRecordType.hasProperty(property)) {
if (!expectedPropertyType.isExplicitlyVoidable()) {
errors.add("\"" + property + "\" was missing, expected to be of " +
"type " + expectedPropertyType);
}
continue;
}
JSType paramPropertyType = paramRecordType.getPropertyType(property);
if (!paramPropertyType.isSubtype(expectedPropertyType)) {
errors.add("\"" + property + "\" was expected to be of type " +
expectedPropertyType + ", instead was " +paramPropertyType);
}
}
if (errors.isEmpty()) {
return null;
}
return JSError.make(paramNode, PROP_TYPES_VALIDATION_MISMATCH, typeName,
" " + Joiner.on("\n ").join(errors));
}
示例10: maybeResolveTemplatedType
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
private void maybeResolveTemplatedType(
JSType paramType,
JSType argType,
Map<TemplateType, JSType> resolvedTypes) {
if (paramType.isTemplateType()) {
// @param {T}
resolvedTemplateType(
resolvedTypes, paramType.toMaybeTemplateType(), argType);
} else if (paramType.isUnionType()) {
// @param {Array.<T>|NodeList|Arguments|{length:number}}
UnionType unionType = paramType.toMaybeUnionType();
for (JSType alernative : unionType.getAlternates()) {
maybeResolveTemplatedType(alernative, argType, resolvedTypes);
}
} else if (paramType.isFunctionType()) {
FunctionType paramFunctionType = paramType.toMaybeFunctionType();
FunctionType argFunctionType = argType
.restrictByNotNullOrUndefined()
.collapseUnion()
.toMaybeFunctionType();
if (argFunctionType != null && argFunctionType.isSubtype(paramType)) {
// infer from return type of the function type
maybeResolveTemplatedType(
paramFunctionType.getTypeOfThis(),
argFunctionType.getTypeOfThis(), resolvedTypes);
// infer from return type of the function type
maybeResolveTemplatedType(
paramFunctionType.getReturnType(),
argFunctionType.getReturnType(), resolvedTypes);
// infer from parameter types of the function type
maybeResolveTemplateTypeFromNodes(
paramFunctionType.getParameters(),
argFunctionType.getParameters(), resolvedTypes);
}
} else if (paramType.isParameterizedType()) {
ParameterizedType paramObjectType = paramType.toMaybeParameterizedType();
JSType typeParameter = paramObjectType.getParameterType();
Preconditions.checkNotNull(typeParameter);
if (typeParameter != null) {
// @param {Array.<T>}
ObjectType argObjectType = argType
.restrictByNotNullOrUndefined()
.collapseUnion()
.toMaybeParameterizedType();
if (argObjectType != null && argObjectType.isSubtype(paramType)) {
JSType argTypeParameter = argObjectType.getParameterType();
Preconditions.checkNotNull(argTypeParameter);
maybeResolveTemplatedType(
typeParameter, argTypeParameter, resolvedTypes);
}
}
}
}
示例11: updateTypeOfParameters
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
/**
* For functions with function parameters, type inference will set the type of
* a function literal argument from the function parameter type.
*/
private void updateTypeOfParameters(Node n, FunctionType fnType) {
checkState(n.isCall() || n.isNew(), n);
int i = 0;
int childCount = n.getChildCount();
Node iArgument = n.getFirstChild();
for (Node iParameter : fnType.getParameters()) {
if (i + 1 >= childCount) {
// TypeCheck#visitParametersList will warn so we bail.
return;
}
// NOTE: the first child of the call node is the call target, which we want to skip, so
// it is correct to call getNext on the first iteration.
iArgument = iArgument.getNext();
JSType iArgumentType = getJSType(iArgument);
JSType iParameterType = getJSType(iParameter);
inferPropertyTypesToMatchConstraint(iArgumentType, iParameterType);
// If the parameter to the call is a function expression, propagate the
// function signature from the call site to the function node.
// Filter out non-function types (such as null and undefined) as
// we only care about FUNCTION subtypes here.
FunctionType restrictedParameter = null;
if (iParameterType.isUnionType()) {
UnionType union = iParameterType.toMaybeUnionType();
for (JSType alternative : union.getAlternates()) {
if (alternative.isFunctionType()) {
// There is only one function type per union.
restrictedParameter = alternative.toMaybeFunctionType();
break;
}
}
} else {
restrictedParameter = iParameterType.toMaybeFunctionType();
}
if (restrictedParameter != null
&& iArgument.isFunction()
&& iArgumentType.isFunctionType()) {
FunctionType argFnType = iArgumentType.toMaybeFunctionType();
boolean declared = iArgument.getJSDocInfo() != null;
iArgument.setJSType(
matchFunction(restrictedParameter, argFnType, declared));
}
i++;
}
}
示例12: updateTypeOfParameters
import com.google.javascript.rhino.jstype.UnionType; //导入方法依赖的package包/类
/**
* For functions with function parameters, type inference will set the type of
* a function literal argument from the function parameter type.
*/
private void updateTypeOfParameters(Node n, FunctionType fnType) {
int i = 0;
int childCount = n.getChildCount();
for (Node iParameter : fnType.getParameters()) {
if (i + 1 >= childCount) {
// TypeCheck#visitParametersList will warn so we bail.
return;
}
JSType iParameterType = getJSType(iParameter);
Node iArgument = n.getChildAtIndex(i + 1);
JSType iArgumentType = getJSType(iArgument);
inferPropertyTypesToMatchConstraint(iArgumentType, iParameterType);
// If the parameter to the call is a function expression, propagate the
// function signature from the call site to the function node.
// Filter out non-function types (such as null and undefined) as
// we only care about FUNCTION subtypes here.
FunctionType restrictedParameter = null;
if (iParameterType.isUnionType()) {
UnionType union = iParameterType.toMaybeUnionType();
for (JSType alternative : union.getAlternates()) {
if (alternative.isFunctionType()) {
// There is only one function type per union.
restrictedParameter = alternative.toMaybeFunctionType();
break;
}
}
} else {
restrictedParameter = iParameterType.toMaybeFunctionType();
}
if (restrictedParameter != null
&& iArgument.isFunction()
&& iArgumentType.isFunctionType()) {
FunctionType argFnType = iArgumentType.toMaybeFunctionType();
boolean declared = iArgument.getJSDocInfo() != null;
iArgument.setJSType(
matchFunction(restrictedParameter, argFnType, declared));
}
i++;
}
}