From 3a781298c9ecd433eeaf4e3ca5da87e8b0d3c9be Mon Sep 17 00:00:00 2001 From: andyexeter Date: Fri, 6 Mar 2026 14:50:43 +0000 Subject: [PATCH] Preserve nested object indentation in multi-line set tags --- src/printer/print/liquid.ts | 3 ++- test/twig-set-object-indentation/fixed.liquid | 14 ++++++++++++++ test/twig-set-object-indentation/index.liquid | 14 ++++++++++++++ test/twig-set-object-indentation/index.spec.ts | 6 ++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/twig-set-object-indentation/fixed.liquid create mode 100644 test/twig-set-object-indentation/index.liquid create mode 100644 test/twig-set-object-indentation/index.spec.ts diff --git a/src/printer/print/liquid.ts b/src/printer/print/liquid.ts index 47f5e02..008a2f8 100644 --- a/src/printer/print/liquid.ts +++ b/src/printer/print/liquid.ts @@ -370,10 +370,11 @@ export function printLiquidBlockStart( } if (lines.length > 1) { + const reindentedLines = reindent(lines, true); return group([ '{%', whitespaceStart, - indent([hardline, node.name, ' ', join(hardline, lines.map(trim))]), + indent([hardline, node.name, ' ', join(hardline, reindentedLines)]), hardline, whitespaceEnd, '%}', diff --git a/test/twig-set-object-indentation/fixed.liquid b/test/twig-set-object-indentation/fixed.liquid new file mode 100644 index 0000000..06a46b7 --- /dev/null +++ b/test/twig-set-object-indentation/fixed.liquid @@ -0,0 +1,14 @@ +It should preserve nested object indentation in multi-line set tags +{% + set defaultAttributes = { + id: null, + alt: null, + class: 'w-full h-full rounded-lg aspect-square object-cover border bg-neutral image-preview', + stimulus: { + controllers: { + target: 'target-name' + } + } + } +%} + diff --git a/test/twig-set-object-indentation/index.liquid b/test/twig-set-object-indentation/index.liquid new file mode 100644 index 0000000..afa0c3c --- /dev/null +++ b/test/twig-set-object-indentation/index.liquid @@ -0,0 +1,14 @@ +It should preserve nested object indentation in multi-line set tags +{% +set defaultAttributes = { + id: null, + alt: null, + class: 'w-full h-full rounded-lg aspect-square object-cover border bg-neutral image-preview', + stimulus: { + controllers: { + target: 'target-name' + } + } +} +%} + diff --git a/test/twig-set-object-indentation/index.spec.ts b/test/twig-set-object-indentation/index.spec.ts new file mode 100644 index 0000000..4587999 --- /dev/null +++ b/test/twig-set-object-indentation/index.spec.ts @@ -0,0 +1,6 @@ +import { assertFormattedEqualsFixed } from '../test-helpers'; +import * as path from 'path'; + +describe(`Unit: ${path.basename(__dirname)}`, () => { + assertFormattedEqualsFixed(__dirname); +});