From 6633c225f1b9510d95c5c0b706036bcd602c73ee Mon Sep 17 00:00:00 2001 From: zackad Date: Thu, 28 Nov 2024 06:40:22 +0700 Subject: [PATCH 1/8] test: add indentation chain method case --- .../__snapshots__/chain_indentation.snap.twig | 11 +++++++++++ tests/Experimental/chain_indentation.twig | 17 +++++++++++++++++ tests/Experimental/experimental.spec.js | 11 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/Experimental/__snapshots__/chain_indentation.snap.twig create mode 100644 tests/Experimental/chain_indentation.twig create mode 100644 tests/Experimental/experimental.spec.js diff --git a/tests/Experimental/__snapshots__/chain_indentation.snap.twig b/tests/Experimental/__snapshots__/chain_indentation.snap.twig new file mode 100644 index 0000000..e523e07 --- /dev/null +++ b/tests/Experimental/__snapshots__/chain_indentation.snap.twig @@ -0,0 +1,11 @@ + + + diff --git a/tests/Experimental/chain_indentation.twig b/tests/Experimental/chain_indentation.twig new file mode 100644 index 0000000..e5dccea --- /dev/null +++ b/tests/Experimental/chain_indentation.twig @@ -0,0 +1,17 @@ + + + diff --git a/tests/Experimental/experimental.spec.js b/tests/Experimental/experimental.spec.js new file mode 100644 index 0000000..5cf8957 --- /dev/null +++ b/tests/Experimental/experimental.spec.js @@ -0,0 +1,11 @@ +import { run_spec } from "tests_config/run_spec"; +import { describe, expect, it } from "vitest"; + +describe("Experimental", () => { + it("Properly indent chain method", async () => { + const { actual, snapshotFile } = await run_spec(import.meta.url, { + source: "chain_indentation.twig" + }); + await expect(actual).toMatchFileSnapshot(snapshotFile); + }); +}); From 63a038d3747b9a3c9b44e6a15ce9ff20a64ff107 Mon Sep 17 00:00:00 2001 From: zackad Date: Thu, 28 Nov 2024 07:05:33 +0700 Subject: [PATCH 2/8] feat: introduce experimental option 'experimentalMethodChainIndentation' --- src/index.js | 6 ++++++ tests/Experimental/experimental.spec.js | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d23aa9d..97dd9fb 100644 --- a/src/index.js +++ b/src/index.js @@ -103,6 +103,12 @@ const options = { category: "Global", default: false, description: "Output the Twig block name in the 'endblock' tag" + }, + experimentalMethodChainIndentation: { + type: "boolean", + category: "Global", + default: false, + description: "Experimental: Fix indentation for chained method call" } }; diff --git a/tests/Experimental/experimental.spec.js b/tests/Experimental/experimental.spec.js index 5cf8957..7c9055b 100644 --- a/tests/Experimental/experimental.spec.js +++ b/tests/Experimental/experimental.spec.js @@ -4,7 +4,10 @@ import { describe, expect, it } from "vitest"; describe("Experimental", () => { it("Properly indent chain method", async () => { const { actual, snapshotFile } = await run_spec(import.meta.url, { - source: "chain_indentation.twig" + source: "chain_indentation.twig", + formatOptions: { + experimentalMethodChainIndentation: true + } }); await expect(actual).toMatchFileSnapshot(snapshotFile); }); From 4b60744f2a4d6b5557ef75d4778f16a12188f1e7 Mon Sep 17 00:00:00 2001 From: zackad Date: Thu, 28 Nov 2024 07:52:28 +0700 Subject: [PATCH 3/8] fix: pass option to printer --- src/print/CallExpression.js | 2 ++ src/print/MemberExpression.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/print/CallExpression.js b/src/print/CallExpression.js index 333ac83..131017e 100644 --- a/src/print/CallExpression.js +++ b/src/print/CallExpression.js @@ -22,6 +22,8 @@ const printCallExpression = (node, path, print) => { // Optimization: No line break between "(" and "{" if // there is exactly one object parameter parts.push(mappedArguments[0], ")"); + } else if (options.experimentalMethodChainIndentation) { + parts.push(indent([join([","], mappedArguments)]), ")"); } else { parts.push( indent([softline, join([",", line], mappedArguments)]), diff --git a/src/print/MemberExpression.js b/src/print/MemberExpression.js index 71dc482..994aab1 100644 --- a/src/print/MemberExpression.js +++ b/src/print/MemberExpression.js @@ -5,13 +5,19 @@ import { wrapExpressionIfNeeded } from "../util/index.js"; -const { group } = doc.builders; +const { group, softline, indent } = doc.builders; const printMemberExpression = (node, path, print) => { node[EXPRESSION_NEEDED] = false; node[STRING_NEEDS_QUOTES] = true; const parts = [path.call(print, "object")]; - parts.push(node.computed ? "[" : "."); + if (node.computed) { + parts.push("["); + } else if (options.experimentalMethodChainIndentation) { + parts.push(indent([softline, "."])); + } else { + parts.push("."); + } parts.push(path.call(print, "property")); if (node.computed) { parts.push("]"); From 67c01273b1b3ff39e58bb3c5d0a555de701ebc33 Mon Sep 17 00:00:00 2001 From: zackad Date: Tue, 3 Dec 2024 12:27:20 +0700 Subject: [PATCH 4/8] fix: add missing space after comma on call argument --- src/print/CallExpression.js | 2 +- tests/Experimental/__snapshots__/chain_indentation.snap.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/print/CallExpression.js b/src/print/CallExpression.js index 131017e..28e0e78 100644 --- a/src/print/CallExpression.js +++ b/src/print/CallExpression.js @@ -23,7 +23,7 @@ const printCallExpression = (node, path, print) => { // there is exactly one object parameter parts.push(mappedArguments[0], ")"); } else if (options.experimentalMethodChainIndentation) { - parts.push(indent([join([","], mappedArguments)]), ")"); + parts.push(indent([join([", "], mappedArguments)]), ")"); } else { parts.push( indent([softline, join([",", line], mappedArguments)]), diff --git a/tests/Experimental/__snapshots__/chain_indentation.snap.twig b/tests/Experimental/__snapshots__/chain_indentation.snap.twig index e523e07..65b4a31 100644 --- a/tests/Experimental/__snapshots__/chain_indentation.snap.twig +++ b/tests/Experimental/__snapshots__/chain_indentation.snap.twig @@ -5,7 +5,7 @@ .setController('App\\Controller\\Dashboard\\CRUD\\TacheCrudController') .setAction('edit') .setEntityId(tache.id) - .set('sort',null) + .set('sort', null) }}"> From f6eb0b67aa283f3e24781343b1284a0aa51d1a03 Mon Sep 17 00:00:00 2001 From: zackad Date: Wed, 4 Dec 2024 10:04:38 +0700 Subject: [PATCH 5/8] test: more test case --- tests/Experimental/chain_indentation.twig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Experimental/chain_indentation.twig b/tests/Experimental/chain_indentation.twig index e5dccea..e368950 100644 --- a/tests/Experimental/chain_indentation.twig +++ b/tests/Experimental/chain_indentation.twig @@ -15,3 +15,12 @@ }}"> + +{{ craft.entries + .section("news") + .section("news") + .orderBy("postDate DESC") + .limit(10) + .all(a.b) }} + +{{ craft.entries.all() }} From f099fa907beb388b17968b18ff372e03c758cc22 Mon Sep 17 00:00:00 2001 From: zackad Date: Wed, 18 Dec 2024 14:16:24 +0700 Subject: [PATCH 6/8] test: this is not experimental feature --- src/index.js | 6 ------ src/print/CallExpression.js | 2 -- src/print/MemberExpression.js | 10 ++-------- tests/Experimental/experimental.spec.js | 14 -------------- .../__snapshots__/chain_indentation.snap.twig | 18 ++++++++++++++++-- .../chain_indentation.twig | 0 tests/Expressions/jsfmt.spec.js | 10 ++++++++++ 7 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 tests/Experimental/experimental.spec.js rename tests/{Experimental => Expressions}/__snapshots__/chain_indentation.snap.twig (60%) rename tests/{Experimental => Expressions}/chain_indentation.twig (100%) diff --git a/src/index.js b/src/index.js index 97dd9fb..d23aa9d 100644 --- a/src/index.js +++ b/src/index.js @@ -103,12 +103,6 @@ const options = { category: "Global", default: false, description: "Output the Twig block name in the 'endblock' tag" - }, - experimentalMethodChainIndentation: { - type: "boolean", - category: "Global", - default: false, - description: "Experimental: Fix indentation for chained method call" } }; diff --git a/src/print/CallExpression.js b/src/print/CallExpression.js index 28e0e78..333ac83 100644 --- a/src/print/CallExpression.js +++ b/src/print/CallExpression.js @@ -22,8 +22,6 @@ const printCallExpression = (node, path, print) => { // Optimization: No line break between "(" and "{" if // there is exactly one object parameter parts.push(mappedArguments[0], ")"); - } else if (options.experimentalMethodChainIndentation) { - parts.push(indent([join([", "], mappedArguments)]), ")"); } else { parts.push( indent([softline, join([",", line], mappedArguments)]), diff --git a/src/print/MemberExpression.js b/src/print/MemberExpression.js index 994aab1..71dc482 100644 --- a/src/print/MemberExpression.js +++ b/src/print/MemberExpression.js @@ -5,19 +5,13 @@ import { wrapExpressionIfNeeded } from "../util/index.js"; -const { group, softline, indent } = doc.builders; +const { group } = doc.builders; const printMemberExpression = (node, path, print) => { node[EXPRESSION_NEEDED] = false; node[STRING_NEEDS_QUOTES] = true; const parts = [path.call(print, "object")]; - if (node.computed) { - parts.push("["); - } else if (options.experimentalMethodChainIndentation) { - parts.push(indent([softline, "."])); - } else { - parts.push("."); - } + parts.push(node.computed ? "[" : "."); parts.push(path.call(print, "property")); if (node.computed) { parts.push("]"); diff --git a/tests/Experimental/experimental.spec.js b/tests/Experimental/experimental.spec.js deleted file mode 100644 index 7c9055b..0000000 --- a/tests/Experimental/experimental.spec.js +++ /dev/null @@ -1,14 +0,0 @@ -import { run_spec } from "tests_config/run_spec"; -import { describe, expect, it } from "vitest"; - -describe("Experimental", () => { - it("Properly indent chain method", async () => { - const { actual, snapshotFile } = await run_spec(import.meta.url, { - source: "chain_indentation.twig", - formatOptions: { - experimentalMethodChainIndentation: true - } - }); - await expect(actual).toMatchFileSnapshot(snapshotFile); - }); -}); diff --git a/tests/Experimental/__snapshots__/chain_indentation.snap.twig b/tests/Expressions/__snapshots__/chain_indentation.snap.twig similarity index 60% rename from tests/Experimental/__snapshots__/chain_indentation.snap.twig rename to tests/Expressions/__snapshots__/chain_indentation.snap.twig index 65b4a31..086921c 100644 --- a/tests/Experimental/__snapshots__/chain_indentation.snap.twig +++ b/tests/Expressions/__snapshots__/chain_indentation.snap.twig @@ -1,4 +1,5 @@ - + }}" +> + +{{ + craft + .entries + .section('news') + .section('news') + .orderBy('postDate DESC') + .limit(10) + .all(a.b) +}} + +{{ craft.entries.all() }} diff --git a/tests/Experimental/chain_indentation.twig b/tests/Expressions/chain_indentation.twig similarity index 100% rename from tests/Experimental/chain_indentation.twig rename to tests/Expressions/chain_indentation.twig diff --git a/tests/Expressions/jsfmt.spec.js b/tests/Expressions/jsfmt.spec.js index 8a67e67..2637e0b 100644 --- a/tests/Expressions/jsfmt.spec.js +++ b/tests/Expressions/jsfmt.spec.js @@ -91,4 +91,14 @@ describe("Expressions", () => { }); await expect(actual).toMatchFileSnapshot(snapshotFile); }); + + it("Properly indent chain method", async () => { + const { actual, snapshotFile } = await run_spec(import.meta.url, { + source: "chain_indentation.twig", + formatOptions: { + experimentalMethodChainIndentation: true + } + }); + await expect(actual).toMatchFileSnapshot(snapshotFile); + }); }); From 520e06a1872b6d57ddfbf42534344adff3f7837e Mon Sep 17 00:00:00 2001 From: zackad Date: Tue, 7 Jan 2025 09:36:28 +0700 Subject: [PATCH 7/8] fix: do not break call expression parameter --- src/print/CallExpression.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/print/CallExpression.js b/src/print/CallExpression.js index 333ac83..f422f06 100644 --- a/src/print/CallExpression.js +++ b/src/print/CallExpression.js @@ -23,11 +23,7 @@ const printCallExpression = (node, path, print) => { // there is exactly one object parameter parts.push(mappedArguments[0], ")"); } else { - parts.push( - indent([softline, join([",", line], mappedArguments)]), - softline, - ")" - ); + parts.push(indent([join([", "], mappedArguments)]), ")"); } wrapExpressionIfNeeded(path, parts, node); From 7100648942f6e17c2b80ac5554cde86df6ed1bd1 Mon Sep 17 00:00:00 2001 From: zackad Date: Tue, 7 Jan 2025 09:48:26 +0700 Subject: [PATCH 8/8] fix: breack chain method call if too long --- src/print/MemberExpression.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/print/MemberExpression.js b/src/print/MemberExpression.js index 71dc482..7ae0763 100644 --- a/src/print/MemberExpression.js +++ b/src/print/MemberExpression.js @@ -5,13 +5,13 @@ import { wrapExpressionIfNeeded } from "../util/index.js"; -const { group } = doc.builders; +const { group, indent, softline } = doc.builders; const printMemberExpression = (node, path, print) => { node[EXPRESSION_NEEDED] = false; node[STRING_NEEDS_QUOTES] = true; const parts = [path.call(print, "object")]; - parts.push(node.computed ? "[" : "."); + parts.push(node.computed ? "[" : indent([softline, "."])); parts.push(path.call(print, "property")); if (node.computed) { parts.push("]");