Skip to content

Commit

Permalink
fix: issue printing adjacent comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 13, 2021
1 parent 6451e1f commit 9bb5263
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- <!--this is--><!--multiple lines--><!--of comments-->Hi!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- <!--this is--><!--multiple lines--><!--of comments-->Hi!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- <!--this is--><!--multiple lines--><!--of comments-->Hi!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- <!--this is--><!--multiple lines--><!--of comments-->Hi!
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div/>
<!--this is-->
<!--multiple lines-->
<!--of comments-->
<div/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
div
<!--this is-->
<!--multiple lines-->
<!--of comments-->
div
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div/>
<!--this is-->
<!--multiple lines-->
<!--of comments-->
<div/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div/>
<!--this is-->
<!--multiple lines-->
<!--of comments-->
<div/>
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/comments-multiple-lines-as-text.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this is
// multiple lines
// of comments
-- Hi!
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div/>
// this is
// multiple lines
// of comments
<div/>
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,8 @@ export const printers: Record<string, Printer<Node>> = {
const isText = isTextLike(childNode, node);

if (isText) {
const isMarkoText = childNode.type === "MarkoText";
textDocs.push(
embedMode && isMarkoText
embedMode && childNode.type === "MarkoText"
? callEmbed(print, child, embedMode, childNode.value)
: print(child)
);
Expand Down
38 changes: 24 additions & 14 deletions src/utils/is-text-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ export default function isTextLike(
node: Node,
parent: t.MarkoTag | t.Program
): boolean {
switch (node.type) {
case "MarkoText":
case "MarkoPlaceholder":
return true;
case "MarkoComment": {
const body = parent.type === "Program" ? parent.body : parent.body.body;
const i = body.indexOf(node);
return (
i > 0 &&
i < body.length - 1 &&
(isTextLike(body[i - 1], parent) || isTextLike(body[i + 1], parent))
);
if (isText(node)) {
return true;
} else if (node.type === "MarkoComment") {
const body = parent.type === "Program" ? parent.body : parent.body.body;
const i = body.indexOf(node);

let j = i;
while (j > 0) {
const check = body[--j];
if (isText(check)) return true;
else if (check.type !== "MarkoComment") break;
}

j = i;
while (j < body.length - 1) {
const check = body[++j];
if (isText(check)) return true;
else if (check.type !== "MarkoComment") break;
}
default:
return false;
}

return false;
}

function isText(node: Node) {
return node.type === "MarkoText" || node.type === "MarkoPlaceholder";
}

0 comments on commit 9bb5263

Please sign in to comment.