Skip to content

Commit

Permalink
BuildGlobalNamespace will not create duplicate Refs
Browse files Browse the repository at this point in the history
Previously it was up to the caller of scanNewNodes() to avoid passing
BuildGlobalNamespace nodes that might already have references.
This has led to exceptions being thrown because multiple references
were created for the same Node.

This change makes BuildGlobalNamespace and the Name class it depends on smart
enough to avoid creation of additional Refs for the same Node even when
scanNewNodes() is passed AST nodes that already have Refs.

Effectively this means that code modifying the AST can safely pass any Nodes
they think may need reconsideration to the scanNewNodes() method and rely on
BuildGlobalNamespace to do the right thing.

On the other hand, I've added several checks to restrict changing a Ref's Node.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=233448314
  • Loading branch information
brad4d authored and tjgq committed Feb 12, 2019
1 parent b0ade67 commit adc0b3b
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 141 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/AggressiveInlineAliases.java
Expand Up @@ -565,8 +565,8 @@ private void inlineGlobalAliasIfPossible(Name name, Ref alias, GlobalNamespace n
checkState(aliasParent.isAssign(), aliasParent); checkState(aliasParent.isAssign(), aliasParent);
Node aliasGrandparent = aliasParent.getParent(); Node aliasGrandparent = aliasParent.getParent();
aliasParent.replaceWith(alias.getNode().detach()); aliasParent.replaceWith(alias.getNode().detach());
aliasingName.removeRef(aliasDeclaration); // remove both of the refs
aliasingName.removeRef(aliasDeclaration.getTwin()); aliasingName.removeTwinRefs(aliasDeclaration);
newNodes.add(new AstChange(alias.module, alias.scope, alias.getNode())); newNodes.add(new AstChange(alias.module, alias.scope, alias.getNode()));
compiler.reportChangeToEnclosingScope(aliasGrandparent); compiler.reportChangeToEnclosingScope(aliasGrandparent);
} else { } else {
Expand Down

0 comments on commit adc0b3b

Please sign in to comment.