Request: More lenient parsing (`Error: unknown block tag: match`)
LeoniePhiline opened this issue · comments
Description
This plugin is currently the only thing the many jinja-style template users can use to be able to stick with prettier.
In my case, I want to use it to format my templates for askama. Askama supports {% match %}
, but this plugin chokes on that.
Askama match
:
https://djc.github.io/askama/template_syntax.html#match
{% match item %}
{% when Some with ("foo") %}
Found literal foo
{% when Some with (val) %}
Found {{ val }}
{% when None %}
{% endmatch %}
Expected behavior
The plugin formats this properly, as it understands {% %}
and hopefully does not need to care much about what's inside the jinja expression.
At default
(block parsing fallthrough) https://github.com/HubSpot/prettier-plugin-hubl/blob/master/parser/src/parser/parser.js#L790 I would expect the parser to parse the block as unknown kind, and preserve its block tags, while formatting its child nodes as usual.
Instead, it fails hard: https://github.com/HubSpot/prettier-plugin-hubl/blob/master/parser/src/parser/parser.js#L799 - which seems unnecessary.
Of course the 🥇 would be if a parseMatch()
method was to be added to the parser. :)
To Reproduce
Original Source Code:
{% match item %}
{% when Some with ("foo") %}
Found literal foo
{% when Some with (val) %}
Found {{ val }}
{% when None %}
{% endmatch %}
Error:
[ERROR] Formatter error: [error] stdin: Error: unknown block tag: match
[error] at new TemplateError (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/lib.js:68:23)
[error] at Parser.error (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:57:16)
[error] at Parser.fail (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:60:20)
[error] at Parser.parseStatement (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:587:22)
[error] at Parser.parseNodes (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:1076:32)
[error] at Parser.parse (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:1103:46)
[error] at Parser.parseUntilBlocks (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:1060:26)
[error] at Parser.parseFor (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:166:26)
[error] at Parser.parseStatement (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:555:29)
[error] at Parser.parseNodes (/path/to/.local/share/pnpm/global/5/.pnpm/@hubspot+prettier-plugin-hubl@0.2.5_prettier@2.8.4/node_modules/@hubspot/prettier-plugin-hubl/parser/dist/parser/parser.js:1076:32)
Additional context
I am using helix editor, configured to invoke prettier with --parser hubl
and --plugin-search-dir /path/to/.local/share/pnpm/global/5/
.
Askama match
examples can be found at https://github.com/djc/askama/tree/main/testing/templates - all template files starting with match
.
Direct link to the error site:
prettier-plugin-hubl/parser/src/parser/parser.js
Lines 754 to 803 in f558aec
Checklist
- I have checked the known issues to make sure this isn’t already a known issue.