Skip to content

Commit 1b479d9

Browse files
Merge branch 'v0.3.0'
2 parents db9fb81 + 1ec163b commit 1b479d9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Sources/Sample/ABCD.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ extension KvEnvironmentScope {
5858
/// A property having no default value.
5959
var c: C
6060
}
61+
}
62+
/// Specify access modifiers (e.g. public, private, etc.) for the extensions
63+
/// to manage visibility of produced environment properties.
64+
private extension KvEnvironmentScope {
6165
/// Constant declarations are transformed to computed properties having getters only.
6266
#kvEnvironment { let a_ee: A = .init(a: 0xEE), a_ff: A? = .init(a: 0xFF) }
6367
}
68+
/// Below is an example of explicit declaration of an environment property.
69+
private extension KvEnvironmentScope {
70+
private struct aZeroKey : KvEnvironmentKey {
71+
static var defaultValue: A { .init(a: 0) }
72+
}
73+
var zeroA: A {
74+
get { self[aZeroKey.self] }
75+
set { self[aZeroKey.self] = newValue }
76+
}
77+
}

Sources/kvEnvironmentMacros/KvEnvironmentScopeEntryMacro.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public struct KvEnvironmentScopeEntryMacro: DeclarationMacro {
3333
) throws -> [DeclSyntax] {
3434
let closure: ClosureExprSyntax
3535

36+
try (node as? WithModifiersSyntax)?.modifiers.forEach {
37+
guard Constants.accessModifiers.contains($0.name)
38+
else { throw ExpansionError("unexpected access modifier `\($0.trimmedDescription)`. Apply access modifiers to the entire extension") }
39+
}
40+
3641
switch node.argumentList.last {
3742
case .none:
3843
guard let trailingClosure = node.trailingClosure
@@ -113,6 +118,19 @@ public struct KvEnvironmentScopeEntryMacro: DeclarationMacro {
113118
}
114119
}
115120

121+
// MARK: .Constants
122+
123+
private enum Constants {
124+
static var accessModifiers: Set<TokenSyntax> { [
125+
.keyword(.fileprivate),
126+
.keyword(.internal),
127+
.keyword(.open),
128+
.keyword(.package),
129+
.keyword(.private),
130+
.keyword(.public),
131+
] }
132+
}
133+
116134
// MARK: .EntryDescription
117135

118136
private struct EntryDescription {

0 commit comments

Comments
 (0)