Skip to content

Commit

Permalink
Retain the UTF8 BOM (Byte Order Mark) in case present in original code (
Browse files Browse the repository at this point in the history
#1027)

Solves #1013

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
  • Loading branch information
paul-dingemans and Paul Dingemans authored Dec 20, 2020
1 parent 509b236 commit c893ac5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,15 @@ public object KtLint {
return params.text
}

return if (hasUTF8BOM) UTF8_BOM else "" + // Restore UTF8 BOM if it was present
preparedCode
.rootNode
.text
.replace("\n", determineLineSeparator(params.text, params.userData))
val code = preparedCode
.rootNode
.text
.replace("\n", determineLineSeparator(params.text, params.userData))
return if (hasUTF8BOM) {
UTF8_BOM + code
} else {
code
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,37 @@ class KtLintTest {
)
assertThat(bus).isEqualTo(listOf("file:a", "file:b", "file:c", "b", "c", "file:d"))
}

@Test
fun testFormatUnicodeBom() {
val code = getResourceAsText("spec/format-unicode-bom.kt.spec")

val actual = KtLint.format(
KtLint.Params(
text = code,
ruleSets = listOf(
RuleSet("standard", DummyRule())
),
cb = { _, _ -> }
)
)

assertThat(actual).isEqualTo(code)
}
}

class DummyRule : Rule("dummy-rule") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
// The rule does not need to do anything except emit
emit(node.startOffset, "Dummy rule", true)
}
}

private fun getResourceAsText(path: String) =
(ClassLoader.getSystemClassLoader().getResourceAsStream(path) ?: throw RuntimeException("$path not found"))
.bufferedReader()
.readText()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Although probably not visible in your editor, this file starts with a UTF8 BOM unicode character

0 comments on commit c893ac5

Please sign in to comment.