keep else inline if there is no newline around
This commit is contained in:
parent
d6ccdfab1f
commit
683901f483
6 changed files with 35 additions and 6 deletions
|
@ -9,6 +9,8 @@ import {
|
|||
nonClosingStatements,
|
||||
} from "./jinja";
|
||||
|
||||
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)('([^']|\\')*'|"([^"]|\\")*"|[\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 -->))/;
|
||||
|
||||
|
@ -145,14 +147,15 @@ export const parse: Parser<Node>["parse"] = (text) => {
|
|||
root.content.indexOf(end.originalText) + end.length
|
||||
);
|
||||
|
||||
const originalText = text.slice(start.index, end.index + end.length);
|
||||
const block = {
|
||||
id: generatePlaceholder(),
|
||||
type: "block",
|
||||
start: start,
|
||||
end: end,
|
||||
content: blockText.slice(start.length, blockText.length - end.length),
|
||||
ownLine: newline,
|
||||
originalText: text.slice(start.index, end.index + end.length),
|
||||
ownLine: originalText.search("\n") !== NOT_FOUND,
|
||||
originalText,
|
||||
index: start.index,
|
||||
length: end.index + end.length - start.index,
|
||||
nodes: root.nodes,
|
||||
|
|
|
@ -42,9 +42,13 @@ const printStatement = (node: Statement): builders.Doc => {
|
|||
{ shouldBreak: node.ownLine }
|
||||
);
|
||||
|
||||
return ["else", "elif"].includes(node.keyword)
|
||||
? [builders.dedent(builders.hardline), statemnt, builders.hardline]
|
||||
: statemnt;
|
||||
if (
|
||||
["else", "elif"].includes(node.keyword) &&
|
||||
surroundingBlock(node)?.ownLine
|
||||
) {
|
||||
return [builders.dedent(builders.hardline), statemnt, builders.hardline];
|
||||
}
|
||||
return statemnt;
|
||||
};
|
||||
|
||||
const printIgnoreBlock = (node: IgnoreBlock): builders.Doc => {
|
||||
|
@ -182,3 +186,9 @@ export const findPlaceholders = (text: string): [number, number][] => {
|
|||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
export const surroundingBlock = (node: Node): Block | undefined => {
|
||||
return Object.values(node.nodes).find(
|
||||
(n) => n.type === "block" && n.content.search(node.id) !== NOT_FOUND
|
||||
) as Block;
|
||||
};
|
||||
|
|
7
test/cases/statement_if_else_2/expected.html
Normal file
7
test/cases/statement_if_else_2/expected.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<body>
|
||||
{% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}
|
||||
<h1>{{ title }}</h1>
|
||||
{% else %}
|
||||
<h2>{{ title }}</h2>
|
||||
{% endif %}
|
||||
</body>
|
5
test/cases/statement_if_else_2/input.html
Normal file
5
test/cases/statement_if_else_2/input.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
<body>
|
||||
{% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}
|
||||
<h1>{{title}}</h1>{%else%}<h2>{{title}}</h2>
|
||||
{% endif %}
|
||||
</body>
|
|
@ -1,3 +1,5 @@
|
|||
<title>{% block title %}{% endblock %}</title>
|
||||
|
||||
<div>class="{% if class %}{{ class }}{% endif %}"</div>
|
||||
|
||||
<div>class="{% if class %}{{ class }}{% else %}default{% endif %}"</div>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
<title>{%block title%} {%endblock%}</title>
|
||||
|
||||
<div>class="{%if class%}{{class}}{%endif%}"</div>
|
||||
<div>class="{%if class%}{{class}}{%endif%}"</div>
|
||||
|
||||
<div>class="{%if class%}{{class}}{%else%}default{%endif%}"</div>
|
Loading…
Reference in a new issue