use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class Matchers method jsDocType.
/**
* Returns a Matcher that matches nodes that contain JS Doc that specify the
* {@code @type} annotation equivalent to the provided type.
*/
public static Matcher jsDocType(final String type) {
return new Matcher() {
@Override
public boolean matches(Node node, NodeMetadata metadata) {
JSType providedJsType = getJsType(metadata, type);
if (providedJsType == null) {
return false;
}
providedJsType = providedJsType.restrictByNotNullOrUndefined();
// The JSDoc for a var declaration is on the VAR node, but the type only
// exists on the NAME node.
// TODO(mknichel): Make NodeUtil.getBestJSDoc public and use that.
JSDocInfo jsDoc = node.getParent().isVar() ? node.getParent().getJSDocInfo() : node.getJSDocInfo();
JSType jsType = node.getJSType();
return jsDoc != null && jsDoc.hasType() && jsType != null && providedJsType.isEquivalentTo(jsType.restrictByNotNullOrUndefined());
}
};
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class Matchers method enumDefinitionOfType.
/**
* Returns a Matcher that matches definitions of an enum of the given type.
*/
public static Matcher enumDefinitionOfType(final String type) {
return new Matcher() {
@Override
public boolean matches(Node node, NodeMetadata metadata) {
JSType providedJsType = getJsType(metadata, type);
if (providedJsType == null) {
return false;
}
providedJsType = providedJsType.restrictByNotNullOrUndefined();
JSType jsType = node.getJSType();
return jsType != null && jsType.isEnumType() && providedJsType.isEquivalentTo(jsType.toMaybeEnumType().getElementsType().getPrimitiveType());
}
};
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class SemanticReverseAbstractInterpreter method caseAndOrNotShortCircuiting.
private FlowScope caseAndOrNotShortCircuiting(Node left, Node right, FlowScope blindScope, boolean outcome) {
// left type
JSType leftType = getTypeIfRefinable(left, blindScope);
boolean leftIsRefineable;
if (leftType != null) {
leftIsRefineable = true;
} else {
leftIsRefineable = false;
leftType = left.getJSType();
blindScope = firstPreciserScopeKnowingConditionOutcome(left, blindScope, outcome);
}
// restricting left type
JSType restrictedLeftType = (leftType == null) ? null : leftType.getRestrictedTypeGivenToBooleanOutcome(outcome);
if (restrictedLeftType == null) {
return firstPreciserScopeKnowingConditionOutcome(right, blindScope, outcome);
}
blindScope = maybeRestrictName(blindScope, left, leftType, leftIsRefineable ? restrictedLeftType : null);
// right type
JSType rightType = getTypeIfRefinable(right, blindScope);
boolean rightIsRefineable;
if (rightType != null) {
rightIsRefineable = true;
} else {
rightIsRefineable = false;
rightType = right.getJSType();
blindScope = firstPreciserScopeKnowingConditionOutcome(right, blindScope, outcome);
}
if (outcome) {
JSType restrictedRightType = (rightType == null) ? null : rightType.getRestrictedTypeGivenToBooleanOutcome(outcome);
// creating new scope
return maybeRestrictName(blindScope, right, rightType, rightIsRefineable ? restrictedRightType : null);
}
return blindScope;
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class SemanticReverseAbstractInterpreter method caseEquality.
private FlowScope caseEquality(Node left, Node right, FlowScope blindScope, Function<TypePair, TypePair> merging) {
// left type
JSType leftType = getTypeIfRefinable(left, blindScope);
boolean leftIsRefineable;
if (leftType != null) {
leftIsRefineable = true;
} else {
leftIsRefineable = false;
leftType = left.getJSType();
}
// right type
JSType rightType = getTypeIfRefinable(right, blindScope);
boolean rightIsRefineable;
if (rightType != null) {
rightIsRefineable = true;
} else {
rightIsRefineable = false;
rightType = right.getJSType();
}
// merged types
TypePair merged = merging.apply(new TypePair(leftType, rightType));
// creating new scope
if (merged != null) {
return maybeRestrictTwoNames(blindScope, left, leftType, leftIsRefineable ? merged.typeA : null, right, rightType, rightIsRefineable ? merged.typeB : null);
}
return blindScope;
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class ClosureReverseAbstractInterpreterTest method testClosureFunction.
private void testClosureFunction(String function, JSType type, JSType trueType, JSType falseType) {
// function(a) where a : type
Node n = compiler.parseTestCode("var a; " + function + "(a)");
Node call = n.getLastChild().getLastChild();
Node name = call.getLastChild();
TypedScope scope = (TypedScope) SyntacticScopeCreator.makeTyped(compiler).createScope(n, null);
FlowScope flowScope = LinkedFlowScope.createEntryLattice(scope);
assertEquals(Token.CALL, call.getToken());
assertEquals(Token.NAME, name.getToken());
flowScope.inferSlotType("a", type);
ClosureReverseAbstractInterpreter rai = new ClosureReverseAbstractInterpreter(registry);
// trueScope
Asserts.assertTypeEquals(trueType, rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true).getSlot("a").getType());
// falseScope
JSType aType = rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false).getSlot("a").getType();
if (falseType == null) {
assertThat(aType).isNull();
} else {
Asserts.assertTypeEquals(falseType, aType);
}
}
Aggregations