From 4e468bd123ee6c71ea2065ae378451968ff84052 Mon Sep 17 00:00:00 2001 From: Lars Marius Garshol Date: Mon, 28 Jan 2019 14:37:54 +0100 Subject: [PATCH] Handle externally defined, unused variables --- .../java/com/schibsted/spt/data/jslt/impl/Scope.java | 7 ++++++- src/test/resources/function-declaration-tests.yaml | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/schibsted/spt/data/jslt/impl/Scope.java b/src/main/java/com/schibsted/spt/data/jslt/impl/Scope.java index 11ed842f..d4f2047b 100644 --- a/src/main/java/com/schibsted/spt/data/jslt/impl/Scope.java +++ b/src/main/java/com/schibsted/spt/data/jslt/impl/Scope.java @@ -26,12 +26,17 @@ public static Scope getRoot(int stackFrameSize) { return new Scope(stackFrameSize); } + /** + * Creates an initialized scope with values for variables supplied + * by client code into the JSLT expression. + */ public static Scope makeScope(Map variables, int stackFrameSize, Map parameterSlots) { Scope scope = new Scope(stackFrameSize); for (String variable : variables.keySet()) - scope.setValue(parameterSlots.get(variable), variables.get(variable)); + if (parameterSlots.containsKey(variable)) // check that variable exists + scope.setValue(parameterSlots.get(variable), variables.get(variable)); return scope; } diff --git a/src/test/resources/function-declaration-tests.yaml b/src/test/resources/function-declaration-tests.yaml index 0f06b073..5370fb14 100644 --- a/src/test/resources/function-declaration-tests.yaml +++ b/src/test/resources/function-declaration-tests.yaml @@ -183,6 +183,17 @@ tests: m:foo() output: 22 + # we're testing what happens when there is an external variable + # (a parameter) that's not actually referred to + - + input: {} + query: > + import "module-as-function-with-global.jslt" as m + m("variable is") + output: "\"variable is global\"" + variables: + thevar: itsvalue # not actually referred to + - input: {} query: >