/** * Regenerates the top scope from scratch. * * @param compiler The compiler for which the global scope is regenerated. * @param root The root of the AST. */ void regenerateGlobalTypedScope(AbstractCompiler compiler, Node root) { internalScopeCreator = new TypedScopeCreator(compiler); typedScopeCreator = new MemoizedScopeCreator(internalScopeCreator); topScope = typedScopeCreator.createScope(root, null); }
/** * Traverses *just* the contents of provided scope nodes (and optionally scopes nested within * them) but will fall back on traversing the entire AST from root if a null scope nodes list is * provided. Also allows for a callback to notify when starting on one of the provided scope * nodes. * @param root If scopeNodes is null, this method will just traverse 'root' instead. If scopeNodes * is not null, this parameter is ignored. */ public static void traverseScopeRoots( AbstractCompiler compiler, @Nullable Node root, @Nullable List<Node> scopeNodes, final Callback cb, @Nullable final ChangeScopeRootCallback changeCallback, final boolean traverseNested) { if (scopeNodes == null) { NodeTraversal.traverse(compiler, root, cb); } else { MemoizedScopeCreator scopeCreator = new MemoizedScopeCreator(new Es6SyntacticScopeCreator(compiler)); for (final Node scopeNode : scopeNodes) { traverseSingleScopeRoot( compiler, cb, changeCallback, traverseNested, scopeCreator, scopeNode); } } }
/** Main entry point of this phase for testing code. */ public TypedScope processForTesting(Node externsRoot, Node jsRoot) { Preconditions.checkState(scopeCreator == null); Preconditions.checkState(topScope == null); Preconditions.checkState(jsRoot.getParent() != null); Node externsAndJsRoot = jsRoot.getParent(); scopeCreator = new MemoizedScopeCreator(new TypedScopeCreator(compiler)); topScope = scopeCreator.createScope(externsAndJsRoot, null); TypeInferencePass inference = new TypeInferencePass(compiler, reverseInterpreter, topScope, scopeCreator); inference.process(externsRoot, jsRoot); process(externsRoot, jsRoot); return topScope; }
@Override public void process(Node externs, Node root) { ScopeCreator scopeCreator = new MemoizedScopeCreator(new TypedScopeCreator(compiler)); NodeTraversal externsTraversal = new NodeTraversal(compiler, new Traversal(true), scopeCreator); NodeTraversal codeTraversal = new NodeTraversal(compiler, new Traversal(false), scopeCreator); Scope topScope = compiler.getTopScope(); if (topScope != null) { externsTraversal.traverseWithScope(externs, topScope); codeTraversal.traverseWithScope(root, topScope); } else { externsTraversal.traverse(externs); codeTraversal.traverse(root); } connectUnknowns(); }