Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit vars after splitting when processing CommonJS Modules #2596

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/com/google/javascript/jscomp/ProcessCommonJSModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,28 @@ public void visit(NodeTraversal t, Node n, Node parent) {
}
break;

case VAR:
case LET:
case CONST:
// Multiple declarations need split apart so that they can be refactored into
// property assignments or removed altogether.
if (n.hasMoreThanOneChild()) {
List<Node> vars = splitMultipleDeclarations(n);
t.reportCodeChange();
for (Node var : vars) {
visit(t, var.getFirstChild(), var);
}
}
break;

case NAME:
{
// If this is a name declaration with multiple names, it will be split apart when
// the parent is visited and then revisit the children.
if (NodeUtil.isNameDeclaration(n.getParent()) && n.getParent().hasMoreThanOneChild()) {
break;
}

String qName = n.getQualifiedName();
if (qName == null) {
break;
Expand Down Expand Up @@ -1071,13 +1091,6 @@ private void updateNameReference(
case VAR:
case LET:
case CONST:
// Multiple declaration - needs split apart.
if (parent.getChildCount() > 1) {
splitMultipleDeclarations(parent);
parent = nameRef.getParent();
newNameDeclaration = t.getScope().getVar(newName);
}

if (newNameIsQualified) {
// Refactor a var declaration to a getprop assignment
Node getProp = NodeUtil.newQName(compiler, newName, nameRef, originalName);
Expand Down Expand Up @@ -1407,13 +1420,17 @@ private void fixTypeNode(NodeTraversal t, Node typeNode) {
}
}

private void splitMultipleDeclarations(Node var) {
private List<Node> splitMultipleDeclarations(Node var) {
checkState(NodeUtil.isNameDeclaration(var));
List<Node> vars = new ArrayList<>();
while (var.getSecondChild() != null) {
Node newVar = new Node(var.getToken(), var.removeFirstChild());
newVar.useSourceInfoFrom(var);
var.getParent().addChildBefore(newVar, var);
vars.add(newVar);
}
vars.add(var);
return vars;
}
}
}
21 changes: 21 additions & 0 deletions test/com/google/javascript/jscomp/ProcessCommonJSModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,25 @@ public void testWebpackAmdPattern() {
" .apply(module$test,__WEBPACK_AMD_DEFINE_ARRAY__$$module$test),",
" module$test!==undefined && module$test)"));
}

public void testIssue2593() {
testModules(
"test.js",
LINE_JOINER.join(
"var first = 1,",
" second = 2,",
" third = 3,",
" fourth = 4,",
" fifth = 5;",
"",
"module.exports = {};"),
LINE_JOINER.join(
"goog.provide('module$test');",
"/** @const */ var module$test={};",
"var first$$module$test=1;",
"var second$$module$test=2;",
"var third$$module$test=3;",
"var fourth$$module$test=4;",
"var fifth$$module$test=5;"));
}
}