Skip to content

Commit

Permalink
Apply @file:Suppress on all toplevel declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans committed Sep 4, 2022
1 parent 55b2048 commit d6cadeb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal object SuppressionLocatorBuilder {
}

private fun toSuppressedRegionsLocator(hintsList: List<SuppressionHint>): 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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down

0 comments on commit d6cadeb

Please # to comment.