-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule to ensure
@State
properties are private
- Loading branch information
1 parent
a0e1476
commit 674a825
Showing
9 changed files
with
184 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// PrivateStateVariables.swift | ||
// SwiftFormatTests | ||
// | ||
// Created by Dave Paul on 9/13/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension FormatRule { | ||
static let privateStateVariables = FormatRule( | ||
help: "Adds `private` access control to @State properties without existing access control modifiers.", | ||
disabledByDefault: true | ||
) { formatter in | ||
formatter.forEachToken(where: { $0 == .keyword("@State") || $0 == .keyword("@StateObject") }) { stateIndex, _ in | ||
guard let endOfScope = formatter.index(after: stateIndex, where: { | ||
$0 == .keyword("let") || $0 == .keyword("var") | ||
}) else { return } | ||
|
||
// Don't override any existing access control: | ||
guard !formatter.tokens[stateIndex ..< endOfScope].contains(where: { | ||
_FormatRules.aclModifiers.contains($0.string) || _FormatRules.aclSetterModifiers.contains($0.string) | ||
}) else { | ||
return | ||
} | ||
|
||
// Check for @Previewable - we won't modify @Previewable macros. | ||
let lineStart = formatter.startOfLine(at: stateIndex) | ||
guard !formatter.tokens[lineStart ..< stateIndex].contains(where: { $0 == .keyword("@Previewable") }) else { | ||
return | ||
} | ||
|
||
formatter.insert([.keyword("private"), .space(" ")], at: endOfScope) | ||
} | ||
} examples: { | ||
""" | ||
```diff | ||
- @State var anInt: Int | ||
+ @State private var anInt: Int | ||
- @StateObject var myInstance: MyObject | ||
+ @StateObject private var myInstace: MyObject | ||
``` | ||
""" | ||
} | ||
} |
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
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,87 @@ | ||
// | ||
// PrivateStateVariablesTests.swift | ||
// SwiftFormatTests | ||
// | ||
// Created by Dave Paul on 9/9/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftFormat | ||
|
||
class PrivateStateVariablesTests: XCTestCase { | ||
func testPrivateState() { | ||
let input = """ | ||
@State var counter: Int | ||
""" | ||
let output = """ | ||
@State private var counter: Int | ||
""" | ||
testFormatting(for: input, output, rule: .privateStateVariables) | ||
} | ||
|
||
func testPrivateStateObject() { | ||
let input = """ | ||
@StateObject var counter: Int | ||
""" | ||
let output = """ | ||
@StateObject private var counter: Int | ||
""" | ||
testFormatting(for: input, output, rule: .privateStateVariables) | ||
} | ||
|
||
func testUseExisting() { | ||
let input = """ | ||
@State private var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
|
||
func testRespectingPublicOverride() { | ||
let input = """ | ||
@StateObject public var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
|
||
func testRespectingPackageOverride() { | ||
let input = """ | ||
@State package var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
|
||
func testRespectingOverrideWithSetterModifier() { | ||
let input = """ | ||
@State private(set) var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
|
||
func testRespectingOverrideWithExistingAccessAndSetterModifier() { | ||
let input = """ | ||
@StateObject public private(set) var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
|
||
func testStateVariableOnPreviousLine() { | ||
let input = """ | ||
@State | ||
var counter: Int | ||
""" | ||
let output = """ | ||
@State | ||
private var counter: Int | ||
""" | ||
testFormatting(for: input, output, rule: .privateStateVariables) | ||
} | ||
|
||
func testWithPreviewable() { | ||
// Don't add `private` to @Previewable property wrappers: | ||
let input = """ | ||
@Previewable @StateObject var counter: Int | ||
""" | ||
testFormatting(for: input, rule: .privateStateVariables) | ||
} | ||
} |
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