Skip to content

Commit fd5b244

Browse files
committed
fix: handle string literal body when using block shortcut syntax
Close GH-106
1 parent a23e3aa commit fd5b244

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ __bracketSameLine: `false`__
6868
<br />
6969
```
7070

71+
- GH-106 Fix handling string literal when using block shortcut syntax
72+
7173
### Internals
7274
- 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
7375

src/print/BlockStatement.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { doc } from "prettier";
2-
import { Node } from "../melody/melody-types/index.js";
3-
import { EXPRESSION_NEEDED, printChildBlock } from "../util/index.js";
2+
import { Node, StringLiteral } from "../melody/melody-types/index.js";
3+
import {
4+
printChildBlock,
5+
EXPRESSION_NEEDED,
6+
STRING_NEEDS_QUOTES
7+
} from "../util/index.js";
48

59
const { hardline, group } = doc.builders;
610

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

3842
return group(parts);
3943
} else if (Node.isPrintExpressionStatement(node.body)) {
44+
if (node.body.value instanceof StringLiteral) {
45+
node[STRING_NEEDS_QUOTES] = true;
46+
}
4047
return [
4148
node.trimLeft ? "{%-" : "{%",
4249
" block ",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% block title %}
2+
{{ page_title|title }}
3+
{% endblock %}
4+
5+
{% block title page_title|title %}
6+
7+
{% block title 'page_title' %}
8+
9+
{% block empty_value '' %}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% block title %}
2+
{{ page_title|title }}
3+
4+
{% endblock %}
5+
6+
{% block title page_title|title %}
7+
8+
{% block title 'page_title'
9+
10+
%}
11+
12+
{%
13+
block empty_value '' %}

tests/IncludeEmbed/jsfmt.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe("Include embed", () => {
1313
});
1414
await expect(actual).toMatchFileSnapshot(snapshotFile);
1515
});
16+
it("should handle block shortcut", async () => {
17+
const { actual, snapshotFile } = await run_spec(import.meta.url, {
18+
source: "block_shortcut.twig"
19+
});
20+
await expect(actual).toMatchFileSnapshot(snapshotFile);
21+
});
1622
it("should handle embed", async () => {
1723
const { actual, snapshotFile } = await run_spec(import.meta.url, {
1824
source: "embed.twig"

0 commit comments

Comments
 (0)