third_party/closure-compiler/src/src/com/google/javascript/jscomp/PeepholeFoldConstants.java
/**
   * Try to fold array-length. e.g [1, 2, 3].length ==> 3, [x, y].length ==> 2
   */
Try to fold array-length.
Try to fold array - length .
private Node tryFoldGetProp(Node n, Node left, Node right) {
    checkArgument(n.isGetProp());
    if (left.isObjectLit()) {
      return tryFoldObjectPropAccess(n, left, right);
    }
    if (right.isString() &&
        right.getString().equals("length")) {
      int knownLength = -1;
      switch (left.getToken()) {
        case ARRAYLIT:
          if (mayHaveSideEffects(left)) {
            return n;
          }
          knownLength = left.getChildCount();
          break;
        case STRING:
          knownLength = left.getString().length();
          break;
        default:
          return n;
      }
      checkState(knownLength != -1);
      Node lengthNode = IR.number(knownLength);
      reportChangeToEnclosingScope(n);
      n.replaceWith(lengthNode);
      return lengthNode;
    }
    return n;
  }
private Node tryFoldGetProp ( Node n , Node left , Node right ) { checkArgument ( n . isGetProp ( ) ) ; if ( left . isObjectLit ( ) ) { return tryFoldObjectPropAccess ( n , left , right ) ; } if ( right . isString ( ) && right . getString ( ) . equals ( "length" ) ) { int knownLength = - 1 ; switch ( left . getToken ( ) ) { case ARRAYLIT : if ( mayHaveSideEffects ( left ) ) { return n ; } knownLength = left . getChildCount ( ) ; break ; case STRING : knownLength = left . getString ( ) . length ( ) ; break ; default : return n ; } checkState ( knownLength != - 1 ) ; Node lengthNode = IR . number ( knownLength ) ; reportChangeToEnclosingScope ( n ) ; n . replaceWith ( lengthNode ) ; return lengthNode ; } return n ; }
Try to fold array-length.
Try to fold array - length .
// Nope, can't fold this, without handling the side-effects. // Not a foldable case, forget it.