@react-native-windows/codegen 0.0.0-canary.9 → 0.0.1-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/CHANGELOG.md +726 -15
- package/README.md +1 -1
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +92 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +12 -0
- package/lib-commonjs/generators/AliasGen.js +88 -0
- package/lib-commonjs/generators/AliasGen.js.map +1 -0
- package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
- package/lib-commonjs/generators/AliasManaging.js +49 -0
- package/lib-commonjs/generators/AliasManaging.js.map +1 -0
- package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
- package/lib-commonjs/generators/GenerateNM2.js +142 -0
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
- package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
- package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
- package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
- package/lib-commonjs/generators/ObjectTypes.js +75 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
- package/lib-commonjs/generators/ParamTypes.js +137 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
- package/lib-commonjs/generators/ReturnTypes.js +29 -0
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
- package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
- package/lib-commonjs/generators/ValidateConstants.js +38 -0
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
- package/lib-commonjs/generators/ValidateMethods.d.ts +9 -0
- package/lib-commonjs/generators/ValidateMethods.js +75 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +37 -0
- package/lib-commonjs/index.js +224 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +37 -20
- package/src/Cli.ts +57 -190
- package/src/generators/AliasGen.ts +149 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +138 -295
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +130 -0
- package/src/generators/ParamTypes.ts +298 -0
- package/src/generators/ReturnTypes.ts +70 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +177 -0
- package/src/index.ts +393 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -583
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
package/README.md
CHANGED
package/bin.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
* Licensed under the MIT License.
|
|
5
|
+
*
|
|
6
|
+
* @format
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
13
|
+
const index_1 = require("./index");
|
|
14
|
+
const argv = yargs_1.default.options({
|
|
15
|
+
file: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
describe: 'file which contains spec',
|
|
18
|
+
},
|
|
19
|
+
files: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
array: true,
|
|
22
|
+
describe: 'glob patterns for files which contains specs',
|
|
23
|
+
},
|
|
24
|
+
modulesTypeScriptTypes: {
|
|
25
|
+
type: 'boolean',
|
|
26
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
27
|
+
default: false,
|
|
28
|
+
},
|
|
29
|
+
modulesCxx: {
|
|
30
|
+
type: 'boolean',
|
|
31
|
+
describe: 'generate C++ JSI turbo module spec files',
|
|
32
|
+
default: false,
|
|
33
|
+
},
|
|
34
|
+
modulesWindows: {
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
describe: 'generate turbo module spec files for REACT_MODULE',
|
|
37
|
+
default: false,
|
|
38
|
+
},
|
|
39
|
+
methodOnly: {
|
|
40
|
+
type: 'boolean',
|
|
41
|
+
describe: 'generate only method metadata in C++ turbo module spec',
|
|
42
|
+
default: false,
|
|
43
|
+
},
|
|
44
|
+
outputDirectory: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
describe: 'output directory',
|
|
47
|
+
default: 'codegen',
|
|
48
|
+
},
|
|
49
|
+
test: {
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
describe: 'Verify that the generated output is unchanged',
|
|
52
|
+
default: false,
|
|
53
|
+
},
|
|
54
|
+
namespace: {
|
|
55
|
+
type: 'string',
|
|
56
|
+
describe: 'C++/C# Namespace to put generated native modules in',
|
|
57
|
+
default: 'MyNamespace',
|
|
58
|
+
},
|
|
59
|
+
libraryName: {
|
|
60
|
+
type: 'string',
|
|
61
|
+
required: true,
|
|
62
|
+
describe: 'Used for part of the path generated within the codegen dir',
|
|
63
|
+
},
|
|
64
|
+
cppStringType: {
|
|
65
|
+
choices: ['std::string', 'std::wstring'],
|
|
66
|
+
describe: 'C++ string type in generated code, should be "std::string" or "std::wstring"',
|
|
67
|
+
default: 'std::string',
|
|
68
|
+
},
|
|
69
|
+
separateDataTypes: {
|
|
70
|
+
type: 'boolean',
|
|
71
|
+
describe: 'generate data types in a separate file',
|
|
72
|
+
default: false,
|
|
73
|
+
},
|
|
74
|
+
}).argv;
|
|
75
|
+
if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
76
|
+
console.error('You must specify either --file or --files.');
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
if (argv.cppStringType !== 'std::string' &&
|
|
80
|
+
argv.cppStringType !== 'std::wstring') {
|
|
81
|
+
console.error('cppStringType should be "std::string" or "std::wstring".');
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
// type casting is necessary here because
|
|
85
|
+
// cppStringType does not become union of string literals
|
|
86
|
+
// until yargs.options get improved in the future
|
|
87
|
+
const changesNecessary = (0, index_1.runCodeGen)(argv);
|
|
88
|
+
if (argv.test && changesNecessary) {
|
|
89
|
+
console.error('There is a change in the output of codegen. Rerun "react-native codegen-windows" to regenerate.');
|
|
90
|
+
process.exit(2);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=Cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,mCAAmD;AAEnD,MAAM,IAAI,GAAG,eAAK,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,0BAA0B;KACrC;IACD,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,IAAI;QACX,QAAQ,EAAE,8CAA8C;KACzD;IACD,sBAAsB,EAAE;QACtB,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,sDAAsD;QAChE,OAAO,EAAE,KAAK;KACf;IACD,UAAU,EAAE;QACV,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,0CAA0C;QACpD,OAAO,EAAE,KAAK;KACf;IACD,cAAc,EAAE;QACd,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,mDAAmD;QAC7D,OAAO,EAAE,KAAK;KACf;IACD,UAAU,EAAE;QACV,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,wDAAwD;QAClE,OAAO,EAAE,KAAK;KACf;IACD,eAAe,EAAE;QACf,IAAI,EAAE,QAAQ;QAC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G,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport {CodeGenOptions, runCodeGen} from './index';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'string',\n array: true,\n describe: 'glob patterns for files which contains specs',\n },\n modulesTypeScriptTypes: {\n type: 'boolean',\n describe: 'generate turbo module definition files in TypeScript',\n default: false,\n },\n modulesCxx: {\n type: 'boolean',\n describe: 'generate C++ JSI turbo module spec files',\n default: false,\n },\n modulesWindows: {\n type: 'boolean',\n describe: 'generate turbo module spec files for REACT_MODULE',\n default: false,\n },\n methodOnly: {\n type: 'boolean',\n describe: 'generate only method metadata in C++ turbo module spec',\n default: false,\n },\n outputDirectory: {\n type: 'string',\n describe: 'output directory',\n default: 'codegen',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\n default: false,\n },\n namespace: {\n type: 'string',\n describe: 'C++/C# Namespace to put generated native modules in',\n default: 'MyNamespace',\n },\n libraryName: {\n type: 'string',\n required: true,\n describe: 'Used for part of the path generated within the codegen dir',\n },\n cppStringType: {\n choices: ['std::string', 'std::wstring'],\n describe:\n 'C++ string type in generated code, should be \"std::string\" or \"std::wstring\"',\n default: 'std::string',\n },\n separateDataTypes: {\n type: 'boolean',\n describe: 'generate data types in a separate file',\n default: false,\n },\n}).argv;\n\nif ((argv.file && argv.files) || (!argv.file && !argv.files)) {\n console.error('You must specify either --file or --files.');\n process.exit(1);\n}\n\nif (\n argv.cppStringType !== 'std::string' &&\n argv.cppStringType !== 'std::wstring'\n) {\n console.error('cppStringType should be \"std::string\" or \"std::wstring\".');\n process.exit(1);\n}\n\n// type casting is necessary here because\n// cppStringType does not become union of string literals\n// until yargs.options get improved in the future\nconst changesNecessary = runCodeGen(<CodeGenOptions>argv);\n\nif (argv.test && changesNecessary) {\n console.error(\n 'There is a change in the output of codegen. Rerun \"react-native codegen-windows\" to regenerate.',\n );\n process.exit(2);\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import type { NativeModuleObjectTypeAnnotation } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
|
+
import { AliasMap } from './AliasManaging';
|
|
8
|
+
import { CppCodegenOptions } from './ObjectTypes';
|
|
9
|
+
export declare function createAliasMap(nativeModuleAliases: {
|
|
10
|
+
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
11
|
+
}): AliasMap;
|
|
12
|
+
export declare function generateAliases(aliases: AliasMap, options: CppCodegenOptions): [string, string];
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.generateAliases = exports.createAliasMap = void 0;
|
|
9
|
+
const AliasManaging_1 = require("./AliasManaging");
|
|
10
|
+
const ObjectTypes_1 = require("./ObjectTypes");
|
|
11
|
+
function translateObjectMembersDefinition(type, aliases, baseAliasName, prefix, options) {
|
|
12
|
+
return type.properties
|
|
13
|
+
.map((prop) => {
|
|
14
|
+
let propType = prop.typeAnnotation;
|
|
15
|
+
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
16
|
+
propType = { type: 'NullableTypeAnnotation', typeAnnotation: propType };
|
|
17
|
+
}
|
|
18
|
+
return `${prefix}${(0, ObjectTypes_1.translateField)(propType, aliases, `${baseAliasName}_${prop.name}`, options)} ${prop.name};`;
|
|
19
|
+
})
|
|
20
|
+
.join('\n');
|
|
21
|
+
}
|
|
22
|
+
function translateObjectMembersReflection(type, aliasCppName, prefix) {
|
|
23
|
+
return type.properties
|
|
24
|
+
.map((prop) => {
|
|
25
|
+
return `${prefix}{L"${prop.name}", &${aliasCppName}::${prop.name}},`;
|
|
26
|
+
})
|
|
27
|
+
.join('\n');
|
|
28
|
+
}
|
|
29
|
+
function createAliasMap(nativeModuleAliases) {
|
|
30
|
+
const aliases = { types: {}, jobs: Object.keys(nativeModuleAliases) };
|
|
31
|
+
for (const aliasName of aliases.jobs) {
|
|
32
|
+
aliases.types[aliasName] = nativeModuleAliases[aliasName];
|
|
33
|
+
}
|
|
34
|
+
return aliases;
|
|
35
|
+
}
|
|
36
|
+
exports.createAliasMap = createAliasMap;
|
|
37
|
+
function generateSingleAlias(aliases, aliasName, aliasCode, options) {
|
|
38
|
+
const aliasCppName = (0, AliasManaging_1.getAliasCppName)(aliasName);
|
|
39
|
+
const aliasType = aliases.types[aliasName];
|
|
40
|
+
const definition = `
|
|
41
|
+
struct ${aliasCppName} {
|
|
42
|
+
${translateObjectMembersDefinition(aliasType, aliases, aliasName, ' ', options)}
|
|
43
|
+
};
|
|
44
|
+
`;
|
|
45
|
+
const reflection = `
|
|
46
|
+
inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {
|
|
47
|
+
winrt::Microsoft::ReactNative::FieldMap fieldMap {
|
|
48
|
+
${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}
|
|
49
|
+
};
|
|
50
|
+
return fieldMap;
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
aliasCode[aliasName] = { definition, reflection };
|
|
54
|
+
}
|
|
55
|
+
function generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options) {
|
|
56
|
+
// retrieve and clean all ungenerated aliases
|
|
57
|
+
const jobs = aliases.jobs;
|
|
58
|
+
aliases.jobs = [];
|
|
59
|
+
// generate each one in its found order
|
|
60
|
+
for (const aliasName of jobs) {
|
|
61
|
+
// generate a new struct and all fields will be examined
|
|
62
|
+
// new anonymous objects could be found
|
|
63
|
+
// they will be stored in aliases.jobs
|
|
64
|
+
generateSingleAlias(aliases, aliasName, aliasCode, options);
|
|
65
|
+
// nested C++ structs must be put before the current C++ struct
|
|
66
|
+
// as they will be used in the current C++ struct
|
|
67
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
68
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
69
|
+
// all referenced C++ structs are generated
|
|
70
|
+
// put the current one following them
|
|
71
|
+
aliasOrder.push(aliasName);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function generateAliases(aliases, options) {
|
|
75
|
+
const aliasCode = {};
|
|
76
|
+
const aliasOrder = [];
|
|
77
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
|
|
78
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
79
|
+
let customTypes = '';
|
|
80
|
+
let customReflection = '';
|
|
81
|
+
for (const aliasName of aliasOrder) {
|
|
82
|
+
customTypes = `${customTypes}${aliasCode[aliasName].definition}`;
|
|
83
|
+
customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;
|
|
84
|
+
}
|
|
85
|
+
return [customTypes, customReflection];
|
|
86
|
+
}
|
|
87
|
+
exports.generateAliases = generateAliases;
|
|
88
|
+
//# sourceMappingURL=AliasGen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AliasGen.js","sourceRoot":"","sources":["../../src/generators/AliasGen.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,mDAA0D;AAC1D,+CAAgE;AAEhE,SAAS,gCAAgC,CACvC,IAAsC,EACtC,OAAiB,EACjB,aAAqB,EACrB,MAAc,EACd,OAA0B;IAE1B,OAAO,IAAI,CAAC,UAAU;SACnB,GAAG,CAAC,CAAC,IAA0D,EAAE,EAAE;QAClE,IAAI,QAAQ,GAAG,IAAI,CAAC,cAAc,CAA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AAW,GAAG,GAAG,WAAW,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,CAAC;QACjE,gBAAgB,GAAG,GAAG,gBAAgB,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,CAAC;KAC5E;IACD,OAAO,CAAC,WAAW,EAAE,gBAAgB,CAAC,CAAC;AACzC,CAAC;AAhBD,0CAgBC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {\n NativeModuleBaseTypeAnnotation,\n NativeModuleObjectTypeAnnotation,\n NamedShape,\n Nullable,\n} from '@react-native/codegen/lib/CodegenSchema';\nimport {AliasMap, getAliasCppName} from './AliasManaging';\nimport {CppCodegenOptions, translateField} from './ObjectTypes';\n\nfunction translateObjectMembersDefinition(\n type: NativeModuleObjectTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n prefix: string,\n options: CppCodegenOptions,\n) {\n return type.properties\n .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {\n let propType = prop.typeAnnotation;\n if (prop.optional && propType.type !== 'NullableTypeAnnotation') {\n propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};\n }\n return `${prefix}${translateField(\n propType,\n aliases,\n `${baseAliasName}_${prop.name}`,\n options,\n )} ${prop.name};`;\n })\n .join('\\n');\n}\n\nfunction translateObjectMembersReflection(\n type: NativeModuleObjectTypeAnnotation,\n aliasCppName: string,\n prefix: string,\n) {\n return type.properties\n .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {\n return `${prefix}{L\"${prop.name}\", &${aliasCppName}::${prop.name}},`;\n })\n .join('\\n');\n}\n\nexport function createAliasMap(nativeModuleAliases: {\n [name: string]: NativeModuleObjectTypeAnnotation;\n}): AliasMap {\n const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};\n for (const aliasName of aliases.jobs) {\n aliases.types[aliasName] = nativeModuleAliases[aliasName];\n }\n return aliases;\n}\n\ninterface AliasCode {\n definition: string;\n reflection: string;\n}\n\ninterface AliasCodeMap {\n [name: string]: AliasCode;\n}\n\nfunction generateSingleAlias(\n aliases: AliasMap,\n aliasName: string,\n aliasCode: AliasCodeMap,\n options: CppCodegenOptions,\n): void {\n const aliasCppName = getAliasCppName(aliasName);\n const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];\n const definition = `\nstruct ${aliasCppName} {\n${translateObjectMembersDefinition(\n aliasType,\n aliases,\n aliasName,\n ' ',\n options,\n)}\n};\n`;\n const reflection = `\ninline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {\n winrt::Microsoft::ReactNative::FieldMap fieldMap {\n${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}\n };\n return fieldMap;\n}\n`;\n aliasCode[aliasName] = {definition, reflection};\n}\n\nfunction generateNestedAliasesInCorrectOrder(\n aliases: AliasMap,\n aliasCode: AliasCodeMap,\n aliasOrder: string[],\n options: CppCodegenOptions,\n): void {\n // retrieve and clean all ungenerated aliases\n const jobs = aliases.jobs;\n aliases.jobs = [];\n\n // generate each one in its found order\n for (const aliasName of jobs) {\n // generate a new struct and all fields will be examined\n // new anonymous objects could be found\n // they will be stored in aliases.jobs\n generateSingleAlias(aliases, aliasName, aliasCode, options);\n // nested C++ structs must be put before the current C++ struct\n // as they will be used in the current C++ struct\n // the order will be perfectly and easily ensured by doing this recursively\n generateNestedAliasesInCorrectOrder(\n aliases,\n aliasCode,\n aliasOrder,\n options,\n );\n // all referenced C++ structs are generated\n // put the current one following them\n aliasOrder.push(aliasName);\n }\n}\n\nexport function generateAliases(\n aliases: AliasMap,\n options: CppCodegenOptions,\n): [string, string] {\n const aliasCode: AliasCodeMap = {};\n const aliasOrder: string[] = [];\n generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);\n\n // aliasOrder now has the correct order of C++ struct code\n let customTypes = '';\n let customReflection = '';\n for (const aliasName of aliasOrder) {\n customTypes = `${customTypes}${aliasCode[aliasName].definition}`;\n customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;\n }\n return [customTypes, customReflection];\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import type { NativeModuleObjectTypeAnnotation } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
|
+
export declare function setPreferredModuleName(moduleName: string): void;
|
|
8
|
+
export declare function getAliasCppName(typeName: string): string;
|
|
9
|
+
export interface AliasMap {
|
|
10
|
+
types: {
|
|
11
|
+
[name: string]: NativeModuleObjectTypeAnnotation | undefined;
|
|
12
|
+
};
|
|
13
|
+
jobs: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function getAnonymousAliasCppName(aliases: AliasMap, baseAliasName: string, objectType: NativeModuleObjectTypeAnnotation): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.getAnonymousAliasCppName = exports.getAliasCppName = exports.setPreferredModuleName = void 0;
|
|
9
|
+
let preferredModuleName = '';
|
|
10
|
+
function setPreferredModuleName(moduleName) {
|
|
11
|
+
preferredModuleName = moduleName;
|
|
12
|
+
}
|
|
13
|
+
exports.setPreferredModuleName = setPreferredModuleName;
|
|
14
|
+
function getAliasCppName(typeName) {
|
|
15
|
+
return `${preferredModuleName}Spec_${typeName}`;
|
|
16
|
+
}
|
|
17
|
+
exports.getAliasCppName = getAliasCppName;
|
|
18
|
+
const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
|
|
19
|
+
function recordAnonymousAlias(aliases, baseAliasName, extended) {
|
|
20
|
+
extended[ExtendedObjectKey] = baseAliasName;
|
|
21
|
+
aliases.types[baseAliasName] = extended;
|
|
22
|
+
aliases.jobs.push(baseAliasName);
|
|
23
|
+
return baseAliasName;
|
|
24
|
+
}
|
|
25
|
+
function getAnonymousAliasCppName(aliases, baseAliasName, objectType) {
|
|
26
|
+
// someone found an anonymous object literal type
|
|
27
|
+
// if the ExtendedObjectKey flag has been set
|
|
28
|
+
// then it is a known one
|
|
29
|
+
// this happens because method signatures are generate twice in spec and error messages
|
|
30
|
+
const extended = objectType;
|
|
31
|
+
const key = extended[ExtendedObjectKey];
|
|
32
|
+
if (key !== undefined) {
|
|
33
|
+
return getAliasCppName(key);
|
|
34
|
+
}
|
|
35
|
+
// if the ExtendedObjectKey flag has not been set
|
|
36
|
+
// it means it is a unknown one
|
|
37
|
+
// associate the name with this object literal type and return
|
|
38
|
+
if (aliases.types[baseAliasName] === undefined) {
|
|
39
|
+
return getAliasCppName(recordAnonymousAlias(aliases, baseAliasName, extended));
|
|
40
|
+
}
|
|
41
|
+
// sometimes names could be anonymous
|
|
42
|
+
let index = 2;
|
|
43
|
+
while (aliases.types[`${baseAliasName}${index}`] !== undefined) {
|
|
44
|
+
index++;
|
|
45
|
+
}
|
|
46
|
+
return getAliasCppName(recordAnonymousAlias(aliases, `${baseAliasName}${index}`, extended));
|
|
47
|
+
}
|
|
48
|
+
exports.getAnonymousAliasCppName = getAnonymousAliasCppName;
|
|
49
|
+
//# sourceMappingURL=AliasManaging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AliasManaging.js","sourceRoot":"","sources":["../../src/generators/AliasManaging.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAIb,IAAI,mBAAmB,GAAW,EAAE,CAAC;AAErC,SAAgB,sBAAsB,CAAC,UAAkB;IACvD,mBAAmB,GAAG,UAAU,CAAC;AACnC,CAAC;AAFD,wDAEC;AAED,SAAgB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esContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {NativeModuleObjectTypeAnnotation} from '@react-native/codegen/lib/CodegenSchema';\n\nlet preferredModuleName: string = '';\n\nexport function setPreferredModuleName(moduleName: string): void {\n preferredModuleName = moduleName;\n}\n\nexport function getAliasCppName(typeName: string): string {\n return `${preferredModuleName}Spec_${typeName}`;\n}\n\nexport interface AliasMap {\n types: {[name: string]: NativeModuleObjectTypeAnnotation | undefined};\n jobs: string[];\n}\n\nconst ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';\ninterface ExtendedObject extends NativeModuleObjectTypeAnnotation {\n '$RNW-TURBOMODULE-ALIAS'?: string;\n}\n\nfunction recordAnonymousAlias(\n aliases: AliasMap,\n baseAliasName: string,\n extended: ExtendedObject,\n): string {\n extended[ExtendedObjectKey] = baseAliasName;\n aliases.types[baseAliasName] = extended;\n aliases.jobs.push(baseAliasName);\n return baseAliasName;\n}\n\nexport function getAnonymousAliasCppName(\n aliases: AliasMap,\n baseAliasName: string,\n objectType: NativeModuleObjectTypeAnnotation,\n): string {\n // someone found an anonymous object literal type\n // if the ExtendedObjectKey flag has been set\n // then it is a known one\n // this happens because method signatures are generate twice in spec and error messages\n const extended = <ExtendedObject>objectType;\n const key = extended[ExtendedObjectKey];\n if (key !== undefined) {\n return getAliasCppName(key);\n }\n\n // if the ExtendedObjectKey flag has not been set\n // it means it is a unknown one\n // associate the name with this object literal type and return\n if (aliases.types[baseAliasName] === undefined) {\n return getAliasCppName(\n recordAnonymousAlias(aliases, baseAliasName, extended),\n );\n }\n\n // sometimes names could be anonymous\n let index = 2;\n while (aliases.types[`${baseAliasName}${index}`] !== undefined) {\n index++;\n }\n\n return getAliasCppName(\n recordAnonymousAlias(aliases, `${baseAliasName}${index}`, extended),\n );\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import type { SchemaType } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
|
+
import type { CppStringTypes } from './ObjectTypes';
|
|
8
|
+
export type { CppStringTypes } from './ObjectTypes';
|
|
9
|
+
type FilesOutput = Map<string, string>;
|
|
10
|
+
export declare function createNM2Generator({ methodOnly, namespace, cppStringType, separateDataTypes, }: {
|
|
11
|
+
methodOnly: boolean;
|
|
12
|
+
namespace: string;
|
|
13
|
+
cppStringType: CppStringTypes;
|
|
14
|
+
separateDataTypes: boolean;
|
|
15
|
+
}): (_libraryName: string, schema: SchemaType, _moduleSpecName: string) => FilesOutput;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createNM2Generator = void 0;
|
|
9
|
+
const AliasManaging_1 = require("./AliasManaging");
|
|
10
|
+
const AliasGen_1 = require("./AliasGen");
|
|
11
|
+
const ValidateConstants_1 = require("./ValidateConstants");
|
|
12
|
+
const ValidateMethods_1 = require("./ValidateMethods");
|
|
13
|
+
const headerTemplate = `/*
|
|
14
|
+
* This file is auto-generated from a NativeModule spec file in js.
|
|
15
|
+
*
|
|
16
|
+
* This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules
|
|
17
|
+
* in a way that also verifies at compile time that the native module matches the interface required
|
|
18
|
+
* by the TurboModule JS spec.
|
|
19
|
+
*/
|
|
20
|
+
#pragma once`;
|
|
21
|
+
const specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::
|
|
22
|
+
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
23
|
+
::_MODULE_MEMBERS_TUPLES_::
|
|
24
|
+
|
|
25
|
+
template <class TModule>
|
|
26
|
+
static constexpr void ValidateModule() noexcept {
|
|
27
|
+
::_MODULE_MEMBERS_CHECKS_::
|
|
28
|
+
|
|
29
|
+
::_MODULE_MEMBERS_ERRORS_::
|
|
30
|
+
}`;
|
|
31
|
+
const typeOnlyTemplate = `
|
|
32
|
+
${headerTemplate}
|
|
33
|
+
|
|
34
|
+
#include <string>
|
|
35
|
+
#include <optional>
|
|
36
|
+
#include <functional>
|
|
37
|
+
#include <vector>
|
|
38
|
+
|
|
39
|
+
namespace ::_NAMESPACE_:: {
|
|
40
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
41
|
+
} // namespace ::_NAMESPACE_::
|
|
42
|
+
`;
|
|
43
|
+
const moduleOnlyTemplate = `
|
|
44
|
+
${headerTemplate}
|
|
45
|
+
|
|
46
|
+
::_TYPE_DEFINITION_INCLUDE_::
|
|
47
|
+
#include <NativeModules.h>
|
|
48
|
+
#include <tuple>
|
|
49
|
+
|
|
50
|
+
namespace ::_NAMESPACE_:: {
|
|
51
|
+
${specTemplate}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
} // namespace ::_NAMESPACE_::
|
|
55
|
+
`;
|
|
56
|
+
const allInOneTemplate = `
|
|
57
|
+
${headerTemplate}
|
|
58
|
+
|
|
59
|
+
#include <NativeModules.h>
|
|
60
|
+
#include <tuple>
|
|
61
|
+
|
|
62
|
+
namespace ::_NAMESPACE_:: {
|
|
63
|
+
::_MODULE_CUSTPM_TYPES_::
|
|
64
|
+
${specTemplate}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
} // namespace ::_NAMESPACE_::
|
|
68
|
+
`;
|
|
69
|
+
function createNM2Generator({ methodOnly, namespace, cppStringType, separateDataTypes, }) {
|
|
70
|
+
return (_libraryName, schema, _moduleSpecName) => {
|
|
71
|
+
const files = new Map();
|
|
72
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
73
|
+
const nativeModule = schema.modules[moduleName];
|
|
74
|
+
// from 0.65 facebook's react-native-codegen
|
|
75
|
+
// the module name has the Native prefix comparing to 0.63
|
|
76
|
+
// when reading files we provided
|
|
77
|
+
const preferredModuleName = moduleName.startsWith('Native')
|
|
78
|
+
? moduleName.substr(6)
|
|
79
|
+
: moduleName;
|
|
80
|
+
(0, AliasManaging_1.setPreferredModuleName)(preferredModuleName);
|
|
81
|
+
if (nativeModule.type === 'NativeModule') {
|
|
82
|
+
console.log(`Generating Native${preferredModuleName}Spec.g.h`);
|
|
83
|
+
// copy all explicit to a map
|
|
84
|
+
const aliases = (0, AliasGen_1.createAliasMap)(nativeModule.aliasMap);
|
|
85
|
+
// prepare methods
|
|
86
|
+
const methods = (0, ValidateMethods_1.generateValidateMethods)(nativeModule, aliases, {
|
|
87
|
+
cppStringType,
|
|
88
|
+
});
|
|
89
|
+
let tuples = `
|
|
90
|
+
static constexpr auto methods = std::tuple{
|
|
91
|
+
${methods[0]}
|
|
92
|
+
};`;
|
|
93
|
+
let checks = `
|
|
94
|
+
constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
|
|
95
|
+
let errors = methods[1];
|
|
96
|
+
// prepare constants
|
|
97
|
+
const constants = (0, ValidateConstants_1.generateValidateConstants)(nativeModule, aliases);
|
|
98
|
+
if (constants !== undefined && !methodOnly) {
|
|
99
|
+
tuples = `
|
|
100
|
+
static constexpr auto constants = std::tuple{
|
|
101
|
+
${constants[0]}
|
|
102
|
+
};${tuples}`;
|
|
103
|
+
checks = `
|
|
104
|
+
constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
|
|
105
|
+
errors = `${constants[1]}
|
|
106
|
+
|
|
107
|
+
${errors}`;
|
|
108
|
+
}
|
|
109
|
+
// generate code for structs
|
|
110
|
+
const [customTypes, customReflection] = (0, AliasGen_1.generateAliases)(aliases, {
|
|
111
|
+
cppStringType,
|
|
112
|
+
});
|
|
113
|
+
const customTypesExist = customTypes !== '';
|
|
114
|
+
const replaceContent = function (template) {
|
|
115
|
+
return template
|
|
116
|
+
.replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)
|
|
117
|
+
.replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)
|
|
118
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
119
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
120
|
+
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
121
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
122
|
+
.replace(/::_TYPE_DEFINITION_INCLUDE_::/g, customTypesExist
|
|
123
|
+
? `// #include "Native${preferredModuleName}DataTypes.g.h" before this file to use the generated type definition`
|
|
124
|
+
: '')
|
|
125
|
+
.replace(/::_NAMESPACE_::/g, namespace);
|
|
126
|
+
};
|
|
127
|
+
if (separateDataTypes) {
|
|
128
|
+
if (customTypesExist) {
|
|
129
|
+
files.set(`Native${preferredModuleName}DataTypes.g.h`, replaceContent(typeOnlyTemplate));
|
|
130
|
+
}
|
|
131
|
+
files.set(`Native${preferredModuleName}Spec.g.h`, replaceContent(moduleOnlyTemplate));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
files.set(`Native${preferredModuleName}Spec.g.h`, replaceContent(allInOneTemplate));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return files;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
exports.createNM2Generator = createNM2Generator;
|
|
142
|
+
//# sourceMappingURL=GenerateNM2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDAA0D;AAO1D,MAAM,cAA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AAC,CAAC;gBAEF,IAAI,iBAAiB,EAAE;oBACrB,IAAI,gBAAgB,EAAE;wBACpB,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,eAAe,EAC3C,cAAc,CAAC,gBAAgB,CAAC,CACjC,CAAC;qBACH;oBACD,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,UAAU,EACtC,cAAc,CAAC,kBAAkB,CAAC,CACnC,CAAC;iBACH;qBAAM;oBACL,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,UAAU,EACtC,cAAc,CAAC,gBAAgB,CAAC,CACjC,CAAC;iBACH;aACF;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AA1GD,gDA0GC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {SchemaType} from '@react-native/codegen/lib/CodegenSchema';\nimport {AliasMap, setPreferredModuleName} from './AliasManaging';\nimport {createAliasMap, generateAliases} from './AliasGen';\nimport {generateValidateConstants} from './ValidateConstants';\nimport {generateValidateMethods} from './ValidateMethods';\nimport type {CppStringTypes} from './ObjectTypes';\n\nexport type {CppStringTypes} from './ObjectTypes';\n\ntype FilesOutput = Map<string, string>;\n\nconst headerTemplate = `/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules\n * in a way that also verifies at compile time that the native module matches the interface required\n * by the TurboModule JS spec.\n */\n#pragma once`;\n\nconst specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::\nstruct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {\n::_MODULE_MEMBERS_TUPLES_::\n\n template <class TModule>\n static constexpr void ValidateModule() noexcept {\n::_MODULE_MEMBERS_CHECKS_::\n\n::_MODULE_MEMBERS_ERRORS_::\n }`;\n\nconst typeOnlyTemplate = `\n${headerTemplate}\n\n#include <string>\n#include <optional>\n#include <functional>\n#include <vector>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_CUSTPM_TYPES_::\n} // namespace ::_NAMESPACE_::\n`;\n\nconst moduleOnlyTemplate = `\n${headerTemplate}\n\n::_TYPE_DEFINITION_INCLUDE_::\n#include <NativeModules.h>\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n${specTemplate}\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nconst allInOneTemplate = `\n${headerTemplate}\n\n#include <NativeModules.h>\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_CUSTPM_TYPES_::\n${specTemplate}\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nexport function createNM2Generator({\n methodOnly,\n namespace,\n cppStringType,\n separateDataTypes,\n}: {\n methodOnly: boolean;\n namespace: string;\n cppStringType: CppStringTypes;\n separateDataTypes: boolean;\n}) {\n return (\n _libraryName: string,\n schema: SchemaType,\n _moduleSpecName: string,\n ): FilesOutput => {\n const files = new Map<string, string>();\n\n for (const moduleName of Object.keys(schema.modules)) {\n const nativeModule = schema.modules[moduleName];\n // from 0.65 facebook's react-native-codegen\n // the module name has the Native prefix comparing to 0.63\n // when reading files we provided\n const preferredModuleName = moduleName.startsWith('Native')\n ? moduleName.substr(6)\n : moduleName;\n setPreferredModuleName(preferredModuleName);\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating Native${preferredModuleName}Spec.g.h`);\n\n // copy all explicit to a map\n const aliases: AliasMap = createAliasMap(nativeModule.aliasMap);\n\n // prepare methods\n const methods = generateValidateMethods(nativeModule, aliases, {\n cppStringType,\n });\n let tuples = `\n static constexpr auto methods = std::tuple{\n${methods[0]}\n };`;\n let checks = `\n constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;\n let errors = methods[1];\n\n // prepare constants\n const constants = generateValidateConstants(nativeModule, aliases);\n if (constants !== undefined && !methodOnly) {\n tuples = `\n static constexpr auto constants = std::tuple{\n${constants[0]}\n };${tuples}`;\n checks = `\n constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;\n errors = `${constants[1]}\n\n${errors}`;\n }\n\n // generate code for structs\n const [customTypes, customReflection] = generateAliases(aliases, {\n cppStringType,\n });\n\n const customTypesExist = customTypes !== '';\n\n const replaceContent = function (template: string): string {\n return template\n .replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)\n .replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)\n .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))\n .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))\n .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(\n /::_TYPE_DEFINITION_INCLUDE_::/g,\n customTypesExist\n ? `// #include \"Native${preferredModuleName}DataTypes.g.h\" before this file to use the generated type definition`\n : '',\n )\n .replace(/::_NAMESPACE_::/g, namespace);\n };\n\n if (separateDataTypes) {\n if (customTypesExist) {\n files.set(\n `Native${preferredModuleName}DataTypes.g.h`,\n replaceContent(typeOnlyTemplate),\n );\n }\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n replaceContent(moduleOnlyTemplate),\n );\n } else {\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n replaceContent(allInOneTemplate),\n );\n }\n }\n }\n\n return files;\n };\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import type { NativeModuleSchema, SchemaType } from '@react-native/codegen/lib/CodegenSchema';
|
|
7
|
+
export declare function setOptionalTurboModule(schema: NativeModuleSchema, optional: boolean): void;
|
|
8
|
+
export declare function getOptionalTurboModule(schema: NativeModuleSchema): boolean;
|
|
9
|
+
type FilesOutput = Map<string, string>;
|
|
10
|
+
export declare function generateTypeScript(_libraryName: string, schema: SchemaType, _moduleSpecName: string): FilesOutput;
|
|
11
|
+
export {};
|