Skip to content

Added setterAccessControlLevel #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ extension WithModifiersSyntax {
TokenKind.typeScopeSpecifiers.contains(name.tokenKind)
}
}
}

extension WithModifiersSyntax {

public var accessControlLevel: TokenSyntax? {
modifiers.lazy.map(\.name).first { name in
TokenKind.accessControlLevels.contains(name.tokenKind)
}
accessControlLevel(detail: nil)
}

public var setterAccessControlLevel: TokenSyntax? {
accessControlLevel(detail: .identifier("set")) ?? accessControlLevel
}

private func accessControlLevel(detail: TokenKind?) -> TokenSyntax? {
modifiers.lazy
.filter { $0.detail?.detail.tokenKind == detail }
.map(\.name)
.first { TokenKind.accessControlLevels.contains($0.tokenKind) }
}
}

extension WithModifiersSyntax {
Comment on lines +42 to +44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Consider adding test for combined access control modifiers

The current implementation handles setter-specific modifiers well, but I don't see any test for combined modifiers like public private(set) var.


🏁 Script executed:

#!/bin/bash
# Check if there are test cases for combined access control modifiers like 'public private(set) var'
grep -r "public\s\+private(set)" --include="*.swift" Tests/

Length of output: 61


To ensure we haven’t missed any existing tests around public private(set), let’s broaden our search:


🏁 Script executed:

#!/bin/bash
# Search for the exact combined modifier and any private(set) usage in Swift tests
grep -R -E "public\s+private\(set\)" --include="*.swift" Tests/ -n
grep -R "private(set)" --include="*.swift" Tests/ -n

Length of output: 238


Add tests for combined access control modifiers

We already have coverage for private(set) in Tests/PrincipleMacrosTests/Parsers/PropertiesParserTests.swift (line 37), but there’s no test exercising a declaration like

public private(set) var myVar = "Hello, world!"

Please add at least one parser test for the combined modifier scenario. For example, in PropertiesParserTests.swift:

func testPublicPrivateSetVar() throws {
    let source = "public private(set) var myVar = 42"
    let syntax = try parseDeclaration(source)
    XCTAssertTrue(syntax.modifiers.contains(.public))
    XCTAssertTrue(syntax.modifiers.contains(.privateSet))
    // …additional asserts…
}
🤖 Prompt for AI Agents
In Sources/PrincipleMacros/Syntax/Extensions/WithModifiersSyntax.swift around
lines 42 to 44, the review suggests adding a test for combined access control
modifiers like 'public private(set) var'. To fix this, add a new parser test in
Tests/PrincipleMacrosTests/Parsers/PropertiesParserTests.swift that parses a
declaration with combined modifiers, such as "public private(set) var myVar =
42", and assert that both .public and .privateSet modifiers are correctly
recognized in the syntax tree.


public func accessControlLevel(
inheritedBy inheritingDeclaration: InheritingDeclaration,
Expand Down
11 changes: 8 additions & 3 deletions Tests/PrincipleMacrosTests/Parsers/PropertiesParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ internal struct PropertiesParserTests {
@Test
func testStoredLet() throws {
let decl: DeclSyntax = """
public static let myLet: Int?
public internal(set) static let myLet: Int?
"""
let property = try #require(PropertiesParser.parse(declaration: decl, in: context).first)
#expect(property.kind == .stored)
#expect(property.mutability == .immutable)
#expect(property.accessControlLevel?.trimmedDescription == "public")
#expect(property.setterAccessControlLevel?.trimmedDescription == "internal")
#expect(property.typeScopeSpecifier?.trimmedDescription == "static")
#expect(property.trimmedName.description == "myLet")
#expect(property.inferredType.description == "Optional<Int>")
Expand All @@ -33,12 +34,13 @@ internal struct PropertiesParserTests {
@Test
func testStoredVar() throws {
let decl: DeclSyntax = """
private var myVar = "Hello, world!"
private(set) var myVar = "Hello, world!"
"""
let property = try #require(PropertiesParser.parse(declaration: decl, in: context).first)
#expect(property.kind == .stored)
#expect(property.mutability == .mutable)
#expect(property.accessControlLevel?.trimmedDescription == "private")
#expect(property.accessControlLevel == nil)
#expect(property.setterAccessControlLevel?.trimmedDescription == "private")
#expect(property.typeScopeSpecifier == nil)
#expect(property.trimmedName.description == "myVar")
#expect(property.inferredType.description == "String")
Expand All @@ -58,6 +60,7 @@ internal struct PropertiesParserTests {
#expect(property.kind == .stored)
#expect(property.mutability == .mutable)
#expect(property.accessControlLevel == nil)
#expect(property.setterAccessControlLevel == nil)
#expect(property.typeScopeSpecifier?.trimmedDescription == "class")
#expect(property.trimmedName.description == "myObservedVar")
#expect(property.inferredType.description == "UIView.Constraint")
Expand All @@ -77,6 +80,7 @@ internal struct PropertiesParserTests {
#expect(property.kind == .computed)
#expect(property.mutability == .immutable)
#expect(property.accessControlLevel?.trimmedDescription == "fileprivate")
#expect(property.setterAccessControlLevel?.trimmedDescription == "fileprivate")
#expect(property.typeScopeSpecifier == nil)
#expect(property.trimmedName.description == "myComputedVar")
#expect(property.inferredType.description == "Array<Model>")
Expand All @@ -96,6 +100,7 @@ internal struct PropertiesParserTests {
#expect(property.kind == .computed)
#expect(property.mutability == .mutable)
#expect(property.accessControlLevel == nil)
#expect(property.setterAccessControlLevel == nil)
#expect(property.typeScopeSpecifier?.trimmedDescription == "static")
#expect(property.trimmedName.description == "mySettableVar")
#expect(property.inferredType.description == "Optional<Array<Model>>")
Expand Down
Loading