Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,16 @@ function printNode(path, options, print) {
if (node.expr) {
const printedExpr = print("expr");

parts.push(" ", node.expr.comments ? indent(printedExpr) : printedExpr);
// Only comments printed around the expression (leading or
// trailing) need the extra indent. Dangling comments are printed
// by the expression itself at its own indent; wrapping those in
// indent() as well shifts them and the closing line one level
// too deep, e.g. an array holding only a comment.
const shouldIndent =
node.expr.comments &&
(hasLeadingComment(node.expr) || hasTrailingComment(node.expr));

parts.push(" ", shouldIndent ? indent(printedExpr) : printedExpr);
}

if (hasDanglingComments(node)) {
Expand Down
93 changes: 93 additions & 0 deletions tests/array/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,99 @@ $var = array(...new ArrayIterator(array("a", "b", "c")));
================================================================================
`;

exports[`empty-comment.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php

return [
// TODO: ...
];

return [
// TODO: ...
'foo' => 'bar',
];

function foo()
{
return [
// TODO: ...
];
}

=====================================output=====================================
<?php

return [
// TODO: ...
];

return [
// TODO: ...
"foo" => "bar",
];

function foo()
{
return [
// TODO: ...
];
}

================================================================================
`;

exports[`empty-comment.php 2`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "5.3"
printWidth: 80
| printWidth
=====================================input======================================
<?php

return [
// TODO: ...
];

return [
// TODO: ...
'foo' => 'bar',
];

function foo()
{
return [
// TODO: ...
];
}

=====================================output=====================================
<?php

return array(
// TODO: ...
);

return array(
// TODO: ...
"foo" => "bar",
);

function foo()
{
return array(
// TODO: ...
);
}

================================================================================
`;

exports[`single.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
17 changes: 17 additions & 0 deletions tests/array/empty-comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
// TODO: ...
];

return [
// TODO: ...
'foo' => 'bar',
];

function foo()
{
return [
// TODO: ...
];
}
Loading