From 87bcf9687cdfb946ea62f04c04a39a8f061ca072 Mon Sep 17 00:00:00 2001 From: Jared Burrows Date: Mon, 3 Jun 2019 12:50:39 -0400 Subject: [PATCH] enable final new line by default (#446) --- .../main/kotlin/com/pinterest/ktlint/core/EditorConfig.kt | 5 ++--- .../pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt | 2 +- .../ktlint/ruleset/standard/FinalNewlineRuleTest.kt | 6 +++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/EditorConfig.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/EditorConfig.kt index 9e977c980d..9b9b83eea0 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/EditorConfig.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/EditorConfig.kt @@ -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 { @@ -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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt index 5fa415fb66..f71e015f6f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt @@ -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')) { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt index 806b894557..7fd5c82801 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt @@ -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(