Skip to content

Commit

Permalink
Fix support of ES6 shorthand object literals in ProcessCommonJSModules
Browse files Browse the repository at this point in the history
We need to rewrite {a} as {a: a$$module$foo}.

From external contributor @Dominator008

Fixes #1592
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=116305550
  • Loading branch information
tbreisacher authored and blickly committed Mar 4, 2016
1 parent a9d1078 commit 1376660
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/com/google/javascript/jscomp/ProcessCommonJSModules.java
Expand Up @@ -591,9 +591,11 @@ public void visit(NodeTraversal t, Node n, Node parent) {
}
}

if (n.isName()) {
boolean isShorthandObjLitKey = n.isStringKey() && !n.hasChildren();
if (n.isName() || isShorthandObjLitKey) {
String name = n.getString();
if (suffix.equals(name)) {
// TODO(moz): Investigate whether we need to return early in this unlikely situation.
return;
}

Expand All @@ -609,8 +611,14 @@ public void visit(NodeTraversal t, Node n, Node parent) {

Var var = t.getScope().getVar(name);
if (var != null && var.isGlobal()) {
n.setString(name + "$$" + suffix);
n.setOriginalName(name);
String newName = name + "$$" + suffix;
if (isShorthandObjLitKey) {
// Change {a} to {a: a$$module$foo}
n.addChildToBack(IR.name(newName).useSourceInfoIfMissingFrom(n));
} else {
n.setString(newName);
n.setOriginalName(name);
}
}
}
}
Expand Down
Expand Up @@ -257,7 +257,7 @@ public void testEs6ObjectShorthand() {
"function foo$$module$test() {}",
"/** @const */ module$test = {",
" /** @const */ prop: 'value',",
" /** @const */ foo",
" /** @const */ foo: foo$$module$test",
"};"));

testModules(
Expand All @@ -276,5 +276,25 @@ public void testEs6ObjectShorthand() {
" console.log('bar');",
" }",
"};"));

testModules(
LINE_JOINER.join(
"var a = require('other');",
"module.exports = {a: a};"),
LINE_JOINER.join(
"goog.provide('module$test');",
"goog.require('module$other');",
"var a$$module$test = module$other;",
"/** @const */ module$test = { /** @const */ a: a$$module$test };"));

testModules(
LINE_JOINER.join(
"var a = require('other');",
"module.exports = {a};"),
LINE_JOINER.join(
"goog.provide('module$test');",
"goog.require('module$other');",
"var a$$module$test = module$other;",
"/** @const */ module$test = { /** @const */ a: a$$module$test };"));
}
}

0 comments on commit 1376660

Please sign in to comment.