Parser: fix incorrect end location for statement suites.
This used to be annoying for Skylint.
RELNOTES: none
PiperOrigin-RevId: 173249428
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
index a19fc83..b3b2c0b 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
@@ -565,6 +565,32 @@
assertThat(getText(input, stmt)).isEqualTo(input);
}
+ @Test
+ public void testIfStatementPosition() throws Exception {
+ assertStatementLocationCorrect(ParsingLevel.LOCAL_LEVEL, "if True:\n pass");
+ assertStatementLocationCorrect(
+ ParsingLevel.LOCAL_LEVEL, "if True:\n pass\nelif True:\n pass");
+ assertStatementLocationCorrect(ParsingLevel.LOCAL_LEVEL, "if True:\n pass\nelse:\n pass");
+ }
+
+ @Test
+ public void testForStatementPosition() throws Exception {
+ assertStatementLocationCorrect(ParsingLevel.LOCAL_LEVEL, "for x in []:\n pass");
+ }
+
+ @Test
+ public void testDefStatementPosition() throws Exception {
+ assertStatementLocationCorrect(ParsingLevel.TOP_LEVEL, "def foo():\n pass");
+ }
+
+ private void assertStatementLocationCorrect(ParsingLevel level, String stmtStr) {
+ Statement stmt = parseStatement(level, stmtStr);
+ assertThat(getText(stmtStr, stmt)).isEqualTo(stmtStr);
+ // Also try it with another token at the end (newline), which broke the location in the past.
+ stmt = parseStatement(level, stmtStr + "\n");
+ assertThat(getText(stmtStr, stmt)).isEqualTo(stmtStr);
+ }
+
private void assertExpressionLocationCorrect(String exprStr) {
Expression expr = parseExpression(exprStr);
assertThat(getText(exprStr, expr)).isEqualTo(exprStr);