Skip to content

Commit

Permalink
fix: handle string literal body when using block shortcut syntax
Browse files Browse the repository at this point in the history
Close GH-106
  • Loading branch information
zackad committed Dec 17, 2024
1 parent a23e3aa commit fd5b244
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ __bracketSameLine: `false`__
<br />
```

- GH-106 Fix handling string literal when using block shortcut syntax

### 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

Expand Down
11 changes: 9 additions & 2 deletions src/print/BlockStatement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { doc } from "prettier";
import { Node } from "../melody/melody-types/index.js";
import { EXPRESSION_NEEDED, printChildBlock } from "../util/index.js";
import { Node, StringLiteral } from "../melody/melody-types/index.js";
import {
printChildBlock,
EXPRESSION_NEEDED,
STRING_NEEDS_QUOTES
} from "../util/index.js";

const { hardline, group } = doc.builders;

Expand Down Expand Up @@ -37,6 +41,9 @@ const p = (node, path, print, options) => {

return group(parts);
} else if (Node.isPrintExpressionStatement(node.body)) {
if (node.body.value instanceof StringLiteral) {
node[STRING_NEEDS_QUOTES] = true;
}
return [
node.trimLeft ? "{%-" : "{%",
" block ",
Expand Down
9 changes: 9 additions & 0 deletions tests/IncludeEmbed/__snapshots__/block_shortcut.snap.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% block title %}
{{ page_title|title }}
{% endblock %}

{% block title page_title|title %}

{% block title 'page_title' %}

{% block empty_value '' %}
13 changes: 13 additions & 0 deletions tests/IncludeEmbed/block_shortcut.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% block title %}
{{ page_title|title }}

{% endblock %}

{% block title page_title|title %}

{% block title 'page_title'

%}

{%
block empty_value '' %}
6 changes: 6 additions & 0 deletions tests/IncludeEmbed/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe("Include embed", () => {
});
await expect(actual).toMatchFileSnapshot(snapshotFile);
});
it("should handle block shortcut", async () => {
const { actual, snapshotFile } = await run_spec(import.meta.url, {
source: "block_shortcut.twig"
});
await expect(actual).toMatchFileSnapshot(snapshotFile);
});
it("should handle embed", async () => {
const { actual, snapshotFile } = await run_spec(import.meta.url, {
source: "embed.twig"
Expand Down

0 comments on commit fd5b244

Please sign in to comment.