Skip to content

Commit

Permalink
Merge pull request #2716 from fredpi/feature/#2670-fix-superfluous-di…
Browse files Browse the repository at this point in the history
…sable

Fix "superfluous_disable_command" / "disable all" interference
  • Loading branch information
marcelofabri authored Apr 12, 2019
2 parents bc8f9d6 + 43e1845 commit c80ffd3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
declaring extensions that add protocol conformances with Swift 5.
[Marcelo Fabri](https://github.com/marcelofabri)
[#2705](https://github.com/realm/SwiftLint/issues/2705)

* Let `disable all` command override `superfluous_disable_command` rule.
[Frederick Pietschmann](https://github.com/fredpi)
[#2670](https://github.com/realm/SwiftLint/issues/2670)

## 0.31.0: Busy Laundromat

Expand Down
12 changes: 8 additions & 4 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ private extension Rule {
let regionsDisablingCurrentRule = regions.filter { region in
return region.isRuleDisabled(self.init())
}
let regionsDisablingSuperflousDisableRule = regions.filter { region in
let regionsDisablingSuperfluousDisableRule = regions.filter { region in
return region.isRuleDisabled(superfluousDisableCommandRule)
}

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

guard !isSuperfluousRuleDisabled else {
return nil
}

Expand Down Expand Up @@ -74,7 +77,8 @@ private extension Rule {
}

let ruleIDs = Self.description.allIdentifiers +
(superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? [])
(superfluousDisableCommandRule.map({ type(of: $0) })?.description.allIdentifiers ?? []) +
[RuleIdentifier.all.stringRepresentation]
let ruleIdentifiers = Set(ruleIDs.map { RuleIdentifier($0) })

let superfluousDisableCommandViolations = Self.superfluousDisableCommandViolations(
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

public let masterRuleList = RuleList(rules: [
Expand Down
7 changes: 5 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

@testable import SwiftLintFrameworkTests
Expand Down Expand Up @@ -109,6 +109,7 @@ extension CommandTests {
("testExpandThisCommand", testExpandThisCommand),
("testExpandNextCommand", testExpandNextCommand),
("testSuperfluousDisableCommands", testSuperfluousDisableCommands),
("testDisableAllOverridesSuperfluousDisableCommand", testDisableAllOverridesSuperfluousDisableCommand),
("testInvalidDisableCommands", testInvalidDisableCommands),
("testSuperfluousDisableCommandsDisabled", testSuperfluousDisableCommandsDisabled),
("testSuperfluousDisableCommandsDisabledOnConfiguration", testSuperfluousDisableCommandsDisabledOnConfiguration)
Expand Down Expand Up @@ -184,7 +185,9 @@ extension ConfigurationTests {

extension ContainsOverFirstNotNilRuleTests {
static var allTests: [(String, (ContainsOverFirstNotNilRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
("testWithDefaultConfiguration", testWithDefaultConfiguration),
("testFirstReason", testFirstReason),
("testFirstIndexReason", testFirstIndexReason)
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

import SwiftLintFramework
Expand Down
25 changes: 25 additions & 0 deletions Tests/SwiftLintFrameworkTests/CommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import SourceKittenFramework
@testable import SwiftLintFramework
import XCTest

// swiftlint:disable type_body_length

private extension Command {
init?(string: String) {
let nsString = string.bridge()
Expand Down Expand Up @@ -223,6 +225,29 @@ class CommandTests: XCTestCase {
)
}

func testDisableAllOverridesSuperfluousDisableCommand() {
XCTAssert(
violations(
"//swiftlint:disable all\n// swiftlint:disable nesting\nprint(123)\n"
).isEmpty
)
XCTAssert(
violations(
"//swiftlint:disable all\n// swiftlint:disable:next nesting\nprint(123)\n"
).isEmpty
)
XCTAssert(
violations(
"//swiftlint:disable all\n// swiftlint:disable:this nesting\nprint(123)\n"
).isEmpty
)
XCTAssert(
violations(
"//swiftlint:disable all\n// swiftlint:disable:previous nesting\nprint(123)\n"
).isEmpty
)
}

func testInvalidDisableCommands() {
XCTAssertEqual(
violations("// swiftlint:disable nesting_foo\nprint(123)\n")[0].ruleDescription.identifier,
Expand Down

0 comments on commit c80ffd3

Please sign in to comment.