Skip to content

Commit

Permalink
Handle import * in MakeDeclaredNamesUnique.
Browse files Browse the repository at this point in the history
We should treat an IMPORT_STAR node like a name node here. Also updates the unit tests to better test renaming exported and imported names.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=172122689
  • Loading branch information
lauraharker authored and Tyler Breisacher committed Oct 13, 2017
1 parent 865a02b commit 240f04b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void exitScope(NodeTraversal t) {
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getToken()) {
case NAME:
case IMPORT_STAR:
visitName(t, n, parent);
break;

Expand Down Expand Up @@ -309,7 +310,7 @@ void handleScopeVar(Var v) {
referencedNames.add(newName);
List<Node> references = nameMap.get(name);
for (Node n : references) {
checkState(n.isName(), n);
checkState(n.isName() || n.isImportStar(), n);
n.setString(newName);
if (markChanges) {
compiler.reportChangeToEnclosingScope(n);
Expand Down Expand Up @@ -357,7 +358,7 @@ public void visit(NodeTraversal t, Node node, Node parent) {
return;
}

if (NodeUtil.isReferenceName(node)) {
if (NodeUtil.isReferenceName(node) || node.isImportStar()) {
String name = node.getString();
// Add all referenced names to the set so it is possible to check for
// conflicts.
Expand Down
26 changes: 24 additions & 2 deletions test/com/google/javascript/jscomp/MakeDeclaredNamesUniqueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ private void testWithInversion(String original, String expected) {
invert = false;
}

private void testWithInversion(String[] original, String[] expected) {
invert = false;
test(original, expected);
invert = true;
test(expected, original);
invert = false;
}

private void testSameWithInversion(String externs, String original) {
invert = false;
testSame(externs, original);
Expand Down Expand Up @@ -936,7 +944,21 @@ public void testExportedOrImportedNamesAreUntouched() {
// The eventual desired behavior is that none of the 'a's in the following test cases
// are renamed to a$jscomp$1. Rewrite this test after that behavior is implemented.
this.useDefaultRenamer = true;
testSame("var a; export {a as a};");
testSame("var a; import {a as a} from './bar.js'");
test(
new String[] {"var a;", "let a; export {a as a};"},
new String[] {"var a;", "let a$jscomp$1; export {a$jscomp$1 as a};"});

test(
new String[] {"var a;", "import {a as a} from './foo.js'; let b = a;"},
new String[] {"var a;", "import {a as a$jscomp$1} from './foo.js'; let b = a$jscomp$1;"});
}

public void testImportStarWithInversion() {
this.useDefaultRenamer = true;
testWithInversion(
new String[] {"let a = 5;", "import * as a from './a.js'; const TAU = 2 * a.PI;"},
new String[] {
"let a = 5;", "import * as a$jscomp$1 from './a.js'; const TAU = 2 * a$jscomp$1.PI"
});
}
}
16 changes: 16 additions & 0 deletions test/com/google/javascript/jscomp/NormalizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,22 @@ public void testMakeLocalNamesUnique() {
// Verify local masking extern made unique.
test("function f() {var window}",
"function f() {var window$jscomp$1}");

// Verify import * as <alias> is renamed.
test(
new String[] {"let a = 5;", "import * as a from './a.js'; const TAU = 2 * a.PI;"},
new String[] {
"let a = 5;", "import * as a$jscomp$1 from './a.js'; const TAU = 2 * a$jscomp$1.PI"
});

// Verify exported and imported names are untouched.
test(
new String[] {"var a;", "let a; export {a as a};"},
new String[] {"var a;", "let a$jscomp$1; export {a$jscomp$1 as a};"});

test(
new String[] {"var a;", "import {a as a} from './foo.js'; let b = a;"},
new String[] {"var a;", "import {a as a$jscomp$1} from './foo.js'; let b = a$jscomp$1;"});
}

public void testMakeParamNamesUnique() {
Expand Down

0 comments on commit 240f04b

Please sign in to comment.