Skip to content

Commit dea76b6

Browse files
authored
Add modifiers and isStatic fields to Property (#32)
1 parent a56e228 commit dea76b6

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/MacroToolkit/Property.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import SwiftSyntax
1010
public struct Property {
1111
public var _syntax: TokenSyntax
1212
public var attributes: [AttributeListElement]
13-
public var isLazy: Bool
13+
public var modifiers: [DeclModifierSyntax]
1414
public var keyword: String
1515
public var identifier: String
1616
public var type: Type?
1717
public var initialValue: Expr?
1818
public var accessors: [AccessorDeclSyntax]
1919

20+
public var isLazy: Bool {
21+
modifiers.contains { $0.name.text == "lazy" }
22+
}
23+
24+
public var isStatic: Bool {
25+
modifiers.contains { $0.name.text == "static" }
26+
}
27+
2028
public var getter: AccessorDeclSyntax? {
2129
accessors.first { $0.accessorSpecifier.tokenKind == .keyword(.get) }
2230
}
@@ -29,9 +37,10 @@ public struct Property {
2937
getter == nil
3038
}
3139

32-
static func properties(from binding: PatternBindingSyntax, in decl: Variable)
33-
-> [Property]
34-
{
40+
static func properties(
41+
from binding: PatternBindingSyntax,
42+
in decl: Variable
43+
) -> [Property] {
3544
let accessors: [AccessorDeclSyntax] =
3645
switch binding.accessorBlock?.accessors {
3746
case .accessors(let block):
@@ -53,8 +62,8 @@ public struct Property {
5362
type: (binding.typeAnnotation?.type).map(Type.init),
5463
accessors: accessors,
5564
attributes: attributes,
65+
modifiers: Array(decl._syntax.modifiers),
5666
keyword: decl._syntax.bindingSpecifier.text,
57-
isLazy: decl._syntax.modifiers.contains { $0.name.text == "lazy" }
5867
)
5968
}
6069

@@ -64,8 +73,8 @@ public struct Property {
6473
type: Type?,
6574
accessors: [AccessorDeclSyntax],
6675
attributes: [AttributeListElement],
67-
keyword: String,
68-
isLazy: Bool
76+
modifiers: [DeclModifierSyntax],
77+
keyword: String
6978
) -> [Property] {
7079
switch pattern.asProtocol(PatternSyntaxProtocol.self) {
7180
case let pattern as IdentifierPatternSyntax:
@@ -93,7 +102,7 @@ public struct Property {
93102
Property(
94103
_syntax: pattern.identifier,
95104
attributes: attributes,
96-
isLazy: isLazy,
105+
modifiers: modifiers,
97106
keyword: keyword,
98107
identifier: pattern.identifier.text,
99108
type: type,
@@ -127,9 +136,11 @@ public struct Property {
127136
initialValue.map { expr in
128137
Expr(
129138
MemberAccessExprSyntax(
130-
leadingTrivia: nil, base: expr._syntax.parenthesized,
139+
leadingTrivia: nil,
140+
base: expr._syntax.parenthesized,
131141
period: .periodToken(),
132-
name: .identifier(String(index)), trailingTrivia: nil
142+
name: .identifier(String(index)),
143+
trailingTrivia: nil
133144
)
134145
)
135146
}
@@ -148,8 +159,13 @@ public struct Property {
148159

149160
// Tuple bindings can't have accessors or attributes (i.e. property wrappers or macros)
150161
return properties(
151-
pattern: element.pattern, initialValue: initialValue, type: type,
152-
accessors: [], attributes: [], keyword: keyword, isLazy: isLazy
162+
pattern: element.pattern,
163+
initialValue: initialValue,
164+
type: type,
165+
accessors: [],
166+
attributes: [],
167+
modifiers: modifiers,
168+
keyword: keyword
153169
)
154170
}
155171
case _ as WildcardPatternSyntax:

0 commit comments

Comments
 (0)