Skip to content

Commit

Permalink
fix: handle empty string as default value on macro declaration
Browse files Browse the repository at this point in the history
Close GH-80
  • Loading branch information
zackad committed Nov 12, 2024
1 parent 6e53029 commit 1d3fa87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## unreleased

### Bugfixes
- Fix handling empty string as default value on macros declaration

---
## 0.11.0 (2024-11-12)

Expand Down
13 changes: 10 additions & 3 deletions src/melody/melody-extension-core/parser/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ export const MacroParser = {
let value;
let node;
if (tokens.nextIf(Types.STRING_START)) {
value = tokens.expect(Types.STRING);
tokens.expect(Types.STRING_END);
node = createNode(StringLiteral, value, value.text);
if (tokens.test(Types.STRING_END)) {
// handle empty string as default value
value = tokens.expect(Types.STRING_END);
node = createNode(StringLiteral, value, "");
} else {
// handle non-empty string as default value
value = tokens.expect(Types.STRING);
tokens.expect(Types.STRING_END);
node = createNode(StringLiteral, value, value.text);
}
}
if (tokens.test(Types.NUMBER)) {
value = tokens.expect(Types.NUMBER);
Expand Down
6 changes: 4 additions & 2 deletions tests/Statements/__snapshots__/macro.snap.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@

{% macro hosted(
fontFamily,
filenameWithoutExt = false,
filenameWithoutExt = true,
fontWeight = 400,
fontSize = '16',
fontStyle = 'normal',
preload = false)
preload = false,
content = '')
%}

{% endmacro %}
2 changes: 1 addition & 1 deletion tests/Statements/macro.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
{{ groupId == 80 ? 'book_hotel_website_test' | translate : value }}
{% endmacro %}

{% macro hosted(fontFamily, filenameWithoutExt = false, fontWeight = 400, fontStyle = 'normal', preload = false) %}
{% macro hosted(fontFamily, filenameWithoutExt = true, fontWeight = 400, fontSize = '16', fontStyle = 'normal', preload = false, content='') %}
{% endmacro %}

0 comments on commit 1d3fa87

Please sign in to comment.