Skip to content

Commit

Permalink
Fix parsing of exptests that come before or after filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Dec 13, 2024
1 parent 2b18dc7 commit 54b89a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,9 @@ protected AstNode value() throws ScanException, ParseException {
}
}

private AstNode parseOperators(AstNode left) throws ScanException, ParseException {
if ("|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER) {
AstNode v = left;

do {
private AstNode parseOperators(AstNode v) throws ScanException, ParseException {
while (true) {
if ("|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER) {
consumeToken(); // '|'
String filterName = consumeToken().getImage();
List<AstNode> filterParams = Lists.newArrayList(v, interpreter());
Expand All @@ -552,25 +550,25 @@ private AstNode parseOperators(AstNode left) throws ScanException, ParseExceptio
true
);
v = createAstMethod(filterProperty, createAstParameters(filterParams)); // function("filter:" + filterName, new AstParameters(filterParams));
} while ("|".equals(getToken().getImage()));

return v;
} else if (
"is".equals(getToken().getImage()) &&
"not".equals(lookahead(0).getImage()) &&
isPossibleExpTest(lookahead(1).getSymbol())
) {
consumeToken(); // 'is'
consumeToken(); // 'not'
return buildAstMethodForIdentifier(left, "evaluateNegated");
} else if (
"is".equals(getToken().getImage()) && isPossibleExpTest(lookahead(0).getSymbol())
) {
consumeToken(); // 'is'
return buildAstMethodForIdentifier(left, "evaluate");
} else if (
"is".equals(getToken().getImage()) &&
"not".equals(lookahead(0).getImage()) &&
isPossibleExpTest(lookahead(1).getSymbol())
) {
consumeToken(); // 'is'
consumeToken(); // 'not'
v = buildAstMethodForIdentifier(v, "evaluateNegated");
} else if (
"is".equals(getToken().getImage()) && isPossibleExpTest(lookahead(0).getSymbol())
) {
consumeToken(); // 'is'
v = buildAstMethodForIdentifier(v, "evaluate");
} else {
break;
}
}

return left;
return v;
}

protected AstParameters createAstParameters(List<AstNode> nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,10 @@ public void itOutputsUndefinedVariableError() {
assertThat(outputtingErrorInterpreters.getErrors().get(0).getCategoryErrors())
.isEqualTo(ImmutableMap.of("variable", "bar"));
}

@Test
public void itGivesFiltersAndExpTestsSamePrecedence() {
String template = "{{ 'A' | lower is defined | string | upper }}";
assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("TRUE");
}
}

0 comments on commit 54b89a3

Please sign in to comment.