Skip to content

Commit f398c48

Browse files
committed
Added setterAccessControlLevel
1 parent 22edb9c commit f398c48

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Sources/PrincipleMacros/Syntax/Extensions/WithModifiersSyntax.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,27 @@ extension WithModifiersSyntax {
2121
TokenKind.typeScopeSpecifiers.contains(name.tokenKind)
2222
}
2323
}
24+
}
25+
26+
extension WithModifiersSyntax {
2427

2528
public var accessControlLevel: TokenSyntax? {
26-
modifiers.lazy.map(\.name).first { name in
27-
TokenKind.accessControlLevels.contains(name.tokenKind)
28-
}
29+
accessControlLevel(detail: nil)
30+
}
31+
32+
public var setterAccessControlLevel: TokenSyntax? {
33+
accessControlLevel(detail: .identifier("set")) ?? accessControlLevel
34+
}
35+
36+
private func accessControlLevel(detail: TokenKind?) -> TokenSyntax? {
37+
modifiers.lazy
38+
.filter { $0.detail?.detail.tokenKind == detail }
39+
.map(\.name)
40+
.first { TokenKind.accessControlLevels.contains($0.tokenKind) }
2941
}
42+
}
43+
44+
extension WithModifiersSyntax {
3045

3146
public func accessControlLevel(
3247
inheritedBy inheritingDeclaration: InheritingDeclaration,

Tests/PrincipleMacrosTests/Parsers/PropertiesParserTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal struct PropertiesParserTests {
2323
#expect(property.kind == .stored)
2424
#expect(property.mutability == .immutable)
2525
#expect(property.accessControlLevel?.trimmedDescription == "public")
26+
#expect(property.setterAccessControlLevel?.trimmedDescription == "public")
2627
#expect(property.typeScopeSpecifier?.trimmedDescription == "static")
2728
#expect(property.trimmedName.description == "myLet")
2829
#expect(property.inferredType.description == "Optional<Int>")
@@ -33,12 +34,13 @@ internal struct PropertiesParserTests {
3334
@Test
3435
func testStoredVar() throws {
3536
let decl: DeclSyntax = """
36-
private var myVar = "Hello, world!"
37+
private(set) var myVar = "Hello, world!"
3738
"""
3839
let property = try #require(PropertiesParser.parse(declaration: decl, in: context).first)
3940
#expect(property.kind == .stored)
4041
#expect(property.mutability == .mutable)
41-
#expect(property.accessControlLevel?.trimmedDescription == "private")
42+
#expect(property.accessControlLevel == nil)
43+
#expect(property.setterAccessControlLevel?.trimmedDescription == "private")
4244
#expect(property.typeScopeSpecifier == nil)
4345
#expect(property.trimmedName.description == "myVar")
4446
#expect(property.inferredType.description == "String")
@@ -58,6 +60,7 @@ internal struct PropertiesParserTests {
5860
#expect(property.kind == .stored)
5961
#expect(property.mutability == .mutable)
6062
#expect(property.accessControlLevel == nil)
63+
#expect(property.setterAccessControlLevel == nil)
6164
#expect(property.typeScopeSpecifier?.trimmedDescription == "class")
6265
#expect(property.trimmedName.description == "myObservedVar")
6366
#expect(property.inferredType.description == "UIView.Constraint")
@@ -77,6 +80,7 @@ internal struct PropertiesParserTests {
7780
#expect(property.kind == .computed)
7881
#expect(property.mutability == .immutable)
7982
#expect(property.accessControlLevel?.trimmedDescription == "fileprivate")
83+
#expect(property.setterAccessControlLevel?.trimmedDescription == "fileprivate")
8084
#expect(property.typeScopeSpecifier == nil)
8185
#expect(property.trimmedName.description == "myComputedVar")
8286
#expect(property.inferredType.description == "Array<Model>")
@@ -96,6 +100,7 @@ internal struct PropertiesParserTests {
96100
#expect(property.kind == .computed)
97101
#expect(property.mutability == .mutable)
98102
#expect(property.accessControlLevel == nil)
103+
#expect(property.setterAccessControlLevel == nil)
99104
#expect(property.typeScopeSpecifier?.trimmedDescription == "static")
100105
#expect(property.trimmedName.description == "mySettableVar")
101106
#expect(property.inferredType.description == "Optional<Array<Model>>")

0 commit comments

Comments
 (0)