Skip to content

Commit

Permalink
Allow parenthesized expressions on the top level in template-literals
Browse files Browse the repository at this point in the history
While adding a test case for arrow functions, I noticed that
parenthesized expressions at the top level, which are safe to convert to
template-literals, were not being converted. This commit adds this
special casing.

I also added a few more test cases.
  • Loading branch information
lencioni committed Mar 13, 2016
1 parent 94f1633 commit 2814a52
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
17 changes: 17 additions & 0 deletions test/template-literals-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test = '(' + foo + ')';
test = '(' + foo + ')' + bar;
test = '(' + (foo + bar) + ')';
test = '(' + (1 + 1) + ')';
test = (a + 'b');

test = `hi${foo}` + bar;

Expand All @@ -57,6 +58,8 @@ test = foo + 'hi' + bar;
test = foo + 'hi' + bar + baz;

test = { a: 'hi' + foo }; // in an object
test = { ['a' + b]: 'c' + d }; // computed properties

test = ['hi' + foo]; // in an array
test = [
foo + 'bar', // comment
Expand Down Expand Up @@ -86,6 +89,7 @@ test = 'hi' + foo.join(','); // function
test = 'hi' + foo.bar; // object member
test = foo.bar + 'hi';
test = '(' + foo.bar + ')';
test = 'hi' + foo['bar'];

test = foo + bar + 'hi'; // foo and bar could be numeric
test = 'hi' + foo + bar;
Expand All @@ -95,3 +99,16 @@ test = 'foo' + (bar ? 'bar' : '');
foo('hi' + foo);
foo(foo + 'hi');
foo(a + '\\?.*' + b);

function a(b = 'c' + d) {
return b + 'e';
}

(b = 'c' + d) => {
return b + 'e';
};

(b = 'c' + d) => b + 'e';
(b = 'c' + d) => (b + 'e');

test = a + 'b' + `c${'d' + e}`; // nested concatenation in template literals
17 changes: 17 additions & 0 deletions test/template-literals-test.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ test = `(${foo})`;
test = `(${foo})${bar}`;
test = `(${foo + bar})`;
test = `(${1 + 1})`;
test = (`${a}b`);

test = `hi${foo}${bar}`;

Expand All @@ -58,6 +59,8 @@ test = `${foo}hi${bar}`;
test = `${foo}hi${bar}${baz}`;

test = { a: `hi${foo}` }; // in an object
test = { [`a${b}`]: `c${d}` }; // computed properties

test = [`hi${foo}`]; // in an array
test = [
`${foo}bar`, // comment
Expand Down Expand Up @@ -87,6 +90,7 @@ test = `hi${foo.join(',')}`; // function
test = `hi${foo.bar}`; // object member
test = `${foo.bar}hi`;
test = `(${foo.bar})`;
test = `hi${foo['bar']}`;

test = `${foo + bar}hi`; // foo and bar could be numeric
test = `hi${foo}${bar}`;
Expand All @@ -96,3 +100,16 @@ test = `foo${bar ? 'bar' : ''}`;
foo(`hi${foo}`);
foo(`${foo}hi`);
foo(`${a}\\?.*${b}`);

function a(b = `c${d}`) {
return `${b}e`;
}

(b = `c${d}`) => {
return `${b}e`;
};

(b = `c${d}`) => `${b}e`;
(b = `c${d}`) => (`${b}e`);

test = `${a}bc${'d' + e}`; // nested concatenation in template literals
12 changes: 8 additions & 4 deletions transforms/template-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
*
* - Better handling of code like 1 + 2 + 'foo' + bar which would ideally become
* `${1 + 2}foo${bar}`.
*
* - Better handling of nested string concatenation inside template literals.
* Currently, a + `b${'c' + d}` becomes `${a}b${'c' + d}` but it would ideally
* become `${a}b${`c${d}`}`.
*/
module.exports = function templateLiterals(file, api, options) {
const j = api.jscodeshift;
const printOptions = options.printOptions || {quote: 'single'};

function extractNodes(node, comments) {
function extractNodes(node, comments, topLevel = false) {
if (comments) {
node.comments = node.comments || [];
node.comments.push(...comments);
Expand All @@ -30,7 +34,7 @@ module.exports = function templateLiterals(file, api, options) {
return [node];
}

if (node.parenthesizedExpression) {
if (!topLevel && node.parenthesizedExpression) {
return [node];
}

Expand All @@ -43,7 +47,7 @@ module.exports = function templateLiterals(file, api, options) {

return [
...extractNodes(node.left),
...extractNodes(node.right, node.comments)
...extractNodes(node.right, node.comments),
];
}

Expand Down Expand Up @@ -134,7 +138,7 @@ module.exports = function templateLiterals(file, api, options) {
}

function convertToTemplateString(p) {
const tempNodes = extractNodes(p.node);
const tempNodes = extractNodes(p.node, null, true);

if (!tempNodes.some(isStringishNode)) {
return p.node;
Expand Down

0 comments on commit 2814a52

Please sign in to comment.