Skip to content

Commit c7bc01c

Browse files
authored
Parse leading negatives (#896)
1 parent d2756ef commit c7bc01c

5 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/main/java/com/hubspot/jinjava/tree/parse/ExpressionToken.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@ public int getType() {
4444
@Override
4545
protected void parse() {
4646
this.expr = WhitespaceUtils.unwrap(image, "{{", "}}");
47-
48-
if (WhitespaceUtils.startsWith(expr, "-")) {
49-
setLeftTrim(true);
50-
this.expr = WhitespaceUtils.unwrap(expr, "-", "");
51-
}
52-
if (WhitespaceUtils.endsWith(expr, "-")) {
53-
setRightTrim(true);
54-
this.expr = WhitespaceUtils.unwrap(expr, "", "-");
55-
}
56-
47+
this.expr = handleTrim(expr);
5748
this.expr = StringUtils.trimToEmpty(this.expr);
5849
}
5950

src/main/java/com/hubspot/jinjava/tree/parse/TagToken.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.hubspot.jinjava.tree.parse;
1717

1818
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
19-
import com.hubspot.jinjava.util.WhitespaceUtils;
2019

2120
public class TagToken extends Token {
2221
private static final long serialVersionUID = -4927751270481832992L;
@@ -54,15 +53,7 @@ protected void parse() {
5453
}
5554

5655
content = image.substring(2, image.length() - 2);
57-
58-
if (WhitespaceUtils.startsWith(content, "-")) {
59-
setLeftTrim(true);
60-
content = WhitespaceUtils.unwrap(content, "-", "");
61-
}
62-
if (WhitespaceUtils.endsWith(content, "-")) {
63-
setRightTrim(true);
64-
content = WhitespaceUtils.unwrap(content, "", "-");
65-
}
56+
content = handleTrim(content);
6657

6758
int nameStart = -1, pos = 0, len = content.length();
6859

src/main/java/com/hubspot/jinjava/tree/parse/Token.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ public void setRightTrimAfterEnd(boolean rightTrimAfterEnd) {
8383
this.rightTrimAfterEnd = rightTrimAfterEnd;
8484
}
8585

86+
/**
87+
* Handle any whitespace control characters, capturing whether leading or trailing
88+
* whitespace should be stripped.
89+
* @param unwrapped the content of the block stripped of its delimeters
90+
* @return the content stripped of any whitespace control characters.
91+
*/
92+
protected final String handleTrim(String unwrapped) {
93+
String result = unwrapped;
94+
if (unwrapped.startsWith("-")) {
95+
setLeftTrim(true);
96+
result = unwrapped.substring(1);
97+
}
98+
if (unwrapped.endsWith("-")) {
99+
setRightTrim(true);
100+
result = unwrapped.substring(0, unwrapped.length() - 1);
101+
}
102+
return result;
103+
}
104+
86105
public int getStartPosition() {
87106
return startPosition;
88107
}

src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public void itHidesWarningErrors() {
335335

336336
@Test
337337
public void itBindsUnaryMinusTighterThanCmp() {
338-
assertThat(interpreter.render("{{ (-5 > 4) }}")).isEqualTo("false");
338+
assertThat(interpreter.render("{{ -5 > 4 }}")).isEqualTo("false");
339339
}
340340

341341
@Test
@@ -351,17 +351,34 @@ public void itBindsUnaryMinusTighterThanIsNot() {
351351

352352
@Test
353353
public void itBindsUnaryMinusTighterThanFilters() {
354-
assertThat(interpreter.render("{{ (-5 | abs) }}")).isEqualTo("5");
354+
assertThat(interpreter.render("{{ -5 | abs }}")).isEqualTo("5");
355+
}
356+
357+
@Test
358+
public void itBindsUnaryMinusTighterThanPlus() {
359+
assertThat(interpreter.render("{{ -10 + 4 }}")).isEqualTo("-6");
360+
assertThat(interpreter.render("{{ 4 + -10 }}")).isEqualTo("-6");
355361
}
356362

357363
@Test
358364
public void itBindsFiltersTighterThanMul() {
359-
assertThat(interpreter.render("{{ (-5 * -4 | abs) }}")).isEqualTo("-20");
365+
assertThat(interpreter.render("{{ -5 * -4 | abs }}")).isEqualTo("-20");
366+
}
367+
368+
@Test
369+
public void itBindsFiltersTighterThanPlus() {
370+
assertThat(interpreter.render("{{ -10 | abs + 4 }}")).isEqualTo("14");
371+
assertThat(interpreter.render("{{ 4 + -10 | abs }}")).isEqualTo("14");
360372
}
361373

362374
@Test
363375
public void itInterpretsFilterChainsInOrder() {
364376
assertThat(interpreter.render("{{ 'foo' | upper | replace('O', 'A') }}"))
365377
.isEqualTo("FAA");
366378
}
379+
380+
@Test
381+
public void itInterpretsStandaloneNegatives() {
382+
assertThat(interpreter.render("{{ -10 }}")).isEqualTo("-10");
383+
}
367384
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% set foo = [1, 1, 2, 1] %}
22
{%- for item in foo -%}
3-
{%- ifchanged item- %}
3+
{%- ifchanged item -%}
44
{{ deferred[item] }}
55
{%- endifchanged -%}
66
{% endfor%}

0 commit comments

Comments
 (0)