HubSpot / prettier-plugin-hubl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

switch (tok.value) {
case "raw":
return this.parseRaw();
case "verbatim":
return this.parseRaw("verbatim");
case "preserve":
return this.parseRaw("preserve");
case "if":
case "ifAsync":
return this.parseIf();
case "for":
case "asyncEach":
case "asyncAll":
return this.parseFor();
case "unless":
return this.parseUnless();
case "block":
return this.parseBlock();
case "extends":
return this.parseExtends();
case "include":
return this.parseInclude();
case "set":
return this.parseSet();
case "macro":
return this.parseMacro();
case "call":
return this.parseCall();
case "import":
return this.parseImport();
case "from":
return this.parseFrom();
case "filter":
return this.parseFilterStatement();
case "switch":
return this.parseSwitch();
default:
if (this.extensions.length) {
for (let i = 0; i < this.extensions.length; i++) {
const ext = this.extensions[i];
if (lib.indexOf(ext.tags || [], tok.value) !== -1) {
return ext.parse(this, nodes, lexer);
}
}
}
this.fail("unknown block tag: " + tok.value, tok.lineno, tok.colno);
}
return node;
}

Checklist

  • I have checked the known issues to make sure this isn’t already a known issue.