src/com/google/javascript/jscomp/FunctionInjector.java
/**
   * Inline a function which fulfills the requirements of
   * canInlineReferenceAsStatementBlock into the call site, replacing the
   * parent expression.
   */
Inline a function which fulfills the requirements of
canInlineReferenceAsStatementBlock into the call site, replacing the
parent expression.
Inline a function which fulfills the requirements of canInlineReferenceAsStatementBlock into the call site replacing the parent expression .
private Node inlineFunction(Reference ref, Node fnNode, String fnName) {
    Node callNode = ref.callNode;
    Node parent = callNode.getParent();
    Node grandParent = parent.getParent();
    CallSiteType callSiteType = classifyCallSite(ref);
    checkArgument(callSiteType != CallSiteType.UNSUPPORTED);
    String resultName = null;
    boolean needsDefaultReturnResult = true;
    switch (callSiteType) {
      case SIMPLE_ASSIGNMENT:
        resultName = parent.getFirstChild().getString();
        removeConstantVarAnnotation(ref.scope, resultName);
        break;
      case VAR_DECL_SIMPLE_ASSIGNMENT:
        resultName = parent.getString();
        removeConstantVarAnnotation(ref.scope, resultName);
        break;
      case SIMPLE_CALL:
        resultName = null;  
        needsDefaultReturnResult = false;
        break;
      case EXPRESSION:
        throw new IllegalStateException(
            "Movable expressions must be moved before inlining.");
      case DECOMPOSABLE_EXPRESSION:
        throw new IllegalStateException(
            "Decomposable expressions must be decomposed before inlining.");
      default:
        throw new IllegalStateException("Unexpected call site type.");
    }
    FunctionToBlockMutator mutator = new FunctionToBlockMutator(compiler, this.safeNameIdSupplier);
    boolean isCallInLoop = NodeUtil.isWithinLoop(callNode);
    Node newBlock = mutator.mutate(
        fnName, fnNode, callNode, resultName,
        needsDefaultReturnResult, isCallInLoop);
    NodeUtil.markNewScopesChanged(newBlock, compiler);
    Node greatGrandParent = grandParent.getParent();
    switch (callSiteType) {
      case VAR_DECL_SIMPLE_ASSIGNMENT:
        Node firstChild = parent.removeFirstChild();
        NodeUtil.markFunctionsDeleted(firstChild, compiler);
        Preconditions.checkState(parent.getFirstChild() == null);
        greatGrandParent.addChildAfter(newBlock, grandParent);
        break;
      case SIMPLE_ASSIGNMENT:
        Preconditions.checkState(grandParent.isExprResult());
        greatGrandParent.replaceChild(grandParent, newBlock);
        NodeUtil.markFunctionsDeleted(grandParent, compiler);
        break;
      case SIMPLE_CALL:
        Preconditions.checkState(parent.isExprResult());
        grandParent.replaceChild(parent, newBlock);
        NodeUtil.markFunctionsDeleted(parent, compiler);
        break;
      default:
        throw new IllegalStateException("Unexpected call site type.");
    }
    return newBlock;
  }
private Node inlineFunction ( Reference ref , Node fnNode , String fnName ) { Node callNode = ref . callNode ; Node parent = callNode . getParent ( ) ; Node grandParent = parent . getParent ( ) ; CallSiteType callSiteType = classifyCallSite ( ref ) ; checkArgument ( callSiteType != CallSiteType . UNSUPPORTED ) ; String resultName = null ; boolean needsDefaultReturnResult = true ; switch ( callSiteType ) { case SIMPLE_ASSIGNMENT : resultName = parent . getFirstChild ( ) . getString ( ) ; removeConstantVarAnnotation ( ref . scope , resultName ) ; break ; case VAR_DECL_SIMPLE_ASSIGNMENT : resultName = parent . getString ( ) ; removeConstantVarAnnotation ( ref . scope , resultName ) ; break ; case SIMPLE_CALL : resultName = null ; needsDefaultReturnResult = false ; break ; case EXPRESSION : throw new IllegalStateException ( "Movable expressions must be moved before inlining." ) ; case DECOMPOSABLE_EXPRESSION : throw new IllegalStateException ( "Decomposable expressions must be decomposed before inlining." ) ; default : throw new IllegalStateException ( "Unexpected call site type." ) ; } FunctionToBlockMutator mutator = new FunctionToBlockMutator ( compiler , this . safeNameIdSupplier ) ; boolean isCallInLoop = NodeUtil . isWithinLoop ( callNode ) ; Node newBlock = mutator . mutate ( fnName , fnNode , callNode , resultName , needsDefaultReturnResult , isCallInLoop ) ; NodeUtil . markNewScopesChanged ( newBlock , compiler ) ; Node greatGrandParent = grandParent . getParent ( ) ; switch ( callSiteType ) { case VAR_DECL_SIMPLE_ASSIGNMENT : Node firstChild = parent . removeFirstChild ( ) ; NodeUtil . markFunctionsDeleted ( firstChild , compiler ) ; Preconditions . checkState ( parent . getFirstChild ( ) == null ) ; greatGrandParent . addChildAfter ( newBlock , grandParent ) ; break ; case SIMPLE_ASSIGNMENT : Preconditions . checkState ( grandParent . isExprResult ( ) ) ; greatGrandParent . replaceChild ( grandParent , newBlock ) ; NodeUtil . markFunctionsDeleted ( grandParent , compiler ) ; break ; case SIMPLE_CALL : Preconditions . checkState ( parent . isExprResult ( ) ) ; grandParent . replaceChild ( parent , newBlock ) ; NodeUtil . markFunctionsDeleted ( parent , compiler ) ; break ; default : throw new IllegalStateException ( "Unexpected call site type." ) ; } return newBlock ; }
Inline a function which fulfills the requirements of
 canInlineReferenceAsStatementBlock into the call site, replacing the
 parent expression.
Inline a function which fulfills the requirements of canInlineReferenceAsStatementBlock into the call site replacing the parent expression .
// TODO(johnlenz): Consider storing the callSite classification in the // reference object and passing it in here. // Store the name for the result. This will be used to // replace "return expr" statements with "resultName = expr" // to replace // "foo()" doesn't need a result. // TODO(nicksantos): Create a common mutation function that // can replace either a VAR name assignment, assignment expression or // a EXPR_RESULT. // Remove the call from the name node. // Add the call, after the VAR. // The assignment is now part of the inline function so // replace it completely. // If nothing is looking at the result just replace the call.