Skip to content

Commit

Permalink
fix: comment printing within expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed May 9, 2023
1 parent 7a21d27 commit ac2a1ef
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<custom-tag id="hello"/>
<custom-tag id=(
"hello" // this comment should not be removed removed
)/>
<!-- <custom-tag
id="hello" // this comment is currently not supported
/> -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<custom-tag id="hello"/>
<custom-tag id=(
"hello" // this comment should not be removed removed
)/>
<!-- <custom-tag
id="hello" // this comment is currently not supported
/> -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<custom-tag id="hello"/>
<custom-tag id=(
"hello" // this comment should not be removed removed
)/>
<!-- <custom-tag
id="hello" // this comment is currently not supported
/> -->
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
)/>
<div data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)/>

<div
Expand All @@ -26,8 +26,8 @@
)
data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)
/>
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ div class=(
)
div data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)

div [
Expand All @@ -26,8 +26,8 @@ div [
)
data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
)/>
<div data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)/>

<div
Expand All @@ -26,8 +26,8 @@
)
data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)
/>
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
)/>
<div data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)/>

<div
Expand All @@ -26,8 +26,8 @@
)
data-current-text=(
current &&
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
current
.replace("{page_number}", "{item_number}")
.replace("{current_page}", "{item_number}")
)
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {
// Comments in a class
aFunction() {
// Test comment
}
}

<div data-test=(
// This was important
"test"
)/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {
// Comments in a class
aFunction() {
// Test comment
}
}

div data-test=(
// This was important
"test"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {
// Comments in a class
aFunction() {
// Test comment
}
}

<div data-test=(
// This was important
"test"
)/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {
// Comments in a class
aFunction() {
// Test comment
}
}

<div data-test=(
// This was important
"test"
)/>
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/attr-with-comment-short.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<custom-tag id=(
"hello" // this comment is removed, it probably should not be
"hello" // this comment should not be removed removed
)></custom-tag>

<!-- <custom-tag
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/expression-comments.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {
// Comments in a class
aFunction() {
// Test comment
}
}

<div data-test=(
// This was important
"test"
)/>
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ const { builders: b, utils } = doc;
const identity = <T>(val: T) => val;
const embeddedPlaceholderReg = /__EMBEDDED_PLACEHOLDER_(\d+)__/g;
const expressionParser: CustomParser = (code, parsers, options) => {
const ast = parsers["babel-ts"](`(${code});`, options);
return { ...ast, program: ast.program.body[0].expression };
const ast = parsers["babel-ts"](code, options);
const { tokens, comments, range } = ast;
const node = ast.program.body[0].expression;
return {
type: "JsExpressionRoot",
tokens,
comments,
node,
range,
};
};

export const languages: SupportLanguage[] = [
Expand Down Expand Up @@ -693,7 +701,7 @@ export const printers: Record<string, Printer<Node>> = {
}
case "params": {
return tryPrintEmbed(
`(${node.code})=>_`,
toExpression(`(${node.code})=>_`),
expressionParser,
(doc: any) => {
const { contents } = doc.contents[0];
Expand Down Expand Up @@ -722,7 +730,7 @@ export const printers: Record<string, Printer<Node>> = {

case "MarkoClass":
return (toDoc as any)(
`class ${getOriginalCodeForNode(opts, node.body)}`,
toExpression(`class ${getOriginalCodeForNode(opts, node.body)}`),
{ parser: expressionParser },
{ stripTrailingHardline: true }
);
Expand All @@ -742,7 +750,7 @@ export const printers: Record<string, Printer<Node>> = {
);
} else {
return tryPrintEmbed(
getOriginalCodeForNode(opts, node),
toExpression(getOriginalCodeForNode(opts, node)),
expressionParser
);
}
Expand Down Expand Up @@ -802,3 +810,7 @@ function replaceEmbeddedPlaceholders(doc: Doc, placeholders: Doc[]) {
return cur;
});
}

function toExpression(code: string) {
return `(${code}\n)`;
}
44 changes: 24 additions & 20 deletions src/utils/get-original-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import babelGenerator from "@babel/generator";
const generate = (babelGenerator as any).default || babelGenerator;

export function getOriginalCodeForNode(opts: ParserOptions<Node>, node: Node) {
const literal = literalToString(node);
if (literal !== undefined) return literal;

const loc = node.loc;
if (!loc) {
return generate(node as any, {
Expand All @@ -18,10 +15,30 @@ export function getOriginalCodeForNode(opts: ParserOptions<Node>, node: Node) {
}).code;
}

return opts.originalText.slice(
locToPos(loc.start, opts),
locToPos(loc.end, opts)
);
let start = loc.start;
if (node.leadingComments?.length) {
const commentStart = node.leadingComments[0].loc.start;
if (
commentStart.line < start.line ||
(commentStart.line === start.line && commentStart.column < start.column)
) {
start = commentStart;
}
}

let end = loc.end;
if (node.trailingComments?.length) {
const commentEnd =
node.trailingComments[node.trailingComments.length - 1].loc.end;
if (
commentEnd.line > end.line ||
(commentEnd.line === end.line && commentEnd.column > end.column)
) {
end = commentEnd;
}
}

return opts.originalText.slice(locToPos(start, opts), locToPos(end, opts));
}

export function getOriginalCodeForList(
Expand All @@ -31,16 +48,3 @@ export function getOriginalCodeForList(
) {
return list.map((node) => getOriginalCodeForNode(opts, node)).join(sep);
}

function literalToString(node: Node) {
switch (node.type) {
case "StringLiteral":
return `"${node.value.replace(/(["\\])/g, "\\$1")}"`;
case "NumericLiteral":
return node.value.toString();
case "BooleanLiteral":
return node.value ? "true" : "false";
case "NullLiteral":
return "null";
}
}
20 changes: 11 additions & 9 deletions src/utils/with-parens-if-needed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export default function withParensIfNeeded(
getValDoc: () => Doc
) {
if (
!enclosedNodeTypeReg.test(node.type) &&
outerCodeMatches(
doc.printer.printDocToString(getValDoc(), {
...opts,
printWidth: 0,
}).formatted,
/\s|>/y,
opts.markoAttrParen
)
(node as any).leadingComments?.length ||
(node as any).trailingComments?.length ||
(!enclosedNodeTypeReg.test(node.type) &&
outerCodeMatches(
doc.printer.printDocToString(getValDoc(), {
...opts,
printWidth: 0,
}).formatted,
/\s|>/y,
opts.markoAttrParen
))
) {
return ["(", b.indent([b.softline, getValDoc()]), b.softline, ")"];
}
Expand Down

0 comments on commit ac2a1ef

Please sign in to comment.