Skip to content

Commit

Permalink
fixed files form Closure #110
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Mar 7, 2017
1 parent cd58693 commit bca53f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ private void findAliases(NodeTraversal t) {
Node n = v.getNode();
Node parent = n.getParent();
boolean isVar = parent.isVar();
boolean isFunctionDecl = NodeUtil.isFunctionDeclaration(parent);
if (isVar && n.getFirstChild() != null && n.getFirstChild().isQualifiedName()) {
recordAlias(v);
} else if (v.isBleedingFunction()) {
Expand All @@ -363,12 +364,13 @@ private void findAliases(NodeTraversal t) {
} else if (parent.getType() == Token.LP) {
// Parameters of the scope function also get a BAD_PARAMETERS
// error.
} else if (isVar) {
} else if (isVar || isFunctionDecl) {
boolean isHoisted = NodeUtil.isHoistedFunctionDeclaration(parent);
Node grandparent = parent.getParent();
Node value = n.hasChildren() ?
v.getInitialValue().detachFromParent() :
Node value = v.getInitialValue() != null ?
v.getInitialValue() :
null;
Node varNode = parent;
Node varNode = null;

String name = n.getString();
int nameCount = scopedAliasNames.count(name);
Expand All @@ -380,7 +382,9 @@ private void findAliases(NodeTraversal t) {

// First, we need to free up the function expression (EXPR)
// to be used in another expression.
if (isFunctionDecl) {
// Replace "function NAME() { ... }" with "var NAME;".
Node existingName = v.getNameNode();

// We can't keep the local name on the function expression,
// because IE is buggy and will leak the name into the global
Expand All @@ -389,9 +393,19 @@ private void findAliases(NodeTraversal t) {
//
// This will only cause problems if this is a hoisted, recursive
// function, and the programmer is using the hoisting.
Node newName = IR.name("").useSourceInfoFrom(existingName);
value.replaceChild(existingName, newName);

varNode = IR.var(existingName).useSourceInfoFrom(existingName);
grandparent.replaceChild(parent, varNode);
} else {
if (value != null) {
// If this is a VAR, we can just detach the expression and
// the tree will still be valid.
value.detachFromParent();
}
varNode = parent;
}

// Add $jscomp.scope.name = EXPR;
// Make sure we copy over all the jsdoc and debug info.
Expand All @@ -405,7 +419,11 @@ private void findAliases(NodeTraversal t) {
NodeUtil.setDebugInformation(
newDecl.getFirstChild().getFirstChild(), n, name);

if (isHoisted) {
grandparent.addChildToFront(newDecl);
} else {
grandparent.addChildBefore(newDecl, varNode);
}
}

// Rewrite "var name = EXPR;" to "var name = $jscomp.scope.name;"
Expand Down
3 changes: 3 additions & 0 deletions projects/Closure/110/com/google/javascript/rhino/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ public Node getChildBefore(Node child) {
return null;
}
Node n = first;
if (n == null) {
throw new RuntimeException("node is not a child");
}

while (n.next != child) {
n = n.next;
Expand Down

0 comments on commit bca53f4

Please sign in to comment.