From 2a4d367c0e282d7261b362ba69c87ece4fe33dec 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 | 20 +++++++++++++++++++ 1 file changed, 20 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..66848999d 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,23 @@ 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.MISSING, + ErrorItem.TOKEN, + "Undefined variable: '" + variable + "'", + null, + lineNumber, + startPosition, + null, + BasicTemplateErrorCategory.UNKNOWN, + ImmutableMap.of("variable", variable) + ) + ); + } } return obj; }