From d6cadeb141670fe7921f7cab354411dfc74f0496 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sun, 4 Sep 2022 13:40:45 +0200 Subject: [PATCH] Apply `@file:Suppress` on all toplevel declarations Closes #1623 --- CHANGELOG.md | 5 ++-- .../com/pinterest/ktlint/core/KtLint.kt | 8 +++++-- .../internal/SuppressionLocatorBuilder.kt | 2 +- .../com/pinterest/ktlint/core/KtLintTest.kt | 24 +++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99279942ef..3766ea46e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/). * Do not add trailing comma in empty parameter/argument list with comments (`trailing-comma-on-call-site`, `trailing-comma-on-declaration-site`) ([#1602](https://github.com/pinterest/ktlint/issue/1602)) * Fix class cast exception when specifying a non-string editorconfig setting in the default ".editorconfig" ([#1627](https://github.com/pinterest/ktlint/issue/1627)) * Fix indentation before semi-colon when it is pushed down after inserting a trailing comma ([#1609](https://github.com/pinterest/ktlint/issue/1609)) - -Do not show deprecation warning about property "disabled_rules" when using CLi-parameter `--disabled-rules` ([#1599](https://github.com/pinterest/ktlint/issues/1599)) - +* Do not show deprecation warning about property "disabled_rules" when using CLi-parameter `--disabled-rules` ([#1599](https://github.com/pinterest/ktlint/issues/1599)) * Traversing directory hierarchy at Windows ([#1600](https://github.com/pinterest/ktlint/issues/1600)) * Ant-style path pattern support ([#1601](https://github.com/pinterest/ktlint/issues/1601)) +* Apply `@file:Suppress` on all toplevel declarations ([#1623](https://github.com/pinterest/ktlint/issues/1623)) ### Added diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt index a7c0a99a20..a552f91035 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt @@ -205,7 +205,9 @@ public object KtLint { ) { if (rule.shouldContinueTraversalOfAST()) { try { - rule.beforeVisitChildNodes(node, autoCorrect, emit) + if (!suppressedRegionLocator(node.startOffset, fqRuleId, node === rootNode)) { + rule.beforeVisitChildNodes(node, autoCorrect, emit) + } if (!rule.runsOnRootNodeOnly() && rule.shouldContinueTraversalOfAST()) { node .getChildren(null) @@ -218,7 +220,9 @@ public object KtLint { } } } - rule.afterVisitChildNodes(node, autoCorrect, emit) + if (!suppressedRegionLocator(node.startOffset, fqRuleId, node === rootNode)) { + rule.afterVisitChildNodes(node, autoCorrect, emit) + } } catch (e: Exception) { if (autoCorrect) { // line/col cannot be reliably mapped as exception might originate from a node not present in the diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/SuppressionLocatorBuilder.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/SuppressionLocatorBuilder.kt index 105d21de1b..7f6733eb32 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/SuppressionLocatorBuilder.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/SuppressionLocatorBuilder.kt @@ -44,7 +44,7 @@ internal object SuppressionLocatorBuilder { } private fun toSuppressedRegionsLocator(hintsList: List): SuppressionLocator = - { offset, ruleId, isRoot -> + { offset, ruleId, isRoot -> // TODO: Remove unused parameter isRoot hintsList .filter { offset in it.range } .any { hint -> hint.disabledRules.isEmpty() || hint.disabledRules.contains(ruleId) } diff --git a/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/KtLintTest.kt b/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/KtLintTest.kt index 176ef44cd9..5ae1798f10 100644 --- a/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/KtLintTest.kt +++ b/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/KtLintTest.kt @@ -1437,6 +1437,30 @@ class KtLintTest { ) } } + + @Test + fun `Issue 1623 - Given a file with multiple top-level declarations then a file suppression annotation should be applied on each top level declaration`() { + val code = + """ + @file:Suppress("ktlint:auto-correct") + val foo = "${AutoCorrectErrorRule.STRING_VALUE_TO_BE_AUTOCORRECTED}" // Won't be auto corrected due to suppress annotation + val bar = "${AutoCorrectErrorRule.STRING_VALUE_TO_BE_AUTOCORRECTED}" // Won't be auto corrected due to suppress annotation + """.trimIndent() + val actualFormattedCode = KtLint.format( + KtLint.ExperimentalParams( + text = code, + ruleProviders = setOf( + RuleProvider { AutoCorrectErrorRule() }, + ), + userData = emptyMap(), + cb = { _, _ -> }, + script = false, + editorConfigPath = null, + debug = false, + ), + ) + assertThat(actualFormattedCode).isEqualTo(code) + } } private class DummyRuleWithCustomEditorConfigProperty :