Package com.google.caja.parser.js

Examples of com.google.caja.parser.js.Noop


    assertTrue(n.children().get(0).isImmutable());
    assertTrue(n.children().get(0).children().get(0).isImmutable());
    assertTrue(n.children().get(1).isImmutable());
    assertTrue(n.children().get(1).children().get(0).isImmutable());
    try {
      n.appendChild(new Noop(FilePosition.UNKNOWN));
      fail();
    } catch (UnsupportedOperationException e) { /* OK */ }
    try {
      n.setComments(Arrays.asList(
          Token.instance("test", JsTokenType.COMMENT, FilePosition.UNKNOWN)));
View Full Code Here


      if (nEmitted < n) {
        newChildren.addAll(children.subList(nEmitted, n));
      }
      if (newChildren.size() < 2) {
        out.node = newChildren.isEmpty()
            ? new Noop(UNK) : (Statement) newChildren.get(0);
        out.digest = optNodeDigest(out.node);
      } else {
        out.node = new Conditional(UNK, null, newChildren);
        out.digest = sb != null ? sb.toString() : null;
      }
View Full Code Here

          flattened == null ? Lists.newArrayList(children) : flattened);
      List<Statement> newChildren = joined != null ? joined : flattened;
      if (newChildren != null) { nChildren = newChildren.size(); }
      if (!needsBlock) {
        switch (nChildren) {
          case 0: return new Noop(n.getFilePosition());
          case 1: return (newChildren == null ? children : newChildren).get(0);
        }
      }
      return newChildren != null
          ? new Block(n.getFilePosition(), newChildren) : n;
    } else if (n instanceof SwitchStmt) {
      return optimizeSwitch((SwitchStmt) n);
    } else if (n instanceof ReturnStmt) {
      ReturnStmt rs = (ReturnStmt) n;
      Expression returnValue = rs.getReturnValue();
      Expression optReturnValue = returnValue != null
          ? (Expression) optimize(returnValue, false)
          : null;
      if (optReturnValue != null && returnValue != null  // 2nd implied by 1st
          && "undefined".equals(returnValue.typeOf())
          && optReturnValue.simplifyForSideEffect() == null) {
        return new ReturnStmt(rs.getFilePosition(), null);
      } else if (optReturnValue != returnValue) {
        return new ReturnStmt(rs.getFilePosition(), optReturnValue);
      }
      return rs;
    } else {
      List<? extends ParseTreeNode> children = n.children();
      int nChildren = children.size();
      List<ParseTreeNode> newChildren = null;
      boolean childNeedsBlock = (
          n instanceof FunctionConstructor || n instanceof TryStmt
          || n instanceof CatchStmt || n instanceof FinallyStmt
          || n instanceof SwitchCase);
      for (int i = 0; i < nChildren; ++i) {
        ParseTreeNode child = children.get(i);
        ParseTreeNode newChild = optimize(child, childNeedsBlock);
        if (child != newChild) {
          if (newChildren == null) {
            newChildren = Lists.newArrayListWithCapacity(nChildren);
          }
          newChildren.addAll(children.subList(newChildren.size(), i));
          newChildren.add(newChild);
        }
      }
      if (newChildren != null) {
        newChildren.addAll(children.subList(newChildren.size(), nChildren));
      }
      List<? extends ParseTreeNode> outChildren = newChildren == null
          ? children : newChildren;
      if (n instanceof ExpressionStmt) {
        Expression e = (Expression) outChildren.get(0);
        Expression simple = e.simplifyForSideEffect();
        if (simple == null) { return new Noop(n.getFilePosition()); }
        if (simple != e) {
          newChildren = Collections.<ParseTreeNode>singletonList(simple);
        }
      } else if (n instanceof Conditional) {
        List<ParseTreeNode> condParts = newChildren != null
View Full Code Here

          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
          } else if (chain.parent.node instanceof ForEachLoop) {
            replacement = new ExpressionStmt(new Reference(id));
          } else {
            replacement = new Noop(decl.getFilePosition());
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return true;
        } else if (node instanceof MultiDeclaration) {
          List<Expression> replacements = Lists.newArrayList();
          for (Declaration decl : ((MultiDeclaration) node).children()) {
            removedIdents.add(decl.getIdentifier());
            if (decl.getInitializer() == null) { continue; }
            visit(chain.child(decl).child(decl.getInitializer()));
            Expression assign = toAssignment(decl);
            replacements.add(assign);
          }
          Statement replacement;
          if (replacements.isEmpty()) {
            replacement = new Noop(node.getFilePosition());
          } else if (replacements.size() == 1) {
            Expression e = replacements.get(0);
            replacement = new ExpressionStmt(e.getFilePosition(), e);
          } else if (chain.parent.node instanceof Block) {
            List<Statement> stmts = Lists.newArrayList();
View Full Code Here

          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
          } else if (chain.parent.node instanceof ForEachLoop) {
            replacement = new ExpressionStmt(new Reference(id));
          } else {
            replacement = new Noop(decl.getFilePosition());
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return true;
        } else if (node instanceof MultiDeclaration) {
          List<Expression> replacements = Lists.newArrayList();
          for (Declaration decl : ((MultiDeclaration) node).children()) {
            removedIdents.add(decl.getIdentifier());
            if (decl.getInitializer() == null) { continue; }
            visit(chain.child(decl).child(decl.getInitializer()));
            Expression assign = toAssignment(decl);
            replacements.add(assign);
          }
          Statement replacement;
          if (replacements.isEmpty()) {
            replacement = new Noop(node.getFilePosition());
          } else if (replacements.size() == 1) {
            Expression e = replacements.get(0);
            replacement = new ExpressionStmt(e.getFilePosition(), e);
          } else if (chain.parent.node instanceof Block) {
            List<Statement> stmts = Lists.newArrayList();
View Full Code Here

    assertTrue(n.children().get(0).isImmutable());
    assertTrue(n.children().get(0).children().get(0).isImmutable());
    assertTrue(n.children().get(1).isImmutable());
    assertTrue(n.children().get(1).children().get(0).isImmutable());
    try {
      n.appendChild(new Noop(FilePosition.UNKNOWN));
      fail();
    } catch (UnsupportedOperationException e) { /* OK */ }
    try {
      n.setComments(Arrays.asList(
          Token.instance("test", JsTokenType.COMMENT, FilePosition.UNKNOWN)));
View Full Code Here

          flattened == null ? Lists.newArrayList(children) : flattened);
      List<Statement> newChildren = joined != null ? joined : flattened;
      if (newChildren != null) { nChildren = newChildren.size(); }
      if (!needsBlock) {
        switch (nChildren) {
          case 0: return new Noop(n.getFilePosition());
          case 1: return (newChildren == null ? children : newChildren).get(0);
        }
      }
      return newChildren != null
          ? new Block(n.getFilePosition(), newChildren) : n;
    } else if (n instanceof SwitchStmt) {
      return optimizeSwitch((SwitchStmt) n);
    } else if (n instanceof ReturnStmt) {
      ReturnStmt rs = (ReturnStmt) n;
      Expression returnValue = rs.getReturnValue();
      Expression optReturnValue = returnValue != null
          ? (Expression) optimize(returnValue, false)
          : null;
      if (optReturnValue != null && returnValue != null  // 2nd implied by 1st
          && "undefined".equals(returnValue.typeOf())
          && optReturnValue.simplifyForSideEffect() == null) {
        return new ReturnStmt(rs.getFilePosition(), null);
      } else if (optReturnValue != returnValue) {
        return new ReturnStmt(rs.getFilePosition(), optReturnValue);
      }
      return rs;
    } else {
      List<? extends ParseTreeNode> children = n.children();
      int nChildren = children.size();
      List<ParseTreeNode> newChildren = null;
      boolean childNeedsBlock = (
          n instanceof FunctionConstructor || n instanceof TryStmt
          || n instanceof CatchStmt || n instanceof FinallyStmt
          || n instanceof SwitchCase);
      for (int i = 0; i < nChildren; ++i) {
        ParseTreeNode child = children.get(i);
        ParseTreeNode newChild = optimize(child, childNeedsBlock);
        if (child != newChild) {
          if (newChildren == null) {
            newChildren = Lists.newArrayList(nChildren);
          }
          newChildren.addAll(children.subList(newChildren.size(), i));
          newChildren.add(newChild);
        }
      }
      if (newChildren != null) {
        newChildren.addAll(children.subList(newChildren.size(), nChildren));
      }
      List<? extends ParseTreeNode> outChildren = newChildren == null
          ? children : newChildren;
      if (n instanceof ExpressionStmt) {
        Expression e = (Expression) outChildren.get(0);
        Expression simple = e.simplifyForSideEffect();
        if (simple == null) { return new Noop(n.getFilePosition()); }
        if (simple != e) {
          newChildren = Collections.<ParseTreeNode>singletonList(simple);
        }
      } else if (n instanceof Conditional) {
        List<ParseTreeNode> condParts = newChildren != null
View Full Code Here

      if (nEmitted < n) {
        newChildren.addAll(children.subList(nEmitted, n));
      }
      if (newChildren.size() < 2) {
        out.node = newChildren.isEmpty()
            ? new Noop(UNK) : (Statement) newChildren.get(0);
        out.digest = optNodeDigest(out.node);
      } else {
        out.node = new Conditional(UNK, null, newChildren);
        out.digest = sb != null ? sb.toString() : null;
      }
View Full Code Here

        Map<String, ParseTreeNode> bindings = this.match(node);
        if (bindings != null && isSynthetic((Identifier) bindings.get("v"))) {
          Declaration d = (Declaration) expandAll(node, scope);
          Statement s;
          if (d.getInitializer() == null) {
            s = new Noop(d.getFilePosition());
          } else {
            s = new ExpressionStmt(
                Operation.createInfix(
                    Operator.ASSIGN, new Reference(d.getIdentifier()),
                    d.getInitializer()));
View Full Code Here

TOP

Related Classes of com.google.caja.parser.js.Noop

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.