Skip to content

Commit

Permalink
Fix superfluous label colon, issue #12 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrockington authored and jkschneider committed Apr 19, 2018
1 parent f8a68bf commit ce96a7a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class PrintVisitor : AstVisitor<String>("") {
is Tr.Assign, is Tr.AssignOp, is Tr.Break, is Tr.Continue, is Tr.MethodInvocation -> ";"
is Tr.NewClass, is Tr.Return, is Tr.Throw, is Tr.Unary, is Tr.VariableDecls -> ";"
is Tr.DoWhileLoop, is Tr.Empty, is Tr.Assert -> ";"
is Tr.Label -> ":"
is Tr.MethodDecl -> if(statement.body == null) ";" else ""
else -> ""
}
Expand Down Expand Up @@ -431,4 +430,4 @@ class PrintVisitor : AstVisitor<String>("") {

private fun visitDims(dims: List<Tr.VariableDecls.Dimension>): String =
dims.fold("") { s, d -> s + d.fmt("[${visit(d.whitespace)}]") }
}
}
42 changes: 33 additions & 9 deletions rewrite-core/src/test/kotlin/com/netflix/rewrite/ast/LabelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,41 @@ abstract class LabelTest(p: Parser): Parser by p {

@Test
fun labeledWhileLoop() {
val a = parse("""
public class A {
public void test() {
labeled: while(true) {
}
}
}
""")
var orig = """
|public class A {
| public void test() {
| labeled: while(true) {
| }
| }
|}
""".trimMargin()
val a = parse(orig)

val labeled = a.firstMethodStatement() as Tr.Label
assertEquals("labeled", labeled.label.simpleName)
assertTrue(labeled.statement is Tr.WhileLoop)
assertEquals("Should recreate original", orig, a.print())
}
}

@Test
fun nonEmptyLabeledWhileLoop() {
val orig = """
|public class A {
| public void test() {
| outer: while(true) {
| while(true) {
| break outer;
| }
| }
| }
|}
""".trimMargin()

val a = parse(orig)

val labeled = a.firstMethodStatement() as Tr.Label
assertEquals("outer", labeled.label.simpleName)
assertTrue(labeled.statement is Tr.WhileLoop)
assertEquals("Should recreate original", orig, a.print())
}
}

0 comments on commit ce96a7a

Please sign in to comment.