From 683901f483ea106c61ad55a359791c267843013a Mon Sep 17 00:00:00 2001 From: davidodenwald Date: Fri, 6 Jan 2023 17:32:27 +0100 Subject: [PATCH] keep else inline if there is no newline around --- src/parser.ts | 7 +++++-- src/printer.ts | 16 +++++++++++++--- test/cases/statement_if_else_2/expected.html | 7 +++++++ test/cases/statement_if_else_2/input.html | 5 +++++ test/cases/statement_inline/expected.html | 2 ++ test/cases/statement_inline/input.html | 4 +++- 6 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 test/cases/statement_if_else_2/expected.html create mode 100644 test/cases/statement_if_else_2/input.html diff --git a/src/parser.ts b/src/parser.ts index 88303cd..eacf5d5 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -9,6 +9,8 @@ import { nonClosingStatements, } from "./jinja"; +const NOT_FOUND = -1; + const regex = /(?
(?\n)?(\s*?))(?{{\s*(?'([^']|\\')*'|"([^"]|\\")*"|[\S\s]*?)\s*}}|{%(?[-+]?)\s*(?(?for|endfor|if|else|elif|endif|macro|endmacro|call|endcall|filter|endfilter|set|endset|include|import|from|extends|block|endblock)('([^']|\\')*'|"([^"]|\\")*"|[\S\s])*?)\s*(?[-+]?)%}|(?{#[\S\s]*?#})|(?<(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)|(?[\s\S]*))/;
 
@@ -145,14 +147,15 @@ export const parse: Parser["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,
diff --git a/src/printer.ts b/src/printer.ts
index 594e4b4..730a39a 100644
--- a/src/printer.ts
+++ b/src/printer.ts
@@ -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;
+};
diff --git a/test/cases/statement_if_else_2/expected.html b/test/cases/statement_if_else_2/expected.html
new file mode 100644
index 0000000..22a5a3d
--- /dev/null
+++ b/test/cases/statement_if_else_2/expected.html
@@ -0,0 +1,7 @@
+
+  {% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %}
+    

{{ title }}

+ {% else %} +

{{ title }}

+ {% endif %} + diff --git a/test/cases/statement_if_else_2/input.html b/test/cases/statement_if_else_2/input.html new file mode 100644 index 0000000..e95f44e --- /dev/null +++ b/test/cases/statement_if_else_2/input.html @@ -0,0 +1,5 @@ + +{% if href in ['layout.html', 'index.html, 'about.html', 'user.html'] %} +

{{title}}

{%else%}

{{title}}

+{% endif %} + \ No newline at end of file diff --git a/test/cases/statement_inline/expected.html b/test/cases/statement_inline/expected.html index 3295289..cdeb1a6 100644 --- a/test/cases/statement_inline/expected.html +++ b/test/cases/statement_inline/expected.html @@ -1,3 +1,5 @@ {% block title %}{% endblock %}
class="{% if class %}{{ class }}{% endif %}"
+ +
class="{% if class %}{{ class }}{% else %}default{% endif %}"
diff --git a/test/cases/statement_inline/input.html b/test/cases/statement_inline/input.html index 63d25b4..bb74985 100644 --- a/test/cases/statement_inline/input.html +++ b/test/cases/statement_inline/input.html @@ -1,3 +1,5 @@ {%block title%} {%endblock%} -
class="{%if class%}{{class}}{%endif%}"
\ No newline at end of file +
class="{%if class%}{{class}}{%endif%}"
+ +
class="{%if class%}{{class}}{%else%}default{%endif%}"
\ No newline at end of file