From a157957df4cecd680e01db34209241751a086e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Ha=CC=88rtwig?= Date: Mon, 7 Oct 2019 01:16:15 +0200 Subject: [PATCH 1/3] Make `toggle_bool` rule substitution correctable --- CHANGELOG.md | 3 ++ Rules.md | 2 +- .../Rules/Idiomatic/ToggleBoolRule.swift | 28 +++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a651a4a63..ab4aa546f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ [Marcelo Fabri](https://github.com/marcelofabri) [#2883](https://github.com/realm/SwiftLint/issues/2883) +* Make `toggle_bool` rule substitution correctable. + [MaxHaertwig](https://github.com/maxhaertwig) + #### Bug Fixes * None. diff --git a/Rules.md b/Rules.md index 33a48cbb4..167044e87 100644 --- a/Rules.md +++ b/Rules.md @@ -19447,7 +19447,7 @@ TODOs and FIXMEs should be resolved. Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version --- | --- | --- | --- | --- | --- -`toggle_bool` | Disabled | No | idiomatic | No | 4.2.0 +`toggle_bool` | Disabled | Yes | idiomatic | No | 4.2.0 Prefer `someBool.toggle()` over `someBool = !someBool`. diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift index c831dd3d3..48a0981d8 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift @@ -1,6 +1,6 @@ import SourceKittenFramework -public struct ToggleBoolRule: ConfigurationProviderRule, OptInRule, AutomaticTestableRule { +public struct ToggleBoolRule: SubstitutionCorrectableRule, ConfigurationProviderRule, OptInRule, AutomaticTestableRule { public var configuration = SeverityConfiguration(.warning) public init() {} @@ -22,16 +22,32 @@ public struct ToggleBoolRule: ConfigurationProviderRule, OptInRule, AutomaticTes "↓isHidden = !isHidden\n", "↓view.clipsToBounds = !view.clipsToBounds\n", "func foo() { ↓abc = !abc }" + ], + corrections: [ + "↓isHidden = !isHidden\n": "isHidden.toggle()\n", + "↓view.clipsToBounds = !view.clipsToBounds\n": "view.clipsToBounds.toggle()\n", + "func foo() { ↓abc = !abc }": "func foo() { abc.toggle() }" ] ) public func validate(file: File) -> [StyleViolation] { - let pattern = "(? [NSRange] { + let pattern = "(? (NSRange, String) { + let violationString = file.contents.bridge().substring(with: violationRange) + let identifier = violationString.components(separatedBy: .whitespaces).first { !$0.isEmpty } + return (violationRange, identifier! + ".toggle()") + } } From b9368cbca6e34667e4684fb514b53d611f4121e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Ha=CC=88rtwig?= Date: Mon, 7 Oct 2019 09:27:31 +0200 Subject: [PATCH 2/3] Fix reference to --- Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift index 48a0981d8..1a89fa50a 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift @@ -32,7 +32,7 @@ public struct ToggleBoolRule: SubstitutionCorrectableRule, ConfigurationProvider public func validate(file: File) -> [StyleViolation] { return violationRanges(in: file).map { - StyleViolation(ruleDescription: Self.description, + StyleViolation(ruleDescription: ToggleBoolRule.description, severity: configuration.severity, location: Location(file: file, characterOffset: $0.location) ) From 1a5aa05c34c63cb2ee93a9c141b51d333a291c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Ha=CC=88rtwig?= Date: Mon, 7 Oct 2019 09:42:19 +0200 Subject: [PATCH 3/3] Add missing Foundation import --- Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift index 1a89fa50a..73010f00c 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift @@ -1,3 +1,4 @@ +import Foundation import SourceKittenFramework public struct ToggleBoolRule: SubstitutionCorrectableRule, ConfigurationProviderRule, OptInRule, AutomaticTestableRule {