diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRuleExamples.swift index 37d05fb1b28..c3325eed548 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRuleExamples.swift @@ -546,5 +546,20 @@ struct OpeningBraceRuleExamples { return } """), + Example(""" + if + "test".isEmpty + // swiftlint:disable:next opening_brace + { + // code here + } + """): Example(""" + if + "test".isEmpty + // swiftlint:disable:next opening_brace + { + // code here + } + """), ] } diff --git a/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift b/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift index 19c15d11865..5fcfac7d7a2 100644 --- a/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift +++ b/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift @@ -40,25 +40,32 @@ public extension SwiftSyntaxCorrectableRule { } // There is no rewriter. Falling back to the correction ranges collected by the visitor (if any). - let violationCorrections = makeVisitor(file: file) + let violations = makeVisitor(file: file) .walk(tree: syntaxTree, handler: \.violations) - .compactMap(\.correction) - if violationCorrections.isEmpty { + if violations.isEmpty { return [] } + + let locationConverter = file.locationConverter + let disabledRegions = file.regions() + .filter { $0.areRulesDisabled(ruleIDs: Self.description.allIdentifiers) } + .compactMap { $0.toSourceRange(locationConverter: locationConverter) } + typealias CorrectionRange = (range: NSRange, correction: String) - let correctionRanges = violationCorrections + let correctionRanges = violations + .filter { !$0.position.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) } + .compactMap(\.correction) .compactMap { correction in file.stringView.NSRange(start: correction.start, end: correction.end).map { range in CorrectionRange(range: range, correction: correction.replacement) } } - .filter { (correctionRange: CorrectionRange) in - file.ruleEnabled(violatingRange: correctionRange.range, for: self) != nil - } .sorted { (lhs: CorrectionRange, rhs: CorrectionRange) -> Bool in lhs.range.location > rhs.range.location } + if correctionRanges.isEmpty { + return [] + } var corrections = [Correction]() var contents = file.contents