Search in sources :

Example 1 with CheckReturnValue

use of com.google.errorprone.annotations.CheckReturnValue in project closure-compiler by google.

the class SemanticReverseAbstractInterpreter method caseInstanceOf.

@CheckReturnValue
private FlowScope caseInstanceOf(Node left, Node right, FlowScope blindScope, Outcome outcome) {
    JSType leftType = getTypeIfRefinable(left, blindScope);
    if (leftType == null) {
        return blindScope;
    }
    JSType rightType = right.getJSType();
    ObjectType targetType = typeRegistry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
    if (rightType != null && rightType.isFunctionType()) {
        targetType = rightType.toMaybeFunctionType();
    }
    Visitor<JSType> visitor;
    if (outcome.isTruthy()) {
        visitor = new RestrictByTrueInstanceOfResultVisitor(targetType);
    } else {
        visitor = new RestrictByFalseInstanceOfResultVisitor(targetType);
    }
    return maybeRestrictName(blindScope, left, leftType, leftType.visit(visitor));
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 2 with CheckReturnValue

use of com.google.errorprone.annotations.CheckReturnValue in project closure-compiler by google.

the class SemanticReverseAbstractInterpreter method caseEquality.

@CheckReturnValue
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;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) TypePair(com.google.javascript.rhino.jstype.JSType.TypePair) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 3 with CheckReturnValue

use of com.google.errorprone.annotations.CheckReturnValue in project closure-compiler by google.

the class TypeInference method traverseCatch.

/**
 * Any value can be thrown, so it's really impossible to determine the type of a CATCH param.
 * Treat it as the UNKNOWN type.
 */
@CheckReturnValue
private FlowScope traverseCatch(Node catchNode, FlowScope scope) {
    Node catchTarget = catchNode.getFirstChild();
    if (catchTarget.isName()) {
        Node name = catchNode.getFirstChild();
        JSType type = name.getJSType();
        // Otherwise use "unknown".
        if (type == null) {
            type = unknownType;
            name.setJSType(unknownType);
        }
        return redeclareSimpleVar(scope, name, type);
    } else if (catchTarget.isDestructuringPattern()) {
        Node pattern = catchNode.getFirstChild();
        return traverseDestructuringPattern(pattern, scope, unknownType, AssignmentType.DECLARATION);
    } else {
        checkState(catchTarget.isEmpty(), catchTarget);
        // ES2019 allows `try {} catch {}` with no catch expression
        return scope;
    }
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 4 with CheckReturnValue

use of com.google.errorprone.annotations.CheckReturnValue in project closure-compiler by google.

the class TypeInference method flowThrough.

@Override
@CheckReturnValue
FlowScope flowThrough(Node n, FlowScope input) {
    // want to infer anything about this scope.
    if (input == bottomScope) {
        return input;
    }
    // This method also does some logic for ES modules right before and after entering/exiting the
    // scope rooted at the module. The reasoning for separating out this logic is that we can just
    // ignore the actual AST nodes for IMPORT/EXPORT, in most cases, because we have already
    // created an abstraction of imports and exports.
    Node root = NodeUtil.getEnclosingScopeRoot(n);
    // Inferred types of ES module imports/exports aren't knowable until after TypeInference runs.
    // First update the type of all imports in the scope, then do flow-sensitive inference, then
    // update the implicit '*exports*' object.
    Module module = ModuleImportResolver.getModuleFromScopeRoot(compiler.getModuleMap(), compiler, root);
    TypedScope syntacticBlockScope = scopeCreator.createScope(root);
    if (module != null && module.metadata().isEs6Module()) {
        moduleImportResolver.declareEsModuleImports(module, syntacticBlockScope, compiler.getInput(NodeUtil.getInputId(n)));
    }
    // This logic is not specific to ES modules.
    FlowScope output = input.withSyntacticScope(syntacticBlockScope);
    output = inferDeclarativelyUnboundVarsWithoutTypes(output);
    output = traverse(n, output);
    updateModuleScope(module, syntacticBlockScope);
    return output;
}
Also used : Node(com.google.javascript.rhino.Node) Module(com.google.javascript.jscomp.modules.Module) FlowScope(com.google.javascript.jscomp.type.FlowScope) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 5 with CheckReturnValue

use of com.google.errorprone.annotations.CheckReturnValue in project closure-compiler by google.

the class TypeInference method traverseAssignOp.

@CheckReturnValue
private FlowScope traverseAssignOp(Node n, FlowScope scope) {
    Node left = n.getFirstChild();
    scope = traverseBigIntCompatibleBinaryOperator(n, scope);
    // The lhs is both an input and an output, so don't update the input type here.
    return updateScopeForAssignment(scope, left, getJSType(n), /* updateNode= */
    null, AssignmentType.ASSIGN);
}
Also used : Node(com.google.javascript.rhino.Node) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Aggregations

CheckReturnValue (com.google.errorprone.annotations.CheckReturnValue)16 JSType (com.google.javascript.rhino.jstype.JSType)11 Node (com.google.javascript.rhino.Node)7 ObjectType (com.google.javascript.rhino.jstype.ObjectType)3 BugCheckerInfo (com.google.errorprone.BugCheckerInfo)2 SeverityLevel (com.google.errorprone.BugPattern.SeverityLevel)2 ErrorProneFlags (com.google.errorprone.ErrorProneFlags)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 Supplier (com.google.common.base.Supplier)1 HashBiMap (com.google.common.collect.HashBiMap)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 ErrorProneOptions (com.google.errorprone.ErrorProneOptions)1 Severity (com.google.errorprone.ErrorProneOptions.Severity)1