com.google.caja.parser.js.Reference

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

1. Scope#walkBlock()

Project: caja
File: Scope.java
private static void walkBlock(final Scope s, ParseTreeNode root) {
    SymbolHarvestVisitor v = new SymbolHarvestVisitor();
    v.visit(root);
    // by the visitor.
    for (Declaration decl : v.getDeclarations()) {
        declare(s, decl.getIdentifier(), computeDeclarationType(decl));
    }
    // harvested), then they must be free variables, so record them as such.
    for (Reference ref : v.getReferences()) {
        String name = ref.getIdentifierName();
        if ("arguments".equals(name)) {
            // JS magic identifier
            s.containsArguments = true;
        } else if (Keyword.THIS.toString().equals(name)) {
            s.hasFreeThis = true;
        } else if (!s.isDefined(name)) {
            addImportedVariable(s, name);
        }
    }
}

2. ParseTreeKB#optimizeMemberAccess()

Project: caja
File: ParseTreeKB.java
private void optimizeMemberAccess(Scope s, Operation ma, boolean isFuzzy, boolean isLhs, boolean throwsOnUndefined, Result out) {
    StringBuilder sb = new StringBuilder();
    sb.append('(');
    Expression obj = ma.children().get(0);
    optimize(s, obj, false, false, false, false, out);
    Reference prop = (Reference) ma.children().get(1);
    if (out.node != obj) {
        ma = Operation.createInfix(Operator.MEMBER_ACCESS, (Expression) out.node, prop);
    }
    sb = addDigest(out.digest, sb);
    int objDigestEnd = sb != null ? sb.length() : -1;
    if (sb != null) {
        nodeDigest(ma.children().get(1), sb);
        nodeTail(ma, sb);
    }
    String digest = sb != null ? sb.toString() : null;
    out.node = ma;
    out.digest = digest;
    if (digest != null) {
        if (!isLhs) {
            Fact f = getFact(digest);
            if (f != null && f.isSubstitutable(isFuzzy)) {
                out.node = f.value.clone();
                out.digest = nodeDigest(out.node);
                return;
            }
        }
        // window.addEventListener -> addEventListener
        String objDigest = digest.substring(1, objDigestEnd);
        Pair<Expression, Fact> objFe = facts.get(objDigest);
        if (objFe != null && objFe.b.isGlobal() && s.isOuter(prop.getIdentifierName())) {
            String propDigest = nodeDigest(prop);
            boolean canSimplify = false;
            if (isLhs || throwsOnUndefined) {
                // If it's being set, we don't need to worry about undefined global
                // errors, and we don't need to worry if the containing expression
                // would throw if it were undefined anyway.
                canSimplify = true;
            } else {
                // No difference between foo and global.foo because foo is
                // not undefined (truthy or (falsey and not undefined).
                Pair<Expression, Fact> propFe = facts.get(propDigest);
                if (propFe != null) {
                    Fact pf = propFe.b;
                    canSimplify = pf.isTruthy() || (pf.type == Fact.Type.IS && !pf.isUndefined());
                }
            }
            if (canSimplify) {
                out.node = prop;
                out.digest = propDigest;
            }
        }
    }
}