Java Code Examples for com.google.javascript.rhino.Node#isArrayLit()

The following examples show how to use com.google.javascript.rhino.Node#isArrayLit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<String> identifyTypeDeclarationCall(Node n) {
  Node callName = n.getFirstChild();
  if ("goog.addDependency".equals(callName.getQualifiedName()) &&
      n.getChildCount() >= 3) {
    Node typeArray = callName.getNext().getNext();
    if (typeArray.isArrayLit()) {
      List<String> typeNames = Lists.newArrayList();
      for (Node name = typeArray.getFirstChild(); name != null;
           name = name.getNext()) {
        if (name.isString()) {
          typeNames.add(name.getString());
        }
      }
      return typeNames;
    }
  }
  return super.identifyTypeDeclarationCall(n);
}
 
Example 2
Source File: Closure_23_PeepholeFoldConstants_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold array-element. e.g [1, 2, 3][10];
 */
private Node tryFoldGetElem(Node n, Node left, Node right) {
  Preconditions.checkArgument(n.isGetElem());

  if (left.isObjectLit()) {
    return tryFoldObjectPropAccess(n, left, right);
  }

  if (left.isArrayLit()) {
    return tryFoldArrayAccess(n, left, right);
  }
  return n;
}
 
Example 3
Source File: Closure_23_PeepholeFoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold array-element. e.g [1, 2, 3][10];
 */
private Node tryFoldGetElem(Node n, Node left, Node right) {
  Preconditions.checkArgument(n.isGetElem());

  if (left.isObjectLit()) {
    return tryFoldObjectPropAccess(n, left, right);
  }

  if (left.isArrayLit()) {
    return tryFoldArrayAccess(n, left, right);
  }
  return n;
}
 
Example 4
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to fold array-element. e.g [1, 2, 3][10];
 */
private Node tryFoldGetElem(Node n, Node left, Node right) {
  Preconditions.checkArgument(n.isGetElem());

  if (left.isObjectLit()) {
    return tryFoldObjectPropAccess(n, left, right);
  }

  if (left.isArrayLit()) {
    return tryFoldArrayAccess(n, left, right);
  }
  return n;
}
 
Example 5
Source File: TransformAMDToCJSModule.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isCall() && n.getFirstChild() != null &&
      n.getFirstChild().isName() &&
      "define".equals(n.getFirstChild().getString())) {
    Scope.Var define = t.getScope().getVar(n.getFirstChild().
        getString());
    if (define != null && !define.isGlobal()) {
      // Ignore non-global define.
      return;
    }
    if (!(parent.isExprResult() && parent.getParent().isScript())) {
      t.report(n, NON_TOP_LEVEL_STATEMENT_DEFINE_ERROR);
      return;
    }
    Node script = parent.getParent();
    Node requiresNode = null;
    Node callback = null;
    int defineArity = n.getChildCount() - 1;
    if (defineArity == 0) {
      unsupportedDefineError(t, n);
      return;
    } else if (defineArity == 1) {
      callback = n.getChildAtIndex(1);
      if (callback.isObjectLit()) {
        handleDefineObjectLiteral(parent, callback, script);
        return;
      }
    } else if (defineArity == 2) {
      requiresNode = n.getChildAtIndex(1);
      callback = n.getChildAtIndex(2);
    } else if (defineArity >= 3) {
      unsupportedDefineError(t, n);
      return;
    }

    if (!callback.isFunction() ||
        (requiresNode != null && !requiresNode.isArrayLit())) {
      unsupportedDefineError(t, n);
      return;
    }

    handleRequiresAndParamList(t, n, script, requiresNode, callback);

    Node callbackBlock = callback.getChildAtIndex(2);
    NodeTraversal.traverse(compiler, callbackBlock,
        new DefineCallbackReturnCallback());

    moveCallbackContentToTopLevel(parent, script, callbackBlock);
    compiler.reportCodeChange();
  }
}
 
Example 6
Source File: PeepholeCollectPropertyAssignments.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
boolean isInterestingValue(Node n) {
  return n.isObjectLit() || n.isArrayLit();
}
 
Example 7
Source File: ExpandJqueryAliases.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Expand a jQuery.expandedEach call
 *
 * Expanded jQuery.expandedEach calls will replace the GETELEM nodes of a
 * property assignment with GETPROP nodes to allow for renaming.
 */
private void maybeExpandJqueryEachCall(NodeTraversal t, Node n) {
  Node objectToLoopOver = n.getChildAtIndex(1);

  if (objectToLoopOver == null) {
    return;
  }

  Node callbackFunction = objectToLoopOver.getNext();
  if (callbackFunction == null || !callbackFunction.isFunction()) {
    return;
  }

  // Run the peephole optimizations on the first argument to handle
  // cases like ("a " + "b").split(" ")
  peepholePasses.process(null, n.getChildAtIndex(1));

  // Create a reference tree
  Node nClone = n.cloneTree();

  objectToLoopOver = nClone.getChildAtIndex(1);

  // Check to see if the first argument is something we recognize and can
  // expand.
  if (!objectToLoopOver.isObjectLit() &&
      !(objectToLoopOver.isArrayLit() &&
      isArrayLitValidForExpansion(objectToLoopOver))) {
    t.report(n, JQUERY_UNABLE_TO_EXPAND_INVALID_LIT_ERROR, (String)null);
    return;
  }

  // Find all references to the callback function arguments
  List<Node> keyNodeReferences = Lists.newArrayList();
  List<Node> valueNodeReferences = Lists.newArrayList();

  NodeTraversal.traverse(compiler,
      NodeUtil.getFunctionBody(callbackFunction),
      new FindCallbackArgumentReferences(callbackFunction,
          keyNodeReferences, valueNodeReferences,
          objectToLoopOver.isArrayLit()));

  if(keyNodeReferences.size() == 0) {
   // We didn't do anything useful ...
    t.report(n, JQUERY_USELESS_EACH_EXPANSION, (String)null);
    return;
  }

  Node fncBlock = tryExpandJqueryEachCall(t, nClone, callbackFunction,
      keyNodeReferences, valueNodeReferences);

  if (fncBlock != null && fncBlock.hasChildren()) {
      replaceOriginalJqueryEachCall(n, fncBlock);
  } else {
    // We didn't do anything useful ...
    t.report(n, JQUERY_USELESS_EACH_EXPANSION, (String)null);
  }
}