Skip to content

Commit

Permalink
Add warnings for unclosed tokens.
Browse files Browse the repository at this point in the history
Fixes #1038 and #373
  • Loading branch information
jasmith-hs committed Jul 24, 2023
1 parent 69379e5 commit 0e83e79
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.hubspot.jinjava.tree.parse.Token;
import com.hubspot.jinjava.tree.parse.TokenScanner;
import com.hubspot.jinjava.tree.parse.TokenScannerSymbols;
import com.hubspot.jinjava.tree.parse.UnclosedToken;
import org.apache.commons.lang3.StringUtils;

public class TreeParser {
Expand Down Expand Up @@ -97,6 +98,20 @@ private Node nextNode() {
Token token = scanner.next();

if (token.getType() == symbols.getFixed()) {
if (token instanceof UnclosedToken) {
interpreter.addError(
new TemplateError(
ErrorType.WARNING,
ErrorReason.SYNTAX_ERROR,
ErrorItem.TAG,
"Unclosed token",
"token",
token.getLineNumber(),
token.getStartPosition(),
null
)
);
}
return text((TextToken) token);
} else if (token.getType() == symbols.getExprStart()) {
return expression((ExpressionToken) token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ private Token getEndToken() {
if (inComment > 0) {
type = symbols.getNote();
}
if (inBlock > 0) {
return new UnclosedToken(
String.valueOf(is, tokenStart, tokenLength),
currLine,
tokenStart - lastNewlinePos + 1,
symbols
);
}
return Token.newToken(
type,
symbols,
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/hubspot/jinjava/tree/parse/UnclosedToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hubspot.jinjava.tree.parse;

public class UnclosedToken extends TextToken {

public UnclosedToken(
String image,
int lineNumber,
int startPosition,
TokenScannerSymbols symbols
) {
super(image, lineNumber, startPosition, symbols);
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ public void itWarnsAgainstUnclosedComment() {
assertThat(interpreter.getErrors().get(0).getFieldName()).isEqualTo("comment");
}

@Test
public void itWarnsAgainstUnclosedExpression() {
String expression = "foo {{ this is an unclosed expression %}";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.getErrors()).hasSize(1);
assertThat(interpreter.getErrors().get(0).getSeverity()).isEqualTo(ErrorType.WARNING);
assertThat(interpreter.getErrors().get(0).getMessage()).isEqualTo("Unclosed token");
assertThat(interpreter.getErrors().get(0).getFieldName()).isEqualTo("token");
assertThat(interpreter.render(tree))
.isEqualTo("foo {{ this is an unclosed expression %}");
}

@Test
public void itWarnsAgainstUnclosedTag() {
String expression = "foo {% this is an unclosed tag }}";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.getErrors()).hasSize(1);
assertThat(interpreter.getErrors().get(0).getSeverity()).isEqualTo(ErrorType.WARNING);
assertThat(interpreter.getErrors().get(0).getMessage()).isEqualTo("Unclosed token");
assertThat(interpreter.getErrors().get(0).getFieldName()).isEqualTo("token");
assertThat(interpreter.render(tree)).isEqualTo("foo {% this is an unclosed tag }}");
}

@Test
public void itWarnsTwiceAgainstUnclosedForTag() {
String expression = "{% for item in list %}\n{% for elem in items %}";
Expand Down

0 comments on commit 0e83e79

Please sign in to comment.