expo-modules-macros 0.26.0 → 0.27.1

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.
Files changed (48) hide show
  1. package/AGENTS.md +8 -8
  2. package/README.md +37 -2
  3. package/apple/ExpoModulesMacros +0 -0
  4. package/apple/Package.swift +10 -4
  5. package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/CLI.swift +45 -2
  6. package/apple/Sources/ExpoModulesCLI/Commands/Expand/ExpandReport.swift +120 -0
  7. package/apple/Sources/ExpoModulesCLI/Commands/Expand/RunExpand.swift +183 -0
  8. package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/Core/SourceScan.swift +8 -2
  9. package/apple/Sources/ExpoModulesMacros/Expand/MacroExpander.swift +484 -0
  10. package/apple/Sources/ExpoModulesMacros/{EventMacro.swift → Macros/EventMacro.swift} +1 -1
  11. package/apple/Sources/ExpoModulesMacros/{RecordMacro.swift → Macros/RecordMacro.swift} +4 -2
  12. package/apple/Sources/ExpoModulesMacros/{UnionMacro.swift → Macros/UnionMacro.swift} +2 -2
  13. package/apple/Sources/ExpoModulesMacros/Plugin.swift +9 -14
  14. package/apple/Sources/ExpoModulesMacros/ProvidedMacros.swift +31 -0
  15. package/apple/Sources/ExpoModulesMacros/{ClosureArguments.swift → Support/ClosureArguments.swift} +4 -2
  16. package/apple/Sources/ExpoModulesMacros/{DecorateModuleBuilder.swift → Support/DecorateModuleBuilder.swift} +14 -6
  17. package/apple/Sources/ExpoModulesMacros/{TupleTypes.swift → Support/TupleTypes.swift} +50 -12
  18. package/build/index.d.ts +31 -1
  19. package/build/index.js +34 -0
  20. package/build/types.d.ts +45 -1
  21. package/build/types.js +3 -2
  22. package/package.json +5 -5
  23. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/Availability.swift +0 -0
  24. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/ExportedSurface.swift +0 -0
  25. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/ResolveRefs.swift +0 -0
  26. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/ScanExports.swift +0 -0
  27. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/SurfaceVisitor.swift +0 -0
  28. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/TupleTypeAliases.swift +0 -0
  29. /package/apple/Sources/{ExpoModulesScanner/Exports → ExpoModulesCLI/Commands/ScanExports}/TypeNode.swift +0 -0
  30. /package/apple/Sources/{ExpoModulesScanner/Modules → ExpoModulesCLI/Commands/ScanModules}/ScanModules.swift +0 -0
  31. /package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/Core/Detection.swift +0 -0
  32. /package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/Core/DetectionVisitor.swift +0 -0
  33. /package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/Core/ScanBuildConfiguration.swift +0 -0
  34. /package/apple/Sources/{ExpoModulesScanner → ExpoModulesCLI}/Core/StandardStreams.swift +0 -0
  35. /package/apple/Sources/ExpoModulesMacros/{ExpoModuleMacro.swift → Macros/ExpoModuleMacro.swift} +0 -0
  36. /package/apple/Sources/ExpoModulesMacros/{ExpoModulesOptimizedMacro.swift → Macros/ExpoModulesOptimizedMacro.swift} +0 -0
  37. /package/apple/Sources/ExpoModulesMacros/{ExpoViewMacro.swift → Macros/ExpoViewMacro.swift} +0 -0
  38. /package/apple/Sources/ExpoModulesMacros/{JSMacro.swift → Macros/JSMacro.swift} +0 -0
  39. /package/apple/Sources/ExpoModulesMacros/{SharedObjectMacro.swift → Macros/SharedObjectMacro.swift} +0 -0
  40. /package/apple/Sources/ExpoModulesMacros/{ViewPropsMacro.swift → Macros/ViewPropsMacro.swift} +0 -0
  41. /package/apple/Sources/ExpoModulesMacros/{FreeFormTypes.swift → Support/FreeFormTypes.swift} +0 -0
  42. /package/apple/Sources/ExpoModulesMacros/{JSConstructor.swift → Support/JSConstructor.swift} +0 -0
  43. /package/apple/Sources/ExpoModulesMacros/{JSNameCollisions.swift → Support/JSNameCollisions.swift} +0 -0
  44. /package/apple/Sources/ExpoModulesMacros/{MacroHelpers.swift → Support/MacroHelpers.swift} +0 -0
  45. /package/apple/Sources/ExpoModulesMacros/{OptimizedFunctionHelpers.swift → Support/OptimizedFunctionHelpers.swift} +0 -0
  46. /package/apple/Sources/ExpoModulesMacros/{Receiver.swift → Support/Receiver.swift} +0 -0
  47. /package/apple/Sources/ExpoModulesMacros/{TypeAliases.swift → Support/TypeAliases.swift} +0 -0
  48. /package/apple/Sources/ExpoModulesMacros/{TypeConformanceAssertion.swift → Support/TypeConformanceAssertion.swift} +0 -0
@@ -50,29 +50,60 @@ internal enum TupleConversion {
50
50
  }
51
51
  }
52
52
 
53
- /// The encode expression, as it appears after `try`, for the value `valueExpression` in the runtime
54
- /// named `runtime`. A tuple whose elements all conform needs no closures. The tuple method takes an
55
- /// unlabeled tuple, so a labeled one is coerced to its unlabeled type first.
56
- func encodeExpression(of valueExpression: String, in runtime: String = "runtime") -> String {
53
+ /// The call, as it appears after `try`, that encodes the value `valueExpression` into the slot named
54
+ /// `slot`, in the runtime named `runtime`. A tuple whose elements all conform needs no closures. The
55
+ /// tuple method takes an unlabeled tuple, so a labeled one is coerced to its unlabeled type first.
56
+ func encodeCall(of valueExpression: String, into slot: String, in runtime: String = "runtime") -> String {
57
57
  switch self {
58
58
  case .conforming(let type):
59
- return "\(expressionType(type)).encode(\(valueExpression), in: \(runtime))"
59
+ return "\(expressionType(type)).encode(\(valueExpression), into: \(slot), in: \(runtime))"
60
60
  case .tuple(let elements, let labels):
61
61
  let value = labels == nil ? valueExpression : "\(valueExpression) as \(unlabeledType)"
62
62
  let closures =
63
63
  conformingElementTypes(elements) == nil
64
- ? elements.map { elementClosure(returning: $0.encodeExpression(of: "value")) } : []
65
- let arguments = [value] + labelsArgument(labels) + ["in: \(runtime)"] + closures
64
+ ? elements.map { encoderClosure(encoding: $0.encodeCall(of: "value", into: "slot")) } : []
65
+ let arguments = [value] + labelsArgument(labels) + ["into: \(slot)", "in: \(runtime)"] + closures
66
66
  return "JavaScriptValue.encodeTuple(\(arguments.joined(separator: ", ")))"
67
67
  case .optional(let wrapped):
68
- return containerEncode("encodeOptional", label: "wrapped", inner: wrapped, of: valueExpression, in: runtime)
68
+ return containerEncode(
69
+ "encodeOptional",
70
+ label: "wrapped",
71
+ inner: wrapped,
72
+ of: valueExpression,
73
+ into: slot,
74
+ in: runtime
75
+ )
69
76
  case .array(let element):
70
- return containerEncode("encodeArray", label: "element", inner: element, of: valueExpression, in: runtime)
77
+ return containerEncode(
78
+ "encodeArray",
79
+ label: "element",
80
+ inner: element,
81
+ of: valueExpression,
82
+ into: slot,
83
+ in: runtime
84
+ )
71
85
  case .dictionary(let value):
72
- return containerEncode("encodeDictionary", label: "value", inner: value, of: valueExpression, in: runtime)
86
+ return containerEncode(
87
+ "encodeDictionary",
88
+ label: "value",
89
+ inner: value,
90
+ of: valueExpression,
91
+ into: slot,
92
+ in: runtime
93
+ )
73
94
  }
74
95
  }
75
96
 
97
+ /// The encode expression, as it appears after `try`, that creates a `JavaScriptValue` from the value
98
+ /// `valueExpression`, for a place that has no slot to encode into, such as an async result. A type that
99
+ /// holds a tuple is encoded into the slot of `JavaScriptValue.encoding(in:_:)`.
100
+ func encodeExpression(of valueExpression: String) -> String {
101
+ if case .conforming(let type) = self {
102
+ return "\(expressionType(type)).encode(\(valueExpression), in: runtime)"
103
+ }
104
+ return "JavaScriptValue.encoding(in: runtime) { slot in try \(encodeCall(of: valueExpression, into: "slot")) }"
105
+ }
106
+
76
107
  /// The JavaScript value kinds the decode accepts, as an expression of type `JavaScriptValueKinds`, for
77
108
  /// a `@Union` that picks a payload by kind. Mirrors the kinds of the matching `JavaScriptCodable`
78
109
  /// conformances: an array decodes a non-array value as a single element.
@@ -147,10 +178,11 @@ internal enum TupleConversion {
147
178
  label: String,
148
179
  inner: TupleConversion,
149
180
  of valueExpression: String,
181
+ into slot: String,
150
182
  in runtime: String
151
183
  ) -> String {
152
- return
153
- "JavaScriptValue.\(method)(\(valueExpression), in: \(runtime), \(label): \(elementClosure(returning: inner.encodeExpression(of: "value"))))"
184
+ let closure = encoderClosure(encoding: inner.encodeCall(of: "value", into: "slot"))
185
+ return "JavaScriptValue.\(method)(\(valueExpression), into: \(slot), in: \(runtime), \(label): \(closure))"
154
186
  }
155
187
 
156
188
  /// The types of a tuple's elements when every element converts through its own `decode` /
@@ -232,3 +264,9 @@ private func structure(of type: TypeSyntax) -> TupleConversion {
232
264
  internal func elementClosure(returning expression: String) -> String {
233
265
  return "{ value, runtime in return try \(expression) }"
234
266
  }
267
+
268
+ /// An element closure that encodes its value into its slot with `call`, as it appears after `try`. It takes
269
+ /// the value, the slot and the runtime as parameters, which shadow the outer ones, so it captures nothing.
270
+ internal func encoderClosure(encoding call: String) -> String {
271
+ return "{ value, slot, runtime in try \(call) }"
272
+ }
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ScanExportsResult, ScanModulesResult } from './types';
1
+ import { ExpandResult, ScanExportsResult, ScanModulesResult } from './types';
2
2
  export * from './types';
3
3
  /** Options accepted by `scanModules`, mirroring the CLI's `scan-modules` flags. */
4
4
  export interface ScanModulesOptions {
@@ -19,6 +19,21 @@ export interface ScanExportsOptions {
19
19
  /** Milliseconds after which the scanner is killed and the scan fails. No limit by default. */
20
20
  timeout?: number;
21
21
  }
22
+ /** Options accepted by `expandMacros`, mirroring the CLI's `expand` flags. */
23
+ export interface ExpandMacrosOptions {
24
+ /**
25
+ * Limits the result to these symbols: a type (`'MyModule'`, `'MyModule.Options'`) or a member
26
+ * (`'MyModule.greet'`). A member expands its type with only the declarations of that name. Each
27
+ * symbol must be declared in one of the files, or the call fails with a `ScannerError`.
28
+ */
29
+ symbols?: string[];
30
+ /** Returns only the diagnostics, with no expansions. */
31
+ diagnosticsOnly?: boolean;
32
+ /** Overrides the scanner binary path. Defaults to the one shipped with this package. */
33
+ binaryPath?: string;
34
+ /** Milliseconds after which the scanner is killed and the call fails. No limit by default. */
35
+ timeout?: number;
36
+ }
22
37
  /**
23
38
  * Thrown when the scanner exits non-zero. Exit code 2 is a usage error (bad subcommand or flags) and
24
39
  * 1 means the report couldn't be encoded; both put a message on stderr, carried here as `stderr`.
@@ -68,3 +83,18 @@ export declare function scanModules(paths: string[], options?: ScanModulesOption
68
83
  * what it guards is not reported.
69
84
  */
70
85
  export declare function scanExports(paths: string[], options?: ScanExportsOptions): Promise<ScanExportsResult>;
86
+ /**
87
+ * Expands the Expo macros in process, without a build, and returns the generated
88
+ * code with the syntax errors and the macro diagnostics. The macros only read the syntax tree, so
89
+ * the code is what the compiler gets, except for the names that a macro makes unique. Errors that
90
+ * need type checking, for example a type that doesn't conform to a protocol, still need a build.
91
+ *
92
+ * Each path is a `.swift` file, which is always expanded and keeps its path in the result, or a
93
+ * directory. A directory is walked like the scans walk it, and only its files that use one of the
94
+ * macros are expanded, with their absolute paths.
95
+ *
96
+ * Error diagnostics don't make the call fail: check `severity` in `diagnostics`. Only an input that
97
+ * can't be expanded (a missing path, a file that isn't Swift, a symbol that no file declares) throws
98
+ * a `ScannerError`.
99
+ */
100
+ export declare function expandMacros(paths: string[], options?: ExpandMacrosOptions): Promise<ExpandResult>;
package/build/index.js CHANGED
@@ -21,6 +21,7 @@ exports.ScannerSchemaVersionError = exports.ScannerError = void 0;
21
21
  exports.getScannerBinaryPath = getScannerBinaryPath;
22
22
  exports.scanModules = scanModules;
23
23
  exports.scanExports = scanExports;
24
+ exports.expandMacros = expandMacros;
24
25
  const node_child_process_1 = require("node:child_process");
25
26
  const node_path_1 = __importDefault(require("node:path"));
26
27
  const types_1 = require("./types");
@@ -210,3 +211,36 @@ async function scanExports(paths, options = {}) {
210
211
  }
211
212
  return result;
212
213
  }
214
+ /**
215
+ * Expands the Expo macros in process, without a build, and returns the generated
216
+ * code with the syntax errors and the macro diagnostics. The macros only read the syntax tree, so
217
+ * the code is what the compiler gets, except for the names that a macro makes unique. Errors that
218
+ * need type checking, for example a type that doesn't conform to a protocol, still need a build.
219
+ *
220
+ * Each path is a `.swift` file, which is always expanded and keeps its path in the result, or a
221
+ * directory. A directory is walked like the scans walk it, and only its files that use one of the
222
+ * macros are expanded, with their absolute paths.
223
+ *
224
+ * Error diagnostics don't make the call fail: check `severity` in `diagnostics`. Only an input that
225
+ * can't be expanded (a missing path, a file that isn't Swift, a symbol that no file declares) throws
226
+ * a `ScannerError`.
227
+ */
228
+ async function expandMacros(paths, options = {}) {
229
+ if (paths.length === 0) {
230
+ throw new TypeError('expandMacros requires at least one path');
231
+ }
232
+ assertRepresentablePaths('expandMacros', paths);
233
+ const args = ['expand', '--json'];
234
+ for (const symbol of options.symbols ?? []) {
235
+ args.push('--symbol', symbol);
236
+ }
237
+ if (options.diagnosticsOnly) {
238
+ args.push('--diagnostics-only');
239
+ }
240
+ args.push(...paths);
241
+ const result = await runScanner(options.binaryPath ?? getScannerBinaryPath(), args, options.timeout);
242
+ if (result.schemaVersion !== types_1.SUPPORTED_EXPAND_SCHEMA_VERSION) {
243
+ throw new ScannerSchemaVersionError('expand', result.schemaVersion, types_1.SUPPORTED_EXPAND_SCHEMA_VERSION);
244
+ }
245
+ return result;
246
+ }
package/build/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Hand-written mirrors of the scanner's JSON output. The Swift `Codable` types in
3
- * `apple/Sources/ExpoModulesScanner` are the source of truth; these are kept in sync by hand, which
3
+ * `apple/Sources/ExpoModulesCLI` are the source of truth; these are kept in sync by hand, which
4
4
  * is why each command carries a `schemaVersion` the wrapper checks at runtime.
5
5
  */
6
6
  /** The JS `typeof` category a boundary type reports, mirroring `JSType`. */
@@ -414,6 +414,49 @@ export interface ScanExportsResult {
414
414
  exports: ExportedSurface;
415
415
  stats: ScanStats;
416
416
  }
417
+ /** A note attached to an `ExpansionDiagnostic`, at its own location. */
418
+ export interface ExpansionNote {
419
+ message: string;
420
+ line: number;
421
+ column: number;
422
+ }
423
+ /**
424
+ * A syntax error in a file or a diagnostic of a macro, at its location in the original file. `line`
425
+ * and `column` are 1-based, and the column counts UTF-8 bytes, as in the compiler's output.
426
+ */
427
+ export interface ExpansionDiagnostic {
428
+ severity: 'error' | 'warning' | 'note' | 'remark';
429
+ message: string;
430
+ line: number;
431
+ column: number;
432
+ notes: ExpansionNote[];
433
+ /** The messages of the Fix-Its, for example `replace 'var' with 'let'`. */
434
+ fixIts: string[];
435
+ }
436
+ /** Expanded source code. */
437
+ export interface Expansion {
438
+ /** The symbol this expansion is for, or absent for the whole file. */
439
+ symbol?: string;
440
+ source: string;
441
+ }
442
+ /** What the macros generate for one file, and what they report about it. */
443
+ export interface ExpandedFile {
444
+ /** The path as it was passed, or the absolute path of a file found in a directory. */
445
+ file: string;
446
+ /**
447
+ * Without symbols, one expansion of the whole file. With symbols, one per symbol that this file
448
+ * declares, in the order they were passed. Empty with `diagnosticsOnly`.
449
+ */
450
+ expansions: Expansion[];
451
+ /** In source order. With symbols, only those inside the declarations of the symbols. */
452
+ diagnostics: ExpansionDiagnostic[];
453
+ }
454
+ /** The `expand --json` result. */
455
+ export interface ExpandResult {
456
+ schemaVersion: number;
457
+ /** One entry per expanded file: the files in the order they were passed, a directory's files sorted. */
458
+ files: ExpandedFile[];
459
+ }
417
460
  /**
418
461
  * The output schema versions this package was written against. The wrapper compares these to the
419
462
  * `schemaVersion` in each report and throws on a mismatch, so a binary/wrapper version skew surfaces
@@ -421,3 +464,4 @@ export interface ScanExportsResult {
421
464
  */
422
465
  export declare const SUPPORTED_SCAN_MODULES_SCHEMA_VERSION = 2;
423
466
  export declare const SUPPORTED_SCAN_EXPORTS_SCHEMA_VERSION = 10;
467
+ export declare const SUPPORTED_EXPAND_SCHEMA_VERSION = 1;
package/build/types.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  /**
3
3
  * Hand-written mirrors of the scanner's JSON output. The Swift `Codable` types in
4
- * `apple/Sources/ExpoModulesScanner` are the source of truth; these are kept in sync by hand, which
4
+ * `apple/Sources/ExpoModulesCLI` are the source of truth; these are kept in sync by hand, which
5
5
  * is why each command carries a `schemaVersion` the wrapper checks at runtime.
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.SUPPORTED_SCAN_EXPORTS_SCHEMA_VERSION = exports.SUPPORTED_SCAN_MODULES_SCHEMA_VERSION = void 0;
8
+ exports.SUPPORTED_EXPAND_SCHEMA_VERSION = exports.SUPPORTED_SCAN_EXPORTS_SCHEMA_VERSION = exports.SUPPORTED_SCAN_MODULES_SCHEMA_VERSION = void 0;
9
9
  /**
10
10
  * The output schema versions this package was written against. The wrapper compares these to the
11
11
  * `schemaVersion` in each report and throws on a mismatch, so a binary/wrapper version skew surfaces
@@ -13,3 +13,4 @@ exports.SUPPORTED_SCAN_EXPORTS_SCHEMA_VERSION = exports.SUPPORTED_SCAN_MODULES_S
13
13
  */
14
14
  exports.SUPPORTED_SCAN_MODULES_SCHEMA_VERSION = 2;
15
15
  exports.SUPPORTED_SCAN_EXPORTS_SCHEMA_VERSION = 10;
16
+ exports.SUPPORTED_EXPAND_SCHEMA_VERSION = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-modules-macros",
3
- "version": "0.26.0",
3
+ "version": "0.27.1",
4
4
  "description": "Swift macro plugin for Expo modules",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -26,9 +26,9 @@
26
26
  "typescript": "^5.4.0"
27
27
  },
28
28
  "optionalDependencies": {
29
- "@expo/modules-macros-linux-arm64": "0.26.0",
30
- "@expo/modules-macros-linux-x64": "0.26.0",
31
- "@expo/modules-macros-win32-arm64": "0.26.0",
32
- "@expo/modules-macros-win32-x64": "0.26.0"
29
+ "@expo/modules-macros-linux-arm64": "0.27.1",
30
+ "@expo/modules-macros-linux-x64": "0.27.1",
31
+ "@expo/modules-macros-win32-arm64": "0.27.1",
32
+ "@expo/modules-macros-win32-x64": "0.27.1"
33
33
  }
34
34
  }