Skip to content

Commit

Permalink
Merge pull request #20 from LuLaValva/array-truncation
Browse files Browse the repository at this point in the history
fix: strictly numerical arrays were truncated
  • Loading branch information
DylanPiercey authored Mar 10, 2023
2 parents 1352984 + cb5066a commit 9927fb0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ const arr = [1, 2, 3];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ const arr = [1, 2, 3];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ const arr = [1, 2, 3];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ const arr = [1, 2, 3];
1 change: 1 addition & 0 deletions src/__tests__/fixtures/scriptlet-array.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ const arr = [1, 2, 3]
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,7 @@ export const printers: Record<string, Printer<Node>> = {
b.group(
enclosedNodeTypeReg.test(node.type)
? attrPath.call(print, "value")
: withParensIfNeeded(
value,
opts,
: withParensIfNeeded(value, opts, () =>
attrPath.call(print, "value")
)
)
Expand All @@ -560,9 +558,7 @@ export const printers: Record<string, Printer<Node>> = {
}
case "MarkoSpreadAttribute": {
return (["..."] as Doc[]).concat(
withParensIfNeeded(
node.value,
opts,
withParensIfNeeded(node.value, opts, () =>
(path as AstPath<t.MarkoSpreadAttribute>).call(print, "value")
)
);
Expand All @@ -579,7 +575,7 @@ export const printers: Record<string, Printer<Node>> = {
opts,
b.group([
node.static ? "static " : "$ ",
withBlockIfNeeded(node.body, opts, path.map(print, "body")),
withBlockIfNeeded(node.body, opts, () => path.map(print, "body")),
])
);
case "MarkoText": {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/with-block-if-needed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ const { builders: b } = doc;
export default function withBlockIfNeeded(
nodes: t.Statement[],
opts: ParserOptions,
docs: Doc[]
/* 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(docs, {
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();
}
9 changes: 5 additions & 4 deletions src/utils/with-parens-if-needed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ 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,
/\s|>/y,
opts.markoAttrParen
)
) {
return ["(", b.indent([b.softline, valDoc]), b.softline, ")"];
return ["(", b.indent([b.softline, getValDoc()]), b.softline, ")"];
}

return valDoc;
return getValDoc();
}

0 comments on commit 9927fb0

Please sign in to comment.