Skip to content

Commit

Permalink
Merge pull request #17 from marko-js/preserve-comment-style
Browse files Browse the repository at this point in the history
fix: preserve comment style
  • Loading branch information
DylanPiercey authored Feb 17, 2023
2 parents b14f267 + 4efb1d3 commit 1352984
Show file tree
Hide file tree
Showing 23 changed files with 4,737 additions and 11,522 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [14.x, 16.x]
node: [16.x, 18.x]
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 16.x
node-version: 18.x
cache: "npm"
- name: Install dependencies
run: npm ci
Expand Down
16,057 changes: 4,600 additions & 11,457 deletions package-lock.json

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
"author": "Dylan Piercey <[email protected]>",
"bugs": "https://github.com/marko-js/prettier/issues",
"devDependencies": {
"@babel/generator": "^7.18.9",
"@commitlint/cli": "^16.2.3",
"@commitlint/config-conventional": "^16.2.1",
"@babel/generator": "^7.20.14",
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@marko/compiler": "^5.21.4",
"@marko/translator-default": "^5.20.5",
"@marko/compiler": "^5.23.4",
"@marko/translator-default": "^5.22.4",
"@types/babel__generator": "^7.6.4",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.25",
"@types/prettier": "^2.6.0",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.14.0",
"@types/prettier": "^2.7.2",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"codecov": "^3.8.3",
"esbuild": "^0.14.38",
"esbuild-register": "^3.3.2",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"fast-glob": "^3.2.11",
"esbuild": "^0.17.8",
"esbuild-register": "^3.4.2",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"fast-glob": "^3.2.12",
"fixpack": "^4.0.0",
"husky": "^7.0.4",
"lint-staged": "^12.4.0",
"marko": "^5.20.5",
"mocha": "^9.2.2",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"marko": "^5.22.4",
"mocha": "^10.2.0",
"mocha-snap": "^4.3.0",
"nyc": "^15.1.0",
"prettier": "^2.6.2",
"semantic-release": "^19.0.2",
"typescript": "^4.6.3"
"prettier": "^2.8.4",
"semantic-release": "^20.1.0",
"typescript": "^4.9.5"
},
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div.hello>
<!-- This is a line comment-->
// This is a line comment

<span>Welcome to <b>Marko</b></span>

Foo<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
-- <!-- this is--><!-- multiple lines--><!-- of comments-->Hi!
--
// this is
// multiple lines
// of comments
Hi!
--
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
-- <!-- this is--><!-- multiple lines--><!-- of comments-->Hi!
--
// this is
// multiple lines
// of comments
Hi!
--
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
-- <!-- this is--><!-- multiple lines--><!-- of comments-->Hi!
--
// this is
// multiple lines
// of comments
Hi!
--
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
-- <!-- this is--><!-- multiple lines--><!-- of comments-->Hi!
--
// this is
// multiple lines
// of comments
Hi!
--
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div/>
<!-- this is-->
<!-- multiple lines-->
<!-- of comments-->
// this is

// multiple lines

// of comments

<div/>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
div
<!-- this is-->
<!-- multiple lines-->
<!-- of comments-->
// this is

// multiple lines

// of comments

div
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div/>
<!-- this is-->
<!-- multiple lines-->
<!-- of comments-->
// this is

// multiple lines

// of comments

<div/>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div/>
<!-- this is-->
<!-- multiple lines-->
<!-- of comments-->
// this is

// multiple lines

// of comments

<div/>
3 changes: 2 additions & 1 deletion src/__tests__/__snapshots__/comments.expected/auto.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/__snapshots__/comments.expected/concise.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/__snapshots__/comments.expected/html.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div.hello>
<!-- This is a line comment-->
// This is a line comment

<span>Welcome to <b>Marko</b></span>

Foo<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.hello
<!-- This is a line comment-->
// This is a line comment

span
-- Welcome to
b -- Marko
Expand Down
62 changes: 46 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Printer,
getFileInfo,
SupportOptions,
AstPath,
} from "prettier";
import { compileSync, types as t } from "@marko/compiler";
import {
Expand Down Expand Up @@ -224,11 +225,24 @@ export const printers: Record<string, Printer<Node>> = {
return `<!${node.value.replace(/\s+/g, " ").trim()}>`;
case "MarkoDeclaration":
return asLiteralTextContent(`<?${node.value}?>`);
case "MarkoComment":
return asLiteralTextContent(`<!--${node.value}-->`);
case "MarkoComment": {
const start = node.loc?.start;
switch (
start != null &&
opts.originalText[locToPos(start, opts) + 1]
) {
case "/":
return [asLiteralTextContent(`//${node.value}`), b.hardline];
case "*":
return asLiteralTextContent(`/*${node.value}*/`);
default:
return asLiteralTextContent(`<!--${node.value}-->`);
}
}
case "MarkoCDATA":
return asLiteralTextContent(`<![CDATA[${node.value}]]>`);
case "MarkoTag": {
const tagPath = path as AstPath<t.MarkoTag>;
const groupId = Symbol();
const doc: Doc[] = [opts.markoSyntax === "html" ? "<" : ""];
const { markoPreservingSpace } = opts;
Expand Down Expand Up @@ -266,7 +280,10 @@ export const printers: Record<string, Printer<Node>> = {
"style",
lang === ".css" ? "" : lang,
" {",
b.indent([b.line, callEmbed(print, path, embedMode, code)]),
b.indent([
b.line,
callEmbed(print, tagPath, embedMode, code),
]),
b.line,
"}",
]);
Expand All @@ -277,7 +294,7 @@ export const printers: Record<string, Printer<Node>> = {
doc.push(
b.group([
"${",
b.indent([b.softline, path.call(print, "name")]),
b.indent([b.softline, tagPath.call(print, "name")]),
b.softline,
"}",
])
Expand All @@ -289,7 +306,7 @@ export const printers: Record<string, Printer<Node>> = {
if (node.var) {
doc.push(
"/",
callEmbed(print, path, "var", getOriginalCode(opts, node.var))
callEmbed(print, tagPath, "var", getOriginalCode(opts, node.var))
);
}

Expand All @@ -299,7 +316,7 @@ export const printers: Record<string, Printer<Node>> = {
"(",
b.indent([
b.softline,
b.join([",", b.line], path.map(print, "arguments")),
b.join([",", b.line], tagPath.map(print, "arguments")),
opts.trailingComma ? b.ifBreak(",") : "",
]),
b.softline,
Expand All @@ -314,7 +331,7 @@ export const printers: Record<string, Printer<Node>> = {
"|",
callEmbed(
print,
path,
tagPath,
"params",
getOriginalCode(
opts,
Expand All @@ -330,7 +347,7 @@ export const printers: Record<string, Printer<Node>> = {
if (node.attributes.length) {
const attrsDoc: Doc[] = [];

path.each((childPath) => {
tagPath.each((childPath) => {
const childNode = childPath.getValue();

if (
Expand Down Expand Up @@ -375,7 +392,7 @@ export const printers: Record<string, Printer<Node>> = {
let textDocs = [] as Doc[];
let textOnly = true;

path.each(
tagPath.each(
(child, i) => {
const childNode = child.getValue();
const isText = isTextLike(childNode, node);
Expand Down Expand Up @@ -461,6 +478,7 @@ export const printers: Record<string, Printer<Node>> = {
return withLineIfNeeded(node, opts, b.group(doc, { id: groupId }));
}
case "MarkoAttribute": {
const attrPath = path as AstPath<t.MarkoAttribute>;
const doc: Doc[] = [];
const { value } = node;

Expand All @@ -477,7 +495,7 @@ export const printers: Record<string, Printer<Node>> = {
"(",
b.indent([
b.softline,
b.join([",", b.line], path.map(print, "arguments")),
b.join([",", b.line], attrPath.map(print, "arguments")),
opts.trailingComma ? b.ifBreak(",") : "",
]),
b.softline,
Expand All @@ -498,7 +516,7 @@ export const printers: Record<string, Printer<Node>> = {
value.params.length
? callEmbed(
print,
path,
attrPath,
"params",
getOriginalCode(
opts,
Expand All @@ -515,7 +533,7 @@ export const printers: Record<string, Printer<Node>> = {
b.line,
b.join(
b.hardline,
path.map(print, "value", "body", "body")
(attrPath as any).map(print, "value", "body", "body")
),
]),
b.line,
Expand All @@ -527,8 +545,12 @@ export const printers: Record<string, Printer<Node>> = {
node.bound ? ":=" : "=",
b.group(
enclosedNodeTypeReg.test(node.type)
? path.call(print, "value")
: withParensIfNeeded(value, opts, path.call(print, "value"))
? attrPath.call(print, "value")
: withParensIfNeeded(
value,
opts,
attrPath.call(print, "value")
)
)
);
}
Expand All @@ -538,11 +560,19 @@ export const printers: Record<string, Printer<Node>> = {
}
case "MarkoSpreadAttribute": {
return (["..."] as Doc[]).concat(
withParensIfNeeded(node.value, opts, path.call(print, "value"))
withParensIfNeeded(
node.value,
opts,
(path as AstPath<t.MarkoSpreadAttribute>).call(print, "value")
)
);
}
case "MarkoPlaceholder":
return [node.escape ? "${" : "$!{", path.call(print, "value"), "}"];
return [
node.escape ? "${" : "$!{",
(path as AstPath<t.MarkoPlaceholder>).call(print, "value"),
"}",
];
case "MarkoScriptlet":
return withLineIfNeeded(
node.body[0],
Expand Down
Loading

0 comments on commit 1352984

Please sign in to comment.