Skip to content

Commit aa1458c

Browse files
committed
fix caller logic
Signed-off-by: Michael Pollind <[email protected]>
1 parent c79da0a commit aa1458c

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

askama_derive/src/generator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct Generator<'a, 'h> {
8787
/// Set of called macros we are currently in. Used to prevent (indirect) recursions.
8888
seen_macros: Vec<(&'a Macro<'a>, Option<FileInfo<'a>>)>,
8989
/// Set of callers to forward into the macro.
90+
seen_callers: Vec<&'a Call<'a>>,
9091
current_caller: Option<&'a Call<'a>>,
9192
}
9293

@@ -114,6 +115,7 @@ impl<'a, 'h> Generator<'a, 'h> {
114115
is_in_filter_block,
115116
seen_macros: Vec::new(),
116117
current_caller: None,
118+
seen_callers: vec![],
117119
}
118120
}
119121

askama_derive/src/generator/node.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ impl<'a> Generator<'a, '_> {
637637
} else {
638638
self.seen_macros.push((def, ctx.file_info_of(call.span())));
639639
}
640+
self.seen_callers.push(call);
640641
self.current_caller = Some(call);
641642
self.flush_ws(ws1); // Cannot handle_ws() here: whitespace from macro definition comes first
642643
let size_hint = self.push_locals(|this| {
@@ -760,6 +761,7 @@ impl<'a> Generator<'a, '_> {
760761
Ok(size_hint)
761762
})?;
762763
self.prepare_ws(ws1);
764+
self.seen_callers.pop();
763765
self.seen_macros.pop();
764766
self.current_caller = None;
765767
Ok(size_hint)
@@ -1177,7 +1179,7 @@ impl<'a> Generator<'a, '_> {
11771179
buf.write('}');
11781180
Ok(size_hint)
11791181
})?;
1180-
self.current_caller = Some(def);
1182+
self.current_caller = self.seen_callers.last().map(|v| &**v);
11811183
return Ok(size_hint);
11821184
}
11831185
}

testing/tests/macro.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,37 @@ fn test_caller_in_caller() {
261261
#[template(
262262
source = r#"
263263
{%- macro test2() -%}
264-
{{~ caller("bb") ~}}
265-
{%- endmacro -%}
264+
{{ caller("bb") }}
265+
{%~ endmacro -%}
266266
{%- macro test() -%}
267-
{{~ caller("a") ~}}
267+
{{ caller("a") }}
268+
{%~ call(b) test2() -%}
269+
five: {{ b }}
270+
{%~ endcall -%}
271+
{%- endmacro -%}
272+
{%- macro test3() -%}
273+
{{ caller("cc") }}
268274
{%- endmacro -%}
275+
{%- macro test4() -%}
276+
{{ caller("dd") }}
277+
{%~ endmacro -%}
269278
{%- call(a) test() -%}
270279
{%- call(b) test2() -%}
271280
one: {{ b }}
281+
{%~ endcall -%}
282+
{%- call(b) test3() -%}
283+
two: {{ b }}
284+
{%- call(b) test4() -%}
285+
three: {{ b }}
286+
{%~ endcall -%}
272287
{%- endcall -%}
273-
two: {{- a -}}
274-
{%- endcall -%}
288+
four: {{ a }}
289+
{%~ endcall -%}
275290
"#,
276291
ext = "txt"
277292
)]
278293
struct CallerInCaller;
279-
assert_eq!(CallerInCaller.render().unwrap(), "one: bbtwo:a");
294+
assert_eq!(CallerInCaller.render().unwrap(), "one: bb\n\ntwo: ccthree: dd\n\nfour: a\n\nfive: bb\n\n");
280295
}
281296

282297

0 commit comments

Comments
 (0)