nitrogen 0.29.8 → 0.30.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 (111) hide show
  1. package/lib/Logger.d.ts +10 -0
  2. package/lib/autolinking/Autolinking.d.ts +8 -0
  3. package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
  4. package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
  5. package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
  6. package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
  7. package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
  8. package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
  9. package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
  10. package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
  11. package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
  12. package/lib/config/NitroConfig.d.ts +52 -0
  13. package/lib/config/NitroUserConfig.d.ts +22 -0
  14. package/lib/config/getConfig.d.ts +2 -0
  15. package/lib/createGitAttributes.d.ts +1 -0
  16. package/lib/createPlatformSpec.d.ts +4 -0
  17. package/lib/getFiles.d.ts +1 -0
  18. package/lib/getPlatformSpecs.d.ts +17 -0
  19. package/lib/index.d.ts +2 -0
  20. package/lib/init.d.ts +1 -0
  21. package/lib/init.js +1 -1
  22. package/lib/nitrogen.d.ts +11 -0
  23. package/lib/prettifyDirectory.d.ts +1 -0
  24. package/lib/syntax/BridgedType.d.ts +41 -0
  25. package/lib/syntax/CodeNode.d.ts +23 -0
  26. package/lib/syntax/Method.d.ts +41 -0
  27. package/lib/syntax/Parameter.d.ts +15 -0
  28. package/lib/syntax/Property.d.ts +50 -0
  29. package/lib/syntax/SourceFile.d.ts +70 -0
  30. package/lib/syntax/c++/CppEnum.d.ts +6 -0
  31. package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
  32. package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
  33. package/lib/syntax/c++/CppStruct.d.ts +3 -0
  34. package/lib/syntax/c++/CppStruct.js +4 -0
  35. package/lib/syntax/c++/CppUnion.d.ts +6 -0
  36. package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
  37. package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
  38. package/lib/syntax/createType.d.ts +15 -0
  39. package/lib/syntax/getAllTypes.d.ts +3 -0
  40. package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
  41. package/lib/syntax/getInterfaceProperties.d.ts +4 -0
  42. package/lib/syntax/getReferencedTypes.d.ts +2 -0
  43. package/lib/syntax/helpers.d.ts +13 -0
  44. package/lib/syntax/isCoreType.d.ts +9 -0
  45. package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
  46. package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
  47. package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
  48. package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
  49. package/lib/syntax/kotlin/KotlinCxxBridgedType.js +0 -14
  50. package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
  51. package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
  52. package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
  53. package/lib/syntax/kotlin/KotlinHybridObject.js +1 -1
  54. package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
  55. package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
  56. package/lib/syntax/kotlin/KotlinStruct.js +51 -46
  57. package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
  58. package/lib/syntax/kotlin/KotlinVariant.js +37 -4
  59. package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
  60. package/lib/syntax/swift/SwiftCxxBridgedType.js +5 -2
  61. package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +0 -1
  62. package/lib/syntax/swift/SwiftCxxTypeHelper.js +3 -6
  63. package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
  64. package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
  65. package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
  66. package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
  67. package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
  68. package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
  69. package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
  70. package/lib/syntax/swift/isPrimitivelyCopyable.d.ts +7 -0
  71. package/lib/syntax/swift/isPrimitivelyCopyable.js +18 -0
  72. package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
  73. package/lib/syntax/types/BigIntType.d.ts +10 -0
  74. package/lib/syntax/types/BooleanType.d.ts +10 -0
  75. package/lib/syntax/types/DateType.d.ts +10 -0
  76. package/lib/syntax/types/EnumType.d.ts +23 -0
  77. package/lib/syntax/types/ErrorType.d.ts +11 -0
  78. package/lib/syntax/types/FunctionType.d.ts +29 -0
  79. package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
  80. package/lib/syntax/types/HybridObjectType.d.ts +23 -0
  81. package/lib/syntax/types/MapType.d.ts +10 -0
  82. package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
  83. package/lib/syntax/types/NullType.d.ts +10 -0
  84. package/lib/syntax/types/NumberType.d.ts +10 -0
  85. package/lib/syntax/types/OptionalType.d.ts +13 -0
  86. package/lib/syntax/types/PromiseType.d.ts +16 -0
  87. package/lib/syntax/types/RecordType.d.ts +13 -0
  88. package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
  89. package/lib/syntax/types/StringType.d.ts +10 -0
  90. package/lib/syntax/types/StructType.d.ts +14 -0
  91. package/lib/syntax/types/TupleType.d.ts +12 -0
  92. package/lib/syntax/types/Type.d.ts +55 -0
  93. package/lib/syntax/types/VariantType.d.ts +19 -0
  94. package/lib/syntax/types/VoidType.d.ts +10 -0
  95. package/lib/syntax/types/getTypeAs.d.ts +2 -0
  96. package/lib/utils.d.ts +22 -0
  97. package/lib/views/CppHybridViewComponent.d.ts +14 -0
  98. package/lib/views/createHostComponentJs.d.ts +3 -0
  99. package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
  100. package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
  101. package/lib/writeFile.d.ts +5 -0
  102. package/package.json +3 -3
  103. package/src/init.ts +1 -1
  104. package/src/syntax/c++/CppStruct.ts +4 -0
  105. package/src/syntax/kotlin/KotlinCxxBridgedType.ts +0 -14
  106. package/src/syntax/kotlin/KotlinHybridObject.ts +1 -1
  107. package/src/syntax/kotlin/KotlinStruct.ts +54 -47
  108. package/src/syntax/kotlin/KotlinVariant.ts +37 -4
  109. package/src/syntax/swift/SwiftCxxBridgedType.ts +5 -2
  110. package/src/syntax/swift/SwiftCxxTypeHelper.ts +3 -7
  111. package/src/syntax/swift/isPrimitivelyCopyable.ts +20 -0
@@ -0,0 +1,23 @@
1
+ import { type NitroConfig } from '../../config/NitroConfig.js';
2
+ import type { Language } from '../../getPlatformSpecs.js';
3
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js';
4
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
5
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
6
+ interface GetHybridObjectCodeOptions extends GetCodeOptions {
7
+ mode?: 'strong' | 'weak';
8
+ }
9
+ export declare class HybridObjectType implements Type {
10
+ readonly hybridObjectName: string;
11
+ readonly implementationLanguage: Language;
12
+ readonly baseTypes: HybridObjectType[];
13
+ readonly sourceConfig: NitroConfig;
14
+ constructor(hybridObjectName: string, implementationLanguage: Language, baseTypes: HybridObjectType[], sourceConfig: NitroConfig);
15
+ constructor(spec: HybridObjectSpec);
16
+ get canBePassedByReference(): boolean;
17
+ get kind(): TypeKind;
18
+ getCode(language: Language, options?: GetHybridObjectCodeOptions): string;
19
+ getExtraFiles(): SourceFile[];
20
+ private getExternalCxxImportName;
21
+ getRequiredImports(language: Language): SourceImport[];
22
+ }
23
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class MapType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(language: Language): SourceImport[];
10
+ }
@@ -0,0 +1,14 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js';
4
+ export declare class NamedWrappingType<T extends Type> implements NamedType {
5
+ readonly type: T;
6
+ readonly name: string;
7
+ constructor(name: string, type: T);
8
+ get escapedName(): string;
9
+ get kind(): TypeKind;
10
+ get canBePassedByReference(): boolean;
11
+ getCode(language: Language, options?: GetCodeOptions): string;
12
+ getExtraFiles(): SourceFile[];
13
+ getRequiredImports(language: Language): SourceImport[];
14
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class NullType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(): SourceImport[];
10
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class NumberType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(): SourceImport[];
10
+ }
@@ -0,0 +1,13 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
4
+ export declare class OptionalType implements Type {
5
+ readonly wrappingType: Type;
6
+ constructor(wrappingType: Type);
7
+ get canBePassedByReference(): boolean;
8
+ get kind(): TypeKind;
9
+ get needsBraces(): boolean;
10
+ getCode(language: Language, options?: GetCodeOptions): string;
11
+ getExtraFiles(): SourceFile[];
12
+ getRequiredImports(language: Language): SourceImport[];
13
+ }
@@ -0,0 +1,16 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import { FunctionType } from './FunctionType.js';
4
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
5
+ export declare class PromiseType implements Type {
6
+ readonly resultingType: Type;
7
+ readonly errorType: Type;
8
+ constructor(resultingType: Type);
9
+ get canBePassedByReference(): boolean;
10
+ get kind(): TypeKind;
11
+ get resolverFunction(): FunctionType;
12
+ get rejecterFunction(): FunctionType;
13
+ getCode(language: Language, options?: GetCodeOptions): string;
14
+ getExtraFiles(): SourceFile[];
15
+ getRequiredImports(language: Language): SourceImport[];
16
+ }
@@ -0,0 +1,13 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
4
+ export declare class RecordType implements Type {
5
+ readonly keyType: Type;
6
+ readonly valueType: Type;
7
+ constructor(keyType: Type, valueType: Type);
8
+ get canBePassedByReference(): boolean;
9
+ get kind(): TypeKind;
10
+ getCode(language: Language, options?: GetCodeOptions): string;
11
+ getExtraFiles(): SourceFile[];
12
+ getRequiredImports(language: Language): SourceImport[];
13
+ }
@@ -0,0 +1,13 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
4
+ export declare class ResultWrappingType implements Type {
5
+ readonly result: Type;
6
+ readonly error: Type;
7
+ constructor(result: Type);
8
+ get canBePassedByReference(): boolean;
9
+ get kind(): TypeKind;
10
+ getCode(language: Language, options?: GetCodeOptions): string;
11
+ getExtraFiles(): SourceFile[];
12
+ getRequiredImports(language: Language): SourceImport[];
13
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class StringType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(language: Language): SourceImport[];
10
+ }
@@ -0,0 +1,14 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type FileWithReferencedTypes, type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js';
4
+ export declare class StructType implements Type {
5
+ readonly structName: string;
6
+ readonly properties: NamedType[];
7
+ readonly declarationFile: FileWithReferencedTypes;
8
+ constructor(structName: string, properties: NamedType[]);
9
+ get canBePassedByReference(): boolean;
10
+ get kind(): TypeKind;
11
+ getCode(language: Language, { fullyQualified }?: GetCodeOptions): string;
12
+ getExtraFiles(): SourceFile[];
13
+ getRequiredImports(language: Language): SourceImport[];
14
+ }
@@ -0,0 +1,12 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
4
+ export declare class TupleType implements Type {
5
+ readonly itemTypes: Type[];
6
+ constructor(itemTypes: Type[]);
7
+ get canBePassedByReference(): boolean;
8
+ get kind(): TypeKind;
9
+ getCode(language: Language, options?: GetCodeOptions): string;
10
+ getExtraFiles(): SourceFile[];
11
+ getRequiredImports(language: Language): SourceImport[];
12
+ }
@@ -0,0 +1,55 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ export type TypeKind = 'array-buffer' | 'array' | 'bigint' | 'boolean' | 'custom-type' | 'enum' | 'error' | 'function' | 'hybrid-object' | 'hybrid-object-base' | 'map' | 'null' | 'number' | 'optional' | 'promise' | 'record' | 'string' | 'struct' | 'tuple' | 'variant' | 'result-wrapper' | 'date' | 'void';
4
+ export interface GetCodeOptions {
5
+ /**
6
+ * Specifies whether the name (e.g. a C++ class name)
7
+ * should use the fully qualified namespace name prefix.
8
+ */
9
+ fullyQualified?: boolean;
10
+ }
11
+ /**
12
+ * Represents a TypeScript Type that can be represented in a native language (C++, Swift, Kotlin)
13
+ */
14
+ export interface Type {
15
+ /**
16
+ * Get whether this type can be passed by reference in C++ (`const T&` vs `T`)
17
+ */
18
+ readonly canBePassedByReference: boolean;
19
+ /**
20
+ * Get the kind of the type.
21
+ */
22
+ readonly kind: TypeKind;
23
+ /**
24
+ * Get the native code required to represent this type for the given language (C++, Swift, Kotlin).
25
+ *
26
+ * E.g. for a `number` type, this would return `'double'` in C++.
27
+ *
28
+ * The `options` parameter can specify custom options. Subclasses may have more options.
29
+ */
30
+ getCode(language: Language, options?: GetCodeOptions): string;
31
+ /**
32
+ * Get all required extra files that need to be **created** for this type to properly work.
33
+ *
34
+ * E.g. for `type Gender = 'male' | 'female'`, the enum `Gender` needs to be created first (as a separate file).
35
+ */
36
+ getExtraFiles(): SourceFile[];
37
+ /**
38
+ * Get all required extra imports that need to be **imported** for this type to properly work.
39
+ */
40
+ getRequiredImports(language: Language): SourceImport[];
41
+ }
42
+ export interface NamedType extends Type {
43
+ /**
44
+ * Get the name of the value if it is a member or parameter.
45
+ *
46
+ * E.g. for a class member `double _something`, this returns `'_something'`.
47
+ */
48
+ readonly name: string;
49
+ /**
50
+ * Get the name of the value escaped as a valid C++ variable name.
51
+ *
52
+ * E.g. for a class member `double some-value`, this returns `'some_value'`.
53
+ */
54
+ readonly escapedName: string;
55
+ }
@@ -0,0 +1,19 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
4
+ export declare const VariantLabels: readonly ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eigth", "ninth", "tenth"];
5
+ type VariantLabel = (typeof VariantLabels)[number];
6
+ export declare class VariantType implements Type {
7
+ readonly variants: Type[];
8
+ readonly aliasName?: string;
9
+ constructor(variants: Type[], aliasName?: string);
10
+ get canBePassedByReference(): boolean;
11
+ get kind(): TypeKind;
12
+ get jsType(): string;
13
+ get cases(): [VariantLabel, Type][];
14
+ getAliasName(language: Language, options?: GetCodeOptions): string;
15
+ getCode(language: Language, options?: GetCodeOptions): string;
16
+ getExtraFiles(): SourceFile[];
17
+ getRequiredImports(language: Language): SourceImport[];
18
+ }
19
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class VoidType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(): SourceImport[];
10
+ }
@@ -0,0 +1,2 @@
1
+ import type { Type } from './Type.js';
2
+ export declare function getTypeAs<T>(type: Type, classReference: new (...args: any[]) => T): T;
package/lib/utils.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { SourceFile } from './syntax/SourceFile.js';
2
+ import type { SwiftCxxHelper } from './syntax/swift/SwiftCxxTypeHelper.js';
3
+ import type { Type } from 'ts-morph';
4
+ import { NitroConfig } from './config/NitroConfig.js';
5
+ export declare const NITROGEN_VERSION: string;
6
+ export declare function capitalizeName(name: string): string;
7
+ export declare function createIndentation(spacesCount: number): string;
8
+ export declare function indent(string: string, spacesCount: number): string;
9
+ export declare function indent(string: string, indentation: string): string;
10
+ export declare function errorToString(error: unknown): string;
11
+ export declare function escapeComments(string: string): string;
12
+ export declare function toUnixPath(p: string): string;
13
+ export declare function unsafeFastJoin(...segments: string[]): string;
14
+ /**
15
+ * Deduplicates all files via their full path.
16
+ * If content differs, you are f*cked.
17
+ */
18
+ export declare function deduplicateFiles(files: SourceFile[]): SourceFile[];
19
+ export declare function filterDuplicateHelperBridges(bridge: SwiftCxxHelper, i: number, array: SwiftCxxHelper[]): boolean;
20
+ export declare function toLowerCamelCase(string: string): string;
21
+ export declare function getBaseTypes(type: Type): Type[];
22
+ export declare function getHybridObjectNitroModuleConfig(type: Type): NitroConfig | undefined;
@@ -0,0 +1,14 @@
1
+ import type { SourceFile } from '../syntax/SourceFile.js';
2
+ import type { HybridObjectSpec } from '../syntax/HybridObjectSpec.js';
3
+ interface ViewComponentNames {
4
+ propsClassName: `${string}Props`;
5
+ stateClassName: `${string}State`;
6
+ nameVariable: `${string}ComponentName`;
7
+ shadowNodeClassName: `${string}ShadowNode`;
8
+ descriptorClassName: `${string}ComponentDescriptor`;
9
+ component: `${string}Component`;
10
+ manager: `${string}Manager`;
11
+ }
12
+ export declare function getViewComponentNames(spec: HybridObjectSpec): ViewComponentNames;
13
+ export declare function createViewComponentShadowNodeFiles(spec: HybridObjectSpec): SourceFile[];
14
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { HybridObjectSpec } from '../syntax/HybridObjectSpec.js';
2
+ import type { SourceFile } from '../syntax/SourceFile.js';
3
+ export declare function createHostComponentJs(spec: HybridObjectSpec): SourceFile[];
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../../syntax/SourceFile.js';
2
+ import type { HybridObjectSpec } from '../../syntax/HybridObjectSpec.js';
3
+ export declare function createKotlinHybridViewManager(spec: HybridObjectSpec): SourceFile[];
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../../syntax/SourceFile.js';
2
+ import type { HybridObjectSpec } from '../../syntax/HybridObjectSpec.js';
3
+ export declare function createSwiftHybridViewManager(spec: HybridObjectSpec): SourceFile[];
@@ -0,0 +1,5 @@
1
+ import type { SourceFile } from './syntax/SourceFile.js';
2
+ /**
3
+ * Writes the given file to disk and returns it's actual path.
4
+ */
5
+ export declare function writeFile(basePath: string, file: SourceFile): Promise<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitrogen",
3
- "version": "0.29.8",
3
+ "version": "0.30.1",
4
4
  "description": "The code-generator for react-native-nitro-modules.",
5
5
  "main": "lib/index",
6
6
  "types": "lib/index.d.ts",
@@ -36,9 +36,9 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "chalk": "^5.3.0",
39
- "react-native-nitro-modules": "^0.29.8",
39
+ "react-native-nitro-modules": "^0.30.1",
40
40
  "ts-morph": "^27.0.0",
41
- "yargs": "^17.7.2",
41
+ "yargs": "^18.0.0",
42
42
  "zod": "^4.0.5"
43
43
  },
44
44
  "devDependencies": {
package/src/init.ts CHANGED
@@ -70,7 +70,7 @@ async function isGitInstalled(): Promise<boolean> {
70
70
  try {
71
71
  execSync('git --version')
72
72
  return true
73
- } catch (error) {
73
+ } catch {
74
74
  return false
75
75
  }
76
76
  }
@@ -69,6 +69,7 @@ ${createFileMetadataString(`${typename}.hpp`)}
69
69
 
70
70
  ${includeNitroHeader('JSIConverter.hpp')}
71
71
  ${includeNitroHeader('NitroDefines.hpp')}
72
+ ${includeNitroHeader('JSIHelpers.hpp')}
72
73
 
73
74
  ${cppForwardDeclarations.join('\n')}
74
75
 
@@ -111,6 +112,9 @@ namespace margelo::nitro {
111
112
  return false;
112
113
  }
113
114
  jsi::Object obj = value.getObject(runtime);
115
+ if (!nitro::isPlainObject(runtime, obj)) {
116
+ return false;
117
+ }
114
118
  ${indent(cppCanConvertCalls, ' ')}
115
119
  return true;
116
120
  }
@@ -41,10 +41,6 @@ export class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
41
41
  case 'function':
42
42
  // Function needs to be converted from JFunc_... to Lambda
43
43
  return true
44
- case 'struct':
45
- // Structs don't need special handling - they have direct mappings
46
- // Returning false prevents secondary constructor generation that causes duplicates
47
- return false
48
44
  case 'optional':
49
45
  // Optionals need special handling if the wrapped type needs special handling
50
46
  const optional = getTypeAs(this.type, OptionalType)
@@ -53,16 +49,6 @@ export class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
53
49
  default:
54
50
  break
55
51
  }
56
- // check if any types this type references (e.g. underlying optional, array element, ...)
57
- // needs special handling. if yes, we need it as well
58
- const referencedTypes = getReferencedTypes(this.type)
59
- .filter((t) => t !== this.type)
60
- .map((t) => new KotlinCxxBridgedType(t))
61
- for (const type of referencedTypes) {
62
- if (type.needsSpecialHandling) {
63
- return true
64
- }
65
- }
66
52
  // no special handling needed
67
53
  return false
68
54
  }
@@ -95,7 +95,7 @@ abstract class ${name.HybridTSpec}: ${kotlinBase}() {
95
95
  private external fun initHybrid(): HybridData
96
96
 
97
97
  companion object {
98
- private const val TAG = "${name.HybridTSpec}"
98
+ protected const val TAG = "${name.HybridTSpec}"
99
99
  }
100
100
  }
101
101
  `.trim()
@@ -9,42 +9,28 @@ import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js'
9
9
 
10
10
  export function createKotlinStruct(structType: StructType): SourceFile[] {
11
11
  const packageName = NitroConfig.current.getAndroidPackage('java/kotlin')
12
- const values = structType.properties.map((p) => {
13
- const bridged = new KotlinCxxBridgedType(p)
14
- return `
12
+ const parameters = structType.properties
13
+ .map((p) =>
14
+ `
15
15
  @DoNotStrip
16
16
  @Keep
17
- val ${p.escapedName}: ${bridged.getTypeCode('kotlin', false)}
17
+ val ${p.escapedName}: ${p.getCode('kotlin')}
18
18
  `.trim()
19
- })
20
- let secondaryConstructor: string
21
- const needsSpecialHandling = structType.properties.some(
22
- (p) => new KotlinCxxBridgedType(p).needsSpecialHandling
23
- )
24
- if (needsSpecialHandling) {
25
- // If we need special handling for any of our properties, we need to add a convenience initializer.
26
- const params = structType.properties.map(
27
- (p) => `${p.escapedName}: ${p.getCode('kotlin')}`
28
19
  )
29
- const paramsForward = structType.properties.map((p) => {
20
+ .join(',\n')
21
+
22
+ const cxxCreateFunctionParameters = structType.properties
23
+ .map((p) => {
30
24
  const bridged = new KotlinCxxBridgedType(p)
31
- if (bridged.needsSpecialHandling) {
32
- // We need special parsing for this type
33
- return bridged.parseFromKotlinToCpp(p.escapedName, 'kotlin', false)
34
- } else {
35
- return p.escapedName
36
- }
25
+ return `${p.escapedName}: ${bridged.getTypeCode('kotlin', false)}`
37
26
  })
38
- secondaryConstructor = `
39
- /**
40
- * Initialize a new instance of \`${structType.structName}\` from Kotlin.
41
- */
42
- constructor(${indent(params.join(', '), 12)})
43
- : this(${indent(paramsForward.join(', '), 12)})
44
- `.trim()
45
- } else {
46
- secondaryConstructor = `/* main constructor */`
47
- }
27
+ .join(', ')
28
+ const cxxCreateFunctionForward = structType.properties
29
+ .map((p) => {
30
+ const bridged = new KotlinCxxBridgedType(p)
31
+ return bridged.parseFromCppToKotlin(p.escapedName, 'kotlin', false)
32
+ })
33
+ .join(', ')
48
34
 
49
35
  const extraImports = structType.properties
50
36
  .flatMap((t) => t.getRequiredImports('kotlin'))
@@ -65,13 +51,21 @@ ${extraImports.join('\n')}
65
51
  */
66
52
  @DoNotStrip
67
53
  @Keep
68
- data class ${structType.structName}
69
- @DoNotStrip
70
- @Keep
71
- constructor(
72
- ${indent(values.join(',\n'), ' ')}
73
- ) {
74
- ${indent(secondaryConstructor, ' ')}
54
+ data class ${structType.structName}(
55
+ ${indent(parameters, ' ')}
56
+ ) {
57
+ private companion object {
58
+ /**
59
+ * Constructor called from C++
60
+ */
61
+ @DoNotStrip
62
+ @Keep
63
+ @Suppress("unused")
64
+ @JvmStatic
65
+ private fun fromCpp(${cxxCreateFunctionParameters}): ${structType.structName} {
66
+ return ${structType.structName}(${cxxCreateFunctionForward})
67
+ }
68
+ }
75
69
  }
76
70
  `.trim()
77
71
 
@@ -185,14 +179,27 @@ function createCppStructInitializer(
185
179
  cppValueName: string,
186
180
  structType: StructType
187
181
  ): string {
188
- const lines: string[] = []
189
- lines.push(`return newInstance(`)
190
- const names = structType.properties.map((p) => {
191
- const name = `${cppValueName}.${p.escapedName}`
192
- const bridge = new KotlinCxxBridgedType(p)
193
- return bridge.parse(name, 'c++', 'kotlin', 'c++')
194
- })
195
- lines.push(` ${indent(names.join(',\n'), ' ')}`)
196
- lines.push(');')
197
- return lines.join('\n')
182
+ const jniTypes = structType.properties
183
+ .map((p) => {
184
+ const bridge = new KotlinCxxBridgedType(p)
185
+ return bridge.asJniReferenceType('alias')
186
+ })
187
+ .join(', ')
188
+ const params = structType.properties
189
+ .map((p) => {
190
+ const name = `${cppValueName}.${p.escapedName}`
191
+ const bridge = new KotlinCxxBridgedType(p)
192
+ return bridge.parse(name, 'c++', 'kotlin', 'c++')
193
+ })
194
+ .join(',\n')
195
+
196
+ return `
197
+ using JSignature = J${structType.structName}(${jniTypes});
198
+ static const auto clazz = javaClassStatic();
199
+ static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
200
+ return create(
201
+ clazz,
202
+ ${indent(params, ' ')}
203
+ );
204
+ `.trim()
198
205
  }
@@ -7,19 +7,21 @@ import {
7
7
  toReferenceType,
8
8
  } from '../helpers.js'
9
9
  import type { SourceFile } from '../SourceFile.js'
10
+ import { OptionalType } from '../types/OptionalType.js'
10
11
  import { type VariantType } from '../types/VariantType.js'
11
12
  import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js'
12
13
 
13
14
  export function createKotlinVariant(variant: VariantType): SourceFile[] {
14
- const jsName = variant.variants.map((v) => v.getCode('kotlin')).join('|')
15
+ const jsName = variant.variants.map((v) => v.getCode('kotlin')).join(' | ')
15
16
  const kotlinName = variant.getAliasName('kotlin')
16
17
  const namespace = `J${kotlinName}_impl`
17
18
 
18
19
  const innerClasses = variant.cases.map(([label, v]) => {
19
20
  const innerName = capitalizeName(label)
21
+ const bridge = new KotlinCxxBridgedType(v)
20
22
  return `
21
23
  @DoNotStrip
22
- data class ${innerName}(@DoNotStrip val value: ${v.getCode('kotlin')}): ${kotlinName}()
24
+ data class ${innerName}(@DoNotStrip val value: ${bridge.getTypeCode('kotlin')}): ${kotlinName}()
23
25
  `.trim()
24
26
  })
25
27
 
@@ -35,14 +37,36 @@ val is${innerName}: Boolean
35
37
  get() = this is ${innerName}
36
38
  `.trim()
37
39
  })
40
+ const asFunctions = variant.cases.map(([label, v]) => {
41
+ const innerName = capitalizeName(label)
42
+ const bridge = new KotlinCxxBridgedType(v)
43
+ const optional = new OptionalType(v)
44
+ return `
45
+ fun as${innerName}OrNull(): ${optional.getCode('kotlin')} {
46
+ val value = (this as? ${innerName})?.value ?: return null
47
+ return ${bridge.parseFromCppToKotlin('value', 'kotlin')}
48
+ }
49
+ `.trim()
50
+ })
51
+ const matchParameters = variant.cases.map(([label, v]) => {
52
+ return `${label}: (${v.getCode('kotlin')}) -> R`
53
+ })
54
+ const matchCases = variant.cases.map(([label, v]) => {
55
+ const innerName = capitalizeName(label)
56
+ const bridge = new KotlinCxxBridgedType(v)
57
+ return `
58
+ is ${innerName} -> ${label}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
59
+ `.trim()
60
+ })
38
61
 
39
62
  const createFunctions = variant.cases.map(([label, v]) => {
63
+ const bridge = new KotlinCxxBridgedType(v)
40
64
  const innerName = capitalizeName(label)
41
65
  return `
42
66
  @JvmStatic
43
67
  @DoNotStrip
44
- fun create(value: ${v.getCode('kotlin')}): ${kotlinName} = ${innerName}(value)
45
- `.trim()
68
+ fun create(value: ${bridge.getTypeCode('kotlin')}): ${kotlinName} = ${innerName}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
69
+ `.trim()
46
70
  })
47
71
 
48
72
  const extraImports = variant.variants
@@ -65,12 +89,21 @@ ${extraImports.join('\n')}
65
89
  sealed class ${kotlinName} {
66
90
  ${indent(innerClasses.join('\n'), ' ')}
67
91
 
92
+ @Deprecated("getAs() is not type-safe. Use fold/asFirstOrNull/asSecondOrNull instead.", level = DeprecationLevel.ERROR)
68
93
  inline fun <reified T> getAs(): T? = when (this) {
69
94
  ${indent(getterCases.join('\n'), ' ')}
70
95
  }
71
96
 
72
97
  ${indent(isFunctions.join('\n'), ' ')}
73
98
 
99
+ ${indent(asFunctions.join('\n'), ' ')}
100
+
101
+ inline fun <R> match(${matchParameters.join(', ')}): R {
102
+ return when (this) {
103
+ ${indent(matchCases.join('\n'), ' ')}
104
+ }
105
+ }
106
+
74
107
  companion object {
75
108
  ${indent(createFunctions.join('\n'), ' ')}
76
109
  }