Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: printing attr method shorthands #56

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
4,479 changes: 2,478 additions & 2,001 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
"author": "Dylan Piercey <[email protected]>",
"bugs": "https://github.com/marko-js/prettier/issues",
"devDependencies": {
"@babel/generator": "^7.23.0",
"@babel/generator": "^7.25.5",
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/babel__generator": "^7.6.5",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.4",
"@types/babel__generator": "^7.6.8",
"@types/mocha": "^10.0.7",
"@types/node": "^22.5.1",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"esbuild": "^0.19.4",
"esbuild-register": "^3.5.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"fast-glob": "^3.3.1",
"fast-glob": "^3.3.2",
"fixpack": "^4.0.0",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"marko": "^5.32.0",
"mocha": "^10.2.0",
"marko": "^5.35.11",
"mocha": "^10.7.3",
"mocha-snap": "^5.0.0",
"nyc": "^15.1.0",
"prettier": "^3.0.3",
"prettier": "^3.3.3",
"semantic-release": "^22.0.5",
"typescript": "^5.2.2"
"typescript": "^5.5.4"
},
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>

<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
custom-tag() { console.log("hi"); }
custom-tag() {
console.log("hi");
}

custom-tag() { console.log("hi"); }
custom-tag() {
console.log("hi");
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>

<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>

<custom-tag() { console.log("hi"); }/>
<custom-tag() {
console.log("hi");
}/>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<effect() { console.log("Hello again"); }/>
<effect() {
console.log("Hello again");
}/>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
effect() { console.log("Hello again"); }
effect() {
console.log("Hello again");
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<effect() { console.log("Hello again"); }/>
<effect() {
console.log("Hello again");
}/>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<effect() { console.log("Hello again"); }/>
<effect() {
console.log("Hello again");
}/>
49 changes: 15 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,40 +533,7 @@ export const printers: Record<string, Printer<types.Node>> = {
t.isFunctionExpression(value) &&
!(value.id || value.async || value.generator)
) {
const methodBodyDocs: Doc[] = [];
(attrPath as any).each(
(childPath: AstPath<types.Statement>) => {
if (childPath.getNode()?.type !== "EmptyStatement") {
methodBodyDocs.push(print(childPath));
}
},
"value",
"body",
"body",
);
doc.push(
b.group([
"(",
b.indent([
b.softline,
b.join(
[",", b.line],
attrPath.map((it) => print(it), "value", "params"),
),
opts.trailingComma === "all" ? b.ifBreak(",") : "",
]),
b.softline,
")",
]),
methodBodyDocs.length
? b.group([
" {",
b.indent([b.line, b.join(b.hardline, methodBodyDocs)]),
b.line,
"}",
])
: " {}",
);
doc.push(attrPath.call(print, "value"));
} else {
doc.push(
node.bound ? ":=" : "=",
Expand Down Expand Up @@ -1033,6 +1000,20 @@ export const printers: Record<string, Printer<types.Node>> = {
},
code,
);
} else if (
parentType === "MarkoAttribute" &&
path.key === "value" &&
node.type === "FunctionExpression" &&
!(node.async || node.generator || node.id)
) {
return tryPrintEmbed(
`({_${code.replace(/^\s*function\s*/, "")}})`,
scriptParser,
(doc: any) => {
return doc[1].contents[1].contents[1].contents.slice(1);
},
code,
);
}

return tryPrintEmbed(code, expressionParser);
Expand Down
23 changes: 19 additions & 4 deletions src/utils/get-original-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ export function getOriginalCodeForNode(
opts: ParserOptions<t.Node>,
node: t.Node,
) {
const hasLeadingComments = node.leadingComments?.length;
const hasTrailingComments = node.trailingComments?.length;

if (!hasLeadingComments && !hasTrailingComments) {
switch (node.type) {
case "StringLiteral":
return JSON.stringify(node.value);
case "BooleanLiteral":
case "NumericLiteral":
return "" + node.value;
case "NullLiteral":
return "null";
}
}

const loc = node.loc;
if (!loc) {
return generate(node as any, {
Expand All @@ -19,8 +34,8 @@ export function getOriginalCodeForNode(
}

let start = loc.start;
if (node.leadingComments?.length) {
const commentStart = node.leadingComments[0].loc.start;
if (hasLeadingComments) {
const commentStart = node.leadingComments![0].loc.start;
if (
commentStart.line < start.line ||
(commentStart.line === start.line && commentStart.column < start.column)
Expand All @@ -30,9 +45,9 @@ export function getOriginalCodeForNode(
}

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