mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
49 lines
1.5 KiB
Swift
49 lines
1.5 KiB
Swift
//
|
|
// RedundantGet.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 11/15/16.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Remove redundant `get {}` clause inside read-only computed property
|
|
static let redundantGet = FormatRule(
|
|
help: "Remove unneeded `get` clause inside computed properties."
|
|
) { formatter in
|
|
formatter.forEach(.identifier("get")) { i, _ in
|
|
if formatter.isAccessorKeyword(at: i, checkKeyword: false),
|
|
let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i, if: {
|
|
$0 == .startOfScope("{")
|
|
}), let openIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: {
|
|
$0 == .startOfScope("{")
|
|
}),
|
|
let closeIndex = formatter.index(of: .endOfScope("}"), after: openIndex),
|
|
let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: closeIndex, if: {
|
|
$0 == .endOfScope("}")
|
|
})
|
|
{
|
|
formatter.removeTokens(in: closeIndex ..< nextIndex)
|
|
formatter.removeTokens(in: prevIndex + 1 ... openIndex)
|
|
// TODO: fix-up indenting of lines in between removed braces
|
|
}
|
|
}
|
|
} examples: {
|
|
"""
|
|
```diff
|
|
var foo: Int {
|
|
- get {
|
|
- return 5
|
|
- }
|
|
}
|
|
|
|
var foo: Int {
|
|
+ return 5
|
|
}
|
|
```
|
|
"""
|
|
}
|
|
}
|