-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legacy_multiple opt-in rule (#2771)
Fixes #2612.
- Loading branch information
1 parent
90cb134
commit 6be5bf7
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Source/SwiftLintFramework/Rules/Idiomatic/LegacyMultipleRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import SourceKittenFramework | ||
|
||
public struct LegacyMultipleRule: OptInRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "legacy_multiple", | ||
name: "Legacy Multiple", | ||
description: "Prefer using the `isMultiple(of:)` function instead of using the remainder operator (`%`).", | ||
kind: .idiomatic, | ||
minSwiftVersion: .five, | ||
nonTriggeringExamples: [ | ||
"cell.contentView.backgroundColor = indexPath.row.isMultiple(of: 2) ? .gray : .white", | ||
"guard count.isMultiple(of: 2) else { throw DecodingError.dataCorrupted(...) }", | ||
"sanityCheck(bytes > 0 && bytes.isMultiple(of: 4), \"capacity must be multiple of 4 bytes\")", | ||
"guard let i = reversedNumbers.firstIndex(where: { $0.isMultiple(of: 2) }) else { return }", | ||
""" | ||
let constant = 56 | ||
let isMultiple = value.isMultiple(of: constant) | ||
""", | ||
""" | ||
let constant = 56 | ||
let secret = value % constant == 5 | ||
""", | ||
"let secretValue = (value % 3) + 2" | ||
], | ||
triggeringExamples: [ | ||
"cell.contentView.backgroundColor = indexPath.row ↓% 2 == 0 ? .gray : .white", | ||
"guard count ↓% 2 == 0 else { throw DecodingError.dataCorrupted(...) }", | ||
"sanityCheck(bytes > 0 && bytes ↓% 4 == 0, \"capacity must be multiple of 4 bytes\")", | ||
"guard let i = reversedNumbers.firstIndex(where: { $0 ↓% 2 == 0 }) else { return }", | ||
""" | ||
let constant = 56 | ||
let isMultiple = value ↓% constant == 0 | ||
""" | ||
] | ||
) | ||
|
||
// MARK: - Rule | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
let pattern = "(?!\\b\\s*)%\(RegexHelpers.variableOrNumber)==\\s*0\\b" | ||
return file.match(pattern: pattern, excludingSyntaxKinds: SyntaxKind.commentAndStringKinds) | ||
.map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters