remove check for the statement keyword

The check was removed because the jinja language can be extended
with new keywords.
This commit is contained in:
davidodenwald 2023-03-17 20:38:24 +01:00
parent 9db2105728
commit 0e85c48fad
4 changed files with 11 additions and 28 deletions

View file

@ -22,7 +22,7 @@ export type Delimiter = "" | "-" | "+";
export interface Statement extends Node {
type: "statement";
keyword: Keyword;
keyword: string;
delimiter: Delimiter;
}
@ -36,30 +36,6 @@ export interface IgnoreBlock extends Node {
type: "ignore";
}
export type Keyword =
| "for"
| "endfor"
| "if"
| "else"
| "elif"
| "endif"
| "macro"
| "endmacro"
| "call"
| "endcall"
| "filter"
| "endfilter"
| "set"
| "endset"
| "include"
| "import"
| "from"
| "extends"
| "block"
| "endblock"
| "with"
| "endwith";
export const nonClosingStatements = [
"else",
"elif",

View file

@ -1,7 +1,6 @@
import { Parser } from "prettier";
import {
Delimiter,
Keyword,
Node,
Placeholder,
Statement,
@ -12,7 +11,7 @@ import {
const NOT_FOUND = -1;
const regex =
/(?<pre>(?<newline>\n)?(\s*?))(?<node>{{\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>for|endfor|if|else|elif|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock|with|endwith)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
/(?<pre>(?<newline>\n)?(\s*?))(?<node>{{\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?<startDelimiter>[-+]?)\s*(?<statement>(?<keyword>\w+)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?<endDelimiter>[-+]?)%}|(?<comment>{#[\S\s]*?#})|(?<scriptBlock><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<styleBlock><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?<ignoreBlock><!-- prettier-ignore-start -->[\s\S]*<!-- prettier-ignore-end -->))/;
export const parse: Parser<Node>["parse"] = (text) => {
const statementStack: Statement[] = [];
@ -86,7 +85,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
}
if (statement) {
const keyword = match.groups.keyword as Keyword;
const keyword = match.groups.keyword;
const delimiter = (match.groups.startDelimiter ||
match.groups.endDelimiter) as Delimiter;

View file

@ -0,0 +1,3 @@
{% foo %}
<h1>Hello</h1>
{% endfoo %}

View file

@ -0,0 +1,5 @@
{%foo %}
<h1>Hello</h1>
{% endfoo
%}