Skip to content

Commit

Permalink
Fix some whitespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Aug 4, 2018
1 parent 06f50ad commit f7ffd41
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
4 changes: 3 additions & 1 deletion src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ function printComments(comments, options) {
const parts = [];
comments.forEach((comment, index, comments) => {
comment.printed = true;
if (index > 0) {
parts.push(hardline);
}
parts.push(comment.value);
parts.push(hardline);
if (
isNextLineEmpty(options.originalText, comment, options) &&
comments.length > index + 1
Expand Down
60 changes: 32 additions & 28 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,40 @@ function parse(text) {
});
};

const [firstLonelyTokens] = lonelyCommentBlocks;
if (
firstLonelyTokens &&
ast.children.length > 0 &&
ast.children[0].loc.start.line >=
firstLonelyTokens[firstLonelyTokens.length - 1][2]
) {
pushTokens(firstLonelyTokens, 0, astIndices);
}

const lastLonelyTokens =
lonelyCommentBlocks[lonelyCommentBlocks.length - 1];
if (
lastLonelyTokens &&
ast.children.length > 0 &&
ast.children[ast.children.length - 1].loc.end.line <
lastLonelyTokens[lastLonelyTokens.length - 1][2]
) {
pushTokens(lastLonelyTokens, ast.children.length, astIndices);
}
if (!ast.children.length) {
lonelyCommentBlocks.forEach(tokens => pushTokens(tokens, 0, astIndices));
} else {
const [firstLonelyTokens] = lonelyCommentBlocks;
if (
firstLonelyTokens &&
ast.children.length > 0 &&
ast.children[0].loc.start.line >=
firstLonelyTokens[firstLonelyTokens.length - 1][2]
) {
pushTokens(firstLonelyTokens, 0, astIndices);
}

astIndices = ast.children.reduce((acc, child, index, children) => {
const fittingTokens = lonelyCommentBlocks.find(tokens =>
inBetweenLines(tokens[0][2], child, children[index + 1])
);
if (fittingTokens) {
pushTokens(fittingTokens, index + 1, acc);
const lastLonelyTokens =
lonelyCommentBlocks[lonelyCommentBlocks.length - 1];
if (
lastLonelyTokens &&
ast.children.length > 0 &&
ast.children[ast.children.length - 1].loc.end.line <=
lastLonelyTokens[lastLonelyTokens.length - 1][2]
) {
pushTokens(lastLonelyTokens, ast.children.length, astIndices);
}
return acc;
}, astIndices);

astIndices = ast.children.reduce((acc, child, index, children) => {
const fittingTokens = lonelyCommentBlocks.find(tokens =>
inBetweenLines(tokens[0][2], child, children[index + 1])
);
if (fittingTokens) {
pushTokens(fittingTokens, index + 1, acc);
}
return acc;
}, astIndices);
}

astIndices.forEach(({ index, loc }, j) => {
ast.children.splice(index + j, 0, {
Expand Down
36 changes: 21 additions & 15 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1355,12 +1355,6 @@ function printLines(path, options, print, childrenAttribute = "children") {
const wrappedParts = wrapPartsIntoGroups(parts, groupIndexes);

if (node.kind === "program") {
if (!wrappedParts.length && node.comments) {
wrappedParts.push(
hardline,
comments.printComments(node.comments, options)
);
}
const originalText = node.loc.source;
const firstNestedChildNode = getFirstNestedChildNode(node);
const lastNestedChildNode = getLastNestedChildNode(node);
Expand All @@ -1378,20 +1372,26 @@ function printLines(path, options, print, childrenAttribute = "children") {
const between = originalText.trim().match(/^<\?(php|=)(\s+)?\S/);

if (between && between[2]) {
afterOpenTag = between[2].includes("\n")
? concat([
hardline,
between[2].split("\n").length > 2 ? hardline : ""
])
: " ";
afterOpenTag =
between[2].includes("\n") &&
firstNestedChildNode.kind !== "lonelyComment"
? concat([
hardline,
between[2].split("\n").length > 2 ? hardline : ""
])
: " ";
}
}

if (hasEndTag) {
const between = originalText.trim().match(/\S(\s*)?\?>$/);

beforeCloseTag =
between && between[1] && between[1].includes("\n") ? hardline : " ";
lastNestedChildNode.kind === "lonelyComment"
? ""
: between && between[1] && between[1].includes("\n")
? hardline
: " ";
}

return concat([
Expand Down Expand Up @@ -2466,8 +2466,14 @@ function printNode(path, options, print) {
]);
case "label":
return concat([node.name, ":"]);
case "lonelyComment":
return comments.printComments(node.comments, options);
case "lonelyComment": {
const isMultiline = node.loc.start.line < node.loc.end.line;
return concat([
isMultiline ? hardline : "",
comments.printComments(node.comments, options),
isMultiline ? hardline : ""
]);
}
case "error":
default:
return `Have not implemented kind ${node.kind} yet.`;
Expand Down
24 changes: 9 additions & 15 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,11 @@ exports[`blank_lines.php 1`] = `
// Other comment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
// The first line of a comment
<?php // The first line of a comment
// The second line of a comment
// A third line which has a blank line before it
// Other comment
`;

exports[`break.php 1`] = `
Expand Down Expand Up @@ -1243,8 +1239,8 @@ exports[`inline3.php 1`] = `
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<h1>Test</h1>
<?php // test
<?php
// test
?>
`;
Expand Down Expand Up @@ -1286,7 +1282,8 @@ exports[`inline5.php 1`] = `
<h1>Test</h1>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<h1>Test</h1>
<?php // test
<?php
// test
?>
<h1>Test</h1>
Expand All @@ -1304,7 +1301,8 @@ exports[`inline6.php 1`] = `
<h1>Test</h1>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<h1>Test</h1>
<?php /**
<?php
/**
* test
*/
?>
Expand All @@ -1323,8 +1321,7 @@ exports[`inline8.php 1`] = `
<?php //test ?>
<?php echo 1; ?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php //test
?>
<?php //test ?>
<?php echo 1; ?>
`;
Expand All @@ -1334,8 +1331,7 @@ exports[`inline9.php 1`] = `
<?php echo 1; ?>
<?php echo 2; ?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php //test
?>
<?php //test ?>
<?php echo 1; ?>
<?php echo 2; ?>
Expand Down Expand Up @@ -1446,15 +1442,13 @@ exports[`no_code.php 1`] = `
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
/**
* Product Loop Start
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.0.0
*/
?>
`;
Expand Down

0 comments on commit f7ffd41

Please sign in to comment.