Skip to content

Commit

Permalink
enable final new line by default (pinterest#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredsburrows authored and shashachu committed Jun 9, 2019
1 parent 9d63ec1 commit 87bcf96
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ interface EditorConfig {
val indentSize: Int
val tabWidth: Int
val maxLineLength: Int
val insertFinalNewline: Boolean?

val insertFinalNewline: Boolean
fun get(key: String): String?

companion object {
Expand All @@ -26,7 +25,7 @@ interface EditorConfig {
}
val tabWidth = map["indent_size"]?.toIntOrNull()
val maxLineLength = map["max_line_length"]?.toIntOrNull() ?: -1
val insertFinalNewline = map["insert_final_newline"]?.toBoolean()
val insertFinalNewline = map["insert_final_newline"]?.toBoolean() ?: true
return object : EditorConfig {
override val indentStyle = indentStyle
override val indentSize = indentSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FinalNewlineRule : Rule("final-newline"), Rule.Modifier.RestrictToRoot {
) {
if (node.isRoot()) {
val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!!
val insertFinalNewline = editorConfig.insertFinalNewline ?: return
val insertFinalNewline = editorConfig.insertFinalNewline
val lastNode = lastChildNodeOf(node)
if (insertFinalNewline) {
if (lastNode !is PsiWhiteSpace || !lastNode.textContains('\n')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class FinalNewlineRuleTest {
@Test
fun testLint() {
// neither true nor false
assertThat(FinalNewlineRule().lint("fun name() {\n}")).isEmpty()
assertThat(FinalNewlineRule().lint("fun name() {\n}")).isEqualTo(
listOf(
LintError(1, 1, "final-newline", "File must end with a newline (\\n)")
)
)
assertThat(FinalNewlineRule().lint("fun name() {\n}\n")).isEmpty()
// true
assertThat(
Expand Down

0 comments on commit 87bcf96

Please # to comment.