Ignore compiler directives in `opening_brace` rule (#4658)

This commit is contained in:
Martin Redington 2022-12-25 17:46:16 +00:00 committed by GitHub
parent c740da48d5
commit 58611e6718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -29,6 +29,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#4643](https://github.com/realm/SwiftLint/issues/4643)
* Fix for compiler directives masking subsequent `opening_brace`
violations.
[Martin Redington](https://github.com/mildm8nnered)
[#3712](https://github.com/realm/SwiftLint/issues/3712)
## 0.50.3: Bundle of Towels
#### Breaking

View File

@ -7,9 +7,9 @@ private extension SwiftLintFile {
func violatingOpeningBraceRanges(allowMultilineFunc: Bool) -> [(range: NSRange, location: Int)] {
let excludingPattern: String
if allowMultilineFunc {
excludingPattern = #"(?:func[^\{\n]*\n[^\{\n]*\n[^\{]*|(?:(?:if|guard|while)\n[^\{]+?\s|\{\s*))\{"#
excludingPattern = #"(?:func[^\{\n]*\n[^\{\n]*\n[^\{]*|(?:\b(?:if|guard|while)\n[^\{]+?\s|\{\s*))\{"#
} else {
excludingPattern = #"(?:(?:if|guard|while)\n[^\{]+?\s|\{\s*)\{"#
excludingPattern = #"(?:\b(?:if|guard|while)\n[^\{]+?\s|\{\s*)\{"#
}
return match(pattern: #"(?:[^( ]|[\s(][\s]+)\{"#,
@ -18,8 +18,8 @@ private extension SwiftLintFile {
if isAnonymousClosure(range: $0) {
return nil
}
let branceRange = contents.bridge().range(of: "{", options: .literal, range: $0)
return ($0, branceRange.location)
let braceRange = contents.bridge().range(of: "{", options: .literal, range: $0)
return ($0, braceRange.location)
}
}
@ -147,6 +147,19 @@ struct OpeningBraceRule: CorrectableRule, ConfigurationProviderRule {
func run_Array_method2x(_ N: Int) {
}
"""),
Example("""
class TestFile {
func problemFunction() {
#if DEBUG
#endif
}
func openingBraceViolation()
{
print("Brackets")
}
}
""")
],
corrections: [