From 63a743232464af5daa4f54485f6829bea5f77620 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 28 Apr 2020 21:08:47 +0900 Subject: [PATCH] FinalNewlineRule: don't report for empty files --- .../ktlint/ruleset/standard/FinalNewlineRule.kt | 1 + .../ktlint/ruleset/standard/FinalNewlineRuleTest.kt | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) 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 f71e015f6f..6ef87e2d83 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 @@ -15,6 +15,7 @@ class FinalNewlineRule : Rule("final-newline"), Rule.Modifier.RestrictToRoot { emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit ) { if (node.isRoot()) { + if (node.textLength == 0) return val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!! val insertFinalNewline = editorConfig.insertFinalNewline val lastNode = lastChildNodeOf(node) 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 a39735cbb8..619e61d244 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 @@ -23,11 +23,7 @@ class FinalNewlineRuleTest { "", mapOf("insert_final_newline" to "true") ) - ).isEqualTo( - listOf( - LintError(1, 1, "final-newline", "File must end with a newline (\\n)") - ) - ) + ).isEmpty() assertThat( FinalNewlineRule().lint( "fun name() {\n}", @@ -52,6 +48,12 @@ class FinalNewlineRuleTest { ) ).isEmpty() // false + assertThat( + FinalNewlineRule().lint( + "", + mapOf("insert_final_newline" to "false") + ) + ).isEmpty() assertThat( FinalNewlineRule().lint( "fun name() {\n}",