Skip to content

Commit

Permalink
Handle get async and get throws in implicit_getter
Browse files Browse the repository at this point in the history
Fixes #3684
  • Loading branch information
marcelofabri committed Oct 12, 2021
1 parent b621fb1 commit bbcdf2e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
[Artem Garmash](https://github.com/agarmash)
[#3651](https://github.com/realm/SwiftLint/issues/3651)

* Handle `get async` and `get throws` in the `implicit_getter` rule.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3684](https://github.com/realm/SwiftLint/issues/3684)

#### Bug Fixes

* Fixes a bug with the `missing_docs` rule where `excludes_inherited_types` would not be set.
Expand Down
2 changes: 2 additions & 0 deletions Source/SwiftLintFramework/Models/SwiftVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public extension SwiftVersion {
static let fiveDotThree = SwiftVersion(rawValue: "5.3.0")
/// Swift 5.4.x - https://swift.org/download/#swift-54
static let fiveDotFour = SwiftVersion(rawValue: "5.4.0")
/// Swift 5.5.x - https://swift.org/download/#swift-55
static let fiveDotFive = SwiftVersion(rawValue: "5.5.0")

/// The current detected Swift compiler version, based on the currently accessible SourceKit version.
///
Expand Down
18 changes: 17 additions & 1 deletion Source/SwiftLintFramework/Rules/Style/ImplicitGetterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct ImplicitGetterRule: ConfigurationProviderRule, AutomaticTestableRu
return nil
}
} else {
guard let range = dict.byteRange.map(file.stringView.byteRangeToNSRange) else {
guard let range = dict.byteRange.flatMap(file.stringView.byteRangeToNSRange) else {
return nil
}

Expand All @@ -51,6 +51,22 @@ public struct ImplicitGetterRule: ConfigurationProviderRule, AutomaticTestableRu
}
}

// If there's another keyword after `get`, it's allowed (e.g. `get async`)
if SwiftVersion.current >= .fiveDotFive {
guard let byteRange = dict.byteRange else {
return nil
}

let nextToken = file.syntaxMap.tokens(inByteRange: byteRange)
.first { $0.offset > token.offset }

let allowedKeywords: Set = ["throws", "async"]
if let nextToken = nextToken,
allowedKeywords.contains(file.contents(for: nextToken) ?? "") {
return nil
}
}

let kind = dict.declarationKind
return (token.offset, kind)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,7 @@ struct ImplicitGetterRuleExamples {
}
}
}
""")
]

guard SwiftVersion.current >= .fourDotOne else {
return commonExamples
}

return commonExamples + [
"""),
Example("""
class Foo {
subscript(i: Int) -> Int {
Expand All @@ -172,6 +165,36 @@ struct ImplicitGetterRuleExamples {
}
""")
]

guard SwiftVersion.current >= .fiveDotFive else {
return commonExamples
}

return commonExamples + [
Example("""
class DatabaseEntity {
var isSynced: Bool {
get async {
await database.isEntitySynced(self)
}
}
}
"""),
Example("""
struct Test {
subscript(value: Int) -> Int {
get throws {
if value == 0 {
throw NSError()
} else {
return value
}
}
}
}
""")
]

}

static var triggeringExamples: [Example] {
Expand Down

0 comments on commit bbcdf2e

Please sign in to comment.