Skip to content

Commit

Permalink
template.expression should not parse string without interpolation as …
Browse files Browse the repository at this point in the history
…statement (#196)

If a template doesn't have any interpolation parameters (e.g., expression`function() {}`),
it should be also surrounded by () to force parsing as expression.
  • Loading branch information
jsnajdr authored and fkling committed Jun 13, 2017
1 parent b6dd1a9 commit a983666
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/template-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ while (i < 10) {
.toEqual(expected);
});

it('correctly parses expressions without any interpolation', () => {
const expected = 'function() {}';

expect(
jscodeshift(
expression`function() {}`
)
.toSource()
)
.toEqual(expected);
});

describe('explode arrays', () => {

it('explodes arrays in function definitions', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ module.exports = function withParser(parser) {
function expression(template/*, ...nodes*/) {
// wrap code in `(...)` to force evaluation as expression
template = Array.from(template);
if (template.length > 1) {
if (template.length > 0) {
template[0] = '(' + template[0];
template[template.length - 1] += ')';
} else if (template.length === 0) {
template[0] = '(' + template[0] + ')';
}

return statement.apply(
Expand Down

0 comments on commit a983666

Please sign in to comment.