Skip to content

Commit

Permalink
Force updates to Ref.node to go through owning Name
Browse files Browse the repository at this point in the history
This is a step toward enforcing that we don't have duplicate
references recorded for a Name other than get/set "twins".

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=232530657
  • Loading branch information
brad4d authored and blickly committed Feb 6, 2019
1 parent dbdbc2c commit ed79f8d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 105 deletions.
36 changes: 18 additions & 18 deletions src/com/google/javascript/jscomp/AggressiveInlineAliases.java
Expand Up @@ -248,7 +248,7 @@ private boolean rewriteAllSubclassInheritedAccesses(
Set<AstChange> newNodes = new LinkedHashSet<>();

// Use this node as a template for rewriteAliasProp.
Node superclassNameNode = superclassNameObj.getDeclaration().node;
Node superclassNameNode = superclassNameObj.getDeclaration().getNode();
if (superclassNameNode.isName()) {
superclassNameNode = superclassNameNode.cloneNode();
} else if (superclassNameNode.isGetProp()) {
Expand Down Expand Up @@ -277,7 +277,7 @@ private boolean mayBeGlobalAlias(Ref alias) {
return true;
}
// If the scope in which the alias is assigned is not global, look up the LHS of the assignment.
Node aliasParent = alias.node.getParent();
Node aliasParent = alias.getNode().getParent();
if (!aliasParent.isAssign() && !aliasParent.isName()) {
// Only handle variable assignments and initializing declarations.
return true;
Expand Down Expand Up @@ -314,7 +314,7 @@ private void inlineAliasIfPossible(Name name, Ref alias, GlobalNamespace namespa
// then the NAME must be the child of a VAR, LET, or CONST node, and we must
// be in a VAR, LET, or CONST assignment.
// Otherwise if the parent is an assign, we are in a "a = alias" case.
Node aliasParent = alias.node.getParent();
Node aliasParent = alias.getNode().getParent();
if (aliasParent.isName() || aliasParent.isAssign()) {
Node aliasLhsNode = aliasParent.isName() ? aliasParent : aliasParent.getFirstChild();
String aliasVarName = aliasLhsNode.getString();
Expand Down Expand Up @@ -453,8 +453,8 @@ private boolean tryReplacingAliasingAssignment(Ref alias, Node aliasLhsNode) {
// TODO(lharker): instead replace the entire assignment with the RHS - "alias = x" becomes "x"
return false;
}
Node aliasParent = alias.node.getParent();
aliasParent.replaceChild(alias.node, IR.nullNode());
Node aliasParent = alias.getNode().getParent();
aliasParent.replaceChild(alias.getNode(), IR.nullNode());
alias.name.removeRef(alias);
codeChanged = true;
compiler.reportChangeToEnclosingScope(aliasParent);
Expand Down Expand Up @@ -496,7 +496,7 @@ private boolean referencesCollapsibleProperty(
* @return an AstChange representing the new node(s) added to the AST *
*/
private AstChange replaceAliasReference(Ref alias, Reference aliasRef) {
Node newNode = alias.node.cloneTree();
Node newNode = alias.getNode().cloneTree();
aliasRef.getParent().replaceChild(aliasRef.getNode(), newNode);
compiler.reportChangeToEnclosingScope(newNode);
return new AstChange(getRefModule(aliasRef), aliasRef.getScope(), newNode);
Expand All @@ -515,7 +515,7 @@ private AstChange replaceAliasReference(Ref alias, Reference aliasRef) {
private void inlineGlobalAliasIfPossible(Name name, Ref alias, GlobalNamespace namespace) {
// Ensure that the alias is assigned to global name at that the
// declaration.
Node aliasParent = alias.node.getParent();
Node aliasParent = alias.getNode().getParent();
if (((aliasParent.isAssign() || aliasParent.isName())
&& NodeUtil.isExecutedExactlyOnce(aliasParent))
// We special-case for constructors here, to inline constructor aliases
Expand Down Expand Up @@ -552,7 +552,7 @@ private void inlineGlobalAliasIfPossible(Name name, Ref alias, GlobalNamespace n

// Rewrite all references to the aliasing name, except for the initialization
rewriteAliasReferences(aliasingName, alias, newNodes);
rewriteAliasProps(aliasingName, alias.node, 0, newNodes);
rewriteAliasProps(aliasingName, alias.getNode(), 0, newNodes);

if (aliasInlinability.shouldRemoveDeclaration()) {
// Rewrite the initialization of the alias, unless this is an unsafe alias inline
Expand All @@ -566,14 +566,14 @@ private void inlineGlobalAliasIfPossible(Name name, Ref alias, GlobalNamespace n
// a.b = aliased.name
checkState(aliasParent.isAssign(), aliasParent);
Node aliasGrandparent = aliasParent.getParent();
aliasParent.replaceWith(alias.node.detach());
aliasParent.replaceWith(alias.getNode().detach());
aliasingName.removeRef(aliasDeclaration);
aliasingName.removeRef(aliasDeclaration.getTwin());
newNodes.add(new AstChange(alias.module, alias.scope, alias.node));
newNodes.add(new AstChange(alias.module, alias.scope, alias.getNode()));
compiler.reportChangeToEnclosingScope(aliasGrandparent);
} else {
// just set the original alias to null.
aliasParent.replaceChild(alias.node, IR.nullNode());
aliasParent.replaceChild(alias.getNode(), IR.nullNode());
compiler.reportChangeToEnclosingScope(aliasParent);
}
codeChanged = true;
Expand Down Expand Up @@ -608,15 +608,15 @@ private void rewriteAliasReferences(Name aliasingName, Ref aliasingRef, Set<AstC
checkState(ref.type == Type.ALIASING_GET, ref);
break;
}
if (ref.node.isStringKey()) {
if (ref.getNode().isStringKey()) {
// e.g. `y` in `const {y} = x;`
DestructuringGlobalNameExtractor.reassignDestructringLvalue(
ref.node, aliasingRef.node.cloneTree(), newNodes, ref, compiler);
ref.getNode(), aliasingRef.getNode().cloneTree(), newNodes, ref, compiler);
} else {
// e.g. `x.y`
checkState(ref.node.isGetProp() || ref.node.isName());
Node newNode = aliasingRef.node.cloneTree();
Node node = ref.node;
checkState(ref.getNode().isGetProp() || ref.getNode().isName());
Node newNode = aliasingRef.getNode().cloneTree();
Node node = ref.getNode();
node.replaceWith(newNode);
compiler.reportChangeToEnclosingScope(newNode);
newNodes.add(new AstChange(ref.module, ref.scope, newNode));
Expand Down Expand Up @@ -678,7 +678,7 @@ private void rewriteAliasProp(Node value, int depth, Set<AstChange> newNodes, Na
rewriteAliasProps(prop, value, depth + 1, newNodes);
List<Ref> refs = new ArrayList<>(prop.getRefs());
for (Ref ref : refs) {
Node target = ref.node;
Node target = ref.getNode();
for (int i = 0; i <= depth; i++) {
if (target.isGetProp()) {
target = target.getFirstChild();
Expand Down Expand Up @@ -714,7 +714,7 @@ private void rewriteAliasProp(Node value, int depth, Set<AstChange> newNodes, Na
compiler.reportChangeToEnclosingScope(newValue);
prop.removeRef(ref);
// Rescan the expression root.
newNodes.add(new AstChange(ref.module, ref.scope, ref.node));
newNodes.add(new AstChange(ref.module, ref.scope, ref.getNode()));
codeChanged = true;
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/com/google/javascript/jscomp/CheckGlobalNames.java
Expand Up @@ -185,12 +185,13 @@ private void validateName(Name name, boolean isDefined) {
? name.getFullName() + ".prototype"
: name.getFullName();
compiler.report(
JSError.make(ref.node,
JSError.make(
ref.getNode(),
NAME_DEFINED_LATE_WARNING,
refName,
owner.getFullName(),
owner.getDeclaration().getSourceFile().getName(),
String.valueOf(owner.getDeclaration().node.getLineno())));
String.valueOf(owner.getDeclaration().getNode().getLineno())));
}
}
}
Expand All @@ -204,9 +205,9 @@ private static boolean isTypedef(Name name) {
}
for (Ref ref : name.getRefs()) {
// If this is an annotated EXPR-GET, don't do anything.
Node parent = ref.node.getParent();
Node parent = ref.getNode().getParent();
if (parent.isExprResult()) {
JSDocInfo info = ref.node.getJSDocInfo();
JSDocInfo info = ref.getNode().getJSDocInfo();
if (info != null && info.hasTypedefType()) {
return true;
}
Expand All @@ -217,10 +218,12 @@ private static boolean isTypedef(Name name) {

private void reportBadModuleReference(Name name, Ref ref) {
compiler.report(
JSError.make(ref.node, STRICT_MODULE_DEP_QNAME,
ref.getModule().getName(),
name.getDeclaration().getModule().getName(),
name.getFullName()));
JSError.make(
ref.getNode(),
STRICT_MODULE_DEP_QNAME,
ref.getModule().getName(),
name.getDeclaration().getModule().getName(),
name.getFullName()));
}

private void reportRefToUndefinedName(Name name, Ref ref) {
Expand All @@ -230,9 +233,7 @@ private void reportRefToUndefinedName(Name name, Ref ref) {
name = name.getParent();
}

compiler.report(
JSError.make(ref.node, level,
UNDEFINED_NAME_WARNING, name.getFullName()));
compiler.report(JSError.make(ref.getNode(), level, UNDEFINED_NAME_WARNING, name.getFullName()));
}

/**
Expand Down

0 comments on commit ed79f8d

Please sign in to comment.