Skip to content

Commit

Permalink
Finish the migration to the Es6SyntacticScopeCreator by renaming the …
Browse files Browse the repository at this point in the history
…class SyntacticScopeCreator.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=257835869
  • Loading branch information
concavelenz authored and tjgq committed Jul 13, 2019
1 parent 40647e9 commit 224ba4b
Show file tree
Hide file tree
Showing 38 changed files with 98 additions and 108 deletions.
Expand Up @@ -325,7 +325,7 @@ private void inlineAliasIfPossible(Name name, Ref alias, GlobalNamespace namespa
new ReferenceCollectingCallback(
compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
new Es6SyntacticScopeCreator(compiler),
new SyntacticScopeCreator(compiler),
Predicates.equalTo(aliasVar));
Scope aliasScope = aliasVar.getScope();
collector.processScope(aliasScope);
Expand Down
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/CoalesceVariableNames.java
Expand Up @@ -135,8 +135,7 @@ public void enterScope(NodeTraversal t) {
ControlFlowGraph<Node> cfg = t.getControlFlowGraph();

liveness =
new LiveVariablesAnalysis(
cfg, scope, null, compiler, new Es6SyntacticScopeCreator(compiler));
new LiveVariablesAnalysis(cfg, scope, null, compiler, new SyntacticScopeCreator(compiler));

if (FeatureSet.ES3.contains(compiler.getOptions().getOutputFeatureSet())) {
// If the function has exactly 2 params, mark them as escaped. This is a work-around for a
Expand Down
4 changes: 1 addition & 3 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -1491,9 +1491,7 @@ public SymbolTable buildKnownSymbolTable() {

ReferenceCollectingCallback refCollector =
new ReferenceCollectingCallback(
this,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
new Es6SyntacticScopeCreator(this));
this, ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR, new SyntacticScopeCreator(this));
refCollector.process(getRoot());
symbolTable.addSymbolsFrom(refCollector);

Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CrossChunkCodeMotion.java
Expand Up @@ -95,7 +95,7 @@ public void process(Node externs, Node root) {
// If there are <2 chunks, then we will never move anything, so we're done
if (graph.getModuleCount() > 1) {
CrossChunkReferenceCollector referenceCollector =
new CrossChunkReferenceCollector(compiler, new Es6SyntacticScopeCreator(compiler));
new CrossChunkReferenceCollector(compiler, new SyntacticScopeCreator(compiler));
referenceCollector.process(root);
Collection<GlobalSymbol> globalSymbols =
new GlobalSymbolCollector().collectGlobalSymbols(referenceCollector);
Expand Down
7 changes: 3 additions & 4 deletions src/com/google/javascript/jscomp/DataFlowAnalysis.java
Expand Up @@ -526,17 +526,16 @@ public int hashCode() {
* referenced outside of the code that we are analyzing. A variable is escaped if any of the
* following is true:
*
* 1. Exported variables as they can be needed after the script terminates.
* 2. Names of named functions because in JavaScript, function foo(){} does not kill
* foo in the dataflow.
* <p>1. Exported variables as they can be needed after the script terminates. 2. Names of named
* functions because in JavaScript, function foo(){} does not kill foo in the dataflow.
*
* @param jsScope Must be a function scope
*/
static void computeEscaped(
final Scope jsScope,
final Set<Var> escaped,
AbstractCompiler compiler,
Es6SyntacticScopeCreator scopeCreator) {
SyntacticScopeCreator scopeCreator) {

checkArgument(jsScope.isFunctionScope());

Expand Down
Expand Up @@ -123,7 +123,7 @@ private void eliminateDeadAssignments(NodeTraversal t) {
ControlFlowGraph<Node> cfg = t.getControlFlowGraph();
liveness =
new LiveVariablesAnalysis(
cfg, functionScope, blockScope, compiler, new Es6SyntacticScopeCreator(compiler));
cfg, functionScope, blockScope, compiler, new SyntacticScopeCreator(compiler));
liveness.analyze();
Map<String, Var> allVarsInFn = liveness.getAllVariables();
tryRemoveDeadAssignments(t, cfg, allVarsInFn);
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Denormalize.java
Expand Up @@ -57,7 +57,7 @@ public void process(Node externs, Node root) {
NodeTraversal.traverse(compiler, root, this);
// Don't inline the VAR declaration if this compilation involves old-style ctemplates.
if (compiler.getOptions().syntheticBlockStartMarker == null) {
(new ReferenceCollectingCallback(compiler, this, new Es6SyntacticScopeCreator(compiler)))
(new ReferenceCollectingCallback(compiler, this, new SyntacticScopeCreator(compiler)))
.process(root);
}
}
Expand Down
Expand Up @@ -68,7 +68,7 @@ public final boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
});

Node block = paramList.getNext();
Es6SyntacticScopeCreator creator = new Es6SyntacticScopeCreator(compiler);
SyntacticScopeCreator creator = new SyntacticScopeCreator(compiler);
Scope fScope = creator.createScope(n, t.getScope());
Scope fBlockScope = creator.createScope(block, fScope);
Table<Node, String, String> renameTable = HashBasedTable.create();
Expand All @@ -81,7 +81,7 @@ public final boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
}
}
new NodeTraversal(
compiler, new Es6RenameReferences(renameTable), new Es6SyntacticScopeCreator(compiler))
compiler, new Es6RenameReferences(renameTable), new SyntacticScopeCreator(compiler))
.traverseInnerNode(block, block.getParent(), fScope);
}

Expand Down
Expand Up @@ -175,7 +175,7 @@ public void enterScope(NodeTraversal t) {
return;
}

Es6SyntacticScopeCreator scopeCreator = (Es6SyntacticScopeCreator) t.getScopeCreator();
SyntacticScopeCreator scopeCreator = (SyntacticScopeCreator) t.getScopeCreator();

// Compute the forward reaching definition.
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
Expand Down Expand Up @@ -260,7 +260,7 @@ public void exitScope(NodeTraversal t) {}

@Override
public void process(Node externs, Node root) {
(new NodeTraversal(compiler, this, new Es6SyntacticScopeCreator(compiler)))
(new NodeTraversal(compiler, this, new SyntacticScopeCreator(compiler)))
.traverseRoots(externs, root);
}

Expand Down
24 changes: 11 additions & 13 deletions src/com/google/javascript/jscomp/IncrementalScopeCreator.java
Expand Up @@ -23,7 +23,7 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.Es6SyntacticScopeCreator.ScopeScanner;
import com.google.javascript.jscomp.SyntacticScopeCreator.ScopeScanner;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -34,23 +34,22 @@
import java.util.Set;

/**
* A reusable scope creator which invalidates scopes based on reported
* AST changes to SCRIPT and FUNCTION codes (aka "change scopes"). This
* class stores an instance of itself on the compiler object which is accessible via
* the "getInstance" static method. To ensure that consumers see a consistent state,
* they must call "freeze"/"thaw" before and after use (typically for the duration
* A reusable scope creator which invalidates scopes based on reported AST changes to SCRIPT and
* FUNCTION codes (aka "change scopes"). This class stores an instance of itself on the compiler
* object which is accessible via the "getInstance" static method. To ensure that consumers see a
* consistent state, they must call "freeze"/"thaw" before and after use (typically for the duration
* of a NodeTraveral).
*
* This class delegates to the Es6SyntacticScopeCreator and requires a consistent
* definition of global Scope (the global scope root must include both externs and code).
* <p>This class delegates to the SyntacticScopeCreator and requires a consistent definition of
* global Scope (the global scope root must include both externs and code).
*/
class IncrementalScopeCreator implements ScopeCreator {

private final AbstractCompiler compiler;
// TODO(johnlenz): This leaks scope object for scopes removed from the AST.
// Soon we will track removed function nodes use that to remove scopes.
private final Map<Node, PersistentScope> scopesByScopeRoot = new HashMap<>();
private final Es6SyntacticScopeCreator delegate;
private final SyntacticScopeCreator delegate;

private final PersistentScopeFactory factory = new PersistentScopeFactory();

Expand Down Expand Up @@ -388,13 +387,12 @@ void refresh(AbstractCompiler compiler, PersistentScope newParent) {
}
}

Es6SyntacticScopeCreator createInternalScopeCreator(AbstractCompiler compiler) {
return new Es6SyntacticScopeCreator(compiler, factory, factory);
SyntacticScopeCreator createInternalScopeCreator(AbstractCompiler compiler) {
return new SyntacticScopeCreator(compiler, factory, factory);
}

private static class PersistentScopeFactory
implements Es6SyntacticScopeCreator.ScopeFactory,
Es6SyntacticScopeCreator.RedeclarationHandler {
implements SyntacticScopeCreator.ScopeFactory, SyntacticScopeCreator.RedeclarationHandler {
@Override
public PersistentScope create(Scope parent, Node n) {
return PersistentScope.create((PersistentScope) parent, n);
Expand Down
5 changes: 2 additions & 3 deletions src/com/google/javascript/jscomp/InferConsts.java
Expand Up @@ -47,15 +47,14 @@ public void process(Node externs, Node js) {
new ReferenceCollectingCallback(
compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
new Es6SyntacticScopeCreator(compiler));
new SyntacticScopeCreator(compiler));
collector.process(js);

for (Var v : collector.getAllSymbols()) {
considerVar(v, collector.getReferences(v));
}

Scope globalExternsScope =
new Es6SyntacticScopeCreator(compiler).createScope(externs, null);
Scope globalExternsScope = new SyntacticScopeCreator(compiler).createScope(externs, null);
for (Var v : globalExternsScope.getAllSymbols()) {
considerVar(v, null);
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/InlineObjectLiterals.java
Expand Up @@ -61,8 +61,9 @@ class InlineObjectLiterals implements CompilerPass {

@Override
public void process(Node externs, Node root) {
ReferenceCollectingCallback callback = new ReferenceCollectingCallback(
compiler, new InliningBehavior(), new Es6SyntacticScopeCreator(compiler));
ReferenceCollectingCallback callback =
new ReferenceCollectingCallback(
compiler, new InliningBehavior(), new SyntacticScopeCreator(compiler));
callback.process(externs, root);
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/InlineVariables.java
Expand Up @@ -86,7 +86,7 @@ public void process(Node externs, Node root) {
new ReferenceCollectingCallback(
compiler,
new InliningBehavior(),
new Es6SyntacticScopeCreator(compiler),
new SyntacticScopeCreator(compiler),
mode.varPredicate);
callback.process(externs, root);
}
Expand Down
Expand Up @@ -142,7 +142,7 @@ public int hashCode() {
Scope jsScope,
@Nullable Scope jsScopeChild,
AbstractCompiler compiler,
Es6SyntacticScopeCreator scopeCreator) {
SyntacticScopeCreator scopeCreator) {
super(cfg, new LiveVariableJoinOp());
checkState(jsScope.isFunctionScope(), jsScope);

Expand Down
Expand Up @@ -56,7 +56,7 @@ class MaybeReachingVariableUse extends
ControlFlowGraph<Node> cfg,
Scope jsScope,
AbstractCompiler compiler,
Es6SyntacticScopeCreator scopeCreator) {
SyntacticScopeCreator scopeCreator) {
super(cfg, new ReachingUsesJoinOp());
this.escaped = new HashSet<>();
this.allVarsInFn = new HashMap<>();
Expand Down
Expand Up @@ -55,7 +55,7 @@ final class MustBeReachingVariableDef extends
ControlFlowGraph<Node> cfg,
Scope jsScope,
AbstractCompiler compiler,
Es6SyntacticScopeCreator scopeCreator) {
SyntacticScopeCreator scopeCreator) {
super(cfg, new MustDefJoin());
this.compiler = compiler;
this.escaped = new HashSet<>();
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/NodeTraversal.java
Expand Up @@ -375,9 +375,9 @@ public void traverse(Node root) {
}
}

/** Traverses using the ES6SyntacticScopeCreator */
/** Traverses using the SyntacticScopeCreator */
public static void traverse(AbstractCompiler compiler, Node root, Callback cb) {
NodeTraversal t = new NodeTraversal(compiler, cb, new Es6SyntacticScopeCreator(compiler));
NodeTraversal t = new NodeTraversal(compiler, cb, new SyntacticScopeCreator(compiler));
t.traverse(root);
}

Expand Down Expand Up @@ -408,7 +408,7 @@ void traverseRoots(Node externs, Node root) {

public static void traverseRoots(
AbstractCompiler compiler, Callback cb, Node externs, Node root) {
NodeTraversal t = new NodeTraversal(compiler, cb, new Es6SyntacticScopeCreator(compiler));
NodeTraversal t = new NodeTraversal(compiler, cb, new SyntacticScopeCreator(compiler));
t.traverseRoots(externs, root);
}

Expand Down Expand Up @@ -592,7 +592,7 @@ public static void traverseScopeRoots(
NodeTraversal.traverse(compiler, root, cb);
} else {
MemoizedScopeCreator scopeCreator =
new MemoizedScopeCreator(new Es6SyntacticScopeCreator(compiler));
new MemoizedScopeCreator(new SyntacticScopeCreator(compiler));

for (final Node scopeNode : scopeNodes) {
traverseSingleScopeRoot(
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -5462,7 +5462,7 @@ static ImmutableSet<String> collectExternVariableNames(AbstractCompiler compiler
new ReferenceCollectingCallback(
compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
new Es6SyntacticScopeCreator(compiler));
new SyntacticScopeCreator(compiler));
externsRefs.process(externs);
ImmutableSet.Builder<String> externsNames = ImmutableSet.builder();
for (Var v : externsRefs.getAllSymbols()) {
Expand Down
10 changes: 4 additions & 6 deletions src/com/google/javascript/jscomp/Normalize.java
Expand Up @@ -782,16 +782,14 @@ private static Node addToFront(Node parent, Node newChild, Node after) {
private void removeDuplicateDeclarations(Node externs, Node root) {
Callback tickler = new ScopeTicklingCallback();
ScopeCreator scopeCreator =
new Es6SyntacticScopeCreator(compiler, new DuplicateDeclarationHandler());
new SyntacticScopeCreator(compiler, new DuplicateDeclarationHandler());
NodeTraversal t = new NodeTraversal(compiler, tickler, scopeCreator);
t.traverseRoots(externs, root);
}

/**
* ScopeCreator duplicate declaration handler.
*/
private final class DuplicateDeclarationHandler implements
Es6SyntacticScopeCreator.RedeclarationHandler {
/** ScopeCreator duplicate declaration handler. */
private final class DuplicateDeclarationHandler
implements SyntacticScopeCreator.RedeclarationHandler {

private final Set<Var> hasOkDuplicateDeclaration = new HashSet<>();

Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/RemoveUnusedCode.java
Expand Up @@ -140,7 +140,7 @@ class RemoveUnusedCode implements CompilerPass {
*/
private final Multimap<String, PolyfillInfo> polyfills = HashMultimap.create();

private final Es6SyntacticScopeCreator scopeCreator;
private final SyntacticScopeCreator scopeCreator;

private final boolean removeUnusedPrototypeProperties;
private final boolean allowRemovalOfExternProperties;
Expand All @@ -163,7 +163,7 @@ class RemoveUnusedCode implements CompilerPass {
this.removeUnusedObjectDefinePropertiesDefinitions =
builder.removeUnusedObjectDefinePropertiesDefinitions;
this.removeUnusedPolyfills = builder.removeUnusedPolyfills;
this.scopeCreator = new Es6SyntacticScopeCreator(builder.compiler);
this.scopeCreator = new SyntacticScopeCreator(builder.compiler);

// All Vars that are completely unremovable will share this VarInfo instance.
canonicalUnremovableVarInfo = new VarInfo();
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/RewriteGoogJsImports.java
Expand Up @@ -284,7 +284,7 @@ private Node findGoogImportNode(Node scriptRoot) {
}

Scope moduleScope =
new Es6SyntacticScopeCreator(compiler)
new SyntacticScopeCreator(compiler)
.createScope(scriptRoot.getFirstChild(), Scope.createGlobalScope(scriptRoot));
Var googVar = moduleScope.getVar("goog");

Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/ScopeCreator.java
Expand Up @@ -37,7 +37,7 @@ interface ScopeCreator {

/**
* A scope creator that can be provided to NodeTraversal to ensure that no scopes are actually
* ever created. This is in contrast to the default Es6SyntacticScopeCreator, which will create
* ever created. This is in contrast to the default SyntacticScopeCreator, which will create
* scopes on demand.
*/
ScopeCreator ASSERT_NO_SCOPES_CREATED =
Expand Down
Expand Up @@ -24,15 +24,15 @@
import javax.annotation.Nullable;

/**
* <p>The syntactic scope creator scans the parse tree to create a Scope object
* containing all the variable declarations in that scope. This class adds support
* for block-level scopes introduced in ECMAScript 6.</p>
* The syntactic scope creator scans the parse tree to create a Scope object containing all the
* variable declarations in that scope. This class adds support for block-level scopes introduced in
* ECMAScript 6.
*
* <p>This implementation is not thread-safe.</p>
* <p>This implementation is not thread-safe.
*
* @author moz@google.com (Michael Zhou)
*/
public class Es6SyntacticScopeCreator implements ScopeCreator {
public class SyntacticScopeCreator implements ScopeCreator {
private final AbstractCompiler compiler;
private final RedeclarationHandler redeclarationHandler;
private final ScopeFactory scopeFactory;
Expand All @@ -44,22 +44,21 @@ public class Es6SyntacticScopeCreator implements ScopeCreator {
public static final RedeclarationHandler DEFAULT_REDECLARATION_HANDLER =
new DefaultRedeclarationHandler();


public Es6SyntacticScopeCreator(AbstractCompiler compiler) {
public SyntacticScopeCreator(AbstractCompiler compiler) {
this(compiler, DEFAULT_REDECLARATION_HANDLER);
}

public Es6SyntacticScopeCreator(AbstractCompiler compiler, ScopeFactory scopeFactory) {
public SyntacticScopeCreator(AbstractCompiler compiler, ScopeFactory scopeFactory) {
this(compiler, DEFAULT_REDECLARATION_HANDLER, scopeFactory);
}

Es6SyntacticScopeCreator(
AbstractCompiler compiler, RedeclarationHandler redeclarationHandler) {
SyntacticScopeCreator(AbstractCompiler compiler, RedeclarationHandler redeclarationHandler) {
this(compiler, redeclarationHandler, new DefaultScopeFactory());
}

Es6SyntacticScopeCreator(
AbstractCompiler compiler, RedeclarationHandler redeclarationHandler,
SyntacticScopeCreator(
AbstractCompiler compiler,
RedeclarationHandler redeclarationHandler,
ScopeFactory scopeFactory) {
this.compiler = compiler;
this.redeclarationHandler = redeclarationHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TypedScopeCreator.java
Expand Up @@ -661,7 +661,7 @@ TypedScope createInitialScope(Node root) {
// Gather global information used in typed scope creation. Use a memoized scope creator because
// scope-building takes a nontrivial amount of time.
MemoizedScopeCreator scopeCreator =
new MemoizedScopeCreator(new Es6SyntacticScopeCreator(compiler));
new MemoizedScopeCreator(new SyntacticScopeCreator(compiler));

new NodeTraversal(compiler, new FirstOrderFunctionAnalyzer(), scopeCreator)
.traverseRoots(root.getFirstChild(), root.getLastChild());
Expand Down

0 comments on commit 224ba4b

Please sign in to comment.