-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add optional_enum_case_matching rule #3002
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
158 changes: 158 additions & 0 deletions
158
Source/SwiftLintFramework/Rules/Style/OptionalEnumCaseMatchingRule.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,158 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct OptionalEnumCaseMatchingRule: SubstitutionCorrectableASTRule, ConfigurationProviderRule, | ||
AutomaticTestableRule, OptInRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "optional_enum_case_matching", | ||
name: "Optional Enum Case Match", | ||
description: "Matching an enum case against an optional enum without '?' is supported on Swift 5.1 and above. ", | ||
kind: .style, | ||
minSwiftVersion: .fiveDotOne, | ||
nonTriggeringExamples: [ | ||
""" | ||
switch foo { | ||
case .bar: break | ||
case .baz: break | ||
default: break | ||
} | ||
""" | ||
], | ||
triggeringExamples: [ | ||
""" | ||
switch foo { | ||
case .bar↓?: break | ||
case .baz: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case Foo.bar↓?: break | ||
case .baz: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case .bar↓?, .baz↓?: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case .bar↓? where x > 1: break | ||
case .baz: break | ||
default: break | ||
} | ||
""" | ||
], | ||
corrections: [ | ||
""" | ||
switch foo { | ||
case .bar↓?: break | ||
case .baz: break | ||
default: break | ||
} | ||
""": """ | ||
switch foo { | ||
case .bar: break | ||
case .baz: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case Foo.bar↓?: break | ||
case .baz: break | ||
default: break | ||
} | ||
""": """ | ||
switch foo { | ||
case Foo.bar: break | ||
case .baz: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case .bar↓?, .baz↓?: break | ||
default: break | ||
} | ||
""": """ | ||
switch foo { | ||
case .bar, .baz: break | ||
default: break | ||
} | ||
""", | ||
""" | ||
switch foo { | ||
case .bar↓? where x > 1: break | ||
case .baz: break | ||
default: break | ||
} | ||
""": """ | ||
switch foo { | ||
case .bar where x > 1: break | ||
case .baz: break | ||
default: break | ||
} | ||
""" | ||
] | ||
) | ||
|
||
// MARK: - ASTRule | ||
|
||
public func validate(file: SwiftLintFile, | ||
kind: StatementKind, | ||
dictionary: SourceKittenDictionary) -> [StyleViolation] { | ||
return violationRanges(in: file, kind: kind, dictionary: dictionary).map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
|
||
// MARK: - SubstitutionCorrectableASTRule | ||
|
||
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? { | ||
return (violationRange, "") | ||
} | ||
|
||
public func violationRanges(in file: SwiftLintFile, | ||
kind: StatementKind, | ||
dictionary: SourceKittenDictionary) -> [NSRange] { | ||
guard SwiftVersion.current >= type(of: self).description.minSwiftVersion, kind == .case else { | ||
return [] | ||
} | ||
|
||
let contents = file.contents.bridge() | ||
return dictionary.elements | ||
.filter { $0.kind == "source.lang.swift.structure.elem.pattern" } | ||
.compactMap { dictionary in | ||
guard let offset = dictionary.offset, let length = dictionary.length else { | ||
return nil | ||
} | ||
|
||
let tokens = file.syntaxMap | ||
.tokens(inByteRange: NSRange(location: offset, length: length)) | ||
.prefix(while: { $0.kind != .keyword }) | ||
|
||
guard let lastToken = tokens.last else { | ||
return nil | ||
} | ||
|
||
let questionMarkByteOffset = lastToken.length + lastToken.offset | ||
guard contents.substringWithByteRange(start: questionMarkByteOffset, length: 1) == "?", | ||
let range = contents.byteRangeToNSRange(start: questionMarkByteOffset, length: 1) else { | ||
return nil | ||
} | ||
|
||
return range | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could've put the cheaper check before the more expensive check