From 4e5d2cf71694ed61f65c441ed67300d8af32f13a Mon Sep 17 00:00:00 2001 From: zackad Date: Mon, 16 Dec 2024 09:05:32 +0700 Subject: [PATCH 1/3] fix: pass options parameter into internal printer This parameter is required to conditionally build doc object that would allow placement of opening tag end. --- src/print/Element.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/print/Element.js b/src/print/Element.js index 94f90ce..c96cdf1 100644 --- a/src/print/Element.js +++ b/src/print/Element.js @@ -8,8 +8,8 @@ import { } from "../util/index.js"; const { group, line, hardline, softline, indent, join } = doc.builders; +const printOpeningTag = (node, path, print, options) => { -const printOpeningTag = (node, path, print) => { const opener = "<" + node.name; const printedAttributes = printSeparatedList(path, print, "", "attributes"); const openingTagEnd = node.selfClosing ? " />" : ">"; @@ -25,10 +25,10 @@ const printSeparatedList = (path, print, separator, attrName) => { return join([separator, line], path.map(print, attrName)); }; -const p = (node, path, print) => { +const p = (node, path, print, options) => { // Set a flag in case attributes contain, e.g., a FilterExpression node[EXPRESSION_NEEDED] = true; - const openingGroup = group(printOpeningTag(node, path, print)); + const openingGroup = group(printOpeningTag(node, path, print, options)); node[EXPRESSION_NEEDED] = false; node[STRING_NEEDS_QUOTES] = false; From a89c3afbb1c4c04f3f07a2916112fbadf382875d Mon Sep 17 00:00:00 2001 From: zackad Date: Mon, 16 Dec 2024 09:13:10 +0700 Subject: [PATCH 2/3] fix: properly implement bracketSameLine options --- CHANGELOG.md | 66 +++++++++++++++++++ src/print/Element.js | 28 ++++++-- .../bracket_same_line_disabled.snap.twig | 22 +++++++ .../bracket_same_line_enabled.snap.twig | 19 ++++++ tests/Options/bracket_same_line.twig | 10 +++ tests/Options/jsfmt.spec.js | 22 +++++++ 6 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 tests/Options/__snapshots__/bracket_same_line_disabled.snap.twig create mode 100644 tests/Options/__snapshots__/bracket_same_line_enabled.snap.twig create mode 100644 tests/Options/bracket_same_line.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index 12dbab9..fb13c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,72 @@ ## unreleased +### Bugfixes +- Properly implement `bracketSameLine` options for html element tag + +__Input__ +```twig + + +some description that should be enough to break this into multiline + +some text + +
+``` + +__bracketSameLine: `true`__ +```twig + + +some description that should be enough to break this into multiline + + + some text + + +
+``` + +__bracketSameLine: `false`__ +```twig + + +some description that should be enough to break this into multiline + + + some text + + +
+``` + ### Internals - Add option to add prefix/suffix for test snapshot output. This will allow to reuse single input file to produce several snapshot output with different configuration diff --git a/src/print/Element.js b/src/print/Element.js index c96cdf1..2b69c80 100644 --- a/src/print/Element.js +++ b/src/print/Element.js @@ -8,17 +8,37 @@ import { } from "../util/index.js"; const { group, line, hardline, softline, indent, join } = doc.builders; + const printOpeningTag = (node, path, print, options) => { + const groupId = Symbol("opening-tag"); const opener = "<" + node.name; const printedAttributes = printSeparatedList(path, print, "", "attributes"); - const openingTagEnd = node.selfClosing ? " />" : ">"; - const hasAttributes = node.attributes && node.attributes.length > 0; + const openingTagEnd = []; + if (node.selfClosing) { + if (!options.bracketSameLine) { + openingTagEnd.push(line); + } else { + openingTagEnd.push(" "); + } + openingTagEnd.push("/>"); + } else { + if (!options.bracketSameLine) { + openingTagEnd.push(softline); + } + openingTagEnd.push(">"); + } + + const hasAttributes = node.attributes && node.attributes.length > 0; if (hasAttributes) { - return [opener, indent([line, printedAttributes]), openingTagEnd]; + return group( + [opener, indent([line, printedAttributes]), openingTagEnd], + { id: groupId } + ); } - return [opener, openingTagEnd]; + + return group([opener, openingTagEnd], { id: groupId }); }; const printSeparatedList = (path, print, separator, attrName) => { diff --git a/tests/Options/__snapshots__/bracket_same_line_disabled.snap.twig b/tests/Options/__snapshots__/bracket_same_line_disabled.snap.twig new file mode 100644 index 0000000..f7ace78 --- /dev/null +++ b/tests/Options/__snapshots__/bracket_same_line_disabled.snap.twig @@ -0,0 +1,22 @@ + + +some description that should be enough to break this into multiline + + + some text + + +
diff --git a/tests/Options/__snapshots__/bracket_same_line_enabled.snap.twig b/tests/Options/__snapshots__/bracket_same_line_enabled.snap.twig new file mode 100644 index 0000000..a43b51a --- /dev/null +++ b/tests/Options/__snapshots__/bracket_same_line_enabled.snap.twig @@ -0,0 +1,19 @@ + + +some description that should be enough to break this into multiline + + + some text + + +
diff --git a/tests/Options/bracket_same_line.twig b/tests/Options/bracket_same_line.twig new file mode 100644 index 0000000..5ef9f76 --- /dev/null +++ b/tests/Options/bracket_same_line.twig @@ -0,0 +1,10 @@ + + +some description that should be enough to break this into multiline + +some text + +
diff --git a/tests/Options/jsfmt.spec.js b/tests/Options/jsfmt.spec.js index e5a04c0..f3c5da4 100644 --- a/tests/Options/jsfmt.spec.js +++ b/tests/Options/jsfmt.spec.js @@ -29,4 +29,26 @@ describe("Options", () => { }); await expect(actual).toMatchFileSnapshot(snapshotFile); }); + + it("bracket same line - enabled", async () => { + const { actual, snapshotFile } = await run_spec(import.meta.url, { + source: "bracket_same_line.twig", + suffix: "_enabled", + formatOptions: { + bracketSameLine: true + } + }); + await expect(actual).toMatchFileSnapshot(snapshotFile); + }); + + it("bracket same line - disabled", async () => { + const { actual, snapshotFile } = await run_spec(import.meta.url, { + source: "bracket_same_line.twig", + suffix: "_disabled", + formatOptions: { + bracketSameLine: false + } + }); + await expect(actual).toMatchFileSnapshot(snapshotFile); + }); }); From 8293679829b74e84631f3ac5682f8894e0dac6e9 Mon Sep 17 00:00:00 2001 From: zackad Date: Mon, 16 Dec 2024 09:13:46 +0700 Subject: [PATCH 3/3] test: update existing snapshot By default, option `bracketSameLine` is set to `false` and behave differently before fixed in this patch set. Thus we need to update existing snapshot to adhere with this changes. --- tests/ControlStructures/__snapshots__/forInclude.snap.twig | 3 ++- tests/ControlStructures/__snapshots__/if.snap.twig | 3 ++- tests/Element/__snapshots__/attributes.snap.twig | 3 ++- tests/Element/__snapshots__/manyAttributes.snap.twig | 3 ++- tests/Element/__snapshots__/selfClosing.snap.twig | 3 ++- tests/Expressions/__snapshots__/mappingExpression.snap.twig | 3 ++- tests/Expressions/__snapshots__/objectExpression.snap.twig | 3 ++- tests/Failing/__snapshots__/controversial.snap.twig | 3 ++- tests/Failing/__snapshots__/failing.snap.twig | 3 ++- tests/Statements/__snapshots__/macro.snap.twig | 3 ++- tests/smoke-test.twig | 3 ++- 11 files changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/ControlStructures/__snapshots__/forInclude.snap.twig b/tests/ControlStructures/__snapshots__/forInclude.snap.twig index 80bc11f..8d40fb2 100644 --- a/tests/ControlStructures/__snapshots__/forInclude.snap.twig +++ b/tests/ControlStructures/__snapshots__/forInclude.snap.twig @@ -1,7 +1,8 @@ {% for foo in range(1, category) %} + class="qtp-item__star icon-ic icon-icn_star--white {{ foo }}" + > {% include './Star.twig' only %} {% endfor %} diff --git a/tests/ControlStructures/__snapshots__/if.snap.twig b/tests/ControlStructures/__snapshots__/if.snap.twig index 16c0576..e6ffde2 100644 --- a/tests/ControlStructures/__snapshots__/if.snap.twig +++ b/tests/ControlStructures/__snapshots__/if.snap.twig @@ -13,7 +13,8 @@ (css.logoWider): useWiderItems }|classes }}" srcABC="{{ partner.logoUrl }}" - alt="{{ partner.name }}" /> + alt="{{ partner.name }}" + /> {%- elseif partnerName %} {{ partnerName }} {% elseif partnerImg -%} diff --git a/tests/Element/__snapshots__/attributes.snap.twig b/tests/Element/__snapshots__/attributes.snap.twig index 9d45bb4..7c986db 100644 --- a/tests/Element/__snapshots__/attributes.snap.twig +++ b/tests/Element/__snapshots__/attributes.snap.twig @@ -6,7 +6,8 @@ ref: intersectionObserver|default, class: 'hotel-item item-order__list-item js_co_item', 'data-co_alt_htl': isAlternative ? '1' - }|attrs }}> + }|attrs }} +> Test diff --git a/tests/Element/__snapshots__/manyAttributes.snap.twig b/tests/Element/__snapshots__/manyAttributes.snap.twig index 00eb740..5ba74a4 100644 --- a/tests/Element/__snapshots__/manyAttributes.snap.twig +++ b/tests/Element/__snapshots__/manyAttributes.snap.twig @@ -6,6 +6,7 @@ attr5="five" attr6="six" attr7="seven" - attr8="eight"> + attr8="eight" +> Text diff --git a/tests/Element/__snapshots__/selfClosing.snap.twig b/tests/Element/__snapshots__/selfClosing.snap.twig index 629f016..f61e06e 100644 --- a/tests/Element/__snapshots__/selfClosing.snap.twig +++ b/tests/Element/__snapshots__/selfClosing.snap.twig @@ -8,7 +8,8 @@ attr5="five" attr6="six" attr7="seven" - attr8="eight" /> + attr8="eight" +/> diff --git a/tests/Expressions/__snapshots__/mappingExpression.snap.twig b/tests/Expressions/__snapshots__/mappingExpression.snap.twig index 31afb31..9689ec9 100644 --- a/tests/Expressions/__snapshots__/mappingExpression.snap.twig +++ b/tests/Expressions/__snapshots__/mappingExpression.snap.twig @@ -68,4 +68,5 @@ theme }) }) - }}> + }} +> diff --git a/tests/Expressions/__snapshots__/objectExpression.snap.twig b/tests/Expressions/__snapshots__/objectExpression.snap.twig index 8d19ef3..d9c8efc 100644 --- a/tests/Expressions/__snapshots__/objectExpression.snap.twig +++ b/tests/Expressions/__snapshots__/objectExpression.snap.twig @@ -10,6 +10,7 @@

+ }|classes }}" +> Heading

diff --git a/tests/Failing/__snapshots__/controversial.snap.twig b/tests/Failing/__snapshots__/controversial.snap.twig index cbc005f..227d96e 100644 --- a/tests/Failing/__snapshots__/controversial.snap.twig +++ b/tests/Failing/__snapshots__/controversial.snap.twig @@ -10,7 +10,8 @@
+ } | classes }}" +>