Skip to content

Commit

Permalink
Support inline template for new calls (#2222)
Browse files Browse the repository at this point in the history
I merged the implementation of new and call expressions in this PR.

Fixes #2215
  • Loading branch information
vjeux committed Jun 21, 2017
1 parent 63c47c8 commit aac0d1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,21 @@ function genericPrintNoParens(path, options, print, args) {
parts.push(semi);

return concat(parts);
case "NewExpression":
case "CallExpression": {
const isNew = n.type === "NewExpression";
if (
// We want to keep require calls as a unit
(n.callee.type === "Identifier" && n.callee.name === "require") ||
(!isNew &&
n.callee.type === "Identifier" &&
n.callee.name === "require") ||
// Template literals as single arguments
(n.arguments.length === 1 &&
isTemplateOnItsOwnLine(n.arguments[0], options.originalText)) ||
// Keep test declarations on a single line
// e.g. `it('long name', () => {`
(n.callee.type === "Identifier" &&
(!isNew &&
n.callee.type === "Identifier" &&
(n.callee.name === "it" ||
n.callee.name === "test" ||
n.callee.name === "describe") &&
Expand All @@ -824,6 +829,7 @@ function genericPrintNoParens(path, options, print, args) {
n.arguments[1].params.length <= 1)
) {
return concat([
isNew ? "new " : "",
path.call(print, "callee"),
path.call(print, "typeParameters"),
concat(["(", join(", ", path.map(print, "arguments")), ")"])
Expand All @@ -832,11 +838,12 @@ function genericPrintNoParens(path, options, print, args) {

// We detect calls on member lookups and possibly print them in a
// special chain format. See `printMemberChain` for more info.
if (n.callee.type === "MemberExpression") {
if (!isNew && n.callee.type === "MemberExpression") {
return printMemberChain(path, options, print);
}

return concat([
isNew ? "new " : "",
path.call(print, "callee"),
printFunctionTypeParameters(path, options, print),
printArgumentsList(path, options, print)
Expand Down Expand Up @@ -1216,18 +1223,6 @@ function genericPrintNoParens(path, options, print, args) {
])
);
}
case "NewExpression":
parts.push(
"new ",
path.call(print, "callee"),
printFunctionTypeParameters(path, options, print)
);

if (n.arguments) {
parts.push(printArgumentsList(path, options, print));
}

return concat(parts);
case "VariableDeclaration": {
const printed = path.map(childPath => {
return print(childPath);
Expand Down
10 changes: 10 additions & 0 deletions tests/template/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ insertRule(\`*, *:before, *:after {
insertRule\`*, *:before, *:after {
box-sizing: inherit;
}\`;
new Error(formatErrorMessage\`
This a really bad error.
Which has more than one line.
\`);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
insertRule(\`*, *:before, *:after {
box-sizing: inherit;
Expand All @@ -36,6 +41,11 @@ insertRule\`*, *:before, *:after {
box-sizing: inherit;
}\`;
new Error(formatErrorMessage\`
This a really bad error.
Which has more than one line.
\`);
`;

exports[`comment.js 1`] = `
Expand Down
5 changes: 5 additions & 0 deletions tests/template/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ insertRule(`*, *:before, *:after {
insertRule`*, *:before, *:after {
box-sizing: inherit;
}`;

new Error(formatErrorMessage`
This a really bad error.
Which has more than one line.
`);

0 comments on commit aac0d1d

Please sign in to comment.