nitrogen 0.35.3 → 0.35.4

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.
@@ -56,6 +56,8 @@ def add_nitrogen_files(spec)
56
56
  "SWIFT_OBJC_INTEROP_MODE" => "objcxx",
57
57
  # Enables stricter modular headers
58
58
  "DEFINES_MODULE" => "YES",
59
+ # Disable auto-generated ObjC header for Swift (Static linkage on Xcode 26.4 breaks here)
60
+ "SWIFT_INSTALL_OBJC_HEADER" => "NO",
59
61
  })
60
62
  end
61
63
  `.trim();
@@ -48,6 +48,13 @@ fun as${innerName}OrNull(): ${optional.getCode('kotlin')} {
48
48
  const bridge = new KotlinCxxBridgedType(v);
49
49
  return `
50
50
  is ${innerName} -> ${label}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
51
+ `.trim();
52
+ });
53
+ const asCases = variant.cases.map(([label, v]) => {
54
+ const innerName = capitalizeName(label);
55
+ const bridge = new KotlinCxxBridgedType(v);
56
+ return `
57
+ is ${innerName} -> (${bridge.parseFromCppToKotlin('value', 'kotlin')}) as? T
51
58
  `.trim();
52
59
  });
53
60
  const createFunctions = variant.cases.map(([label, v]) => {
@@ -79,16 +86,24 @@ ${extraImports.join('\n')}
79
86
  sealed class ${kotlinName} {
80
87
  ${indent(innerClasses.join('\n'), ' ')}
81
88
 
82
- ${indent(isFunctions.join('\n'), ' ')}
83
-
84
- ${indent(asFunctions.join('\n'), ' ')}
85
-
89
+ inline fun <reified T> asType(): T? {
90
+ return when (this) {
91
+ ${indent(asCases.join('\n'), ' ')}
92
+ }
93
+ }
94
+ inline fun <reified T> isType(): Boolean {
95
+ return asType<T>() != null
96
+ }
86
97
  inline fun <R> match(${matchParameters.join(', ')}): R {
87
98
  return when (this) {
88
99
  ${indent(matchCases.join('\n'), ' ')}
89
100
  }
90
101
  }
91
102
 
103
+ ${indent(isFunctions.join('\n'), ' ')}
104
+
105
+ ${indent(asFunctions.join('\n'), ' ')}
106
+
92
107
  companion object {
93
108
  ${indent(createFunctions.join('\n'), ' ')}
94
109
  }
@@ -29,6 +29,11 @@ export function createSwiftVariant(variant) {
29
29
  return `case ${label}(${type})`;
30
30
  })
31
31
  .join('\n');
32
+ const asCases = variant.cases
33
+ .map(([label]) => {
34
+ return `case .${label}(let value): return value as? T`;
35
+ })
36
+ .join('\n');
32
37
  const jsSignature = variant.variants.map((t) => t.kind).join(' | ');
33
38
  const allPrimitives = variant.variants.every((v) => isPrimitive(v));
34
39
  const enumDeclaration = allPrimitives ? 'enum' : 'indirect enum';
@@ -49,6 +54,17 @@ ${extraImports.join('\n')}
49
54
  public ${enumDeclaration} ${typename} {
50
55
  ${indent(cases, ' ')}
51
56
  }
57
+
58
+ public extension ${typename} {
59
+ func asType<T>(_ type: T.Type = T.self) -> T? {
60
+ switch self {
61
+ ${indent(asCases, ' ')}
62
+ }
63
+ }
64
+ func isType<T>(_ type: T.Type = T.self) -> Bool {
65
+ return self.asType(type) != nil
66
+ }
67
+ }
52
68
  `.trim();
53
69
  return {
54
70
  content: code,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitrogen",
3
- "version": "0.35.3",
3
+ "version": "0.35.4",
4
4
  "description": "The code-generator for react-native-nitro-modules.",
5
5
  "main": "lib/index",
6
6
  "types": "lib/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "chalk": "^5.3.0",
38
- "react-native-nitro-modules": "^0.35.3",
38
+ "react-native-nitro-modules": "^0.35.4",
39
39
  "ts-morph": "^27.0.0",
40
40
  "yargs": "^18.0.0",
41
41
  "zod": "^4.0.5"
@@ -63,6 +63,8 @@ def add_nitrogen_files(spec)
63
63
  "SWIFT_OBJC_INTEROP_MODE" => "objcxx",
64
64
  # Enables stricter modular headers
65
65
  "DEFINES_MODULE" => "YES",
66
+ # Disable auto-generated ObjC header for Swift (Static linkage on Xcode 26.4 breaks here)
67
+ "SWIFT_INSTALL_OBJC_HEADER" => "NO",
66
68
  })
67
69
  end
68
70
  `.trim()
@@ -58,6 +58,13 @@ fun as${innerName}OrNull(): ${optional.getCode('kotlin')} {
58
58
  is ${innerName} -> ${label}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
59
59
  `.trim()
60
60
  })
61
+ const asCases = variant.cases.map(([label, v]) => {
62
+ const innerName = capitalizeName(label)
63
+ const bridge = new KotlinCxxBridgedType(v)
64
+ return `
65
+ is ${innerName} -> (${bridge.parseFromCppToKotlin('value', 'kotlin')}) as? T
66
+ `.trim()
67
+ })
61
68
 
62
69
  const createFunctions = variant.cases.map(([label, v]) => {
63
70
  const bridge = new KotlinCxxBridgedType(v)
@@ -90,16 +97,24 @@ ${extraImports.join('\n')}
90
97
  sealed class ${kotlinName} {
91
98
  ${indent(innerClasses.join('\n'), ' ')}
92
99
 
93
- ${indent(isFunctions.join('\n'), ' ')}
94
-
95
- ${indent(asFunctions.join('\n'), ' ')}
96
-
100
+ inline fun <reified T> asType(): T? {
101
+ return when (this) {
102
+ ${indent(asCases.join('\n'), ' ')}
103
+ }
104
+ }
105
+ inline fun <reified T> isType(): Boolean {
106
+ return asType<T>() != null
107
+ }
97
108
  inline fun <R> match(${matchParameters.join(', ')}): R {
98
109
  return when (this) {
99
110
  ${indent(matchCases.join('\n'), ' ')}
100
111
  }
101
112
  }
102
113
 
114
+ ${indent(isFunctions.join('\n'), ' ')}
115
+
116
+ ${indent(asFunctions.join('\n'), ' ')}
117
+
103
118
  companion object {
104
119
  ${indent(createFunctions.join('\n'), ' ')}
105
120
  }
@@ -34,6 +34,12 @@ export function createSwiftVariant(variant: VariantType): SourceFile {
34
34
  return `case ${label}(${type})`
35
35
  })
36
36
  .join('\n')
37
+ const asCases = variant.cases
38
+ .map(([label]) => {
39
+ return `case .${label}(let value): return value as? T`
40
+ })
41
+ .join('\n')
42
+
37
43
  const jsSignature = variant.variants.map((t) => t.kind).join(' | ')
38
44
 
39
45
  const allPrimitives = variant.variants.every((v) => isPrimitive(v))
@@ -57,6 +63,17 @@ ${extraImports.join('\n')}
57
63
  public ${enumDeclaration} ${typename} {
58
64
  ${indent(cases, ' ')}
59
65
  }
66
+
67
+ public extension ${typename} {
68
+ func asType<T>(_ type: T.Type = T.self) -> T? {
69
+ switch self {
70
+ ${indent(asCases, ' ')}
71
+ }
72
+ }
73
+ func isType<T>(_ type: T.Type = T.self) -> Bool {
74
+ return self.asType(type) != nil
75
+ }
76
+ }
60
77
  `.trim()
61
78
 
62
79
  return {