Skip to content

Commit

Permalink
Parse leading negatives (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkindy authored Jul 21, 2022
1 parent d2756ef commit c7bc01c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,7 @@ public int getType() {
@Override
protected void parse() {
this.expr = WhitespaceUtils.unwrap(image, "{{", "}}");

if (WhitespaceUtils.startsWith(expr, "-")) {
setLeftTrim(true);
this.expr = WhitespaceUtils.unwrap(expr, "-", "");
}
if (WhitespaceUtils.endsWith(expr, "-")) {
setRightTrim(true);
this.expr = WhitespaceUtils.unwrap(expr, "", "-");
}

this.expr = handleTrim(expr);
this.expr = StringUtils.trimToEmpty(this.expr);
}

Expand Down
11 changes: 1 addition & 10 deletions src/main/java/com/hubspot/jinjava/tree/parse/TagToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.hubspot.jinjava.tree.parse;

import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.util.WhitespaceUtils;

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

content = image.substring(2, image.length() - 2);

if (WhitespaceUtils.startsWith(content, "-")) {
setLeftTrim(true);
content = WhitespaceUtils.unwrap(content, "-", "");
}
if (WhitespaceUtils.endsWith(content, "-")) {
setRightTrim(true);
content = WhitespaceUtils.unwrap(content, "", "-");
}
content = handleTrim(content);

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

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/hubspot/jinjava/tree/parse/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ public void setRightTrimAfterEnd(boolean rightTrimAfterEnd) {
this.rightTrimAfterEnd = rightTrimAfterEnd;
}

/**
* Handle any whitespace control characters, capturing whether leading or trailing
* whitespace should be stripped.
* @param unwrapped the content of the block stripped of its delimeters
* @return the content stripped of any whitespace control characters.
*/
protected final String handleTrim(String unwrapped) {
String result = unwrapped;
if (unwrapped.startsWith("-")) {
setLeftTrim(true);
result = unwrapped.substring(1);
}
if (unwrapped.endsWith("-")) {
setRightTrim(true);
result = unwrapped.substring(0, unwrapped.length() - 1);
}
return result;
}

public int getStartPosition() {
return startPosition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void itHidesWarningErrors() {

@Test
public void itBindsUnaryMinusTighterThanCmp() {
assertThat(interpreter.render("{{ (-5 > 4) }}")).isEqualTo("false");
assertThat(interpreter.render("{{ -5 > 4 }}")).isEqualTo("false");
}

@Test
Expand All @@ -351,17 +351,34 @@ public void itBindsUnaryMinusTighterThanIsNot() {

@Test
public void itBindsUnaryMinusTighterThanFilters() {
assertThat(interpreter.render("{{ (-5 | abs) }}")).isEqualTo("5");
assertThat(interpreter.render("{{ -5 | abs }}")).isEqualTo("5");
}

@Test
public void itBindsUnaryMinusTighterThanPlus() {
assertThat(interpreter.render("{{ -10 + 4 }}")).isEqualTo("-6");
assertThat(interpreter.render("{{ 4 + -10 }}")).isEqualTo("-6");
}

@Test
public void itBindsFiltersTighterThanMul() {
assertThat(interpreter.render("{{ (-5 * -4 | abs) }}")).isEqualTo("-20");
assertThat(interpreter.render("{{ -5 * -4 | abs }}")).isEqualTo("-20");
}

@Test
public void itBindsFiltersTighterThanPlus() {
assertThat(interpreter.render("{{ -10 | abs + 4 }}")).isEqualTo("14");
assertThat(interpreter.render("{{ 4 + -10 | abs }}")).isEqualTo("14");
}

@Test
public void itInterpretsFilterChainsInOrder() {
assertThat(interpreter.render("{{ 'foo' | upper | replace('O', 'A') }}"))
.isEqualTo("FAA");
}

@Test
public void itInterpretsStandaloneNegatives() {
assertThat(interpreter.render("{{ -10 }}")).isEqualTo("-10");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set foo = [1, 1, 2, 1] %}
{%- for item in foo -%}
{%- ifchanged item- %}
{%- ifchanged item -%}
{{ deferred[item] }}
{%- endifchanged -%}
{% endfor%}

0 comments on commit c7bc01c

Please sign in to comment.