@expo/expo-modules-macros-plugin 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/resources/expo-modules-macros.svg +23 -0
- package/.github/workflows/publish.yml +4 -0
- package/.github/workflows/swift.yml +6 -0
- package/README.md +119 -0
- package/apple/ExpoModulesMacros-tool +0 -0
- package/apple/Sources/ExpoModulesMacros/DecorateModuleBuilder.swift +3 -1
- package/apple/Sources/ExpoModulesMacros/ExpoModuleMacro.swift +17 -0
- package/apple/Sources/ExpoModulesMacros/ExpoViewMacro.swift +259 -0
- package/apple/Sources/ExpoModulesMacros/JSMacro.swift +78 -2
- package/apple/Sources/ExpoModulesMacros/MacroHelpers.swift +89 -2
- package/apple/Sources/ExpoModulesMacros/Plugin.swift +3 -0
- package/apple/Sources/ExpoModulesMacros/RecordMacro.swift +0 -47
- package/apple/Sources/ExpoModulesMacros/SharedObjectMacro.swift +8 -2
- package/apple/Sources/ExpoModulesMacros/TypeConformanceAssertion.swift +6 -0
- package/apple/Sources/ExpoModulesMacros/UnionMacro.swift +365 -0
- package/apple/Sources/ExpoModulesMacros/ViewPropsMacro.swift +487 -0
- package/apple/Sources/ExpoModulesScanner/CLI.swift +8 -4
- package/apple/Sources/ExpoModulesScanner/Core/Detection.swift +4 -4
- package/apple/Sources/ExpoModulesScanner/Core/DetectionVisitor.swift +28 -7
- package/apple/Sources/ExpoModulesScanner/Core/ScanBuildConfiguration.swift +3 -9
- package/apple/Sources/ExpoModulesScanner/Core/SourceScan.swift +17 -7
- package/apple/Sources/ExpoModulesScanner/Exports/ExportedSurface.swift +7 -0
- package/apple/Sources/ExpoModulesScanner/Exports/ScanExports.swift +1 -0
- package/apple/Sources/ExpoModulesScanner/Modules/ScanModules.swift +89 -22
- package/build/index.d.ts +51 -0
- package/build/index.js +153 -0
- package/build/types.d.ts +186 -0
- package/build/types.js +15 -0
- package/package.json +12 -2
|
@@ -31,6 +31,44 @@ internal func boolArgument(of attribute: AttributeSyntax, label: String) -> Bool
|
|
|
31
31
|
return nil
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/// True if the attribute lists the given `JSOptions` member, e.g. `@JS(.concurrent)` or
|
|
35
|
+
/// `@JS("name", [.concurrent])` -> true for "concurrent". Options are written as member-access
|
|
36
|
+
/// expressions (`.concurrent`), optionally inside an array literal when more than one is combined, so
|
|
37
|
+
/// both spellings are scanned. The leading string literal, when present, is the JS name and is skipped.
|
|
38
|
+
internal func hasJSOption(_ attribute: AttributeSyntax, named option: String) -> Bool {
|
|
39
|
+
guard let args = attribute.arguments?.as(LabeledExprListSyntax.self) else {
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
for arg in args {
|
|
43
|
+
if namesOption(arg.expression, option) {
|
|
44
|
+
return true
|
|
45
|
+
}
|
|
46
|
+
if let array = arg.expression.as(ArrayExprSyntax.self),
|
|
47
|
+
array.elements.contains(where: { namesOption($0.expression, option) }) {
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// True when the expression is the member access `.<option>` (or `JSOptions.<option>`).
|
|
55
|
+
private func namesOption(_ expression: ExprSyntax, _ option: String) -> Bool {
|
|
56
|
+
guard let member = expression.as(MemberAccessExprSyntax.self) else {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
return member.declName.baseName.text == option
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// True if the `@JS`-marked declaration opted into running off the JavaScript thread with
|
|
63
|
+
/// `@JS(.concurrent)`. Such a member is left unstamped and gets `@concurrent` instead, so its body
|
|
64
|
+
/// runs on the concurrent pool rather than inheriting the JS thread.
|
|
65
|
+
internal func isConcurrentJSMember(_ decl: DeclSyntaxProtocol) -> Bool {
|
|
66
|
+
guard let attribute = memberAttributes(of: decl).firstAttribute(named: "JS") else {
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
return hasJSOption(attribute, named: "concurrent")
|
|
70
|
+
}
|
|
71
|
+
|
|
34
72
|
/// True if the type is written as an optional: `T?`, `T!`, or the explicit `Optional<T>`. Used to
|
|
35
73
|
/// decide argument requiredness (an optional parameter may be omitted) and record-field nullability.
|
|
36
74
|
internal func isOptionalType(_ type: TypeSyntax) -> Bool {
|
|
@@ -97,10 +135,17 @@ internal func classListArgument(of attribute: AttributeSyntax, label: String) ->
|
|
|
97
135
|
return array.elements.compactMap { element -> String? in
|
|
98
136
|
guard let memberAccess = element.expression.as(MemberAccessExprSyntax.self),
|
|
99
137
|
memberAccess.declName.baseName.text == "self",
|
|
100
|
-
let base = memberAccess.base
|
|
138
|
+
let base = memberAccess.base else {
|
|
101
139
|
return nil
|
|
102
140
|
}
|
|
103
|
-
|
|
141
|
+
// A bare `CardView.self` has a `DeclReferenceExprSyntax` base; a qualified
|
|
142
|
+
// `Outer.CardView.self` has a `MemberAccessExprSyntax` one. Both name a type the generated
|
|
143
|
+
// code can spell, so take the base verbatim rather than only its last component: dropping the
|
|
144
|
+
// qualified form would silently omit the entry, and the type would never be registered.
|
|
145
|
+
if base.is(DeclReferenceExprSyntax.self) || base.is(MemberAccessExprSyntax.self) {
|
|
146
|
+
return base.trimmedDescription
|
|
147
|
+
}
|
|
148
|
+
return nil
|
|
104
149
|
}
|
|
105
150
|
}
|
|
106
151
|
return []
|
|
@@ -156,6 +201,7 @@ internal func memberHasJSAttribute(_ decl: DeclSyntaxProtocol) -> Bool {
|
|
|
156
201
|
|
|
157
202
|
/// Decides whether the macro should stamp `@JavaScriptActor` on a `@JS`-marked member.
|
|
158
203
|
/// The macro defers to the user when they've already chosen an isolation:
|
|
204
|
+
/// - the member opted out with `@JS(.concurrent)`
|
|
159
205
|
/// - the `nonisolated` modifier is present on the member
|
|
160
206
|
/// - any attribute whose name matches a known global actor (`@MainActor`, `@JavaScriptActor`)
|
|
161
207
|
/// or follows the `*Actor` naming convention is present on the member or its enclosing type
|
|
@@ -165,6 +211,10 @@ internal func shouldStampJavaScriptActor(
|
|
|
165
211
|
on member: DeclSyntaxProtocol,
|
|
166
212
|
enclosedBy enclosing: some DeclGroupSyntax
|
|
167
213
|
) -> Bool {
|
|
214
|
+
if isConcurrentJSMember(member) {
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
|
|
168
218
|
let modifiers = memberModifiers(of: member)
|
|
169
219
|
if modifiers.contains(where: { $0.name.text == "nonisolated" }) {
|
|
170
220
|
return false
|
|
@@ -326,3 +376,40 @@ internal func bindingIsSettable(_ binding: PatternBindingSyntax) -> Bool {
|
|
|
326
376
|
return false
|
|
327
377
|
}
|
|
328
378
|
}
|
|
379
|
+
|
|
380
|
+
/// True if the variable declaration carries a modifier that excludes it from being a stored property
|
|
381
|
+
/// of the surface: `static`, `class` (type-level storage), `private`, `fileprivate`, or `lazy`.
|
|
382
|
+
/// Shared by `@Record` and `@ViewProps`, which apply the same "every stored property counts" rule.
|
|
383
|
+
internal func isExcludedByModifier(_ modifiers: DeclModifierListSyntax) -> Bool {
|
|
384
|
+
for modifier in modifiers {
|
|
385
|
+
switch modifier.name.tokenKind {
|
|
386
|
+
case .keyword(.static), .keyword(.class), .keyword(.private), .keyword(.fileprivate), .keyword(.lazy):
|
|
387
|
+
return true
|
|
388
|
+
default:
|
|
389
|
+
continue
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return false
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/// True if the type's inheritance clause already lists a protocol with the given name. Matches
|
|
396
|
+
/// either the bare identifier (`Record`) or a qualified member access ending in the name
|
|
397
|
+
/// (`ExpoModulesCore.Record`). Used by the extension macros to skip a conformance the author already
|
|
398
|
+
/// spelled out. Works for `struct`, `class`, and `enum` declarations; any other declaration kind has no
|
|
399
|
+
/// inheritance clause to read and reports `false`.
|
|
400
|
+
internal func inheritsProtocol(named name: String, in declaration: some DeclGroupSyntax) -> Bool {
|
|
401
|
+
let inheritanceClause: InheritanceClauseSyntax?
|
|
402
|
+
if let structDecl = declaration.as(StructDeclSyntax.self) {
|
|
403
|
+
inheritanceClause = structDecl.inheritanceClause
|
|
404
|
+
} else if let classDecl = declaration.as(ClassDeclSyntax.self) {
|
|
405
|
+
inheritanceClause = classDecl.inheritanceClause
|
|
406
|
+
} else if let enumDecl = declaration.as(EnumDeclSyntax.self) {
|
|
407
|
+
inheritanceClause = enumDecl.inheritanceClause
|
|
408
|
+
} else {
|
|
409
|
+
return false
|
|
410
|
+
}
|
|
411
|
+
guard let inherited = inheritanceClause?.inheritedTypes else {
|
|
412
|
+
return false
|
|
413
|
+
}
|
|
414
|
+
return inherited.contains { baseIdentifier(of: $0.type) == name }
|
|
415
|
+
}
|
|
@@ -462,53 +462,6 @@ private func initializerParameterLabels(of declaration: some DeclGroupSyntax) ->
|
|
|
462
462
|
return signatures
|
|
463
463
|
}
|
|
464
464
|
|
|
465
|
-
/**
|
|
466
|
-
True if the variable declaration carries a modifier that excludes it from being a property:
|
|
467
|
-
`static`, `class` (type-level storage), `private`, `fileprivate`, or `lazy`.
|
|
468
|
-
*/
|
|
469
|
-
private func isExcludedByModifier(_ modifiers: DeclModifierListSyntax) -> Bool {
|
|
470
|
-
for modifier in modifiers {
|
|
471
|
-
switch modifier.name.tokenKind {
|
|
472
|
-
case .keyword(.static), .keyword(.class), .keyword(.private), .keyword(.fileprivate), .keyword(.lazy):
|
|
473
|
-
return true
|
|
474
|
-
default:
|
|
475
|
-
continue
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
return false
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
True if the type's inheritance clause already lists a protocol with the given name.
|
|
483
|
-
Matches either the bare identifier (`Record`) or a qualified member access ending in
|
|
484
|
-
the name (`ExpoModulesCore.Record`).
|
|
485
|
-
*/
|
|
486
|
-
private func inheritsProtocol(named name: String, in declaration: some DeclGroupSyntax) -> Bool {
|
|
487
|
-
let inheritanceClause: InheritanceClauseSyntax?
|
|
488
|
-
if let structDecl = declaration.as(StructDeclSyntax.self) {
|
|
489
|
-
inheritanceClause = structDecl.inheritanceClause
|
|
490
|
-
} else if let classDecl = declaration.as(ClassDeclSyntax.self) {
|
|
491
|
-
inheritanceClause = classDecl.inheritanceClause
|
|
492
|
-
} else {
|
|
493
|
-
return false
|
|
494
|
-
}
|
|
495
|
-
guard let inherited = inheritanceClause?.inheritedTypes else {
|
|
496
|
-
return false
|
|
497
|
-
}
|
|
498
|
-
for entry in inherited {
|
|
499
|
-
let typeSyntax = entry.type
|
|
500
|
-
if let identifier = typeSyntax.as(IdentifierTypeSyntax.self),
|
|
501
|
-
identifier.name.text == name {
|
|
502
|
-
return true
|
|
503
|
-
}
|
|
504
|
-
if let member = typeSyntax.as(MemberTypeSyntax.self),
|
|
505
|
-
member.name.text == name {
|
|
506
|
-
return true
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
return false
|
|
510
|
-
}
|
|
511
|
-
|
|
512
465
|
/**
|
|
513
466
|
True if the class declaration has any inheritance clause. Used as a heuristic for
|
|
514
467
|
whether the superclass also conforms to `Record` and provides the synthesized methods;
|
|
@@ -141,8 +141,14 @@ extension SharedObjectMacro: MemberAttributeMacro {
|
|
|
141
141
|
// `@Event(sync: true)` members are stamped alongside `@JS` ones: a sync event dispatches
|
|
142
142
|
// inline, so the isolation forces its call site onto the JS thread. Async events (the
|
|
143
143
|
// default) stay unstamped — their `emit` schedules onto the JS thread itself.
|
|
144
|
-
guard memberHasJSAttribute(member) || isSyncEventMember(member)
|
|
145
|
-
|
|
144
|
+
guard memberHasJSAttribute(member) || isSyncEventMember(member) else {
|
|
145
|
+
return []
|
|
146
|
+
}
|
|
147
|
+
// `@JS(.concurrent)` opts the member out of the JS-thread stamp and onto the concurrent pool.
|
|
148
|
+
if isConcurrentJSMember(member) {
|
|
149
|
+
return ["@concurrent"]
|
|
150
|
+
}
|
|
151
|
+
guard shouldStampJavaScriptActor(on: member, enclosedBy: declaration) else {
|
|
146
152
|
return []
|
|
147
153
|
}
|
|
148
154
|
return ["@JavaScriptActor"]
|
|
@@ -21,6 +21,12 @@ internal let javaScriptEncodableProtocolName = "JavaScriptEncodable"
|
|
|
21
21
|
/// support both directions, so the assertion requires the intersection.
|
|
22
22
|
internal let recordFieldProtocolName = "AnyArgument & JavaScriptDecodable & JavaScriptEncodable"
|
|
23
23
|
|
|
24
|
+
/// The constraint a `@Union` case payload type must satisfy: the union decodes by trying each payload's
|
|
25
|
+
/// `decode` and encodes through the matching payload's `encode`, so every alternative must convert in
|
|
26
|
+
/// both directions. Unlike a `@Record` field, a union never crosses the native-`Any` dictionary path, so
|
|
27
|
+
/// `AnyArgument` is not required.
|
|
28
|
+
internal let unionPayloadProtocolName = "JavaScriptDecodable & JavaScriptEncodable"
|
|
29
|
+
|
|
24
30
|
/// The protocol a type must conform to for `self.emit(event:…)` to resolve; core conforms
|
|
25
31
|
/// `BaseModule` and `SharedObject` to it. Asserted by `@Event` so attaching it to a type that can't
|
|
26
32
|
/// emit fails with a conformance diagnostic instead of an opaque "no member 'emit'" error.
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import SwiftDiagnostics
|
|
2
|
+
import SwiftSyntax
|
|
3
|
+
import SwiftSyntaxBuilder
|
|
4
|
+
import SwiftSyntaxMacros
|
|
5
|
+
|
|
6
|
+
/// Member + extension macro applied to an `enum` whose cases each carry one associated value: a typed
|
|
7
|
+
/// union of the payload types (`A | B | C` in TypeScript). The enum is a tagged union at the Swift
|
|
8
|
+
/// level, so the author switches over it exhaustively with each payload keeping its static type, and
|
|
9
|
+
/// the macro synthesizes the conversion surface that makes it a JS boundary type:
|
|
10
|
+
///
|
|
11
|
+
/// - `decode(_:in:)`: an ordered decode that tries each case's payload converter in declaration order
|
|
12
|
+
/// and returns the first case that decodes; when none does, it throws
|
|
13
|
+
/// `Exceptions.UnionCaseMismatch` naming the union, the JS kind received, and the alternatives.
|
|
14
|
+
/// - `encode(_:in:)`: a `switch` over the cases, encoding the payload through its own type.
|
|
15
|
+
/// - `as(_:)`: one throwing overload per case, keyed by the payload's metatype, returning that payload
|
|
16
|
+
/// (`try source.as(String.self)` is `String`; `try? source.as(String.self)` is `String?`). It unwraps
|
|
17
|
+
/// by type without naming the case and throws `Exceptions.UnionCaseMismatch` when the union holds a
|
|
18
|
+
/// different case. Since a payload type may appear only once, each overload is unambiguous, and asking
|
|
19
|
+
/// for a type the union doesn't carry is a compile error.
|
|
20
|
+
///
|
|
21
|
+
/// The type is auto-conformed to `JavaScriptDecodable` and `JavaScriptEncodable`, so it can be a `@JS`
|
|
22
|
+
/// argument or return value, an `@Event` payload, or nested inside an optional, array, or dictionary.
|
|
23
|
+
/// Author-facing shape:
|
|
24
|
+
///
|
|
25
|
+
/// @Union
|
|
26
|
+
/// enum Source {
|
|
27
|
+
/// case text(String)
|
|
28
|
+
/// case options(SourceOptions) // a @Record
|
|
29
|
+
/// }
|
|
30
|
+
///
|
|
31
|
+
/// Discrimination is structural and order-dependent: the first case whose payload decodes wins. When
|
|
32
|
+
/// two payload shapes overlap (two records with compatible fields, `Int` and `Double`), the earlier
|
|
33
|
+
/// case matches; the author orders the more specific case first. A case may not repeat another case's
|
|
34
|
+
/// payload type, since it could never be chosen.
|
|
35
|
+
///
|
|
36
|
+
/// The named-union counterpart of core's `Either`: any number of named cases instead of two anonymous
|
|
37
|
+
/// slots, no `Any?` box, and an exhaustive `switch` as the primary way to read it (`as(_:)` covers the
|
|
38
|
+
/// one-type lookup `Either.as(_:)` offered, with the same throwing shape).
|
|
39
|
+
public struct UnionMacro: MemberMacro, ExtensionMacro {
|
|
40
|
+
public static func expansion(
|
|
41
|
+
of node: AttributeSyntax,
|
|
42
|
+
providingMembersOf declaration: some DeclGroupSyntax,
|
|
43
|
+
conformingTo protocols: [TypeSyntax],
|
|
44
|
+
in context: some MacroExpansionContext
|
|
45
|
+
) throws -> [DeclSyntax] {
|
|
46
|
+
let union = try validatedUnion(of: declaration)
|
|
47
|
+
|
|
48
|
+
var members: [DeclSyntax] = []
|
|
49
|
+
|
|
50
|
+
// A single never-called member that makes the compiler verify each payload type converts both
|
|
51
|
+
// ways. Each case keeps its own named assertion inside, so the conformance diagnostic names the
|
|
52
|
+
// offending case (see `typeConformanceAssertions`). Emitted first so a non-conforming payload
|
|
53
|
+
// reports the clear "requires that '…' conform to …" error ahead of the noisier "no member
|
|
54
|
+
// 'decode'"/"'encode'" errors from the conversion code below.
|
|
55
|
+
let assertions = union.cases.map { ConformanceAssertion(name: $0.name, types: [$0.payloadType]) }
|
|
56
|
+
if let assertionMember = typeConformanceAssertions(for: assertions, constraint: unionPayloadProtocolName) {
|
|
57
|
+
members.append(assertionMember)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
members.append(decodeMethod(union: union))
|
|
61
|
+
members.append(encodeMethod(union: union))
|
|
62
|
+
members.append(contentsOf: accessorMethods(union: union))
|
|
63
|
+
members.append(payloadTypeNameProperty(union: union))
|
|
64
|
+
return members
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Auto-conforms the enum to `JavaScriptDecodable` and `JavaScriptEncodable`, the protocols whose
|
|
68
|
+
/// requirements are exactly the `decode`/`encode` members synthesized above. A conformance the author
|
|
69
|
+
/// already spelled out in the inheritance clause is not repeated.
|
|
70
|
+
public static func expansion(
|
|
71
|
+
of node: AttributeSyntax,
|
|
72
|
+
attachedTo declaration: some DeclGroupSyntax,
|
|
73
|
+
providingExtensionsOf type: some TypeSyntaxProtocol,
|
|
74
|
+
conformingTo protocols: [TypeSyntax],
|
|
75
|
+
in context: some MacroExpansionContext
|
|
76
|
+
) throws -> [ExtensionDeclSyntax] {
|
|
77
|
+
// Diagnostics are owned by the member expansion; an invalid declaration silently emits no extension
|
|
78
|
+
// here, so each error is reported once and no witness-less conformance piles "does not conform"
|
|
79
|
+
// errors on top of it.
|
|
80
|
+
guard (try? validatedUnion(of: declaration)) != nil else {
|
|
81
|
+
return []
|
|
82
|
+
}
|
|
83
|
+
// The compiler hands over only the conformances the type still lacks; the test harness passes the
|
|
84
|
+
// declared list verbatim, so filter against the inheritance clause here as well.
|
|
85
|
+
let missing = protocols.filter { protocolType in
|
|
86
|
+
guard let name = baseIdentifier(of: protocolType) else {
|
|
87
|
+
return true
|
|
88
|
+
}
|
|
89
|
+
return !inheritsProtocol(named: name, in: declaration)
|
|
90
|
+
}
|
|
91
|
+
guard !missing.isEmpty else {
|
|
92
|
+
return []
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let conformances = missing.map { $0.trimmedDescription }.joined(separator: ", ")
|
|
96
|
+
let ext: DeclSyntax = """
|
|
97
|
+
extension \(type.trimmed): \(raw: conformances) {}
|
|
98
|
+
"""
|
|
99
|
+
guard let extDecl = ext.as(ExtensionDeclSyntax.self) else {
|
|
100
|
+
return []
|
|
101
|
+
}
|
|
102
|
+
return [extDecl]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// MARK: - Union model
|
|
107
|
+
|
|
108
|
+
/// A validated `@Union` enum: its spelled name (for the mismatch error) and its cases in declaration
|
|
109
|
+
/// order (the decode order).
|
|
110
|
+
private struct UnionType {
|
|
111
|
+
let name: String
|
|
112
|
+
let cases: [UnionCase]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// One alternative of the union: the case name, its single payload type as written, and the payload's
|
|
116
|
+
/// argument label when the author gave it one (`case id(value: Int)`), needed to construct the case.
|
|
117
|
+
private struct UnionCase {
|
|
118
|
+
let name: String
|
|
119
|
+
let payloadType: String
|
|
120
|
+
let payloadLabel: String?
|
|
121
|
+
|
|
122
|
+
/// The expression constructing this case from a `payload` local: `.id(payload)`, or
|
|
123
|
+
/// `.id(value: payload)` for a labeled associated value.
|
|
124
|
+
var construction: String {
|
|
125
|
+
if let payloadLabel {
|
|
126
|
+
return ".\(name)(\(payloadLabel): payload)"
|
|
127
|
+
}
|
|
128
|
+
return ".\(name)(payload)"
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// The pattern binding this case's payload to a `payload` local in a `switch`.
|
|
132
|
+
var pattern: String {
|
|
133
|
+
return ".\(name)(let payload)"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Reads and validates the union off the attached declaration. Every check is a compile error located
|
|
138
|
+
/// on the offending node: the macro must be on a non-generic `enum` with at least one case, and every
|
|
139
|
+
/// case must carry exactly one associated value, its payload type distinct from every earlier case's.
|
|
140
|
+
private func validatedUnion(of declaration: some DeclGroupSyntax) throws -> UnionType {
|
|
141
|
+
guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
|
|
142
|
+
throw MacroExpansionErrorMessage("@Union can only be applied to an enum")
|
|
143
|
+
}
|
|
144
|
+
if let genericParameterClause = enumDecl.genericParameterClause {
|
|
145
|
+
throw DiagnosticsError(diagnostics: [
|
|
146
|
+
Diagnostic(
|
|
147
|
+
node: genericParameterClause,
|
|
148
|
+
message: UnionDiagnosticMessage(
|
|
149
|
+
"@Union cannot be applied to a generic enum: each case's payload type must be concrete so the macro can select its converter.",
|
|
150
|
+
id: "union-generic-enum"
|
|
151
|
+
))
|
|
152
|
+
])
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var cases: [UnionCase] = []
|
|
156
|
+
var seenPayloadTypes: [String: String] = [:]
|
|
157
|
+
var diagnostics: [Diagnostic] = []
|
|
158
|
+
|
|
159
|
+
for member in enumDecl.memberBlock.members {
|
|
160
|
+
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else {
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
for element in caseDecl.elements {
|
|
164
|
+
let name = element.name.text
|
|
165
|
+
guard let parameters = element.parameterClause?.parameters, !parameters.isEmpty else {
|
|
166
|
+
diagnostics.append(
|
|
167
|
+
Diagnostic(
|
|
168
|
+
node: element,
|
|
169
|
+
message: UnionDiagnosticMessage(
|
|
170
|
+
"@Union case '\(name)' must carry exactly one associated value: the type this alternative decodes from. A payload-less enum is not a union; make it a raw-value enum conforming to 'Enumerable' instead.",
|
|
171
|
+
id: "union-case-without-payload"
|
|
172
|
+
)))
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
guard parameters.count == 1, let parameter = parameters.first else {
|
|
176
|
+
diagnostics.append(
|
|
177
|
+
Diagnostic(
|
|
178
|
+
node: element,
|
|
179
|
+
message: UnionDiagnosticMessage(
|
|
180
|
+
"@Union case '\(name)' must carry exactly one associated value, but has \(parameters.count). Group them in a @Record type and use it as the single payload.",
|
|
181
|
+
id: "union-case-with-multiple-payloads"
|
|
182
|
+
)))
|
|
183
|
+
continue
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// A default on the associated value can never apply: the macro constructs the case from the
|
|
187
|
+
// decoded JS value every time, and there is no "omitted" slot the way a `@Record` property has.
|
|
188
|
+
// Left in place it would read as a JS-side default that doesn't exist, so it's rejected with a
|
|
189
|
+
// fix-it that removes it.
|
|
190
|
+
if let defaultValue = parameter.defaultValue {
|
|
191
|
+
diagnostics.append(defaultValueDiagnostic(for: parameter, defaultValue: defaultValue, caseName: name))
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let payloadType = parameter.type.trimmedDescription
|
|
196
|
+
// Alternatives decode in declaration order and the first success wins, so a case whose payload
|
|
197
|
+
// type repeats an earlier case's is unreachable: an exact-spelling duplicate is an error. (An
|
|
198
|
+
// overlap between *different* types, like `Int` and `Double`, is the author's ordering call and
|
|
199
|
+
// isn't checked here.)
|
|
200
|
+
let normalizedType = payloadType.filter { !$0.isWhitespace }
|
|
201
|
+
if let earlierCase = seenPayloadTypes[normalizedType] {
|
|
202
|
+
diagnostics.append(
|
|
203
|
+
Diagnostic(
|
|
204
|
+
node: element,
|
|
205
|
+
message: UnionDiagnosticMessage(
|
|
206
|
+
"@Union case '\(name)' repeats the payload type '\(payloadType)' of case '\(earlierCase)' and can never be decoded: alternatives are tried in declaration order and the first match wins.",
|
|
207
|
+
id: "union-duplicate-payload-type"
|
|
208
|
+
)))
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
seenPayloadTypes[normalizedType] = name
|
|
212
|
+
|
|
213
|
+
// `firstName` is the associated value's label (`case id(value: Int)`); a `_` label is the same as
|
|
214
|
+
// none for construction purposes.
|
|
215
|
+
let label = parameter.firstName.flatMap { $0.text == "_" ? nil : $0.text }
|
|
216
|
+
cases.append(UnionCase(name: name, payloadType: payloadType, payloadLabel: label))
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if !diagnostics.isEmpty {
|
|
221
|
+
throw DiagnosticsError(diagnostics: diagnostics)
|
|
222
|
+
}
|
|
223
|
+
if cases.isEmpty {
|
|
224
|
+
throw MacroExpansionErrorMessage("@Union requires at least one case carrying an associated value")
|
|
225
|
+
}
|
|
226
|
+
return UnionType(name: enumDecl.name.text, cases: cases)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// MARK: - Synthesized members
|
|
230
|
+
|
|
231
|
+
/// `decode(_:in:)`: tries each case's payload converter in declaration order and returns the first
|
|
232
|
+
/// case that decodes. `try?` turns a candidate's failure into "try the next one" without erasing the
|
|
233
|
+
/// payload (each `payload` local keeps its concrete type); the candidate's own error is discarded, since
|
|
234
|
+
/// with several alternatives there is no single failure to surface. When no alternative accepts the
|
|
235
|
+
/// value the factory throws `Exceptions.UnionCaseMismatch`, naming the union, the JS kind of the value
|
|
236
|
+
/// received, and every payload type the union accepts.
|
|
237
|
+
private func decodeMethod(union: UnionType) -> DeclSyntax {
|
|
238
|
+
var lines: [String] = []
|
|
239
|
+
for unionCase in union.cases {
|
|
240
|
+
let payloadType = expressionType(unionCase.payloadType)
|
|
241
|
+
lines.append(" if let payload = try? \(payloadType).decode(value, in: runtime) {")
|
|
242
|
+
lines.append(" return \(unionCase.construction)")
|
|
243
|
+
lines.append(" }")
|
|
244
|
+
}
|
|
245
|
+
let expected = union.cases.map { "\"\($0.payloadType)\"" }.joined(separator: ", ")
|
|
246
|
+
let mismatch = "(unionName: \"\(union.name)\", received: value.kind.rawValue, expected: [\(expected)])"
|
|
247
|
+
lines.append(" throw Exceptions.UnionCaseMismatch(\(mismatch))")
|
|
248
|
+
let body = lines.joined(separator: "\n")
|
|
249
|
+
return """
|
|
250
|
+
@JavaScriptActor
|
|
251
|
+
public static func decode(_ value: borrowing JavaScriptValue, in runtime: borrowing JavaScriptRuntime) throws -> Self {
|
|
252
|
+
\(raw: body)
|
|
253
|
+
}
|
|
254
|
+
"""
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/// `encode(_:in:)`: a `switch` over the cases, each encoding its payload through the payload type's
|
|
258
|
+
/// own `encode`. Exhaustive by construction, so a case added later without re-expansion can't slip
|
|
259
|
+
/// through silently.
|
|
260
|
+
private func encodeMethod(union: UnionType) -> DeclSyntax {
|
|
261
|
+
var lines: [String] = [" switch value {"]
|
|
262
|
+
for unionCase in union.cases {
|
|
263
|
+
lines.append(" case \(unionCase.pattern):")
|
|
264
|
+
lines.append(" return try \(expressionType(unionCase.payloadType)).encode(payload, in: runtime)")
|
|
265
|
+
}
|
|
266
|
+
lines.append(" }")
|
|
267
|
+
let body = lines.joined(separator: "\n")
|
|
268
|
+
return """
|
|
269
|
+
@JavaScriptActor
|
|
270
|
+
public static func encode(_ value: Self, in runtime: borrowing JavaScriptRuntime) throws -> JavaScriptValue {
|
|
271
|
+
\(raw: body)
|
|
272
|
+
}
|
|
273
|
+
"""
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/// `as(_:)`: a typed accessor per case, selected by the payload's metatype rather than the case name,
|
|
277
|
+
/// so a caller that only knows the type it wants writes `try value.as(String.self)` and gets a `String`.
|
|
278
|
+
/// When
|
|
279
|
+
/// the union holds a different case it throws `Exceptions.UnionCaseMismatch`, the same error `decode`
|
|
280
|
+
/// throws, with the held case's payload type as `received` and the requested type as `expected`. The
|
|
281
|
+
/// overloads can't collide because a payload type appears in at most one case (enforced above), and a
|
|
282
|
+
/// metatype the union doesn't carry fails to resolve at compile time. `as` is a keyword, so the
|
|
283
|
+
/// declaration is backticked; a call site after a dot (`value.as(…)`) needs no backticks. The parameter
|
|
284
|
+
/// is spelled in expression form (`T!` rewritten to `T?`), since `T!.Type` isn't valid.
|
|
285
|
+
private func accessorMethods(union: UnionType) -> [DeclSyntax] {
|
|
286
|
+
return union.cases.map { unionCase in
|
|
287
|
+
let payloadType = expressionType(unionCase.payloadType)
|
|
288
|
+
let mismatch = "(unionName: \"\(union.name)\", received: _payloadTypeName, expected: [\"\(unionCase.payloadType)\"])"
|
|
289
|
+
return """
|
|
290
|
+
public func `as`(_ type: \(raw: payloadType).Type) throws -> \(raw: payloadType) {
|
|
291
|
+
if case \(raw: unionCase.pattern) = self {
|
|
292
|
+
return payload
|
|
293
|
+
}
|
|
294
|
+
throw Exceptions.UnionCaseMismatch(\(raw: mismatch))
|
|
295
|
+
}
|
|
296
|
+
"""
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/// `_payloadTypeName`: the spelled payload type of the case the union currently holds, as written in the
|
|
301
|
+
/// declaration, so every `as(_:)` overload reports the actual type in its mismatch error through one
|
|
302
|
+
/// shared `switch` instead of each overload enumerating the other cases.
|
|
303
|
+
private func payloadTypeNameProperty(union: UnionType) -> DeclSyntax {
|
|
304
|
+
var lines: [String] = [" switch self {"]
|
|
305
|
+
for unionCase in union.cases {
|
|
306
|
+
lines.append(" case .\(unionCase.name):")
|
|
307
|
+
lines.append(" return \"\(unionCase.payloadType)\"")
|
|
308
|
+
}
|
|
309
|
+
lines.append(" }")
|
|
310
|
+
let body = lines.joined(separator: "\n")
|
|
311
|
+
return """
|
|
312
|
+
private var _payloadTypeName: String {
|
|
313
|
+
\(raw: body)
|
|
314
|
+
}
|
|
315
|
+
"""
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// MARK: - Diagnostics
|
|
319
|
+
|
|
320
|
+
/// The error for a default value on a case's associated value, attached to the `= …` clause and carrying
|
|
321
|
+
/// a fix-it that deletes it (trimming the space the type carried before the `=`).
|
|
322
|
+
private func defaultValueDiagnostic(
|
|
323
|
+
for parameter: EnumCaseParameterSyntax,
|
|
324
|
+
defaultValue: InitializerClauseSyntax,
|
|
325
|
+
caseName: String
|
|
326
|
+
) -> Diagnostic {
|
|
327
|
+
let fixIt = FixIt(
|
|
328
|
+
message: UnionFixItMessage("Remove the default value", id: "union-remove-default-value"),
|
|
329
|
+
changes: [
|
|
330
|
+
.replace(
|
|
331
|
+
oldNode: Syntax(parameter),
|
|
332
|
+
newNode: Syntax(
|
|
333
|
+
parameter
|
|
334
|
+
.with(\.type, parameter.type.with(\.trailingTrivia, []))
|
|
335
|
+
.with(\.defaultValue, nil))
|
|
336
|
+
)
|
|
337
|
+
]
|
|
338
|
+
)
|
|
339
|
+
let message = UnionDiagnosticMessage(
|
|
340
|
+
"@Union case '\(caseName)' cannot give its associated value a default: the payload is always decoded from the JavaScript value, so the default would never apply. Remove '\(defaultValue.trimmedDescription)'.",
|
|
341
|
+
id: "union-case-default-value"
|
|
342
|
+
)
|
|
343
|
+
return Diagnostic(node: defaultValue, message: message, fixIts: [fixIt])
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private struct UnionDiagnosticMessage: DiagnosticMessage {
|
|
347
|
+
let message: String
|
|
348
|
+
let diagnosticID: MessageID
|
|
349
|
+
let severity: DiagnosticSeverity = .error
|
|
350
|
+
|
|
351
|
+
init(_ message: String, id: String) {
|
|
352
|
+
self.message = message
|
|
353
|
+
self.diagnosticID = MessageID(domain: "ExpoModulesMacros", id: id)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private struct UnionFixItMessage: FixItMessage {
|
|
358
|
+
let message: String
|
|
359
|
+
let fixItID: MessageID
|
|
360
|
+
|
|
361
|
+
init(_ message: String, id: String) {
|
|
362
|
+
self.message = message
|
|
363
|
+
self.fixItID = MessageID(domain: "ExpoModulesMacros", id: id)
|
|
364
|
+
}
|
|
365
|
+
}
|