mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fa6bf50a22
A long-standing limitation with SourceKit's "editor open" request is
that we weren't able to get certain tokens, such as braces, brackets and
parentheses.
This meant that this code block would be counted as two lines:
```swift
print(
"hi"
)
```
because the trailing `)` would be treated as a whitespace line.
This meant that our "body length" family of rules that measure the
effective line count of declarations like functions, types or closures
would often significantly under-count the number of content lines in a
body.
Now with SwiftSyntax, we can get all tokens, including the ones
SourceKit was previously ignoring, so we can get much more accurate line
counts when ignoring whitespace and comments.
In addition, we weren't very thorough in how we measured body length.
As an exercise, how many lines long would you say the body of this
function is?
```swift
func hello() {
print("hello")
}
```
Does the body span one line or three lines?
I propose that we consistently ignore the left and right brace lines
when calculating the body line count of these scopes so that we measure
body line counts like this:
```swift
// 1 line
{ print("foo") }
// 1 line
{
}
// 1 line
{
print("foo")
}
// 2 lines
{
let sum = 1 + 2
print(sum)
}
```
Now with those changes in place, in order to keep the default
configuration thresholds to similar levels as before, we need to adjust
them slightly. Here's what I'm suggesting:
|Rule|Before|After|
|-|-|-|
|closure_body_length|20/100|30/100|
|function_body_length|40/100|50/100|
|type_body_length|200/350|250/350|
This is a pretty significant breaking change and I suspect we'll hear
from users who are surprised that some of their declarations now exceed
the rule limits, but I believe this new approach to calculating body
lines is more correct and intuitive compared to what we've had until
now.
OSSCheck is also going to report a bazillion changes with this, which is
expected given the scope of this change.
249 lines
6.4 KiB
Swift
249 lines
6.4 KiB
Swift
struct ImplicitGetterRuleExamples {
|
|
static let nonTriggeringExamples: [Example] = [
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
get { return 3 }
|
|
set { _abc = newValue }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
return 20
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
static var foo: Int {
|
|
return 20
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
static var foo: Int {
|
|
get { return 3 }
|
|
set { _abc = newValue }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
return getValueFromDisk()
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: String {
|
|
return "get"
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
protocol Foo {
|
|
var foo: Int { get }
|
|
}
|
|
"""),
|
|
Example("""
|
|
protocol Foo {
|
|
var foo: Int { get set }
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
struct Bar {
|
|
var bar: Int {
|
|
get { return 1 }
|
|
set { _ = newValue }
|
|
}
|
|
}
|
|
|
|
return Bar().bar
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
var _objCTaggedPointerBits: UInt {
|
|
@inline(__always) get { return 0 }
|
|
}
|
|
"""),
|
|
Example("""
|
|
var next: Int? {
|
|
mutating get {
|
|
defer { self.count += 1 }
|
|
return self.count
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Foo {
|
|
var bar: Bool {
|
|
get { _bar }
|
|
set { self._bar = newValue }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Foo {
|
|
var bar: Bool {
|
|
get { _bar }
|
|
set(newValue) { self._bar = newValue }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Float {
|
|
var clamped: Float {
|
|
set {
|
|
self = min(1, max(0, newValue))
|
|
}
|
|
get {
|
|
min(1, max(0, self))
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Reactive where Base: UITapGestureRecognizer {
|
|
var tapped: CocoaAction<Base>? {
|
|
get {
|
|
return associatedAction.withValue { $0.flatMap { $0.action } }
|
|
}
|
|
nonmutating set {
|
|
setAction(newValue)
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Test {
|
|
var foo: Bool {
|
|
get {
|
|
bar?.boolValue ?? true // Comment mentioning word set which triggers violation
|
|
}
|
|
set {
|
|
bar = NSNumber(value: newValue as Bool)
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
subscript(i: Int) -> Int {
|
|
return 20
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
subscript(i: Int) -> Int {
|
|
get { return 3 }
|
|
set { _abc = newValue }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
protocol Foo {
|
|
subscript(i: Int) -> Int { get }
|
|
}
|
|
"""),
|
|
Example("""
|
|
protocol Foo {
|
|
subscript(i: Int) -> Int { get set }
|
|
}
|
|
"""),
|
|
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 let triggeringExamples: [Example] = [
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
↓get {
|
|
return 20
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
var foo: Int {
|
|
↓get{ return 20 }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
static var foo: Int {
|
|
↓get {
|
|
return 20
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
var foo: Int {
|
|
↓get { return 20 }
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
@objc func bar() {}
|
|
var foo: Int {
|
|
↓get {
|
|
return 20
|
|
}
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
extension Foo {
|
|
var bar: Bool {
|
|
↓get { _bar }
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo {
|
|
subscript(i: Int) -> Int {
|
|
↓get {
|
|
return 20
|
|
}
|
|
}
|
|
}
|
|
""")
|
|
]
|
|
}
|