From e2b4c07a2d15426d683a1df42ab5cd5e24294dcb Mon Sep 17 00:00:00 2001 From: Manhey Chiu Date: Fri, 19 Apr 2024 04:24:07 +0100 Subject: [PATCH] Add undefined variable warning when config enabled --- .../jinjava/interpret/JinjavaInterpreter.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 8e0510aca..5ff87fa7c 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -78,6 +78,9 @@ public class JinjavaInterpreter implements PyishSerializable { public static final String IGNORED_OUTPUT_FROM_EXTENDS_NOTE = "ignored_output_from_extends"; + + public static final String OUTPUT_UNDEFINED_VARIABLES_ERROR = + "OUTPUT_UNDEFINED_VARIABLES_ERROR"; private final Multimap blocks = ArrayListMultimap.create(); private final LinkedList extendParentRoots = new LinkedList<>(); private final Map revertibleObjects = new HashMap<>(); @@ -585,6 +588,28 @@ public Object retraceVariable(String variable, int lineNumber, int startPosition } } obj = var.resolve(obj); + } else { + if ( + getConfig() + .getFeatures() + .getActivationStrategy(OUTPUT_UNDEFINED_VARIABLES_ERROR) + .isActive(getContext()) + ) { + addError( + new TemplateError( + ErrorType.WARNING, + ErrorReason.UNKNOWN, + ErrorItem.TOKEN, + "Undefined variable: '" + variable + "'", + null, + lineNumber, + startPosition, + null, + BasicTemplateErrorCategory.UNKNOWN, + ImmutableMap.of("variable", variable) + ) + ); + } } return obj; }