Skip to content

Commit

Permalink
Handle externally defined, unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
larsga committed Jan 28, 2019
1 parent 1b0b930 commit 4e468bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/com/schibsted/spt/data/jslt/impl/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonNode> variables,
int stackFrameSize,
Map<String, Integer> 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;
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/function-declaration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down

0 comments on commit 4e468bd

Please sign in to comment.