Skip to content

Commit

Permalink
Account for semicolons on top-level statements. (#275)
Browse files Browse the repository at this point in the history
Summary:
Before this, such semicolons caused a crash because OpBuilder noticed they were skipped in the token stream.

Pull Request resolved: #275

Reviewed By: hick209

Differential Revision: D34083427

Pulled By: cgrushko

fbshipit-source-id: 2bff6471ecda58965ae2280895ea6bf9c6c78730
  • Loading branch information
nreid260 authored and facebook-github-bot committed Feb 8, 2022
1 parent a2ed7c9 commit b17e505
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,7 @@ class KotlinInputAstVisitor(
builder.blankLineWanted(OpsBuilder.BlankLineWanted.YES)
}
visit(child)
builder.guessToken(";")
lastChildHadBlankLineBefore = childGetsBlankLineBefore
first = false
}
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,31 @@ class FormatterTest {
|}
|""".trimMargin())

@Test
fun `don't crash on top level statements with semicolons`() {
val code =
"""
|val x = { 0 };
|
|foo({ 0 });
|
|foo { 0 };
|
|val fill = 0;
|""".trimMargin()
val expected =
"""
|val x = { 0 }
|
|foo({ 0 })
|
|foo { 0 }
|
|val fill = 0
|""".trimMargin()
assertThatFormatting(code).isEqualTo(expected)
}

@Test
fun `preserve semicolons in enums`() {
val code =
Expand Down

0 comments on commit b17e505

Please sign in to comment.