Skip to content

Commit

Permalink
fix: make sure that empty block will have empty line in between
Browse files Browse the repository at this point in the history
Close GH-84

Example:
```twig
{% block content %}{% endblock %}
```

Will be printed as:
```twig
{% block content %}

{% endblock %}
```
  • Loading branch information
zackad committed Nov 21, 2024
1 parent 5b1399a commit e5c678c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ __Example__
{{ 'Hello %s %s!'|format(...['Fabien', 'Potencier']) }}
```

### Bugfixes
- Make sure that empty block statement will have empty line in between for consistency

---
## 0.11.1 (2024-11-13)

Expand Down
11 changes: 7 additions & 4 deletions src/print/BlockStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const p = (node, path, print, options) => {
node.trimRightBlock ? " -%}" : " %}"
];
const parts = [opener];

if (node.body.length === 0) {
parts.push(hardline);
}

if (node.body.length > 0) {
const indentedBody = printChildBlock(node, path, print, "body");
parts.push(indentedBody);
Expand All @@ -30,18 +35,16 @@ const p = (node, path, print, options) => {
node.trimRight ? " -%}" : " %}"
);

const result = group(parts);
return result;
return group(parts);
} else if (Node.isPrintExpressionStatement(node.body)) {
const parts = [
return [
node.trimLeft ? "{%-" : "{%",
" block ",
path.call(print, "name"),
" ",
path.call(print, "body", "value"),
node.trimRight ? " -%}" : " %}"
];
return parts;
}
};

Expand Down
15 changes: 14 additions & 1 deletion tests/IncludeEmbed/__snapshots__/block.snap.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@

{{ block('hello') }}

{% block content %}
{% block content_empty %}

{% endblock %}

{% block content_noline %}

{% endblock %}

{% block content_multiline %}

{% endblock %}

{% block content_empty_indented %}

{% endblock %}

{%- block hello -%}
Expand Down
16 changes: 15 additions & 1 deletion tests/IncludeEmbed/block.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@

{{ block('hello') }}

{% block content %}{% endblock %}
{% block content_empty %}{% endblock %}

{% block content_noline %}
{% endblock %}

{% block content_multiline %}





{% endblock %}

{% block content_empty_indented %}
{% endblock %}

{%- block hello -%}
Hello
Expand Down

0 comments on commit e5c678c

Please sign in to comment.