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

Fix superfluous disable command for custom rules #4760

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ private struct LintResult {
}

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

let regionsDisablingCurrentRule = regions.filter { region in
return region.isRuleDisabled(self.init())
return region.isRuleDisabled(self)
}
let regionsDisablingSuperfluousDisableRule = regions.filter { region in
return region.isRuleDisabled(superfluousDisableCommandRule)
Expand All @@ -50,7 +50,7 @@ private extension Rule {
ruleDescription: type(of: superfluousDisableCommandRule).description,
severity: superfluousDisableCommandRule.configuration.severity,
location: region.start,
reason: superfluousDisableCommandRule.reason(for: self)
reason: superfluousDisableCommandRule.reason(for: Self.self)
)
}
}
Expand Down Expand Up @@ -91,12 +91,17 @@ private extension Rule {
return region?.isRuleEnabled(self) ?? true
}

let ruleIDs = Self.description.allIdentifiers +
(superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) +
let ruleIDs: [String]
if let customRules = self as? CustomRules {
ruleIDs = customRules.customRuleIdentifiers
} else {
ruleIDs = Self.description.allIdentifiers
}
let allRuleIDs = ruleIDs + (superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) +
[RuleIdentifier.all.stringRepresentation]
let ruleIdentifiers = Set(ruleIDs.map { RuleIdentifier($0) })

let superfluousDisableCommandViolations = Self.superfluousDisableCommandViolations(
let ruleIdentifiers = Set(allRuleIDs.map { RuleIdentifier($0) })
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/SwiftLintFramework/Models/Region.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public struct Region: Equatable {
return true
}

let identifiersToCheck = type(of: rule).description.allIdentifiers
let identifiersToCheck: [String]
if let customRules = rule as? CustomRules {
identifiersToCheck = customRules.customRuleIdentifiers
} else {
identifiersToCheck = type(of: rule).description.allIdentifiers
}
let regionIdentifiers = Set(disabledRuleIdentifiers.map { $0.stringRepresentation })
return !regionIdentifiers.isDisjoint(with: identifiersToCheck)
}
Expand Down
2 changes: 2 additions & 0 deletions Source/SwiftLintFramework/Rules/Style/CustomRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct CustomRules: Rule, ConfigurationProviderRule, CacheDescriptionProvider {
kind: .style)

var configuration = CustomRulesConfiguration()

var customRuleIdentifiers: [String] { configuration.customRuleConfigurations.map { $0.identifier } }

func validate(file: SwiftLintFile) -> [StyleViolation] {
var configurations = configuration.customRuleConfigurations
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftLintFrameworkTests/CustomRulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ class CustomRulesTests: XCTestCase {
reason: configs.0.message)])
}

func testSuperfluousDisableCommandWithCustomRules() {
let customRulesConfiguration: [String:Any] = [
"custom1": [
"regex": "pattern",
"match_kinds": "comment"
]
]

let example = Example(
"// swiftlint:disable custom1\n",
configuration: customRulesConfiguration
).skipWrappingInCommentTest()
let configuration = try! Configuration(dict: ["custom_rules": customRulesConfiguration])
let violations = violations(example, config: configuration)

XCTAssertEqual(violations.count, 1)
XCTAssertTrue(violations.allSatisfy { $0.ruleIdentifier == "superfluous_disable_command" })
}

func testCustomRulesIncludedDefault() {
// Violation detected when included is omitted.
let (_, customRules) = getCustomRules()
Expand Down