Add support for delimiters in expressions

This commit is contained in:
David Odenwald 2023-05-01 09:22:18 +02:00
parent e54ce4ba27
commit 7620e52d97
5 changed files with 30 additions and 4 deletions

View file

@ -16,6 +16,7 @@ export interface Node {
export interface Expression extends Node {
type: "expression";
delimiter: Delimiter;
}
export type Delimiter = "" | "-" | "+";

View file

@ -3,6 +3,7 @@ import {
Delimiter,
Node,
Placeholder,
Expression,
Statement,
Block,
nonClosingStatements,
@ -11,7 +12,7 @@ import {
const NOT_FOUND = -1;
const regex =
/(?<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 -->))/;
/(?<pre>(?<newline>\n)?(\s*?))(?<node>{{(?<startDelimiterEx>[-+]?)\s*(?<expression>'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*(?<endDelimiterEx>[-+]?)}}|{%(?<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[] = [];
@ -76,12 +77,16 @@ export const parse: Parser<Node>["parse"] = (text) => {
}
if (expression) {
const delimiter = (match.groups.startDelimiterEx ||
match.groups.endDelimiterEx) as Delimiter;
root.content = root.content.replace(matchText, placeholder);
root.nodes[node.id] = {
...node,
type: "expression",
content: expression,
};
delimiter,
} as Expression;
}
if (statement) {

View file

@ -35,11 +35,13 @@ const printExpression = (node: Expression): builders.Doc => {
return builders.group(
builders.join(" ", [
"{{",
["{{", node.delimiter],
multiline
? builders.indent([getMultilineGroup(node.content)])
: node.content,
multiline ? [builders.hardline, "}}"] : "}}",
multiline
? [builders.hardline, node.delimiter, "}}"]
: [node.delimiter, "}}"],
]),
{
shouldBreak: node.ownLine,

View file

@ -0,0 +1,9 @@
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>

View file

@ -0,0 +1,9 @@
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{ some_variable -}}
</div>
<div class="extemely_long__class_name extemely_long__class_name--modifier">
{{- some_variable }}
</div>