Skip to content

Commit

Permalink
Restored remaining code
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered committed Jul 18, 2024
1 parent c894b19 commit 15da439
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct TypesafeArrayInitRule: AnalyzerRule {
enum MyError: Error {}
let myResult: Result<String, MyError> = .success("")
let result: Result<Any, MyError> = myResult.map { $0 }
"""),
"""),
Example("""
struct IntArray {
let elements = [1, 2, 3]
Expand All @@ -24,28 +24,28 @@ struct TypesafeArrayInitRule: AnalyzerRule {
}
let ints = IntArray()
let intsCopy = ints.map { $0 }
"""),
"""),
],
triggeringExamples: [
Example("""
func f<Seq: Sequence>(s: Seq) -> [Seq.Element] {
s.↓map({ $0 })
}
"""),
""", testDisableCommand: false),
Example("""
func f(array: [Int]) -> [Int] {
array.↓map { $0 }
}
"""),
""", testDisableCommand: false),
Example("""
let myInts = [1, 2, 3].↓map { return $0 }
"""),
""", testDisableCommand: false),
Example("""
struct Generator: Sequence, IteratorProtocol {
func next() -> Int? { nil }
}
let array = Generator().↓map { i in i }
"""),
""", testDisableCommand: false),
],
requiresFileOnDisk: true
)
Expand Down
80 changes: 52 additions & 28 deletions Source/SwiftLintCore/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,56 @@ private struct LintResult {
}

private extension Rule {
static func superfluousDisableCommandViolations(regions: [Region],
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
allViolations: [StyleViolation]) -> [StyleViolation] {
func superfluousDisableCommandViolations(regions: [Region],
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
allViolations: [StyleViolation]) -> [StyleViolation] {
guard regions.isNotEmpty, let superfluousDisableCommandRule else {
return []
}

let regionsDisablingCurrentRule = regions.filter { region in
region.isRuleDisabled(self.init())
}
let regionsDisablingSuperfluousDisableRule = regions.filter { region in
region.isRuleDisabled(superfluousDisableCommandRule)
}

return regionsDisablingCurrentRule.compactMap { region -> StyleViolation? in
let isSuperfluousRuleDisabled = regionsDisablingSuperfluousDisableRule.contains {
$0.contains(region.start)
}

guard !isSuperfluousRuleDisabled else {
return nil
let regionsWithIdentifiers: [(String, [Region])] = {
if let customRules = self as? CustomRules {
return customRules.configuration.customRuleConfigurations.map { configuration in
let regionsDisablingCurrentRule = regions.filter { region in
region.isRuleDisabled(customRuleIdentifier: configuration.identifier)
}
return (configuration.identifier, regionsDisablingCurrentRule)
}
}

let noViolationsInDisabledRegion = !allViolations.contains { violation in
region.contains(violation.location)
let regionsDisablingCurrentRule = regions.filter { region in
region.isRuleDisabled(self)
}
guard noViolationsInDisabledRegion else {
return nil
return [(Self.description.identifier, regionsDisablingCurrentRule)]
}()

return regionsWithIdentifiers.flatMap { ruleIdentifier, regionsDisablingCurrentRule in
regionsDisablingCurrentRule.compactMap { region -> StyleViolation? in
let isSuperfluousRuleDisabled = regionsDisablingSuperfluousDisableRule.contains {
$0.contains(region.start)
}

guard !isSuperfluousRuleDisabled else {
return nil
}

let noViolationsInDisabledRegion = !allViolations.contains { violation in
region.contains(violation.location) && violation.ruleIdentifier == ruleIdentifier
}
guard noViolationsInDisabledRegion else {
return nil
}

return StyleViolation(
ruleDescription: type(of: superfluousDisableCommandRule).description,
severity: superfluousDisableCommandRule.configuration.severity,
location: region.start,
reason: superfluousDisableCommandRule.reason(forRuleIdentifier: ruleIdentifier)
)
}

return StyleViolation(
ruleDescription: type(of: superfluousDisableCommandRule).description,
severity: superfluousDisableCommandRule.configuration.severity,
location: region.start,
reason: superfluousDisableCommandRule.reason(for: self)
)
}
}

Expand Down Expand Up @@ -93,16 +107,26 @@ private extension Rule {

let (disabledViolationsAndRegions, enabledViolationsAndRegions) = violations.map { violation in
(violation, regions.first { $0.contains(violation.location) })
}.partitioned { _, region in
region?.isRuleEnabled(self) ?? true
}.partitioned { violation, region in
if self is CustomRules {
return !(region?.isRuleDisabled(customRuleIdentifier: violation.ruleIdentifier) ?? true)
}
return region?.isRuleEnabled(self) ?? true
}

let customRulesIDs: [String] = {
guard let customRules = self as? CustomRules else {
return []
}
return customRules.configuration.customRuleConfigurations.map(\.identifier)
}()
let ruleIDs = Self.description.allIdentifiers +
customRulesIDs +
(superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) +
[RuleIdentifier.all.stringRepresentation]
let ruleIdentifiers = Set(ruleIDs.map { RuleIdentifier($0) })

let superfluousDisableCommandViolations = Self.superfluousDisableCommandViolations(
let superfluousDisableCommandViolations = superfluousDisableCommandViolations(
regions: regions.count > 1 ? file.regions(restrictingRuleIdentifiers: ruleIdentifiers) : regions,
superfluousDisableCommandRule: superfluousDisableCommandRule,
allViolations: violations
Expand Down
7 changes: 6 additions & 1 deletion Source/SwiftLintCore/Models/Region.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public struct Region: Equatable {
///
/// - returns: True if the specified rule is disabled in this region.
public func isRuleDisabled(_ rule: some Rule) -> Bool {
areRulesDisabled(ruleIDs: type(of: rule).description.allIdentifiers)
if rule is CustomRules {
let customRulesConfiguration = rule.configuration as? CustomRulesConfiguration
let identifiers = customRulesConfiguration?.customRuleConfigurations.map { $0.identifier } ?? []
return areRulesDisabled(ruleIDs: identifiers)
}
return areRulesDisabled(ruleIDs: type(of: rule).description.allIdentifiers)
}

/// Whether the given rules are disabled in this region.
Expand Down
13 changes: 4 additions & 9 deletions Source/SwiftLintCore/Rules/CustomRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,14 @@ struct CustomRules: Rule, CacheDescriptionProvider {
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location),
reason: configuration.message)
}).filter { violation in
guard let region = file.regions().first(where: { $0.contains(violation.location) }) else {
return true
}

return !region.isRuleDisabled(customRuleIdentifier: configuration.identifier)
}
})
}
}
}

private extension Region {
extension Region {
func isRuleDisabled(customRuleIdentifier: String) -> Bool {
disabledRuleIdentifiers.contains(RuleIdentifier(customRuleIdentifier))
disabledRuleIdentifiers.contains(.all) ||
disabledRuleIdentifiers.contains(RuleIdentifier(customRuleIdentifier))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ package struct SuperfluousDisableCommandRule: SourceKitFreeRule {
[]
}

func reason(for rule: (some Rule).Type) -> String {
func reason(forRuleIdentifier ruleIdentifier: String) -> String {
"""
SwiftLint rule '\(rule.description.identifier)' did not trigger a violation in the disabled region; \
SwiftLint rule '\(ruleIdentifier)' did not trigger a violation in the disabled region; \
remove the disable command
"""
}
Expand Down

0 comments on commit 15da439

Please sign in to comment.