From ff8e37c90acffb09da8909dc7ada1fc9d5fcffc8 Mon Sep 17 00:00:00 2001 From: LuLaValva Date: Thu, 9 Mar 2023 17:19:20 -0800 Subject: [PATCH 1/3] fix: removed function with side effects --- .../scriptlet-array.expected/auto.marko | 1 + .../scriptlet-array.expected/concise.marko | 1 + .../scriptlet-array.expected/html.marko | 1 + .../with-parens.marko | 1 + .../scriptlet-multi-line.expected/auto.marko | 8 +++---- .../concise.marko | 8 +++---- .../scriptlet-multi-line.expected/html.marko | 8 +++---- .../with-parens.marko | 8 +++---- src/__tests__/fixtures/scriptlet-array.marko | 1 + src/index.ts | 2 +- src/utils/with-block-if-needed.ts | 22 +++---------------- 11 files changed, 21 insertions(+), 40 deletions(-) create mode 100644 src/__tests__/__snapshots__/scriptlet-array.expected/auto.marko create mode 100644 src/__tests__/__snapshots__/scriptlet-array.expected/concise.marko create mode 100644 src/__tests__/__snapshots__/scriptlet-array.expected/html.marko create mode 100644 src/__tests__/__snapshots__/scriptlet-array.expected/with-parens.marko create mode 100644 src/__tests__/fixtures/scriptlet-array.marko diff --git a/src/__tests__/__snapshots__/scriptlet-array.expected/auto.marko b/src/__tests__/__snapshots__/scriptlet-array.expected/auto.marko new file mode 100644 index 0000000..1389ced --- /dev/null +++ b/src/__tests__/__snapshots__/scriptlet-array.expected/auto.marko @@ -0,0 +1 @@ +$ const arr = [1, 2, 3]; diff --git a/src/__tests__/__snapshots__/scriptlet-array.expected/concise.marko b/src/__tests__/__snapshots__/scriptlet-array.expected/concise.marko new file mode 100644 index 0000000..1389ced --- /dev/null +++ b/src/__tests__/__snapshots__/scriptlet-array.expected/concise.marko @@ -0,0 +1 @@ +$ const arr = [1, 2, 3]; diff --git a/src/__tests__/__snapshots__/scriptlet-array.expected/html.marko b/src/__tests__/__snapshots__/scriptlet-array.expected/html.marko new file mode 100644 index 0000000..1389ced --- /dev/null +++ b/src/__tests__/__snapshots__/scriptlet-array.expected/html.marko @@ -0,0 +1 @@ +$ const arr = [1, 2, 3]; diff --git a/src/__tests__/__snapshots__/scriptlet-array.expected/with-parens.marko b/src/__tests__/__snapshots__/scriptlet-array.expected/with-parens.marko new file mode 100644 index 0000000..1389ced --- /dev/null +++ b/src/__tests__/__snapshots__/scriptlet-array.expected/with-parens.marko @@ -0,0 +1 @@ +$ const arr = [1, 2, 3]; diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko index a9bd6fa..0bb0780 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko @@ -1,5 +1,3 @@ -$ { - var foo = { - bar: "foo", - }; -} +$ var foo = { + bar: "foo", +}; diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko index a9bd6fa..0bb0780 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko @@ -1,5 +1,3 @@ -$ { - var foo = { - bar: "foo", - }; -} +$ var foo = { + bar: "foo", +}; diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko index a9bd6fa..0bb0780 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko @@ -1,5 +1,3 @@ -$ { - var foo = { - bar: "foo", - }; -} +$ var foo = { + bar: "foo", +}; diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko index a9bd6fa..0bb0780 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko @@ -1,5 +1,3 @@ -$ { - var foo = { - bar: "foo", - }; -} +$ var foo = { + bar: "foo", +}; diff --git a/src/__tests__/fixtures/scriptlet-array.marko b/src/__tests__/fixtures/scriptlet-array.marko new file mode 100644 index 0000000..654aa01 --- /dev/null +++ b/src/__tests__/fixtures/scriptlet-array.marko @@ -0,0 +1 @@ +$ const arr = [1, 2, 3] \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 0f7f987..1f7ad4f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -579,7 +579,7 @@ export const printers: Record> = { opts, b.group([ node.static ? "static " : "$ ", - withBlockIfNeeded(node.body, opts, path.map(print, "body")), + withBlockIfNeeded(node.body, path.map(print, "body")), ]) ); case "MarkoText": { diff --git a/src/utils/with-block-if-needed.ts b/src/utils/with-block-if-needed.ts index eff024b..67783b8 100644 --- a/src/utils/with-block-if-needed.ts +++ b/src/utils/with-block-if-needed.ts @@ -1,26 +1,10 @@ import { types as t } from "@marko/compiler"; -import { doc, Doc, ParserOptions } from "prettier"; -import { enclosedNodeTypeReg } from "../constants"; -import outerCodeMatches from "./outer-code-matches"; +import { doc, Doc } from "prettier"; const { builders: b } = doc; -export default function withBlockIfNeeded( - nodes: t.Statement[], - opts: ParserOptions, - docs: Doc[] -) { - if ( - nodes.length > 1 || - (!enclosedNodeTypeReg.test(nodes[0].type) && - outerCodeMatches( - doc.printer.printDocToString(docs, { - ...opts, - printWidth: 0, - }).formatted, - /[\n\r]/y - )) - ) { +export default function withBlockIfNeeded(nodes: t.Statement[], docs: Doc[]) { + if (nodes.length > 1) { return [ b.indent([b.ifBreak(["{", b.line]), b.join(b.hardline, docs)]), b.ifBreak([b.line, "}"]), From c27d7a0c8b45fc2cd1fa88741b87a92435ba9a54 Mon Sep 17 00:00:00 2001 From: LuLaValva Date: Thu, 9 Mar 2023 18:06:03 -0800 Subject: [PATCH 2/3] fix: brought back Dylan's stuff --- .../scriptlet-multi-line.expected/auto.marko | 8 +++--- .../concise.marko | 8 +++--- .../scriptlet-multi-line.expected/html.marko | 8 +++--- .../with-parens.marko | 8 +++--- src/index.ts | 12 +++------ src/utils/with-block-if-needed.ts | 27 +++++++++++++++---- src/utils/with-parens-if-needed.ts | 9 ++++--- 7 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko index 0bb0780..a9bd6fa 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/auto.marko @@ -1,3 +1,5 @@ -$ var foo = { - bar: "foo", -}; +$ { + var foo = { + bar: "foo", + }; +} diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko index 0bb0780..a9bd6fa 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/concise.marko @@ -1,3 +1,5 @@ -$ var foo = { - bar: "foo", -}; +$ { + var foo = { + bar: "foo", + }; +} diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko index 0bb0780..a9bd6fa 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/html.marko @@ -1,3 +1,5 @@ -$ var foo = { - bar: "foo", -}; +$ { + var foo = { + bar: "foo", + }; +} diff --git a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko index 0bb0780..a9bd6fa 100644 --- a/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/scriptlet-multi-line.expected/with-parens.marko @@ -1,3 +1,5 @@ -$ var foo = { - bar: "foo", -}; +$ { + var foo = { + bar: "foo", + }; +} diff --git a/src/index.ts b/src/index.ts index 1f7ad4f..58a881f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -546,9 +546,7 @@ export const printers: Record> = { b.group( enclosedNodeTypeReg.test(node.type) ? attrPath.call(print, "value") - : withParensIfNeeded( - value, - opts, + : withParensIfNeeded(value, opts, () => attrPath.call(print, "value") ) ) @@ -560,10 +558,8 @@ export const printers: Record> = { } case "MarkoSpreadAttribute": { return (["..."] as Doc[]).concat( - withParensIfNeeded( - node.value, - opts, - (path as AstPath).call(print, "value") + withParensIfNeeded(node.value, opts, () => + path.call(print, "value") ) ); } @@ -579,7 +575,7 @@ export const printers: Record> = { opts, b.group([ node.static ? "static " : "$ ", - withBlockIfNeeded(node.body, path.map(print, "body")), + withBlockIfNeeded(node.body, opts, () => path.map(print, "body")), ]) ); case "MarkoText": { diff --git a/src/utils/with-block-if-needed.ts b/src/utils/with-block-if-needed.ts index 67783b8..3d0f92a 100644 --- a/src/utils/with-block-if-needed.ts +++ b/src/utils/with-block-if-needed.ts @@ -1,15 +1,32 @@ import { types as t } from "@marko/compiler"; -import { doc, Doc } from "prettier"; +import { doc, Doc, ParserOptions } from "prettier"; +import { enclosedNodeTypeReg } from "../constants"; +import outerCodeMatches from "./outer-code-matches"; const { builders: b } = doc; -export default function withBlockIfNeeded(nodes: t.Statement[], docs: Doc[]) { - if (nodes.length > 1) { +export default function withBlockIfNeeded( + nodes: t.Statement[], + opts: ParserOptions, + /* must use a factory function because `printDocToString` has side effects */ + getDocs: () => Doc[] +) { + if ( + nodes.length > 1 || + (!enclosedNodeTypeReg.test(nodes[0].type) && + outerCodeMatches( + doc.printer.printDocToString(getDocs(), { + ...opts, + printWidth: 0, + }).formatted, + /[\n\r]/y + )) + ) { return [ - b.indent([b.ifBreak(["{", b.line]), b.join(b.hardline, docs)]), + b.indent([b.ifBreak(["{", b.line]), b.join(b.hardline, getDocs())]), b.ifBreak([b.line, "}"]), ]; } - return docs; + return getDocs(); } diff --git a/src/utils/with-parens-if-needed.ts b/src/utils/with-parens-if-needed.ts index 7ffaac0..9372918 100644 --- a/src/utils/with-parens-if-needed.ts +++ b/src/utils/with-parens-if-needed.ts @@ -7,12 +7,13 @@ const { builders: b } = doc; export default function withParensIfNeeded( node: Node, opts: ParserOptions, - valDoc: Doc + /* must use a factory function because `printDocToString` has side effects */ + getValDoc: () => Doc ) { if ( !enclosedNodeTypeReg.test(node.type) && outerCodeMatches( - doc.printer.printDocToString(valDoc, { + doc.printer.printDocToString(getValDoc(), { ...opts, printWidth: 0, }).formatted, @@ -20,8 +21,8 @@ export default function withParensIfNeeded( opts.markoAttrParen ) ) { - return ["(", b.indent([b.softline, valDoc]), b.softline, ")"]; + return ["(", b.indent([b.softline, getValDoc()]), b.softline, ")"]; } - return valDoc; + return getValDoc(); } From cb5066a282cee7ad1bcaaa1d8ec2cc15d5a26e4c Mon Sep 17 00:00:00 2001 From: LuLaValva Date: Thu, 9 Mar 2023 18:21:23 -0800 Subject: [PATCH 3/3] fix: typescript --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 58a881f..fd1acab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -559,7 +559,7 @@ export const printers: Record> = { case "MarkoSpreadAttribute": { return (["..."] as Doc[]).concat( withParensIfNeeded(node.value, opts, () => - path.call(print, "value") + (path as AstPath).call(print, "value") ) ); }