Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: More lenient parsing (Error: unknown block tag: match) #63

Open
1 task done
LeoniePhiline opened this issue Feb 11, 2023 · 0 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@LeoniePhiline
Copy link

LeoniePhiline commented Feb 11, 2023

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant