Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
import com.hubspot.jinjava.util.LengthLimitingStringBuilder;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand DownExpand Up@@ -78,24 +79,25 @@ public Object doEvaluate(
Optional<String> importFile = getImportFile(interpreter);
try (InterpreterScopeClosable c = interpreter.enterScope()) {
String result = getEvaluationResult(argMap, kwargMap, varArgs, interpreter);

if (
!interpreter.getContext().isPartialMacroEvaluation() &&
(
!interpreter.getContext().getDeferredNodes().isEmpty() ||
!interpreter.getContext().getDeferredTokens().isEmpty()
)
!interpreter.getContext().getDeferredNodes().isEmpty() ||
!interpreter.getContext().getDeferredTokens().isEmpty()
) {
String tempVarName = MacroFunctionTempVariable.getVarName(
getName(),
hashCode(),
currentCallCount
);
interpreter
.getContext()
.getParent()
.put(tempVarName, new MacroFunctionTempVariable(result));
throw new DeferredParsingException(this, tempVarName);
if (!interpreter.getContext().isPartialMacroEvaluation()) {
String tempVarName = MacroFunctionTempVariable.getVarName(
getName(),
hashCode(),
currentCallCount
);
interpreter
.getContext()
.getParent()
.put(tempVarName, new MacroFunctionTempVariable(result));
throw new DeferredParsingException(this, tempVarName);
}
if (interpreter.getContext().isDeferredExecutionMode()) {
return EagerReconstructionUtils.wrapInChildScope(result, interpreter);
}
}

return result;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -1273,4 +1273,19 @@ public void itRunsMacroFunctionInDeferredExecutionMode() {
"runs-macro-function-in-deferred-execution-mode"
);
}

@Test
public void itKeepsMacroModificationsInScope() {
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"keeps-macro-modifications-in-scope"
);
}

@Test
public void itKeepsMacroModificationsInScopeSecondPass() {
interpreter.getContext().put("deferred", true);
expectedTemplateInterpreter.assertExpectedOutput(
"keeps-macro-modifications-in-scope.expected"
);
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1
2
3
3
2
3
3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
{% set list = [] %}{% if deferred %}

{% set list = [] %}{% for __ignored__ in [0] %}{% do list.append(1) %}1{% set depth = 2 %}
{% for __ignored__ in [0] %}{% do list.append(2) %}2{% set depth = 3 %}
{% for __ignored__ in [0] %}{% do list.append(3) %}3{% endfor %}
{% for __ignored__ in [0] %}{% do list.append(3) %}3{% endfor %}{% endfor %}
{% for __ignored__ in [0] %}{% do list.append(2) %}2{% set depth = 3 %}
{% for __ignored__ in [0] %}{% do list.append(3) %}3{% endfor %}
{% for __ignored__ in [0] %}{% do list.append(3) %}3{% endfor %}{% endfor %}{% endfor %}
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
{%- set list = [] %}
{%- if deferred %}
{%- macro inc(val, depth) %}
{%- do list.append(depth) -%}
{{ depth }}
{%- if depth < 3 %}
{%- set depth = depth + 1 %}
{{ inc(val, depth) }}
{{ inc(val, depth) }}
{%- endif %}
{%- endmacro %}

{{ inc('a', 1) }}
{% endif %}