Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply @file:Suppress on all toplevel declarations #1634

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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