private static void removeVarDeclaration ( Node name ) { Node var = NodeUtil . getEnclosingNode ( name , NodeUtil :: isNameDeclaration ) ; Node parent = var . getParent ( ) ; if ( var . getFirstChild ( ) . isDestructuringLhs ( ) ) { // convert `const [x] = arr` to `[x] = arr` // a precondition for this method is that `x` is the only lvalue in the destructuring pattern Node destructuringLhs = var . getFirstChild ( ) ; Node pattern = destructuringLhs . getFirstChild ( ) . detach ( ) ; if ( NodeUtil . isEnhancedFor ( parent ) ) { var . replaceWith ( pattern ) ; } else { Node rvalue = var . getFirstFirstChild ( ) . detach ( ) ; var . replaceWith ( NodeUtil . newExpr ( IR . assign ( pattern , rvalue ) . srcref ( var ) ) ) ; } } else if ( NodeUtil . isEnhancedFor ( parent ) ) { // convert `for (let x of ...` to `for (x of ...` parent . replaceChild ( var , name . detach ( ) ) ; } else { // either `var x = 0;` or `var x;` checkState ( var . hasOneChild ( ) && var . getFirstChild ( ) == name , var ) ; if ( name . hasChildren ( ) ) { // convert `let x = 0;` to `x = 0;` Node value = name . removeFirstChild ( ) ; var . removeChild ( name ) ; Node assign = IR . assign ( name , value ) . srcref ( name ) ; // We don't need to wrapped it with EXPR node if it is within a FOR. if ( ! parent . isVanillaFor ( ) ) { assign = NodeUtil . newExpr ( assign ) ; } parent . replaceChild ( var , assign ) ; } else { // convert `let x;` to `` // and `for (let x;;) {}` to `for (;;) {}` NodeUtil . removeChild ( parent , var ) ; } } }
Remove variable declaration if the variable has been coalesced with another variable that has already been declared .