com.google.caja.parser.js.Block

Here are the examples of the java api class com.google.caja.parser.js.Block taken from open source projects.

1. ConstLocalOptimizationTest#testIdentityOfOutput()

View license
public final void testIdentityOfOutput() throws Exception {
    Block canOptimize = js(fromString("" + "function f() {" + "  var x = true;" + "  return x;" + "}"));
    Block cannotOptimize = js(fromString("" + "function f() {" + "  return true;" + "}"));
    assertSame(cannotOptimize, ConstLocalOptimization.optimize(cannotOptimize));
    Block optimized = ConstLocalOptimization.optimize(canOptimize);
    assertEquals(render(cannotOptimize), render(optimized));
    assertFalse(canOptimize == optimized);
    assertSame(optimized, ConstLocalOptimization.optimize(optimized));
}

2. ScopeTest#testMaskedExceptionVariablesSame()

Project: caja
Source File: ScopeTest.java
View license
public final void testMaskedExceptionVariablesSame() throws Exception {
    Block outerBlock = js(fromString("try { } catch (e) { try { } catch (e) { var x; } }"));
    TryStmt t0 = (TryStmt) outerBlock.children().get(0);
    CatchStmt c0 = t0.getCatchClause();
    Block b0 = c0.getBody();
    TryStmt t1 = (TryStmt) b0.children().get(0);
    CatchStmt c1 = t1.getCatchClause();
    Scope sn = fromProgram(outerBlock);
    Scope sc0 = Scope.fromCatchStmt(sn, c0);
    Scope.fromCatchStmt(sc0, c1);
    assertEquals(0, mq.getMessages().size());
}

3. ScopeTest#testStartStatementsForCatchStmt()

Project: caja
Source File: ScopeTest.java
View license
public final void testStartStatementsForCatchStmt() throws Exception {
    Scope s0 = fromProgram(js(fromString("{}")));
    Block block = js(fromString("try {} catch (e) {}"));
    TryStmt t = (TryStmt) block.children().get(0);
    Scope s1 = Scope.fromCatchStmt(s0, t.getCatchClause());
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(0, s1.getStartStatements().size());
    s1.addStartStatement(js(fromString("{}")));
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(1, s1.getStartStatements().size());
    s1.addStartOfScopeStatement(js(fromString("{}")));
    assertEquals(1, s0.getStartStatements().size());
    assertEquals(1, s1.getStartStatements().size());
    s1.declareStartOfScopeTempVariable();
    assertEquals(2, s0.getStartStatements().size());
    assertEquals(1, s1.getStartStatements().size());
}

4. ScopeTest#testMaskedExceptionVariablesErrorA()

Project: caja
Source File: ScopeTest.java
View license
public final void testMaskedExceptionVariablesErrorA() throws Exception {
    Block n = js(fromString("var e; try { } catch (e) { var x; }"));
    TryStmt t = (TryStmt) n.children().get(1);
    CatchStmt c = (CatchStmt) t.children().get(1);
    Scope s0 = fromProgram(n);
    Scope.fromCatchStmt(s0, c);
    assertMsgType(MessageType.MASKING_SYMBOL, mq.getMessages().get(0));
    assertMsgLevel(MessageLevel.LINT, mq.getMessages().get(0));
}

5. ScopeTest#testCatchBlocks()

Project: caja
Source File: ScopeTest.java
View license
public final void testCatchBlocks() throws Exception {
    Block n = js(fromString("try { } catch (e) { var x; }"));
    TryStmt t = (TryStmt) n.children().get(0);
    CatchStmt c = (CatchStmt) t.children().get(1);
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromCatchStmt(s0, c);
    // e only defined in catch scope
    assertFalse(s0.isDefined("e"));
    assertTrue(s1.isDefined("e"));
    assertTrue(s1.isException("e"));
    // Definition of x appears in main scope
    assertTrue(s0.isDefined("x"));
}

6. ScopeTest#testMaskedExceptionVariablesErrorB()

Project: caja
Source File: ScopeTest.java
View license
public final void testMaskedExceptionVariablesErrorB() throws Exception {
    Block n = js(fromString("try { } catch (e) { function foo() { var e; } }"));
    TryStmt t = (TryStmt) n.children().get(0);
    CatchStmt c = (CatchStmt) t.children().get(1);
    Declaration d = findNodeWithIdentifier(n, Declaration.class, "foo");
    FunctionConstructor fc = (FunctionConstructor) d.getInitializer();
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromCatchStmt(s0, c);
    Scope.fromFunctionConstructor(s1, fc);
    assertEquals(1, mq.getMessages().size());
    assertMsgType(MessageType.MASKING_SYMBOL, mq.getMessages().get(0));
    assertMsgLevel(MessageLevel.LINT, mq.getMessages().get(0));
}

7. ScopeTest#testNamedFunction()

Project: caja
Source File: ScopeTest.java
View license
public final void testNamedFunction() throws Exception {
    Block n = js(fromString("var x = function foo() {};"));
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromFunctionConstructor(s0, findFunctionConstructor(n, "foo"));
    assertTrue(s0.isDefined("x"));
    assertFalse(s0.isImported("x"));
    assertFalse(s0.isDefined("foo"));
    assertFalse(s0.isImported("foo"));
    assertTrue(s1.isDefined("x"));
    assertFalse(s1.isImported("x"));
    assertTrue(s1.isDefined("foo"));
    assertFalse(s1.isImported("foo"));
}

8. ScopeTest#assertFreeVariables()

Project: caja
Source File: ScopeTest.java
View license
private void assertFreeVariables(String code, String freeVariables, String notFreeVariables) throws Exception {
    Block n = js(fromString(code));
    Scope s = fromProgram(n);
    for (String v : freeVariables.split(",")) {
        assertTrue("<" + v + "> should be a free variable in <" + code + ">", s.isImported(v));
    }
    for (String v : notFreeVariables.split(",")) {
        assertFalse("<" + v + "> should not be a free variable in <" + code + ">", s.isImported(v));
    }
}

9. ScopeTest#assertFunctionRedefined()

Project: caja
Source File: ScopeTest.java
View license
private void assertFunctionRedefined(String code, boolean recurseIntoFunction, MessageType type, MessageLevel level) throws Exception {
    Block b = js(fromString(code));
    Scope s0 = fromProgram(b);
    if (recurseIntoFunction) {
        Scope.fromFunctionConstructor(s0, findFunctionConstructor(b, "foo"));
    }
    assertFalse(mq.getMessages().size() == 0);
    assertMsgType(type, mq.getMessages().get(0));
    assertMsgLevel(level, mq.getMessages().get(0));
}

10. ParseTreeNodeTest#testIncompleteImmutability()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testIncompleteImmutability() throws Exception {
    // Immutability should be best-effort, stopping at a node that refuses
    Block n = new Block(FilePosition.UNKNOWN, Arrays.asList(new ExpressionStmt(new IntegerLiteral(FilePosition.UNKNOWN, 42)), new ExpressionStmt(new AlwaysMutable())));
    assertFalse(n.makeImmutable());
    assertFalse(n.isImmutable());
    assertTrue(n.children().get(0).isImmutable());
    assertTrue(n.children().get(0).children().get(0).isImmutable());
    assertFalse(n.children().get(1).isImmutable());
    assertFalse(n.children().get(1).children().get(0).isImmutable());
}

11. ParseTreeNodeTest#testImmutability()

Project: caja
Source File: ParseTreeNodeTest.java
View license
@SuppressWarnings("unchecked")
public final // setComments causes a warning about generic array for varargs
void testImmutability() throws Exception {
    Block n = new Block(FilePosition.UNKNOWN, Arrays.asList(new ExpressionStmt(new IntegerLiteral(FilePosition.UNKNOWN, 42)), new ExpressionStmt(new IntegerLiteral(FilePosition.UNKNOWN, 13))));
    assertTrue(n.makeImmutable());
    assertTrue(n.isImmutable());
    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) {
    }
    try {
        n.setComments(Arrays.asList(Token.instance("test", JsTokenType.COMMENT, FilePosition.UNKNOWN)));
        fail();
    } catch (UnsupportedOperationException e) {
    }
}

12. ScopeTest#testStartStatementsForFunctionConstructor()

Project: caja
Source File: ScopeTest.java
View license
public final void testStartStatementsForFunctionConstructor() throws Exception {
    Scope s0 = fromProgram(js(fromString("{}")));
    Block block = js(fromString("function() {};"));
    FunctionConstructor fc = (FunctionConstructor) block.children().get(0).children().get(0);
    Scope s1 = Scope.fromFunctionConstructor(s0, fc);
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(0, s1.getStartStatements().size());
    s1.addStartStatement(js(fromString("{}")));
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(1, s1.getStartStatements().size());
    s1.addStartOfScopeStatement(js(fromString("{}")));
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(2, s1.getStartStatements().size());
    s1.declareStartOfScopeTempVariable();
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(3, s1.getStartStatements().size());
}

13. ScopeTest#testUnmaskableFormals()

Project: caja
Source File: ScopeTest.java
View license
public final void testUnmaskableFormals() throws Exception {
    Block b = js(fromString("function NaN(Infinity, arguments) {}"));
    Scope top = fromProgram(b);
    FunctionDeclaration fn = ((FunctionDeclaration) b.children().get(0));
    Scope.fromFunctionConstructor(top, fn.getInitializer());
    assertMessage(RewriterMessageType.CANNOT_MASK_IDENTIFIER, MessageLevel.FATAL_ERROR, MessagePart.Factory.valueOf("NaN"));
    assertMessage(RewriterMessageType.CANNOT_MASK_IDENTIFIER, MessageLevel.FATAL_ERROR, MessagePart.Factory.valueOf("Infinity"));
    assertMessage(RewriterMessageType.CANNOT_MASK_IDENTIFIER, MessageLevel.FATAL_ERROR, MessagePart.Factory.valueOf("arguments"));
}

14. HtmlDefinitions#generateSourceText()

Project: caja
Source File: HtmlDefinitions.java
View license
private static void generateSourceText(HtmlSchema schema, Writer out) throws IOException {
    String currentDate = "" + new Date();
    if (currentDate.indexOf("\n") >= 0) {
        throw new SomethingWidgyHappenedError("Date should not contain newline");
    }
    out.write("// Copyright Google Inc.\n");
    out.write("// Licensed under the Apache Licence Version 2.0\n");
    out.write("// Autogenerated at " + currentDate + "\n");
    out.write("// @overrides window\n");
    out.write("// @provides html4\n");
    Block node = generateJavascriptDefinitions(schema);
    RenderContext rc = new RenderContext(node.makeRenderer(out, null)).withPropertyNameQuotingMode(PropertyNameQuotingMode.PRESERVE_QUOTES);
    renderFlattenedBlocks(node, rc);
    rc.getOut().noMoreTokens();
    out.write("\n");
    out.write("// export for Closure Compiler\n");
    out.write("if (typeof window !== 'undefined') {\n");
    out.write("  window['html4'] = html4;\n");
    out.write("}\n");
}

15. JsOptimizer#optimize()

Project: caja
Source File: JsOptimizer.java
View license
/**
   * Returns an optimized version of the concatenation of the programs
   * registered via {@link #addInput}.
   */
public Statement optimize() {
    Block block = new Block(FilePosition.UNKNOWN, compUnits);
    // Do first since this improves the performance of the ConstVarInliner.
    VarCollector.optimize(block);
    if (optimizer != null) {
        block = optimizer.optimize(block, mq);
    }
    if (rename) {
        // We pool after the ConstLocalOptimizer invoked by optimizer has run.
        block = ConstantPooler.optimize(block);
        // Now we shorten any long names introduced by the constant pooler.
        block = new LocalVarRenamer(mq).optimize(block);
    }
    // where it will make things shorter.
    return (Statement) StatementSimplifier.optimize(block, mq);
}

16. HtmlDefinitions#generateJavascriptDefinitions()

Project: caja
Source File: HtmlDefinitions.java
View license
public static Block generateJavascriptDefinitions(HtmlSchema schema) {
    final FilePosition unk = FilePosition.UNKNOWN;
    Map<AttribKey, HTML.Attribute.Type> atypes = attributeTypes(schema);
    Map<ElKey, EnumSet<EFlag>> eflags = elementFlags(schema);
    Map<ElKey, String> einterfaces = elementDOMInterfaces(schema);
    Map<AttribKey, UriEffect> uriEffects = uriEffects(schema);
    Map<AttribKey, LoaderType> ltypes = loaderTypes(schema);
    Block definitions = new Block();
    definitions.appendChild(QuasiBuilder.substV("var html4 = {};"));
    definitions.appendChild(mapFromEnum(EnumSet.allOf(HTML.Attribute.Type.class), "atype", new Function<HTML.Attribute.Type, String>() {

        public String apply(HTML.Attribute.Type f) {
            return f.name();
        }
    }, new Function<HTML.Attribute.Type, Integer>() {

        public Integer apply(HTML.Attribute.Type f) {
            return A_TYPE_MAP.get(f);
        }
    }));
    {
        List<StringLiteral> keys = new ArrayList<StringLiteral>();
        List<IntegerLiteral> values = new ArrayList<IntegerLiteral>();
        for (Map.Entry<AttribKey, HTML.Attribute.Type> e : atypes.entrySet()) {
            AttribKey key = e.getKey();
            if (ElKey.HTML_WILDCARD.equals(key.el) || schema.isElementAllowed(key.el) || // Whitelisted to allow dynamic script loading via proxy
            SCRIPT_SRC.equals(key)) {
                keys.add(StringLiteral.valueOf(unk, key.toString()));
                values.add(new IntegerLiteral(unk, A_TYPE_MAP.get(e.getValue())));
            }
        }
        definitions.appendChild(export("ATTRIBS", (Expression) QuasiBuilder.substV("({ @k*: @v* })", "k", new ParseTreeNodeContainer(keys), "v", new ParseTreeNodeContainer(values))));
    }
    definitions.appendChild(mapFromEnum(EnumSet.allOf(EFlag.class), "eflags", new Function<EFlag, String>() {

        public String apply(EFlag f) {
            return f.name();
        }
    }, new Function<EFlag, Integer>() {

        public Integer apply(EFlag f) {
            return f.bitMask;
        }
    }));
    definitions.appendChild(mapFromMap(eflags, "ELEMENTS", new Function<EnumSet<EFlag>, Expression>() {

        public Expression apply(EnumSet<EFlag> flags) {
            int value = 0;
            for (EFlag f : flags) {
                value |= f.bitMask;
            }
            return new IntegerLiteral(unk, value);
        }
    }));
    definitions.appendChild(mapFromMap(einterfaces, "ELEMENT_DOM_INTERFACES", new Function<String, Expression>() {

        public Expression apply(String domInterface) {
            return new StringLiteral(unk, domInterface);
        }
    }));
    definitions.appendChild(mapFromEnum(EnumSet.allOf(UriEffect.class), "ueffects", new Function<UriEffect, String>() {

        public String apply(UriEffect f) {
            return f.name();
        }
    }, new Function<UriEffect, Integer>() {

        public Integer apply(UriEffect f) {
            return A_UEFFECT_MAP.get(f);
        }
    }));
    definitions.appendChild(mapFromEnum(uriEffects.entrySet(), "URIEFFECTS", new Function<Entry<AttribKey, UriEffect>, String>() {

        public String apply(Entry<AttribKey, UriEffect> f) {
            return f.getKey().toString();
        }
    }, new Function<Entry<AttribKey, UriEffect>, Integer>() {

        public Integer apply(Entry<AttribKey, UriEffect> f) {
            return A_UEFFECT_MAP.get(f.getValue());
        }
    }));
    definitions.appendChild(mapFromEnum(EnumSet.allOf(LoaderType.class), "ltypes", new Function<LoaderType, String>() {

        public String apply(LoaderType f) {
            return f.name();
        }
    }, new Function<LoaderType, Integer>() {

        public Integer apply(LoaderType f) {
            return L_TYPE_MAP.get(f);
        }
    }));
    definitions.appendChild(mapFromEnum(ltypes.entrySet(), "LOADERTYPES", new Function<Entry<AttribKey, LoaderType>, String>() {

        public String apply(Entry<AttribKey, LoaderType> f) {
            return f.getKey().toString();
        }
    }, new Function<Entry<AttribKey, LoaderType>, Integer>() {

        public Integer apply(Entry<AttribKey, LoaderType> f) {
            return L_TYPE_MAP.get(f.getValue());
        }
    }));
    return definitions;
}

17. ConstantPooler#optimizeWithin()

Project: caja
Source File: ConstantPooler.java
View license
private static void optimizeWithin(FunctionConstructor fc) {
    final Map<LitVal, LitVal> uses = Maps.newLinkedHashMap();
    Block body = fc.getBody();
    body.acceptPreOrder(new Visitor() {

        public boolean visit(AncestorChain<?> chain) {
            if (chain.node instanceof Literal && !(chain.node instanceof RegexpLiteral)) {
                AncestorChain<Literal> litAc = chain.cast(Literal.class);
                LitVal key = new LitVal(litAc);
                LitVal stored = uses.get(key);
                if (stored == null) {
                    uses.put(key, stored = key);
                }
                stored.uses.add(litAc);
            } else if (chain.node instanceof ObjectConstructor) {
                List<? extends ObjProperty> children = chain.cast(ObjectConstructor.class).node.children();
                for (ObjProperty prop : children) {
                    visit(chain.child(prop).child(prop.children().get(1)));
                }
                return false;
            }
            return true;
        }
    }, null);
    List<Declaration> decls = Lists.newArrayList();
    FilePosition pos = FilePosition.startOf(body.getFilePosition());
    for (LitVal v : uses.values()) {
        // Size now = canonLen * nUses.
        // Size after = "var aa=".length + canonLen + ";".length + "aa" * nUses
        // Switch if now > after;
        //           canonLen * nUses > 8 + canonLen + 2 * nUses
        // TUNING PARAMETER
        int requiredSavings = 30;
        int canonLen = v.canonForm().length();
        int nUses = v.uses.size();
        if (canonLen * nUses > 8 + canonLen + 2 * nUses + requiredSavings) {
            // TODO(mikesamuel): choose a guaranteed non-interfering name.
            String name = "$_$__litpool__" + decls.size() + "$_$";
            decls.add(new Declaration(pos, new Identifier(pos, name), v.uses.get(0).node));
            for (AncestorChain<Literal> use : v.uses) {
                Reference ref = new Reference(new Identifier(use.node.getFilePosition(), name));
                use.parent.cast(MutableParseTreeNode.class).node.replaceChild(ref, use.node);
            }
        }
    }
    if (!decls.isEmpty()) {
        Statement first = body.children().get(0);
        MultiDeclaration md;
        if (first instanceof MultiDeclaration) {
            md = (MultiDeclaration) first;
        } else if (first instanceof Declaration) {
            md = new MultiDeclaration(FilePosition.span(pos, first.getFilePosition()), Collections.singletonList((Declaration) first));
            body.replaceChild(md, first);
        } else if (decls.size() == 1) {
            body.insertBefore(decls.get(0), first);
            return;
        } else {
            md = new MultiDeclaration(pos, Collections.<Declaration>emptyList());
            body.insertBefore(md, first);
        }
        MutableParseTreeNode.Mutation mut = md.createMutation();
        Declaration firstDecl = md.children().get(0);
        for (Declaration decl : decls) {
            mut = mut.insertBefore(decl, firstDecl);
        }
        mut.execute();
    }
}

18. ScopeTest#testAnonymousFunction()

Project: caja
Source File: ScopeTest.java
View license
public final void testAnonymousFunction() throws Exception {
    Block n = js(fromString("var x = function() {};"));
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromFunctionConstructor(s0, findFunctionConstructor(n, null));
    assertTrue(s0.isDefined("x"));
    assertFalse(s0.isImported("x"));
    assertTrue(s1.isDefined("x"));
    assertFalse(s1.isImported("x"));
}

19. ScopeTest#testUnmaskableIdentifiersInCatch()

Project: caja
Source File: ScopeTest.java
View license
public final void testUnmaskableIdentifiersInCatch() throws Exception {
    Block b = js(fromString("try {} catch (Object) {}"));
    TryStmt tryStmt = (TryStmt) b.children().get(0);
    Scope top = fromProgram(b);
    Scope.fromCatchStmt(top, tryStmt.getCatchClause());
    assertMessage(RewriterMessageType.CANNOT_MASK_IDENTIFIER, MessageLevel.FATAL_ERROR, MessagePart.Factory.valueOf("Object"));
}

20. ScopeTest#testUnmaskableIdentifiersInDeclarations()

Project: caja
Source File: ScopeTest.java
View license
public final void testUnmaskableIdentifiersInDeclarations() throws Exception {
    Block b = js(fromString("var Array, undefined;"));
    fromProgram(b);
    assertMessage(RewriterMessageType.CANNOT_MASK_IDENTIFIER, MessageLevel.FATAL_ERROR, MessagePart.Factory.valueOf("Array"));
}

21. CajaTestCase#js()

Project: caja
Source File: CajaTestCase.java
View license
protected Block js(CharProducer cp, Criterion<Token<JsTokenType>> filt, boolean quasi) throws ParseException {
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(lexer, sourceOf(cp), filt);
    Parser p = new Parser(tq, mq, quasi);
    Block b = p.parse();
    tq.expectEmpty();
    return b;
}

22. ScopeTest#testBodyOfNamedFunction()

Project: caja
Source File: ScopeTest.java
View license
public final void testBodyOfNamedFunction() throws Exception {
    Block n = js(fromString("function foo() { var x; }"));
    Scope s0 = fromProgram(n);
    assertEquals(0, mq.getMessages().size());
    assertTrue(s0.isDefined("foo"));
}

23. ScopeTest#testFormalParams()

Project: caja
Source File: ScopeTest.java
View license
public final void testFormalParams() throws Exception {
    Block n = js(fromString("function f(x) {};"));
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromFunctionConstructor(s0, findFunctionConstructor(n, "f"));
    assertFalse(s0.isDefined("x"));
    assertTrue(s1.isDefined("x"));
}

24. ScopeTest#testNamedFunctionSameName()

Project: caja
Source File: ScopeTest.java
View license
public final void testNamedFunctionSameName() throws Exception {
    Block n = js(fromString("var x = function x() {};"));
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromFunctionConstructor(s0, findFunctionConstructor(n, "x"));
    assertTrue(s0.isDefined("x"));
    assertFalse(s0.isImported("x"));
    assertTrue(s1.isDefined("x"));
    assertFalse(s1.isImported("x"));
}

25. ScopeTest#testSimpleDeclaredFunction()

Project: caja
Source File: ScopeTest.java
View license
public final void testSimpleDeclaredFunction() throws Exception {
    Block n = js(fromString("var x = 3;" + "function foo() {" + "  var y = 3;" + "  z = 4;" + "};"));
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromFunctionConstructor(s0, findFunctionConstructor(n, "foo"));
    assertTrue(s0.isDefined("x"));
    assertFalse(s0.isImported("x"));
    assertFalse(s0.isFunction("x"));
    assertFalse(s0.isDeclaredFunction("x"));
    assertTrue(s0.isDefined("foo"));
    assertFalse(s0.isImported("foo"));
    assertTrue(s0.isFunction("foo"));
    assertTrue(s0.isDeclaredFunction("foo"));
    assertFalse(s0.isDefined("y"));
    assertFalse(s0.isImported("y"));
    assertFalse(s0.isFunction("y"));
    assertFalse(s0.isDeclaredFunction("y"));
    assertFalse(s0.isDefined("z"));
    assertTrue(s0.isImported("z"));
    assertFalse(s0.isFunction("z"));
    assertFalse(s0.isDeclaredFunction("z"));
    assertTrue(s1.isDefined("x"));
    assertFalse(s1.isImported("x"));
    assertFalse(s1.isFunction("x"));
    assertFalse(s1.isDeclaredFunction("x"));
    assertTrue(s1.isDefined("foo"));
    assertFalse(s1.isImported("foo"));
    assertTrue(s1.isFunction("foo"));
    assertFalse(s1.isDeclaredFunction("foo"));
    assertTrue(s1.isDefined("y"));
    assertFalse(s1.isImported("y"));
    assertFalse(s1.isFunction("y"));
    assertFalse(s1.isDeclaredFunction("y"));
    assertFalse(s1.isDefined("z"));
    assertTrue(s1.isImported("z"));
    assertFalse(s1.isFunction("z"));
    assertFalse(s1.isDeclaredFunction("z"));
}

26. VarCollectorTest#assertOptimized()

Project: caja
Source File: VarCollectorTest.java
View license
private void assertOptimized(String golden, String input) throws ParseException {
    Block program = js(fromString(input));
    VarCollector.optimize(program);
    assertEquals(render(js(fromString(golden))), render(program));
}

27. StatementSimplifierTest#assertSimplified()

Project: caja
Source File: StatementSimplifierTest.java
View license
private void assertSimplified(List<String> expected, List<String> input) throws ParseException {
    Block expectedStmt = js(fromString(Join.join("\n", expected)));
    Statement inputStmt = js(fromString(Join.join("\n", input)));
    ParseTreeNode optimized = StatementSimplifier.optimize(inputStmt, mq);
    assertEquals(renderBody(expectedStmt), renderStmt((Statement) optimized));
}

28. JsOptimizerTest#assertOptimized()

Project: caja
Source File: JsOptimizerTest.java
View license
private void assertOptimized(Statement golden, Block... inputs) {
    for (Block input : inputs) {
        opt.addInput(input);
    }
    Statement optimized = opt.optimize();
    assertEquals(renderProgram(golden), renderProgram(optimized));
}

29. ConstantPoolerTest#assertOptimized()

Project: caja
Source File: ConstantPoolerTest.java
View license
private void assertOptimized(String golden, String input) throws ParseException {
    Block prog = js(fromString(input));
    assertEquals(render(js(fromString(golden))), render(ConstantPooler.optimize(prog)));
}

30. QuasiBuilder#parseQuasiNode()

Project: caja
Source File: QuasiBuilder.java
View license
/**
   * Given a quasiliteral pattern expressed as text, return a {@code QuasiNode}
   * representing the pattern.
   *
   * @param inputSource description of input source of pattern text.
   * @param pattern a quasiliteral pattern.
   * @return the QuasiNode representation of the input.
   * @exception ParseException if there is a parsing problem.
   */
public static QuasiNode parseQuasiNode(InputSource inputSource, String pattern) throws ParseException {
    // The top-level node returned from the parser is always a Block.
    Block topLevelBlock = (Block) parse(inputSource, pattern);
    ParseTreeNode topLevelNode = topLevelBlock;
    // match anywhere.
    if (topLevelNode.children().size() == 1) {
        topLevelNode = topLevelNode.children().get(0);
    }
    // anywhere.
    if (topLevelNode instanceof ExpressionStmt) {
        topLevelNode = topLevelNode.children().get(0);
    }
    // context.
    if (topLevelNode instanceof FunctionDeclaration) {
        topLevelNode = ((FunctionDeclaration) topLevelNode).getInitializer();
    }
    return build(topLevelNode);
}

31. ConstLocalOptimization#optimize()

Project: caja
Source File: ConstLocalOptimization.java
View license
public static Block optimize(Block program) {
    Block clone = (Block) program.clone();
    while (true) {
        Optimizer opt = new Optimizer();
        opt.examine(AncestorChain.instance(clone));
        opt.finish();
        if (!opt.changed) {
            return program;
        }
        program = clone;
    }
}