Skip to content

Commit

Permalink
Fixes change tracking in more passes and reenables tests.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=153616691
  • Loading branch information
stalcup authored and dimvar committed Apr 20, 2017
1 parent 9597118 commit f0a7cb8
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 58 deletions.
7 changes: 5 additions & 2 deletions src/com/google/javascript/jscomp/AggressiveInlineAliases.java
Expand Up @@ -53,7 +53,8 @@ class AggressiveInlineAliases implements CompilerPass {
* @param depth The chain depth.
* @param newNodes Expression nodes that have been updated.
*/
private static void rewriteAliasProps(Name name, Node value, int depth, Set<AstChange> newNodes) {
private void rewriteAliasProps(
Name name, Node value, int depth, Set<AstChange> newNodes) {
if (name.props == null) {
return;
}
Expand Down Expand Up @@ -85,7 +86,9 @@ private static void rewriteAliasProps(Name name, Node value, int depth, Set<AstC
}
}
Preconditions.checkState(target.isGetProp() || target.isName());
target.replaceWith(value.cloneTree());
Node newValue = value.cloneTree();
target.replaceWith(newValue);
compiler.reportChangeToEnclosingScope(newValue);
prop.removeRef(ref);
// Rescan the expression root.
newNodes.add(new AstChange(ref.module, ref.scope, ref.node));
Expand Down
9 changes: 6 additions & 3 deletions src/com/google/javascript/jscomp/ClosureRewriteClass.java
Expand Up @@ -26,7 +26,6 @@
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -501,12 +500,16 @@ private void rewriteGoogDefineClass(Node exprRoot, final ClassDefinition cls) {
Node argList = cls.classModifier.getSecondChild();
Node arg = argList.getFirstChild();
final String argName = arg.getString();
NodeTraversal.traverseEs6(compiler, cls.classModifier.getLastChild(),
NodeTraversal.traverseEs6(
compiler,
cls.classModifier.getLastChild(),
new AbstractPostOrderCallback() {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isName() && n.getString().equals(argName)) {
parent.replaceChild(n, cls.name.cloneTree());
Node newName = cls.name.cloneTree();
parent.replaceChild(n, newName);
compiler.reportChangeToEnclosingScope(newName);
}
}
});
Expand Down
47 changes: 24 additions & 23 deletions src/com/google/javascript/jscomp/ConvertToTypedInterface.java
Expand Up @@ -258,26 +258,27 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
currentFile.markNameProcessed(n.getFirstChild().getString());
}
break;
case FUNCTION: {
if (parent.isCall()) {
Preconditions.checkState(!parent.getFirstChild().matchesQualifiedName("goog.scope"),
parent);
}
if (NodeUtil.isStatementParent(parent)) {
currentFile.markNameProcessed(n.getFirstChild().getString());
}
processFunctionParameters(n.getSecondChild());
Node body = n.getLastChild();
if (body.isNormalBlock() && body.hasChildren()) {
if (isConstructor(n)) {
currentFile.markConstructorToProcess(n);
return false;
case FUNCTION:
{
if (parent.isCall()) {
Preconditions.checkState(
!parent.getFirstChild().matchesQualifiedName("goog.scope"), parent);
}
n.getLastChild().removeChildren();
compiler.reportCodeChange();
if (NodeUtil.isStatementParent(parent)) {
currentFile.markNameProcessed(n.getFirstChild().getString());
}
processFunctionParameters(n.getSecondChild());
Node body = n.getLastChild();
if (body.isNormalBlock() && body.hasChildren()) {
if (isConstructor(n)) {
currentFile.markConstructorToProcess(n);
return false;
}
body.removeChildren();
compiler.reportChangeToEnclosingScope(body);
}
break;
}
break;
}
case EXPR_RESULT:
Node expr = n.getFirstChild();
switch (expr.getToken()) {
Expand Down Expand Up @@ -384,7 +385,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
parent.addChildBefore(initializer.detach(), body);
processName(initializer.getFirstChild(), initializer);
}
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(parent);
break;
}
case LABEL:
Expand Down Expand Up @@ -418,7 +419,7 @@ private void processFunctionParameters(Node paramList) {
Node replacement = arg.getFirstChild().detach();
arg.replaceWith(replacement);
arg = replacement;
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(replacement);
}
}
}
Expand Down Expand Up @@ -457,15 +458,15 @@ public void visit(NodeTraversal t, Node n, Node parent) {
newProtoAssignStmt.useSourceInfoIfMissingFromForTree(expr);
// TODO(blickly): Preserve the declaration order of the this properties.
insertionPoint.getParent().addChildAfter(newProtoAssignStmt, insertionPoint);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(newProtoAssignStmt);
currentFile.markNameProcessed(fullyQualifiedName);
}
}
});
final Node functionBody = function.getLastChild();
Preconditions.checkState(functionBody.isNormalBlock());
functionBody.removeChildren();
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(functionBody);
}

enum RemovalType {
Expand Down Expand Up @@ -550,12 +551,12 @@ private void processName(Node nameNode, Node statement) {
}

private void removeNode(Node n) {
compiler.reportChangeToEnclosingScope(n);
if (NodeUtil.isStatement(n)) {
n.detach();
} else {
n.replaceWith(IR.empty().srcref(n));
}
compiler.reportCodeChange();
}

private void maybeRemoveRhs(Node nameNode, Node statement, JSDocInfo jsdoc) {
Expand Down
Expand Up @@ -396,6 +396,7 @@ private void rewriteDefinition(Node node, String newMethodName) {
Node argList = functionNode.getSecondChild();
argList.addChildToFront(IR.name(self)
.useSourceInfoIfMissingFrom(functionNode));
compiler.reportChangeToEnclosingScope(argList);

// rewrite body
Node body = functionNode.getLastChild();
Expand Down
1 change: 1 addition & 0 deletions src/com/google/javascript/jscomp/Es6ConvertSuper.java
Expand Up @@ -100,6 +100,7 @@ private void addSyntheticConstructor(Node classNode) {
}
memberDef.useSourceInfoIfMissingFromForTree(classNode);
classMembers.addChildToFront(memberDef);
compiler.reportChangeToEnclosingScope(memberDef);
}

private boolean isInterface(Node classNode) {
Expand Down
Expand Up @@ -212,8 +212,8 @@ private void varify() {
private class RewriteBlockScopedFunctionDeclaration extends AbstractPostOrderCallback {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isFunction() && NormalizeStatements.maybeNormalizeFunctionDeclaration(n)) {
compiler.reportCodeChange();
if (n.isFunction()) {
NormalizeStatements.maybeNormalizeFunctionDeclaration(n, compiler);
}
}
}
Expand Down Expand Up @@ -414,6 +414,7 @@ private void transformLoopClosure() {
reference.getParent().putBooleanProp(Node.FREE_CALL, false);
}
// Change reference to GETPROP.
compiler.reportChangeToEnclosingScope(reference);
reference.replaceWith(
IR.getprop(IR.name(object.name), IR.string(var.name))
.useSourceInfoIfMissingFromForTree(reference));
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Expand Up @@ -307,7 +307,7 @@ private void visitGenerator(Node n, Node parent) {
}

parent.useSourceInfoIfMissingFromForTree(parent);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(genBlock);
}

/** Returns {@code true} if a new case node should be added */
Expand Down
20 changes: 10 additions & 10 deletions src/com/google/javascript/jscomp/Es6ToEs3Converter.java
Expand Up @@ -216,7 +216,7 @@ private void initSymbolBefore(Node n) {
Node statement = NodeUtil.getEnclosingStatement(n);
Node initSymbol = IR.exprResult(IR.call(NodeUtil.newQName(compiler, "$jscomp.initSymbol")));
statement.getParent().addChildBefore(initSymbol.useSourceInfoFromForTree(statement), statement);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(initSymbol);
}

private void visitExponentiationExpression(Node n, Node parent) {
Expand All @@ -226,7 +226,7 @@ private void visitExponentiationExpression(Node n, Node parent) {
IR.call(NodeUtil.newQName(compiler, "Math.pow"), left, right)
.useSourceInfoIfMissingFromForTree(n);
parent.replaceChild(n, mathDotPowCall);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(mathDotPowCall);
}

private void visitExponentiationAssignmentExpression(Node n, Node parent) {
Expand All @@ -235,7 +235,7 @@ private void visitExponentiationAssignmentExpression(Node n, Node parent) {
Node mathDotPowCall = IR.call(NodeUtil.newQName(compiler, "Math.pow"), left.cloneTree(), right);
Node assign = IR.assign(left, mathDotPowCall).useSourceInfoIfMissingFromForTree(n);
parent.replaceChild(n, assign);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(assign);
}

// TODO(tbreisacher): Do this for all well-known symbols.
Expand All @@ -248,7 +248,7 @@ private void visitGetprop(NodeTraversal t, Node n) {
Node statement = NodeUtil.getEnclosingStatement(n);
Node init = IR.exprResult(IR.call(NodeUtil.newQName(compiler, "$jscomp.initSymbolIterator")));
statement.getParent().addChildBefore(init.useSourceInfoFromForTree(statement), statement);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(init);
}
}

Expand All @@ -261,7 +261,7 @@ private void visitMemberFunctionDefInObjectLit(Node n, Node parent) {
Node stringKey = IR.stringKey(name, n.getFirstChild().detach());
stringKey.setJSDocInfo(n.getJSDocInfo());
parent.replaceChild(n, stringKey);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(stringKey);
}

/**
Expand All @@ -273,7 +273,7 @@ private void visitStringKey(Node n) {
Node name = IR.name(n.getString());
name.useSourceInfoIfMissingFrom(n);
n.addChildToBack(name);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(name);
}
}

Expand Down Expand Up @@ -327,7 +327,7 @@ private void visitForOf(Node node, Node parent) {
Node newFor = IR.forNode(init, cond, incr, newBody);
newFor.useSourceInfoIfMissingFromForTree(node);
parent.replaceChild(node, newFor);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(newFor);
}

private void checkClassReassignment(Node clazz) {
Expand Down Expand Up @@ -410,7 +410,7 @@ private void visitRestParam(Node restParam, Node paramList) {
functionBody.addChildAfter(IR.forNode(init, cond, incr, body)
.useSourceInfoIfMissingFromForTree(restParam), newArr);
functionBody.addChildToBack(newBlock);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(newBlock);

// For now, we are running transpilation before type-checking, so we'll
// need to make sure changes don't invalidate the JSDoc annotations.
Expand Down Expand Up @@ -487,7 +487,7 @@ private void visitArrayLitOrCallWithSpread(Node node, Node parent) {
}
result.useSourceInfoIfMissingFromForTree(node);
parent.replaceChild(node, result);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(result);
}

private void visitObject(Node obj) {
Expand Down Expand Up @@ -558,7 +558,7 @@ private void visitObjectWithComputedProperty(Node obj) {
Node var = IR.var(IR.name(objName), obj);
var.useSourceInfoIfMissingFromForTree(statement);
statement.getParent().addChildBefore(var, statement);
compiler.reportCodeChange();
compiler.reportChangeToEnclosingScope(var);
}

/**
Expand Down
Expand Up @@ -112,7 +112,11 @@ private void setTypeExpression(Node n, JSTypeExpression type) {
TypeDeclarationNode node = TypeDeclarationsIRFactory.convert(type);
if (node != null) {
n.setDeclaredTypeExpression(node);
compiler.reportChangeToEnclosingScope(n);
if (n.isFunction()) {
compiler.reportChangeToEnclosingScope(n.getFirstChild());
} else {
compiler.reportChangeToEnclosingScope(n);
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/com/google/javascript/jscomp/Normalize.java
Expand Up @@ -353,7 +353,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
break;

case FUNCTION:
if (maybeNormalizeFunctionDeclaration(n)) {
if (maybeNormalizeFunctionDeclaration(n, compiler)) {
reportCodeChange(n, "Function declaration");
}
break;
Expand Down Expand Up @@ -423,10 +423,10 @@ private void annotateConstantsByConvention(Node n, Node parent) {
*
* @see https://github.com/google/closure-compiler/pull/429
*/
static boolean maybeNormalizeFunctionDeclaration(Node n) {
static boolean maybeNormalizeFunctionDeclaration(Node n, AbstractCompiler compiler) {
Preconditions.checkState(n.isFunction(), n);
if (NodeUtil.isFunctionDeclaration(n) && !NodeUtil.isHoistedFunctionDeclaration(n)) {
rewriteFunctionDeclaration(n);
rewriteFunctionDeclaration(n, compiler);
return true;
}
return false;
Expand All @@ -448,14 +448,15 @@ static boolean maybeNormalizeFunctionDeclaration(Node n) {
* PARAM_LIST
* BLOCK
*/
private static void rewriteFunctionDeclaration(Node n) {
private static void rewriteFunctionDeclaration(Node n, AbstractCompiler compiler) {
// Prepare a spot for the function.
Node oldNameNode = n.getFirstChild();
Node fnNameNode = oldNameNode.cloneNode();
Node var = IR.var(fnNameNode).srcref(n);

// Prepare the function
oldNameNode.setString("");
compiler.reportChangeToEnclosingScope(oldNameNode);

// Move the function if it's not the child of a label node
Node parent = n.getParent();
Expand All @@ -465,6 +466,7 @@ private static void rewriteFunctionDeclaration(Node n) {
parent.removeChild(n);
parent.addChildToFront(var);
}
compiler.reportChangeToEnclosingScope(var);
fnNameNode.addChildToFront(n);
}

Expand Down
Expand Up @@ -43,7 +43,6 @@ public CompilerPass getProcessor(Compiler compiler) {
@Override
public void setUp() {
enableNormalize();
validateAstChangeMarking(false);
}

public void test_b19179602() {
Expand Down
Expand Up @@ -56,7 +56,6 @@ protected void setUp() throws Exception {
disableTypeCheck();
runTypeCheckAfterProcessing = true;
compareJsDoc = true;
validateAstChangeMarking(false);
}

@Override
Expand Down
Expand Up @@ -24,11 +24,6 @@ protected CompilerPass getProcessor(final Compiler compiler) {
return new ConvertToTypedInterface(compiler);
}

@Override
public void setUp() {
validateAstChangeMarking(false);
}

@Override
protected int getNumRepetitions() {
return 1;
Expand Down
Expand Up @@ -49,7 +49,6 @@ protected int getNumRepetitions() {
protected void setUp() throws Exception {
super.setUp();
disableTypeCheck();
validateAstChangeMarking(false);
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/com/google/javascript/jscomp/Es6InlineTypesTest.java
Expand Up @@ -29,7 +29,6 @@ public final class Es6InlineTypesTest extends CompilerTestCase {
public void setUp() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT6_TYPED);
compareJsDoc = false;
validateAstChangeMarking(false);
}

@Override
Expand Down
Expand Up @@ -32,7 +32,6 @@ public void setUp() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
runTypeCheckAfterProcessing = true;
compareJsDoc = true;
validateAstChangeMarking(false);
}

@Override
Expand Down
Expand Up @@ -29,7 +29,6 @@ public void setUp() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
runTypeCheckAfterProcessing = true;
compareJsDoc = true;
validateAstChangeMarking(false);
}

@Override
Expand Down

0 comments on commit f0a7cb8

Please sign in to comment.