Skip to content

Commit

Permalink
Merge pull request #1487 from paul-dingemans/1029-suppress-in-file
Browse files Browse the repository at this point in the history
Disable all rules or a list of specific rules in the entire file (#1029)
  • Loading branch information
paul-dingemans authored Jun 7, 2022
2 parents 650ce55 + 8950c29 commit 5d2b29f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter
- Keep formatting of for-loop in sync with default IntelliJ formatter (`indent`) and a newline in the expression in a for-statement should not force to wrap it `wrapping` ([#1350](https://github.com/pinterest/ktlint/issues/1350))
- Fix indentation of property getter/setter when the property has an initializer on a separate line `indent` ([#1335](https://github.com/pinterest/ktlint/issues/1335))
- When `.editorconfig` setting `indentSize` is set to value `tab` then return the default tab width as value for `indentSize` ([#1485](https://github.com/pinterest/ktlint/issues/1485))

- Allow suppressing all rules or a list of specific rules in the entire file with `@file:Suppress(...)` ([#1029](https://github.com/pinterest/ktlint/issues/1029))


### Changed
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,13 @@ To suppress the violations of all ktlint rules, use:
val foo = "some really looooooooooooooooong string exceeding the max line length"
```

Like with other `@Suppress` annotations, it can be placed on targets supported by the annotation.
Like with other `@Suppress` annotations, it can be placed on targets supported by the annotation. As of this it is possible to disable rules in the entire file with:
```kotlin
@file:Suppress("ktlint") // Suppressing all rules for the entire file
// or
@file:Suppress("ktlint:max-line-length","ktlint:experimental:trailing-comma") // Suppressing specific rules for the entire file
```


### How do I globally disable a rule?
See the [EditorConfig section](https://github.com/pinterest/ktlint#editorconfig) for details on how to use the `disabled_rules` property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal object SuppressionLocatorBuilder {
"RemoveCurlyBracesFromTemplate" to "string-template"
)
private val suppressAnnotations = setOf("Suppress", "SuppressWarnings")
private const val suppresAllKtlintRules = "ktlint-all"
private const val suppressAllKtlintRules = "ktlint-all"

private val commentRegex = Regex("\\s")

Expand All @@ -40,21 +40,9 @@ internal object SuppressionLocatorBuilder {
return if (hintsList.isEmpty()) {
noSuppression
} else { offset, ruleId, isRoot ->
if (isRoot) {
val h = hintsList[0]
h.range.last == 0 && (
h.disabledRules.isEmpty() ||
h.disabledRules.contains(ruleId)
)
} else {
hintsList.any { hint ->
(
hint.disabledRules.isEmpty() ||
hint.disabledRules.contains(ruleId)
) &&
hint.range.contains(offset)
}
}
hintsList
.filter { offset in it.range }
.any { hint -> hint.disabledRules.isEmpty() || hint.disabledRules.contains(ruleId) }
}
}

Expand Down Expand Up @@ -166,7 +154,7 @@ internal object SuppressionLocatorBuilder {
.let { suppressedRules ->
when {
suppressedRules.isEmpty() -> null
suppressedRules.contains(suppresAllKtlintRules) ->
suppressedRules.contains(suppressAllKtlintRules) ->
SuppressionHint(
IntRange(psi.startOffset, psi.endOffset),
emptySet()
Expand All @@ -187,7 +175,7 @@ internal object SuppressionLocatorBuilder {
when {
it == "ktlint" -> {
// Disable all rules
suppresAllKtlintRules
suppressAllKtlintRules
}
it.startsWith("ktlint:") -> {
// Disable specific rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class SuppressionLocatorBuilderTest {
}

@Test
private fun `Given that a NoFooIdentifierRule violation is suppressed with @Suppress for all rules at class level then do not find a violation for that rule in that class`() {
fun `Given that a NoFooIdentifierRule violation is suppressed with @Suppress for all rules at class level then do not find a violation for that rule in that class`() {
val code =
"""
@Suppress("ktlint")
Expand All @@ -169,6 +169,40 @@ class SuppressionLocatorBuilderTest {
assertThat(lint(code)).isEmpty()
}

@Test
fun `Given that the NoFooIdentifierRule is suppressed in the entire file with @file-colon-Suppress then do not find any NoFooIdentifierRule violation`() {
val code =
"""
@file:Suppress("ktlint:no-foo-identifier-standard", "ktlint:custom:no-foo-identifier")
class Foo {
fun foo() {
val fooNotReported = "foo"
}
}
val fooNotReported = "foo"
""".trimIndent()
assertThat(lint(code)).isEmpty()
}

@Test
fun `Given that all rules are suppressed in the entire file with @file-colon-Suppress then do not find any violation`() {
val code =
"""
@file:Suppress("ktlint")
class Foo {
fun foo() {
val fooNotReported = "foo"
}
}
val fooNotReported = "foo"
""".trimIndent()
assertThat(lint(code)).isEmpty()
}

private class NoFooIdentifierRule(id: String) : Rule(id) {
override fun visit(
node: ASTNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public class TrailingCommaRule :
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.isRoot()) {
getEditorConfigValues(node)
return
allowTrailingComma = node.getEditorConfigValue(allowTrailingCommaProperty)
allowTrailingCommaOnCallSite = node.getEditorConfigValue(allowTrailingCommaOnCallSiteProperty)
}

// Keep processing of element types in sync with Intellij Kotlin formatting settings.
Expand All @@ -102,11 +102,6 @@ public class TrailingCommaRule :
}
}

private fun getEditorConfigValues(node: ASTNode) {
allowTrailingComma = node.getEditorConfigValue(allowTrailingCommaProperty)
allowTrailingCommaOnCallSite = node.getEditorConfigValue(allowTrailingCommaOnCallSiteProperty)
}

private fun visitCollectionLiteralExpression(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
Expand Down

0 comments on commit 5d2b29f

Please sign in to comment.