Skip to content

Commit

Permalink
Actually inline template literal interpolation in the Inline Variable…
Browse files Browse the repository at this point in the history
…s pass.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160007564
  • Loading branch information
lillymliu authored and blickly committed Jun 23, 2017
1 parent 06acce7 commit aa96d83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
14 changes: 1 addition & 13 deletions src/com/google/javascript/jscomp/InlineVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,6 @@ private boolean canInline(
}
}

if (isRefTemplateLiteral(reference)) {
return false;
}

return canMoveAggressively(value) ||
canMoveModerately(initialization, reference);
}
Expand Down Expand Up @@ -729,19 +725,11 @@ private boolean isImmutableAndWellDefinedVariable(Var v,

for (int i = startingReadRef; i < refSet.size(); i++) {
Reference ref = refSet.get(i);
if (!isValidReference(ref) || isRefTemplateLiteral(ref)) {
if (!isValidReference(ref)) {
return false;
}
}
return true;
}

private boolean isRefTemplateLiteral(Reference ref) {
if (ref.getParent().isTemplateLitSub()) {
return true;
}
return false;
}

}
}
26 changes: 19 additions & 7 deletions test/com/google/javascript/jscomp/InlineVariablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1411,12 +1411,14 @@ public void testForOf() {
}

public void testTemplateStrings() {
testSame(" var name = 'Foo'; `Hello ${name}`");
test(" var name = 'Foo'; `Hello ${name}`",
"`Hello ${'Foo'}`");

test(" var name = 'Foo'; var foo = name; `Hello ${foo}`",
" var foo = 'Foo'; `Hello ${foo}`");
" `Hello ${'Foo'}`");

testSame(" var age = 3; `Age: ${age}`");
test(" var age = 3; `Age: ${age}`",
"`Age: ${3}`");
}

public void testTaggedTemplateLiterals() {
Expand All @@ -1434,17 +1436,16 @@ public void testTaggedTemplateLiterals() {
"var output = myTag`My name is ${name} ${3}`;"
),
LINE_JOINER.join(
"var name = 'Foo';",
"var output = function myTag(strings, nameExp, numExp) {",
" var modStr;",
" if (numExp > 2) {",
" modStr = nameExp + 'Bar'",
" } else { ",
" modStr = nameExp + 'BarBar'",
" }",
"}`My name is ${name} ${3}`;"));
"}`My name is ${'Foo'} ${3}`;"));

testSame(
test(
LINE_JOINER.join(
"var name = 'Foo';",
"function myTag(strings, nameExp, numExp) {",
Expand All @@ -1456,7 +1457,18 @@ public void testTaggedTemplateLiterals() {
" }",
"}",
"var output = myTag`My name is ${name} ${3}`;",
"output = myTag`My name is ${name} ${2}`;"));
"output = myTag`My name is ${name} ${2}`;"),
LINE_JOINER.join(
"function myTag(strings, nameExp, numExp) {",
" var modStr;",
" if (numExp > 2) {",
" modStr = nameExp + 'Bar'",
" } else { ",
" modStr = nameExp + 'BarBar'",
" }",
"}",
"var output = myTag`My name is ${'Foo'} ${3}`;",
"output = myTag`My name is ${'Foo'} ${2}`;"));
}

public void testDestructuring() {
Expand Down

0 comments on commit aa96d83

Please sign in to comment.