@react-native-windows/codegen 0.68.1 → 0.69.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.
- package/CHANGELOG.md +89 -18
- package/lib-commonjs/Cli.js +41 -9
- package/lib-commonjs/Cli.js.map +1 -1
- 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/ParamTypes.js +2 -2
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/ValidateConstants.js +1 -1
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -1
- package/lib-commonjs/generators/ValidateMethods.js +2 -2
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -1
- package/package.json +10 -9
- package/src/Cli.ts +50 -9
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ParamTypes.ts +2 -2
- package/src/generators/ValidateConstants.ts +1 -1
- package/src/generators/ValidateMethods.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,45 +1,116 @@
|
|
|
1
1
|
# Change Log - @react-native-windows/codegen
|
|
2
2
|
|
|
3
|
-
This log was last generated on Mon,
|
|
3
|
+
This log was last generated on Mon, 12 Sep 2022 18:48:27 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
-
## 0.
|
|
7
|
+
## 0.69.1
|
|
8
8
|
|
|
9
|
-
Mon,
|
|
9
|
+
Mon, 12 Sep 2022 18:48:27 GMT
|
|
10
10
|
|
|
11
11
|
### Patches
|
|
12
12
|
|
|
13
|
-
- Promote 0.
|
|
14
|
-
- Bump @react-native-windows/fs to v0.
|
|
13
|
+
- Promote 0.69 to legacy (34109996+chiaramooney@users.noreply.github.com)
|
|
14
|
+
- Bump @react-native-windows/fs to v0.69.1
|
|
15
15
|
|
|
16
|
-
## 0.
|
|
16
|
+
## 0.69.0
|
|
17
17
|
|
|
18
|
-
Mon,
|
|
18
|
+
Mon, 27 Jun 2022 15:09:48 GMT
|
|
19
19
|
|
|
20
20
|
### Patches
|
|
21
21
|
|
|
22
|
-
-
|
|
23
|
-
- Bump @react-native-windows/fs to v0.
|
|
22
|
+
- Integrate 0.69.0 (34109996+chiaramooney@users.noreply.github.com)
|
|
23
|
+
- Bump @react-native-windows/fs to v0.69.0
|
|
24
24
|
|
|
25
|
-
## 0.
|
|
25
|
+
## 0.69.0-preview.1
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
Wed, 04 May 2022 23:30:23 GMT
|
|
28
28
|
|
|
29
29
|
### Changes
|
|
30
30
|
|
|
31
|
-
-
|
|
32
|
-
- Bump @react-native-windows/fs to v0.
|
|
31
|
+
- Promote 0.69 to preview (jthysell@microsoft.com)
|
|
32
|
+
- Bump @react-native-windows/fs to v0.69.0-preview.1
|
|
33
33
|
|
|
34
|
-
## 0.
|
|
34
|
+
## 0.0.0-canary.32
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
Wed, 27 Apr 2022 05:10:23 GMT
|
|
37
37
|
|
|
38
38
|
### Changes
|
|
39
39
|
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
- Bump @
|
|
40
|
+
- Merge (34109996+chiaramooney@users.noreply.github.com)
|
|
41
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.3
|
|
42
|
+
- Bump @rnw-scripts/eslint-config to v1.1.12
|
|
43
|
+
|
|
44
|
+
## 0.0.0-canary.31
|
|
45
|
+
|
|
46
|
+
Mon, 25 Apr 2022 22:50:30 GMT
|
|
47
|
+
|
|
48
|
+
### Changes
|
|
49
|
+
|
|
50
|
+
- Update globby of @react-native-windows/codegen to remove glob-parent 3.x dependency (dannyvv@microsoft.com)
|
|
51
|
+
|
|
52
|
+
## 0.0.0-canary.30
|
|
53
|
+
|
|
54
|
+
Tue, 12 Apr 2022 05:10:38 GMT
|
|
55
|
+
|
|
56
|
+
### Changes
|
|
57
|
+
|
|
58
|
+
- Fix Overrides (34109996+chiaramooney@users.noreply.github.com)
|
|
59
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.2
|
|
60
|
+
|
|
61
|
+
## 0.0.0-canary.29
|
|
62
|
+
|
|
63
|
+
Fri, 01 Apr 2022 05:09:56 GMT
|
|
64
|
+
|
|
65
|
+
### Changes
|
|
66
|
+
|
|
67
|
+
- Fix publish after promotion to preview (ngerlem@microsoft.com)
|
|
68
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.1
|
|
69
|
+
|
|
70
|
+
## 0.0.0-canary.28
|
|
71
|
+
|
|
72
|
+
Wed, 09 Mar 2022 06:09:07 GMT
|
|
73
|
+
|
|
74
|
+
### Changes
|
|
75
|
+
|
|
76
|
+
- Fix turbo module codegen to generate correct import statement and registration (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
77
|
+
|
|
78
|
+
## 0.0.0-canary.27
|
|
79
|
+
|
|
80
|
+
Fri, 04 Mar 2022 06:09:13 GMT
|
|
81
|
+
|
|
82
|
+
### Changes
|
|
83
|
+
|
|
84
|
+
- Generate fabric eventemitter.cpp (acoates@microsoft.com)
|
|
85
|
+
|
|
86
|
+
## 0.0.0-canary.26
|
|
87
|
+
|
|
88
|
+
Thu, 24 Feb 2022 06:07:47 GMT
|
|
89
|
+
|
|
90
|
+
### Changes
|
|
91
|
+
|
|
92
|
+
- Add TypeScript turbo module definition as new codegen traget (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
93
|
+
|
|
94
|
+
## 0.0.0-canary.25
|
|
95
|
+
|
|
96
|
+
Wed, 09 Feb 2022 06:09:36 GMT
|
|
97
|
+
|
|
98
|
+
### Changes
|
|
99
|
+
|
|
100
|
+
- Bump minimum Node version to 14 (jthysell@microsoft.com)
|
|
101
|
+
- Bump @react-native-windows/fs to v1.0.2
|
|
102
|
+
- Bump @rnw-scripts/eslint-config to v1.1.11
|
|
103
|
+
- Bump @rnw-scripts/jest-unittest-config to v1.2.6
|
|
104
|
+
- Bump @rnw-scripts/just-task to v2.2.3
|
|
105
|
+
- Bump @rnw-scripts/ts-config to v2.0.2
|
|
106
|
+
|
|
107
|
+
## 0.0.0-canary.24
|
|
108
|
+
|
|
109
|
+
Tue, 08 Feb 2022 18:21:23 GMT
|
|
110
|
+
|
|
111
|
+
### Changes
|
|
112
|
+
|
|
113
|
+
- Fix TurboModule codegen document (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
43
114
|
|
|
44
115
|
## 0.0.0-canary.23
|
|
45
116
|
|
package/lib-commonjs/Cli.js
CHANGED
|
@@ -14,6 +14,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
14
14
|
const fs_1 = __importDefault(require("@react-native-windows/fs"));
|
|
15
15
|
const globby_1 = __importDefault(require("globby"));
|
|
16
16
|
const GenerateNM2_1 = require("./generators/GenerateNM2");
|
|
17
|
+
const GenerateTypeScript_1 = require("./generators/GenerateTypeScript");
|
|
17
18
|
// @ts-ignore
|
|
18
19
|
const flow_1 = require("react-native-tscodegen/lib/rncodegen/src/parsers/flow");
|
|
19
20
|
// @ts-ignore
|
|
@@ -27,6 +28,16 @@ const argv = yargs_1.default.options({
|
|
|
27
28
|
type: 'array',
|
|
28
29
|
describe: 'glob patterns for files which contains specs',
|
|
29
30
|
},
|
|
31
|
+
ts: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
34
|
+
default: false,
|
|
35
|
+
},
|
|
36
|
+
outdir: {
|
|
37
|
+
type: 'string',
|
|
38
|
+
describe: 'output directory',
|
|
39
|
+
default: 'codegen',
|
|
40
|
+
},
|
|
30
41
|
test: {
|
|
31
42
|
type: 'boolean',
|
|
32
43
|
describe: 'Verify that the generated output is unchanged',
|
|
@@ -76,11 +87,9 @@ function checkFilesForChanges(map, outputDir) {
|
|
|
76
87
|
let hasChanges = false;
|
|
77
88
|
const allExistingFiles = globby_1.default
|
|
78
89
|
.sync(`${outputDir}/**`)
|
|
79
|
-
.map(
|
|
80
|
-
.sort();
|
|
81
|
-
const allGeneratedFiles = [...map.keys()]
|
|
82
|
-
.map((_) => path_1.default.normalize(_))
|
|
90
|
+
.map(_ => path_1.default.normalize(_))
|
|
83
91
|
.sort();
|
|
92
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path_1.default.normalize(_)).sort();
|
|
84
93
|
if (allExistingFiles.length !== allGeneratedFiles.length ||
|
|
85
94
|
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index]))
|
|
86
95
|
return true;
|
|
@@ -102,7 +111,7 @@ function writeMapToFiles(map, outputDir) {
|
|
|
102
111
|
let success = true;
|
|
103
112
|
// This ensures that we delete any generated files from modules that have been deleted
|
|
104
113
|
const allExistingFiles = globby_1.default.sync(`${outputDir}/**`);
|
|
105
|
-
allExistingFiles.forEach(
|
|
114
|
+
allExistingFiles.forEach(existingFile => {
|
|
106
115
|
if (!map.has(path_1.default.normalize(existingFile))) {
|
|
107
116
|
fs_1.default.unlinkSync(existingFile);
|
|
108
117
|
}
|
|
@@ -128,7 +137,25 @@ function writeMapToFiles(map, outputDir) {
|
|
|
128
137
|
}
|
|
129
138
|
function parseFlowFile(filename) {
|
|
130
139
|
try {
|
|
131
|
-
|
|
140
|
+
const schema = (0, flow_1.parseFile)(filename);
|
|
141
|
+
// there will be at most one turbo module per file
|
|
142
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
143
|
+
if (moduleName) {
|
|
144
|
+
const spec = schema.modules[moduleName];
|
|
145
|
+
if (spec.type === 'NativeModule') {
|
|
146
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
147
|
+
if (contents) {
|
|
148
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
149
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
150
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, true);
|
|
151
|
+
}
|
|
152
|
+
else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
153
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, false);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return schema;
|
|
132
159
|
}
|
|
133
160
|
catch (e) {
|
|
134
161
|
if (e instanceof Error) {
|
|
@@ -161,8 +188,12 @@ function generate({ libraryName, schema, outputDirectory, moduleSpecName }, { /*
|
|
|
161
188
|
const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;
|
|
162
189
|
const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;
|
|
163
190
|
const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;
|
|
191
|
+
const generatorEventEmitterCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;
|
|
164
192
|
normalizeFileMap(generateNM2(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
165
|
-
if (
|
|
193
|
+
if (argv.ts) {
|
|
194
|
+
normalizeFileMap((0, GenerateTypeScript_1.generateTypeScript)(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
195
|
+
}
|
|
196
|
+
if (Object.keys(schema.modules).some(moduleName => schema.modules[moduleName].type === 'Component')) {
|
|
166
197
|
const componentGenerators = [
|
|
167
198
|
generatorPropsH,
|
|
168
199
|
generatorPropsCPP,
|
|
@@ -170,8 +201,9 @@ function generate({ libraryName, schema, outputDirectory, moduleSpecName }, { /*
|
|
|
170
201
|
generatorShadowNodeCPP,
|
|
171
202
|
generatorComponentDescriptorH,
|
|
172
203
|
generatorEventEmitterH,
|
|
204
|
+
generatorEventEmitterCPP,
|
|
173
205
|
];
|
|
174
|
-
componentGenerators.forEach(
|
|
206
|
+
componentGenerators.forEach(generator => {
|
|
175
207
|
const generated = generator(libraryName, schema, moduleSpecName);
|
|
176
208
|
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
177
209
|
});
|
|
@@ -194,6 +226,6 @@ else {
|
|
|
194
226
|
}
|
|
195
227
|
const libraryName = argv.libraryName;
|
|
196
228
|
const moduleSpecName = 'moduleSpecName';
|
|
197
|
-
const outputDirectory =
|
|
229
|
+
const outputDirectory = argv.outdir;
|
|
198
230
|
generate({ libraryName, schema, outputDirectory, moduleSpecName }, { generators: [], test: false });
|
|
199
231
|
//# sourceMappingURL=Cli.js.map
|
package/lib-commonjs/Cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,gDAAwB;AACxB,kEAA0C;AAC1C,oDAA4B;AAC5B,0DAA4D;AAC5D,aAAa;AACb,gFAAgF;AAChF,aAAa;AACb,+GAAuF;AAEvF,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,OAAO;QACb,QAAQ,EAAE,8CAA8C;KACzD;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,+CAA+C;KAC1D;IACD,SAAS,EAAE;QACT,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,qDAAqD;QAC/D,OAAO,EAAE,aAAa;KACvB;IACD,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,4DAA4D;KACvE;CACF,CAAC,CAAC,IAAI,CAAC;AAgBR;;;;;;;;;;;;;;;;;;;;;;;EAuBE;AAEF,SAAS,gBAAgB,CACvB,GAAwB,EACxB,SAAiB,EACjB,MAA2B;IAE3B,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ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C9D;IAED,OAAO,eAAe,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;AAC1D,CAAC;AAED,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;IAC5D,OAAO,CAAC,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAC5D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB;AAED,IAAI,MAAkB,CAAC;AACvB,IAAI,IAAI,CAAC,IAAI,EAAE;IACb,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;CACnC;KAAM;IACL,MAAM,GAAG,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAiB,CAAC,CAAC,CAAC;CAC9D;AAED,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;AACrC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AACxC,MAAM,eAAe,GAAG,SAAS,CAAC;AAClC,QAAQ,CACN,EAAC,WAAW,EAAE,MAAM,EAAE,eAAe,EAAE,cAAc,EAAC,EACtD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAC,CAC9B,CAAC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\n// @ts-ignore\nimport {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';\n// @ts-ignore\nimport schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'array',\n describe: 'glob patterns for files which contains specs',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\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}).argv;\n\nimport {SchemaType} from 'react-native-tscodegen';\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\n/*\nconst GENERATORS = {\n descriptors: [generateComponentDescriptorH.generate],\n events: [\n generateEventEmitterCpp.generate,\n generateEventEmitterH.generate,\n generateModuleHObjCpp.generate,\n generateModuleMm.generate,\n ],\n props: [\n generateComponentHObjCpp.generate,\n generatePropsCpp.generate,\n generatePropsH.generate,\n generatePropsJavaInterface.generate,\n generatePropsJavaDelegate.generate,\n ],\n modules: [generateModuleCpp.generate, generateModuleH.generate],\n tests: [generateTests.generate],\n 'shadow-nodes': [\n generateShadowNodeCpp.generate,\n generateShadowNodeH.generate,\n ],\n};\n*/\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map((_) => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()]\n .map((_) => path.normalize(_))\n .sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.error(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach((existingFile) => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nfunction parseFlowFile(filename: string): SchemaType {\n try {\n return parseFile(filename);\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nfunction combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFlowFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nfunction generate(\n {libraryName, schema, outputDirectory, moduleSpecName}: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({namespace: argv.namespace});\n const generatorPropsH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;\n const generatorPropsCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;\n const generatorShadowNodeH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;\n const generatorShadowNodeCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;\n const generatorComponentDescriptorH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;\n const generatorEventEmitterH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;\n\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (\n Object.keys(schema.modules).some(\n (moduleName) => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorPropsH,\n generatorPropsCPP,\n generatorShadowNodeH,\n generatorShadowNodeCPP,\n generatorComponentDescriptorH,\n generatorEventEmitterH,\n ];\n\n componentGenerators.forEach((generator) => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\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\nlet schema: SchemaType;\nif (argv.file) {\n schema = parseFlowFile(argv.file);\n} else {\n schema = combineSchemas(globby.sync(argv.files as string[]));\n}\n\nconst libraryName = argv.libraryName;\nconst moduleSpecName = 'moduleSpecName';\nconst outputDirectory = 'codegen';\ngenerate(\n {libraryName, schema, outputDirectory, moduleSpecName},\n {generators: [], test: false},\n);\n"]}
|
|
1
|
+
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,gDAAwB;AACxB,kEAA0C;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Ae,GAAG,IAAI,CAAC,MAAM,CAAC;AACpC,QAAQ,CACN,EAAC,WAAW,EAAE,MAAM,EAAE,eAAe,EAAE,cAAc,EAAC,EACtD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAC,CAC9B,CAAC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\n// @ts-ignore\nimport {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';\n// @ts-ignore\nimport schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'array',\n describe: 'glob patterns for files which contains specs',\n },\n ts: {\n type: 'boolean',\n describe: 'generate turbo module definition files in TypeScript',\n default: false,\n },\n outdir: {\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 },\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}).argv;\n\nimport {SchemaType} from 'react-native-tscodegen';\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\n/*\nconst GENERATORS = {\n descriptors: [generateComponentDescriptorH.generate],\n events: [\n generateEventEmitterCpp.generate,\n generateEventEmitterH.generate,\n generateModuleHObjCpp.generate,\n generateModuleMm.generate,\n ],\n props: [\n generateComponentHObjCpp.generate,\n generatePropsCpp.generate,\n generatePropsH.generate,\n generatePropsJavaInterface.generate,\n generatePropsJavaDelegate.generate,\n ],\n modules: [generateModuleCpp.generate, generateModuleH.generate],\n tests: [generateTests.generate],\n 'shadow-nodes': [\n generateShadowNodeCpp.generate,\n generateShadowNodeH.generate,\n ],\n};\n*/\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map(_ => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.error(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach(existingFile => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nfunction parseFlowFile(filename: string): SchemaType {\n try {\n const schema = parseFile(filename);\n // there will be at most one turbo module per file\n const moduleName = Object.keys(schema.modules)[0];\n if (moduleName) {\n const spec = schema.modules[moduleName];\n if (spec.type === 'NativeModule') {\n const contents = fs.readFileSync(filename, 'utf8');\n if (contents) {\n // This is a temporary implementation until such information is added to the schema in facebook/react-native\n if (contents.includes('TurboModuleRegistry.get<')) {\n setOptionalTurboModule(spec, true);\n } else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {\n setOptionalTurboModule(spec, false);\n }\n }\n }\n }\n return schema;\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nfunction combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFlowFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nfunction generate(\n {libraryName, schema, outputDirectory, moduleSpecName}: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({namespace: argv.namespace});\n\n const generatorPropsH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;\n const generatorPropsCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;\n const generatorShadowNodeH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;\n const generatorShadowNodeCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;\n const generatorComponentDescriptorH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;\n const generatorEventEmitterH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;\n const generatorEventEmitterCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;\n\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (argv.ts) {\n normalizeFileMap(\n generateTypeScript(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n }\n\n if (\n Object.keys(schema.modules).some(\n moduleName => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorPropsH,\n generatorPropsCPP,\n generatorShadowNodeH,\n generatorShadowNodeCPP,\n generatorComponentDescriptorH,\n generatorEventEmitterH,\n generatorEventEmitterCPP,\n ];\n\n componentGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\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\nlet schema: SchemaType;\nif (argv.file) {\n schema = parseFlowFile(argv.file);\n} else {\n schema = combineSchemas(globby.sync(argv.files as string[]));\n}\n\nconst libraryName = argv.libraryName;\nconst moduleSpecName = 'moduleSpecName';\nconst outputDirectory = argv.outdir;\ngenerate(\n {libraryName, schema, outputDirectory, moduleSpecName},\n {generators: [], test: false},\n);\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModuleSchema, SchemaType } from 'react-native-tscodegen';
|
|
7
|
+
export declare function setOptionalTurboModule(schema: NativeModuleSchema, optional: boolean): void;
|
|
8
|
+
export declare function getOptionalTurboModule(schema: NativeModuleSchema): boolean;
|
|
9
|
+
declare type FilesOutput = Map<string, string>;
|
|
10
|
+
export declare function generateTypeScript(_libraryName: string, schema: SchemaType, _moduleSpecName: string): FilesOutput;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
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.generateTypeScript = exports.getOptionalTurboModule = exports.setOptionalTurboModule = void 0;
|
|
9
|
+
function setOptionalTurboModule(schema, optional) {
|
|
10
|
+
const cs = schema;
|
|
11
|
+
cs.optionalTurboModule = optional;
|
|
12
|
+
}
|
|
13
|
+
exports.setOptionalTurboModule = setOptionalTurboModule;
|
|
14
|
+
function getOptionalTurboModule(schema) {
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = schema.optionalTurboModule) !== null && _a !== void 0 ? _a : false;
|
|
17
|
+
}
|
|
18
|
+
exports.getOptionalTurboModule = getOptionalTurboModule;
|
|
19
|
+
const moduleTemplate = `
|
|
20
|
+
/*
|
|
21
|
+
* This file is auto-generated from a NativeModule spec file in js.
|
|
22
|
+
*
|
|
23
|
+
* This is a TypeScript turbo module definition file.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {TurboModule, TurboModuleRegistry} from 'react-native';
|
|
27
|
+
'use strict';
|
|
28
|
+
::_MODULE_ALIASED_STRUCTS_::
|
|
29
|
+
export interface Spec extends TurboModule {
|
|
30
|
+
::_MODULE_MEMBERS_::
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');
|
|
34
|
+
`;
|
|
35
|
+
function optionalSign(obj) {
|
|
36
|
+
return obj.optional ? '?' : '';
|
|
37
|
+
}
|
|
38
|
+
function translateType(type) {
|
|
39
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
40
|
+
const returnType = type.type;
|
|
41
|
+
switch (type.type) {
|
|
42
|
+
case 'StringTypeAnnotation':
|
|
43
|
+
return 'string';
|
|
44
|
+
case 'NumberTypeAnnotation':
|
|
45
|
+
case 'FloatTypeAnnotation':
|
|
46
|
+
case 'DoubleTypeAnnotation':
|
|
47
|
+
case 'Int32TypeAnnotation':
|
|
48
|
+
return 'number';
|
|
49
|
+
case 'BooleanTypeAnnotation':
|
|
50
|
+
return 'boolean';
|
|
51
|
+
case 'ArrayTypeAnnotation':
|
|
52
|
+
if (type.elementType) {
|
|
53
|
+
return `${translateType(type.elementType)}[]`;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return `Array`;
|
|
57
|
+
}
|
|
58
|
+
case 'GenericObjectTypeAnnotation':
|
|
59
|
+
return 'object';
|
|
60
|
+
case 'ObjectTypeAnnotation':
|
|
61
|
+
return `{${type.properties
|
|
62
|
+
.map((prop) => {
|
|
63
|
+
return `${prop.name}${optionalSign(prop)}: ${translateType(prop.typeAnnotation)}`;
|
|
64
|
+
})
|
|
65
|
+
.join(', ')}}`;
|
|
66
|
+
case 'ReservedTypeAnnotation': {
|
|
67
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
68
|
+
const name = type.name;
|
|
69
|
+
// (#6597)
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
71
|
+
if (name !== 'RootTag')
|
|
72
|
+
throw new Error(`Unknown reserved function: ${name} in translateReturnType`);
|
|
73
|
+
return 'number';
|
|
74
|
+
}
|
|
75
|
+
case 'TypeAliasTypeAnnotation':
|
|
76
|
+
return type.name;
|
|
77
|
+
case 'NullableTypeAnnotation':
|
|
78
|
+
return `(${translateType(type.typeAnnotation)} | null | undefined)`;
|
|
79
|
+
case 'VoidTypeAnnotation':
|
|
80
|
+
return `void`;
|
|
81
|
+
case 'PromiseTypeAnnotation':
|
|
82
|
+
return `Promise`;
|
|
83
|
+
case `FunctionTypeAnnotation`:
|
|
84
|
+
return `((${type.params
|
|
85
|
+
.map((param) => {
|
|
86
|
+
return `${param.name}${optionalSign(param)}: ${translateType(param.typeAnnotation)}`;
|
|
87
|
+
})
|
|
88
|
+
.join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;
|
|
89
|
+
default:
|
|
90
|
+
throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function translateAlias(name, type) {
|
|
94
|
+
return `
|
|
95
|
+
export interface ${name} {
|
|
96
|
+
${type.properties
|
|
97
|
+
.map((prop) => {
|
|
98
|
+
return ` ${prop.name}${optionalSign(prop)}: ${translateType(prop.typeAnnotation)};`;
|
|
99
|
+
})
|
|
100
|
+
.join('\n')}
|
|
101
|
+
}
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
function tryGetConstantType(nativeModule) {
|
|
105
|
+
const candidates = nativeModule.spec.properties.filter(prop => prop.name === 'getConstants');
|
|
106
|
+
if (candidates.length === 0) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const getConstant = candidates[0];
|
|
110
|
+
const funcType = getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
111
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
112
|
+
: getConstant.typeAnnotation;
|
|
113
|
+
if (funcType.params.length > 0 ||
|
|
114
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation') {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
118
|
+
if (constantType.properties.length === 0) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return constantType;
|
|
122
|
+
}
|
|
123
|
+
function translateMethod(func) {
|
|
124
|
+
const funcType = func.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
125
|
+
? func.typeAnnotation.typeAnnotation
|
|
126
|
+
: func.typeAnnotation;
|
|
127
|
+
return `
|
|
128
|
+
${func.name}(${funcType.params
|
|
129
|
+
.map((param) => {
|
|
130
|
+
return `${param.name}${optionalSign(param)}: ${translateType(param.typeAnnotation)}`;
|
|
131
|
+
})
|
|
132
|
+
.join(', ')})${optionalSign(func)}: ${translateType(funcType.returnTypeAnnotation)}${funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'}`;
|
|
133
|
+
}
|
|
134
|
+
function generateTypeScript(_libraryName, schema, _moduleSpecName) {
|
|
135
|
+
const files = new Map();
|
|
136
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
137
|
+
const nativeModule = schema.modules[moduleName];
|
|
138
|
+
// from 0.65 facebook's react-native-codegen
|
|
139
|
+
// the module name has the Native prefix comparing to 0.63
|
|
140
|
+
// when reading files we provided
|
|
141
|
+
const nativePrefix = 'Native';
|
|
142
|
+
const preferredModuleName = moduleName.startsWith(nativePrefix)
|
|
143
|
+
? moduleName.substr(nativePrefix.length)
|
|
144
|
+
: moduleName;
|
|
145
|
+
if (nativeModule.type === 'NativeModule') {
|
|
146
|
+
console.log(`Generating ${preferredModuleName}Spec.g.ts`);
|
|
147
|
+
const aliasCode = Object.keys(nativeModule.aliases)
|
|
148
|
+
.map(name => translateAlias(name, nativeModule.aliases[name]))
|
|
149
|
+
.join('');
|
|
150
|
+
const constantType = tryGetConstantType(nativeModule);
|
|
151
|
+
const constantCode = constantType === undefined
|
|
152
|
+
? ''
|
|
153
|
+
: ` getConstants(): ${translateType(constantType)}`;
|
|
154
|
+
const methods = nativeModule.spec.properties.filter(prop => prop.name !== 'getConstants');
|
|
155
|
+
const membersCode = methods.map(translateMethod).join('');
|
|
156
|
+
files.set(`${preferredModuleName}Spec.g.ts`, moduleTemplate
|
|
157
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)
|
|
158
|
+
.replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)
|
|
159
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
160
|
+
.replace(/::_MODULE_GETTER_::/g, getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing'));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return files;
|
|
164
|
+
}
|
|
165
|
+
exports.generateTypeScript = generateTypeScript;
|
|
166
|
+
//# sourceMappingURL=GenerateTypeScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenerateTypeScript.js","sourceRoot":"","sources":["../../src/generators/GenerateTypeScript.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAkBb,SAAgB,sBAAsB,CACpC,MAA0B,EAC1B,QAAiB;IAEjB,MAAM,EAAE,GAA8B,MAAM,CAAC;IAC7C,EAAE,CAAC,mBAAmB,GAAG,QAAQ,CAAC;AACpC,CAAC;AAND,wDAMC;AAED,SAAgB,sBAAsB,CAAC,MAA0B;;IAC/D,OAAO,MAA4B,MAAO,CAAC,mBAAmB,mCAAI,KAAK,CAAC;AAC1E,CAAC;AAFD,wDAEC;AAOD,MAAM,cAAc,GAAG;;;;;;;;;;;;;;;CAetB,CAAC;AAEF,SAAS,YAAY,CAAI,GAAkB;IACzC,OAAO,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;AACjC,CAAC;AAED,SAAS,aAAa,CACpB,IAIC;IAED,wDAAwD;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;IAC7B,QAAQ,IAAI,CAAC,IAAI,EAAE;QACjB,KAAK,sBAAsB;YACzB,OAAO,QAAQ,CAAC;QAClB,KAAK,sBAAsB,CAAC;QAC5B,KAAK,qBAAqB,CAAC;Q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CAAC;AACf,CAAC;AAlDD,gDAkDC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {\n NamedShape,\n NativeModuleBaseTypeAnnotation,\n NativeModuleFunctionTypeAnnotation,\n NativeModuleObjectTypeAnnotation,\n NativeModuleParamTypeAnnotation,\n NativeModuleReturnTypeAnnotation,\n NativeModuleSchema,\n Nullable,\n SchemaType,\n} from 'react-native-tscodegen';\n\ninterface CodegenNativeModuleSchema extends NativeModuleSchema {\n optionalTurboModule?: boolean;\n}\n\nexport function setOptionalTurboModule(\n schema: NativeModuleSchema,\n optional: boolean,\n): void {\n const cs = <CodegenNativeModuleSchema>schema;\n cs.optionalTurboModule = optional;\n}\n\nexport function getOptionalTurboModule(schema: NativeModuleSchema): boolean {\n return (<CodegenNativeModuleSchema>schema).optionalTurboModule ?? false;\n}\n\ntype ObjectProp = NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>;\ntype FunctionParam = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;\ntype FunctionDecl = NamedShape<Nullable<NativeModuleFunctionTypeAnnotation>>;\ntype FilesOutput = Map<string, string>;\n\nconst moduleTemplate = `\n/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a TypeScript turbo module definition file.\n */\n\nimport {TurboModule, TurboModuleRegistry} from 'react-native';\n'use strict';\n::_MODULE_ALIASED_STRUCTS_::\nexport interface Spec extends TurboModule {\n::_MODULE_MEMBERS_::\n}\n\nexport default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');\n`;\n\nfunction optionalSign<T>(obj: NamedShape<T>): string {\n return obj.optional ? '?' : '';\n}\n\nfunction translateType(\n type: Nullable<\n | NativeModuleBaseTypeAnnotation\n | NativeModuleParamTypeAnnotation\n | NativeModuleReturnTypeAnnotation\n >,\n): string {\n // avoid: Property 'type' does not exist on type 'never'\n const returnType = type.type;\n switch (type.type) {\n case 'StringTypeAnnotation':\n return 'string';\n case 'NumberTypeAnnotation':\n case 'FloatTypeAnnotation':\n case 'DoubleTypeAnnotation':\n case 'Int32TypeAnnotation':\n return 'number';\n case 'BooleanTypeAnnotation':\n return 'boolean';\n case 'ArrayTypeAnnotation':\n if (type.elementType) {\n return `${translateType(type.elementType)}[]`;\n } else {\n return `Array`;\n }\n case 'GenericObjectTypeAnnotation':\n return 'object';\n case 'ObjectTypeAnnotation':\n return `{${type.properties\n .map((prop: ObjectProp) => {\n return `${prop.name}${optionalSign(prop)}: ${translateType(\n prop.typeAnnotation,\n )}`;\n })\n .join(', ')}}`;\n case 'ReservedTypeAnnotation': {\n // avoid: Property 'name' does not exist on type 'never'\n const name = type.name;\n // (#6597)\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (name !== 'RootTag')\n throw new Error(\n `Unknown reserved function: ${name} in translateReturnType`,\n );\n return 'number';\n }\n case 'TypeAliasTypeAnnotation':\n return type.name;\n case 'NullableTypeAnnotation':\n return `(${translateType(type.typeAnnotation)} | null | undefined)`;\n case 'VoidTypeAnnotation':\n return `void`;\n case 'PromiseTypeAnnotation':\n return `Promise`;\n case `FunctionTypeAnnotation`:\n return `((${type.params\n .map((param: FunctionParam) => {\n return `${param.name}${optionalSign(param)}: ${translateType(\n param.typeAnnotation,\n )}`;\n })\n .join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;\n default:\n throw new Error(`Unhandled type in translateReturnType: ${returnType}`);\n }\n}\n\nfunction translateAlias(\n name: string,\n type: NativeModuleObjectTypeAnnotation,\n): string {\n return `\nexport interface ${name} {\n${type.properties\n .map((prop: ObjectProp) => {\n return ` ${prop.name}${optionalSign(prop)}: ${translateType(\n prop.typeAnnotation,\n )};`;\n })\n .join('\\n')}\n}\n`;\n}\n\nfunction tryGetConstantType(\n nativeModule: NativeModuleSchema,\n): NativeModuleObjectTypeAnnotation | undefined {\n const candidates = nativeModule.spec.properties.filter(\n prop => prop.name === 'getConstants',\n );\n if (candidates.length === 0) {\n return undefined;\n }\n\n const getConstant = candidates[0];\n const funcType =\n getConstant.typeAnnotation.type === 'NullableTypeAnnotation'\n ? getConstant.typeAnnotation.typeAnnotation\n : getConstant.typeAnnotation;\n if (\n funcType.params.length > 0 ||\n funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'\n ) {\n return undefined;\n }\n\n const constantType = funcType.returnTypeAnnotation;\n if (constantType.properties.length === 0) {\n return undefined;\n }\n\n return constantType;\n}\n\nfunction translateMethod(func: FunctionDecl): string {\n const funcType =\n func.typeAnnotation.type === 'NullableTypeAnnotation'\n ? func.typeAnnotation.typeAnnotation\n : func.typeAnnotation;\n\n return `\n ${func.name}(${funcType.params\n .map((param: FunctionParam) => {\n return `${param.name}${optionalSign(param)}: ${translateType(\n param.typeAnnotation,\n )}`;\n })\n .join(', ')})${optionalSign(func)}: ${translateType(\n funcType.returnTypeAnnotation,\n )}${\n funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'\n }`;\n}\n\nexport function generateTypeScript(\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 nativePrefix = 'Native';\n const preferredModuleName = moduleName.startsWith(nativePrefix)\n ? moduleName.substr(nativePrefix.length)\n : moduleName;\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating ${preferredModuleName}Spec.g.ts`);\n\n const aliasCode = Object.keys(nativeModule.aliases)\n .map(name => translateAlias(name, nativeModule.aliases[name]))\n .join('');\n\n const constantType = tryGetConstantType(nativeModule);\n const constantCode =\n constantType === undefined\n ? ''\n : ` getConstants(): ${translateType(constantType)}`;\n\n const methods = nativeModule.spec.properties.filter(\n prop => prop.name !== 'getConstants',\n );\n const membersCode = methods.map(translateMethod).join('');\n\n files.set(\n `${preferredModuleName}Spec.g.ts`,\n moduleTemplate\n .replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)\n .replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(\n /::_MODULE_GETTER_::/g,\n getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing',\n ),\n );\n }\n }\n\n return files;\n}\n"]}
|
|
@@ -98,14 +98,14 @@ function translateFunctionParam(param, aliases, baseAliasName) {
|
|
|
98
98
|
return translateNullableParamType(param.typeAnnotation, aliases, baseAliasName, 'template', 'method-arg');
|
|
99
99
|
}
|
|
100
100
|
function translateSpecArgs(params, aliases, baseAliasName) {
|
|
101
|
-
return params.map(
|
|
101
|
+
return params.map(param => {
|
|
102
102
|
const translatedParam = translateSpecFunctionParam(param, aliases, `${baseAliasName}_${param.name}`);
|
|
103
103
|
return `${translatedParam}`;
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
106
|
exports.translateSpecArgs = translateSpecArgs;
|
|
107
107
|
function translateArgs(params, aliases, baseAliasName) {
|
|
108
|
-
return params.map(
|
|
108
|
+
return params.map(param => {
|
|
109
109
|
const translatedParam = translateFunctionParam(param, aliases, `${baseAliasName}_${param.name}`);
|
|
110
110
|
return `${translatedParam} ${param.name}`;
|
|
111
111
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ParamTypes.js","sourceRoot":"","sources":["../../src/generators/ParamTypes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAOb,mDAIyB;AAQzB,SAAS,YAAY,CAAC,IAAY,EAAE,MAAmB;IACrD,QAAQ,MAAM,EAAE;QACd,KAAK,YAAY;YACf,OAAO,GAAG,IAAI,KAAK,CAAC;QACtB,KAAK,cAAc;YACjB,OAAO,GAAG,IAAI,UAAU,CAAC;QAC3B;YACE,OAAO,IAAI,CAAC;KACf;AACH,CAAC;AAED,SAAS,cAAc,CACrB,KAAsC,EACtC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SAAS,CAAC,cAAc,EACxB,OAAO,EACP,aAAa,EACb,cAAc,CACf,GAAG,CAAC;QACP;YACE,OAAO,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;KACpE;AACH,CAAC;AAED,SAAS,0BAA0B,CACjC,KAA6B,EAC7B,OAAiB,EACjB,aAAqB;IAErB,OAAO,0BAA0B,CAC/B,KAAK,CAAC,cAAc,EACpB,OAAO,EACP,aAAa,EACb,MAAM,EACN,MAAM,CACP,CAAC;AACJ,CAAC;AAED,SAAS,sBAAsB,CAC7B,KAA6B,EAC7B,OAAiB,EACjB,aAAqB;IAErB,OAAO,0BAA0B,CAC/B,KAAK,CAAC,cAAc,EACpB,OAAO,EACP,aAAa,EACb,UAAU,EACV,cAAc,CACf,CAAC;AACJ,CAAC;AAED,SAAS,sBAAsB,CAC7B,KAA6B,EAC7B,OAAiB,EACjB,aAAqB;IAErB,OAAO,0BAA0B,CAC/B,KAAK,CAAC,cAAc,EACpB,OAAO,EACP,aAAa,EACb,UAAU,EACV,YAAY,CACb,CAAC;AACJ,CAAC;AAED,SAAgB,iBAAiB,CAC/B,MAA6C,EAC7C,OAAiB,EACjB,aAAqB;IAErB,OAAO,MAAM,CAAC,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"ParamTypes.js","sourceRoot":"","sources":["../../src/generators/ParamTypes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAOb,mDAIyB;AAQzB,SAAS,YAAY,CAAC,IAAY,EAAE,MAAmB;IACrD,QAAQ,MAAM,EAAE;QACd,KAAK,YAAY;YACf,OAAO,GAAG,IAAI,KAAK,CAAC;QACtB,KAAK,cAAc;YACjB,OAAO,GAAG,IAAI,UAAU,CAAC;QAC3B;YACE,OAAO,IAAI,CAAC;KACf;AACH,CAAC;AAED,SAAS,cAAc,CACrB,KAAsC,EACtC,OAAiB,EACjB,aAAqB,EACrB,MAAmB;IAEnB,wDAAwD;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AAE,CAAC;IAC5C,CAAC,CAAC,CAAC;AACL,CAAC;AAbD,sCAaC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {\n NamedShape,\n NativeModuleParamTypeAnnotation,\n Nullable,\n} from 'react-native-tscodegen';\nimport {\n AliasMap,\n getAliasCppName,\n getAnonymousAliasCppName,\n} from './AliasManaging';\n\ntype NativeModuleParamShape = NamedShape<\n Nullable<NativeModuleParamTypeAnnotation>\n>;\n\ntype ParamTarget = 'spec' | 'template' | 'callback-arg' | 'method-arg';\n\nfunction decorateType(type: string, target: ParamTarget): string {\n switch (target) {\n case 'method-arg':\n return `${type} &&`;\n case 'callback-arg':\n return `${type} const &`;\n default:\n return type;\n }\n}\n\nfunction translateParam(\n param: NativeModuleParamTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n target: ParamTarget,\n): string {\n // avoid: Property 'type' does not exist on type 'never'\n const paramType = param.type;\n switch (param.type) {\n case 'StringTypeAnnotation':\n return 'std::string';\n case 'NumberTypeAnnotation':\n case 'FloatTypeAnnotation':\n case 'DoubleTypeAnnotation':\n return 'double';\n case 'Int32TypeAnnotation':\n return 'int';\n case 'BooleanTypeAnnotation':\n return 'bool';\n case 'FunctionTypeAnnotation': {\n // TODO: type.returnTypeAnnotation\n switch (target) {\n case 'spec':\n return `Callback<${param.params\n .map((p: NativeModuleParamShape) =>\n translateSpecFunctionParam(\n p,\n aliases,\n `${baseAliasName}_${p.name}`,\n ),\n )\n .join(', ')}>`;\n case 'template':\n return `std::function<void(${param.params\n .map((p: NativeModuleParamShape) =>\n translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),\n )\n .join(', ')})>`;\n default:\n return `std::function<void(${param.params\n .map((p: NativeModuleParamShape) =>\n translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),\n )\n .join(', ')})> const &`;\n }\n }\n case 'ArrayTypeAnnotation':\n if (param.elementType) {\n switch (target) {\n case 'spec':\n case 'template':\n return `std::vector<${translateNullableParamType(\n param.elementType,\n aliases,\n `${baseAliasName}_element`,\n 'template',\n 'template',\n )}>`;\n default:\n return `std::vector<${translateNullableParamType(\n param.elementType,\n aliases,\n `${baseAliasName}_element`,\n 'template',\n 'template',\n )}> const &`;\n }\n } else {\n return decorateType('::React::JSValueArray', target);\n }\n case 'GenericObjectTypeAnnotation':\n return decorateType('::React::JSValue', target);\n case 'ObjectTypeAnnotation':\n return decorateType(\n getAnonymousAliasCppName(aliases, baseAliasName, param),\n target,\n );\n case 'ReservedTypeAnnotation': {\n // avoid: Property 'name' does not exist on type 'never'\n const name = param.name;\n // (#6597)\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (name !== 'RootTag')\n throw new Error(`Unknown reserved function: ${name} in translateParam`);\n return 'double';\n }\n case 'TypeAliasTypeAnnotation':\n return decorateType(getAliasCppName(param.name), target);\n default:\n throw new Error(`Unhandled type in translateParam: ${paramType}`);\n }\n}\n\nfunction translateNullableParamType(\n paramType: Nullable<NativeModuleParamTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n nullableTarget: ParamTarget,\n target: ParamTarget,\n): string {\n switch (paramType.type) {\n case 'NullableTypeAnnotation':\n return `std::optional<${translateParam(\n paramType.typeAnnotation,\n aliases,\n baseAliasName,\n nullableTarget,\n )}>`;\n default:\n return translateParam(paramType, aliases, baseAliasName, target);\n }\n}\n\nfunction translateSpecFunctionParam(\n param: NativeModuleParamShape,\n aliases: AliasMap,\n baseAliasName: string,\n): string {\n return translateNullableParamType(\n param.typeAnnotation,\n aliases,\n baseAliasName,\n 'spec',\n 'spec',\n );\n}\n\nfunction translateCallbackParam(\n param: NativeModuleParamShape,\n aliases: AliasMap,\n baseAliasName: string,\n): string {\n return translateNullableParamType(\n param.typeAnnotation,\n aliases,\n baseAliasName,\n 'template',\n 'callback-arg',\n );\n}\n\nfunction translateFunctionParam(\n param: NativeModuleParamShape,\n aliases: AliasMap,\n baseAliasName: string,\n): string {\n return translateNullableParamType(\n param.typeAnnotation,\n aliases,\n baseAliasName,\n 'template',\n 'method-arg',\n );\n}\n\nexport function translateSpecArgs(\n params: ReadonlyArray<NativeModuleParamShape>,\n aliases: AliasMap,\n baseAliasName: string,\n) {\n return params.map(param => {\n const translatedParam = translateSpecFunctionParam(\n param,\n aliases,\n `${baseAliasName}_${param.name}`,\n );\n return `${translatedParam}`;\n });\n}\n\nexport function translateArgs(\n params: ReadonlyArray<NativeModuleParamShape>,\n aliases: AliasMap,\n baseAliasName: string,\n) {\n return params.map(param => {\n const translatedParam = translateFunctionParam(\n param,\n aliases,\n `${baseAliasName}_${param.name}`,\n );\n return `${translatedParam} ${param.name}`;\n });\n}\n"]}
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.generateValidateConstants = void 0;
|
|
9
9
|
const AliasManaging_1 = require("./AliasManaging");
|
|
10
10
|
function generateValidateConstants(nativeModule, aliases) {
|
|
11
|
-
const candidates = nativeModule.spec.properties.filter(
|
|
11
|
+
const candidates = nativeModule.spec.properties.filter(prop => prop.name === 'getConstants');
|
|
12
12
|
if (candidates.length === 0) {
|
|
13
13
|
return undefined;
|
|
14
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidateConstants.js","sourceRoot":"","sources":["../../src/generators/ValidateConstants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAmE;AAEnE,SAAgB,yBAAyB,CACvC,YAAgC,EAChC,OAAiB;IAEjB,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CACpD,
|
|
1
|
+
{"version":3,"file":"ValidateConstants.js","sourceRoot":"","sources":["../../src/generators/ValidateConstants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAmE;AAEnE,SAAgB,yBAAyB,CACvC,YAAgC,EAChC,OAAiB;IAEjB,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CACpD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CACrC,CAAC;IACF,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QAC3B,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,QAAQ,GACZ,WAAW,CAAC,cAAc,CAAC,IAAI,KAAK,wBAAwB;QAC1D,CAAC,CAAC,WAAW,CAAC,cAAc,CAAC,cAAc;QAC3C,CAAC,CAAC,WAAW,CAAC,cAAc,CAAC;IACjC,IACE,QAAQ,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC;QAC1B,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,sBAAsB,EAC7D;QACA,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,YAAY,GAAG,QAAQ,CAAC,oBAAoB,CAAC;IACnD,IAAI,YAAY,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QACxC,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,OAAO,GAAG,IAAA,wCAAwB,EAAC,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IAE7E,OAAO;QACL,uBAAuB,OAAO,OAAO;QACrC;;aAES,OAAO;mDAC+B,OAAO;0DACA,OAAO,qDAAqD;KACnH,CAAC;AACJ,CAAC;AAtCD,8DAsCC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {NativeModuleSchema} from 'react-native-tscodegen';\nimport {AliasMap, getAnonymousAliasCppName} from './AliasManaging';\n\nexport function generateValidateConstants(\n nativeModule: NativeModuleSchema,\n aliases: AliasMap,\n): [string, string] | undefined {\n const candidates = nativeModule.spec.properties.filter(\n prop => prop.name === 'getConstants',\n );\n if (candidates.length === 0) {\n return undefined;\n }\n\n const getConstant = candidates[0];\n const funcType =\n getConstant.typeAnnotation.type === 'NullableTypeAnnotation'\n ? getConstant.typeAnnotation.typeAnnotation\n : getConstant.typeAnnotation;\n if (\n funcType.params.length > 0 ||\n funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'\n ) {\n return undefined;\n }\n\n const constantType = funcType.returnTypeAnnotation;\n if (constantType.properties.length === 0) {\n return undefined;\n }\n\n const cppName = getAnonymousAliasCppName(aliases, 'Constants', constantType);\n\n return [\n ` TypedConstant<${cppName}>{0},`,\n ` REACT_SHOW_CONSTANT_SPEC_ERRORS(\n 0,\n \"${cppName}\",\n \" REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\\\n\"\n \" REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\\\n\");`,\n ];\n}\n"]}
|
|
@@ -28,13 +28,13 @@ function getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
|
28
28
|
}
|
|
29
29
|
function translatePossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
30
30
|
return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
|
|
31
|
-
.map(
|
|
31
|
+
.map(sig => `" ${sig}\\n"`)
|
|
32
32
|
.join('\n ');
|
|
33
33
|
}
|
|
34
34
|
function renderProperties(properties, aliases, tuple) {
|
|
35
35
|
// TODO: generate code for constants
|
|
36
36
|
return properties
|
|
37
|
-
.filter(
|
|
37
|
+
.filter(prop => prop.name !== 'getConstants')
|
|
38
38
|
.map((prop, index) => {
|
|
39
39
|
// TODO: prop.optional === true
|
|
40
40
|
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidateMethods.js","sourceRoot":"","sources":["../../src/generators/ValidateMethods.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAQb,6CAA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B,OAAO,EACP,aAAa,CACd,CAAC;QAEF,IAAI,qBAAqB,CAAC,QAAQ,CAAC,EAAE;YACnC,4DAA4D;YAC5D,aAAa,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;SACjD;QAED,IAAI,KAAK,EAAE;YACT,OAAO,SACL,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EACpC,UAAU,qBAAqB,IAAI,aAAa,CAAC,IAAI,CACnD,IAAI,CACL,eAAe,KAAK,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC;SAC5C;aAAM;YACL,OAAO;YACH,KAAK;aACJ,IAAI,CAAC,IAAI;YACV,iCAAiC,CACjC,IAAI,EACJ,QAAQ,EACR,OAAO,EACP,aAAa,CACd,IAAI,CAAC;SACT;IACH,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAgB,uBAAuB,CACrC,YAAgC,EAChC,OAAiB;IAEjB,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC;IAChD,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,UAAU,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACzE,MAAM,uBAAuB,GAAG,gBAAgB,CAAC,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,CAAC,uBAAuB,EAAE,mBAAmB,CAAC,CAAC;AACxD,CAAC;AARD,0DAQC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {\n NativeModuleFunctionTypeAnnotation,\n NativeModulePropertyShape,\n NativeModuleSchema,\n} from 'react-native-tscodegen';\nimport {AliasMap} from './AliasManaging';\nimport {translateArgs, translateSpecArgs} from './ParamTypes';\nimport {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';\n\nfunction isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {\n return (\n funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&\n funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation'\n );\n}\n\nfunction isMethodReturnPromise(funcType: NativeModuleFunctionTypeAnnotation) {\n return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';\n}\n\nfunction getPossibleMethodSignatures(\n prop: NativeModulePropertyShape,\n funcType: NativeModuleFunctionTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n): string[] {\n const args = translateArgs(funcType.params, aliases, baseAliasName);\n if (isMethodReturnPromise(funcType)) {\n // TODO: type of the promise could be provided in the future\n args.push('::React::ReactPromise<::React::JSValue> &&result');\n }\n\n // TODO: be much more exhastive on the possible method signatures that can be used..\n const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${\n prop.name\n }) ${translateImplReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n baseAliasName,\n )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;\n\n const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${\n prop.name\n }) static ${translateImplReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n baseAliasName,\n )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;\n\n return [sig, staticsig];\n}\n\nfunction translatePossibleMethodSignatures(\n prop: NativeModulePropertyShape,\n funcType: NativeModuleFunctionTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n): string {\n return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)\n .map(sig => `\" ${sig}\\\\n\"`)\n .join('\\n ');\n}\n\nfunction renderProperties(\n properties: ReadonlyArray<NativeModulePropertyShape>,\n aliases: AliasMap,\n tuple: boolean,\n): string {\n // TODO: generate code for constants\n return properties\n .filter(prop => prop.name !== 'getConstants')\n .map((prop, index) => {\n // TODO: prop.optional === true\n // TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'\n const propAliasName = prop.name;\n const funcType =\n prop.typeAnnotation.type === 'NullableTypeAnnotation'\n ? prop.typeAnnotation.typeAnnotation\n : prop.typeAnnotation;\n\n const traversedArgs = translateSpecArgs(\n funcType.params,\n aliases,\n propAliasName,\n );\n\n const translatedReturnParam = translateSpecReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n propAliasName,\n );\n\n if (isMethodReturnPromise(funcType)) {\n // TODO: type of the promise could be provided in the future\n traversedArgs.push('Promise<::React::JSValue>');\n }\n\n if (tuple) {\n return ` ${\n isMethodSync(funcType) ? 'Sync' : ''\n }Method<${translatedReturnParam}(${traversedArgs.join(\n ', ',\n )}) noexcept>{${index}, L\"${prop.name}\"},`;\n } else {\n return ` REACT_SHOW_METHOD_SPEC_ERRORS(\n ${index},\n \"${prop.name}\",\n ${translatePossibleMethodSignatures(\n prop,\n funcType,\n aliases,\n propAliasName,\n )});`;\n }\n })\n .join('\\n');\n}\n\nexport function generateValidateMethods(\n nativeModule: NativeModuleSchema,\n aliases: AliasMap,\n): [string, string] {\n const properties = nativeModule.spec.properties;\n const traversedProperties = renderProperties(properties, aliases, false);\n const traversedPropertyTuples = renderProperties(properties, aliases, true);\n return [traversedPropertyTuples, traversedProperties];\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-windows/codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.69.1",
|
|
4
4
|
"description": "Generators for react-native-codegen targeting react-native-windows",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": "https://github.com/microsoft/react-native-windows",
|
|
@@ -18,21 +18,22 @@
|
|
|
18
18
|
"react-native-windows-codegen": "./bin.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@react-native-windows/fs": "0.
|
|
21
|
+
"@react-native-windows/fs": "0.69.1",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^5.20.0",
|
|
23
|
+
"@typescript-eslint/parser": "^5.20.0",
|
|
22
24
|
"chalk": "^4.1.0",
|
|
23
|
-
"globby": "^
|
|
25
|
+
"globby": "^11.0.4",
|
|
24
26
|
"mustache": "^4.0.1",
|
|
25
27
|
"react-native-tscodegen": "0.68.4",
|
|
26
28
|
"source-map-support": "^0.5.19",
|
|
27
29
|
"yargs": "^16.2.0"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"@rnw-scripts/eslint-config": "1.1.
|
|
31
|
-
"@rnw-scripts/jest-unittest-config": "1.2.
|
|
32
|
-
"@rnw-scripts/just-task": "2.2.
|
|
33
|
-
"@rnw-scripts/ts-config": "2.0.
|
|
32
|
+
"@rnw-scripts/eslint-config": "1.1.12",
|
|
33
|
+
"@rnw-scripts/jest-unittest-config": "1.2.6",
|
|
34
|
+
"@rnw-scripts/just-task": "2.2.3",
|
|
35
|
+
"@rnw-scripts/ts-config": "2.0.2",
|
|
34
36
|
"@types/chalk": "^2.2.0",
|
|
35
|
-
"@types/globby": "^9.1.0",
|
|
36
37
|
"@types/jest": "^26.0.20",
|
|
37
38
|
"@types/node": "^14.14.22",
|
|
38
39
|
"@types/yargs": "16.0.0",
|
|
@@ -51,7 +52,7 @@
|
|
|
51
52
|
"src"
|
|
52
53
|
],
|
|
53
54
|
"beachball": {
|
|
54
|
-
"defaultNpmTag": "v0.
|
|
55
|
+
"defaultNpmTag": "v0.69-stable",
|
|
55
56
|
"disallowedChangeTypes": [
|
|
56
57
|
"major",
|
|
57
58
|
"minor",
|
package/src/Cli.ts
CHANGED
|
@@ -10,6 +10,10 @@ import path from 'path';
|
|
|
10
10
|
import fs from '@react-native-windows/fs';
|
|
11
11
|
import globby from 'globby';
|
|
12
12
|
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
|
+
import {
|
|
14
|
+
generateTypeScript,
|
|
15
|
+
setOptionalTurboModule,
|
|
16
|
+
} from './generators/GenerateTypeScript';
|
|
13
17
|
// @ts-ignore
|
|
14
18
|
import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
|
|
15
19
|
// @ts-ignore
|
|
@@ -24,6 +28,16 @@ const argv = yargs.options({
|
|
|
24
28
|
type: 'array',
|
|
25
29
|
describe: 'glob patterns for files which contains specs',
|
|
26
30
|
},
|
|
31
|
+
ts: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
34
|
+
default: false,
|
|
35
|
+
},
|
|
36
|
+
outdir: {
|
|
37
|
+
type: 'string',
|
|
38
|
+
describe: 'output directory',
|
|
39
|
+
default: 'codegen',
|
|
40
|
+
},
|
|
27
41
|
test: {
|
|
28
42
|
type: 'boolean',
|
|
29
43
|
describe: 'Verify that the generated output is unchanged',
|
|
@@ -98,11 +112,9 @@ function checkFilesForChanges(
|
|
|
98
112
|
|
|
99
113
|
const allExistingFiles = globby
|
|
100
114
|
.sync(`${outputDir}/**`)
|
|
101
|
-
.map(
|
|
102
|
-
.sort();
|
|
103
|
-
const allGeneratedFiles = [...map.keys()]
|
|
104
|
-
.map((_) => path.normalize(_))
|
|
115
|
+
.map(_ => path.normalize(_))
|
|
105
116
|
.sort();
|
|
117
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
106
118
|
|
|
107
119
|
if (
|
|
108
120
|
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
@@ -132,7 +144,7 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
|
132
144
|
|
|
133
145
|
// This ensures that we delete any generated files from modules that have been deleted
|
|
134
146
|
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
135
|
-
allExistingFiles.forEach(
|
|
147
|
+
allExistingFiles.forEach(existingFile => {
|
|
136
148
|
if (!map.has(path.normalize(existingFile))) {
|
|
137
149
|
fs.unlinkSync(existingFile);
|
|
138
150
|
}
|
|
@@ -162,7 +174,24 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
|
162
174
|
|
|
163
175
|
function parseFlowFile(filename: string): SchemaType {
|
|
164
176
|
try {
|
|
165
|
-
|
|
177
|
+
const schema = parseFile(filename);
|
|
178
|
+
// there will be at most one turbo module per file
|
|
179
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
180
|
+
if (moduleName) {
|
|
181
|
+
const spec = schema.modules[moduleName];
|
|
182
|
+
if (spec.type === 'NativeModule') {
|
|
183
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
184
|
+
if (contents) {
|
|
185
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
186
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
187
|
+
setOptionalTurboModule(spec, true);
|
|
188
|
+
} else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
189
|
+
setOptionalTurboModule(spec, false);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return schema;
|
|
166
195
|
} catch (e) {
|
|
167
196
|
if (e instanceof Error) {
|
|
168
197
|
e.message = `(${filename}): ${e.message}`;
|
|
@@ -209,6 +238,7 @@ function generate(
|
|
|
209
238
|
);
|
|
210
239
|
|
|
211
240
|
const generateNM2 = createNM2Generator({namespace: argv.namespace});
|
|
241
|
+
|
|
212
242
|
const generatorPropsH =
|
|
213
243
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;
|
|
214
244
|
const generatorPropsCPP =
|
|
@@ -221,6 +251,8 @@ function generate(
|
|
|
221
251
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;
|
|
222
252
|
const generatorEventEmitterH =
|
|
223
253
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;
|
|
254
|
+
const generatorEventEmitterCPP =
|
|
255
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;
|
|
224
256
|
|
|
225
257
|
normalizeFileMap(
|
|
226
258
|
generateNM2(libraryName, schema, moduleSpecName),
|
|
@@ -228,9 +260,17 @@ function generate(
|
|
|
228
260
|
generatedFiles,
|
|
229
261
|
);
|
|
230
262
|
|
|
263
|
+
if (argv.ts) {
|
|
264
|
+
normalizeFileMap(
|
|
265
|
+
generateTypeScript(libraryName, schema, moduleSpecName),
|
|
266
|
+
outputDirectory,
|
|
267
|
+
generatedFiles,
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
231
271
|
if (
|
|
232
272
|
Object.keys(schema.modules).some(
|
|
233
|
-
|
|
273
|
+
moduleName => schema.modules[moduleName].type === 'Component',
|
|
234
274
|
)
|
|
235
275
|
) {
|
|
236
276
|
const componentGenerators = [
|
|
@@ -240,9 +280,10 @@ function generate(
|
|
|
240
280
|
generatorShadowNodeCPP,
|
|
241
281
|
generatorComponentDescriptorH,
|
|
242
282
|
generatorEventEmitterH,
|
|
283
|
+
generatorEventEmitterCPP,
|
|
243
284
|
];
|
|
244
285
|
|
|
245
|
-
componentGenerators.forEach(
|
|
286
|
+
componentGenerators.forEach(generator => {
|
|
246
287
|
const generated: Map<string, string> = generator(
|
|
247
288
|
libraryName,
|
|
248
289
|
schema,
|
|
@@ -273,7 +314,7 @@ if (argv.file) {
|
|
|
273
314
|
|
|
274
315
|
const libraryName = argv.libraryName;
|
|
275
316
|
const moduleSpecName = 'moduleSpecName';
|
|
276
|
-
const outputDirectory =
|
|
317
|
+
const outputDirectory = argv.outdir;
|
|
277
318
|
generate(
|
|
278
319
|
{libraryName, schema, outputDirectory, moduleSpecName},
|
|
279
320
|
{generators: [], test: false},
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
NamedShape,
|
|
11
|
+
NativeModuleBaseTypeAnnotation,
|
|
12
|
+
NativeModuleFunctionTypeAnnotation,
|
|
13
|
+
NativeModuleObjectTypeAnnotation,
|
|
14
|
+
NativeModuleParamTypeAnnotation,
|
|
15
|
+
NativeModuleReturnTypeAnnotation,
|
|
16
|
+
NativeModuleSchema,
|
|
17
|
+
Nullable,
|
|
18
|
+
SchemaType,
|
|
19
|
+
} from 'react-native-tscodegen';
|
|
20
|
+
|
|
21
|
+
interface CodegenNativeModuleSchema extends NativeModuleSchema {
|
|
22
|
+
optionalTurboModule?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function setOptionalTurboModule(
|
|
26
|
+
schema: NativeModuleSchema,
|
|
27
|
+
optional: boolean,
|
|
28
|
+
): void {
|
|
29
|
+
const cs = <CodegenNativeModuleSchema>schema;
|
|
30
|
+
cs.optionalTurboModule = optional;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getOptionalTurboModule(schema: NativeModuleSchema): boolean {
|
|
34
|
+
return (<CodegenNativeModuleSchema>schema).optionalTurboModule ?? false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type ObjectProp = NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>;
|
|
38
|
+
type FunctionParam = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
|
|
39
|
+
type FunctionDecl = NamedShape<Nullable<NativeModuleFunctionTypeAnnotation>>;
|
|
40
|
+
type FilesOutput = Map<string, string>;
|
|
41
|
+
|
|
42
|
+
const moduleTemplate = `
|
|
43
|
+
/*
|
|
44
|
+
* This file is auto-generated from a NativeModule spec file in js.
|
|
45
|
+
*
|
|
46
|
+
* This is a TypeScript turbo module definition file.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import {TurboModule, TurboModuleRegistry} from 'react-native';
|
|
50
|
+
'use strict';
|
|
51
|
+
::_MODULE_ALIASED_STRUCTS_::
|
|
52
|
+
export interface Spec extends TurboModule {
|
|
53
|
+
::_MODULE_MEMBERS_::
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
function optionalSign<T>(obj: NamedShape<T>): string {
|
|
60
|
+
return obj.optional ? '?' : '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function translateType(
|
|
64
|
+
type: Nullable<
|
|
65
|
+
| NativeModuleBaseTypeAnnotation
|
|
66
|
+
| NativeModuleParamTypeAnnotation
|
|
67
|
+
| NativeModuleReturnTypeAnnotation
|
|
68
|
+
>,
|
|
69
|
+
): string {
|
|
70
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
71
|
+
const returnType = type.type;
|
|
72
|
+
switch (type.type) {
|
|
73
|
+
case 'StringTypeAnnotation':
|
|
74
|
+
return 'string';
|
|
75
|
+
case 'NumberTypeAnnotation':
|
|
76
|
+
case 'FloatTypeAnnotation':
|
|
77
|
+
case 'DoubleTypeAnnotation':
|
|
78
|
+
case 'Int32TypeAnnotation':
|
|
79
|
+
return 'number';
|
|
80
|
+
case 'BooleanTypeAnnotation':
|
|
81
|
+
return 'boolean';
|
|
82
|
+
case 'ArrayTypeAnnotation':
|
|
83
|
+
if (type.elementType) {
|
|
84
|
+
return `${translateType(type.elementType)}[]`;
|
|
85
|
+
} else {
|
|
86
|
+
return `Array`;
|
|
87
|
+
}
|
|
88
|
+
case 'GenericObjectTypeAnnotation':
|
|
89
|
+
return 'object';
|
|
90
|
+
case 'ObjectTypeAnnotation':
|
|
91
|
+
return `{${type.properties
|
|
92
|
+
.map((prop: ObjectProp) => {
|
|
93
|
+
return `${prop.name}${optionalSign(prop)}: ${translateType(
|
|
94
|
+
prop.typeAnnotation,
|
|
95
|
+
)}`;
|
|
96
|
+
})
|
|
97
|
+
.join(', ')}}`;
|
|
98
|
+
case 'ReservedTypeAnnotation': {
|
|
99
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
100
|
+
const name = type.name;
|
|
101
|
+
// (#6597)
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
103
|
+
if (name !== 'RootTag')
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Unknown reserved function: ${name} in translateReturnType`,
|
|
106
|
+
);
|
|
107
|
+
return 'number';
|
|
108
|
+
}
|
|
109
|
+
case 'TypeAliasTypeAnnotation':
|
|
110
|
+
return type.name;
|
|
111
|
+
case 'NullableTypeAnnotation':
|
|
112
|
+
return `(${translateType(type.typeAnnotation)} | null | undefined)`;
|
|
113
|
+
case 'VoidTypeAnnotation':
|
|
114
|
+
return `void`;
|
|
115
|
+
case 'PromiseTypeAnnotation':
|
|
116
|
+
return `Promise`;
|
|
117
|
+
case `FunctionTypeAnnotation`:
|
|
118
|
+
return `((${type.params
|
|
119
|
+
.map((param: FunctionParam) => {
|
|
120
|
+
return `${param.name}${optionalSign(param)}: ${translateType(
|
|
121
|
+
param.typeAnnotation,
|
|
122
|
+
)}`;
|
|
123
|
+
})
|
|
124
|
+
.join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;
|
|
125
|
+
default:
|
|
126
|
+
throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function translateAlias(
|
|
131
|
+
name: string,
|
|
132
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
133
|
+
): string {
|
|
134
|
+
return `
|
|
135
|
+
export interface ${name} {
|
|
136
|
+
${type.properties
|
|
137
|
+
.map((prop: ObjectProp) => {
|
|
138
|
+
return ` ${prop.name}${optionalSign(prop)}: ${translateType(
|
|
139
|
+
prop.typeAnnotation,
|
|
140
|
+
)};`;
|
|
141
|
+
})
|
|
142
|
+
.join('\n')}
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function tryGetConstantType(
|
|
148
|
+
nativeModule: NativeModuleSchema,
|
|
149
|
+
): NativeModuleObjectTypeAnnotation | undefined {
|
|
150
|
+
const candidates = nativeModule.spec.properties.filter(
|
|
151
|
+
prop => prop.name === 'getConstants',
|
|
152
|
+
);
|
|
153
|
+
if (candidates.length === 0) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const getConstant = candidates[0];
|
|
158
|
+
const funcType =
|
|
159
|
+
getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
160
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
161
|
+
: getConstant.typeAnnotation;
|
|
162
|
+
if (
|
|
163
|
+
funcType.params.length > 0 ||
|
|
164
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'
|
|
165
|
+
) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
170
|
+
if (constantType.properties.length === 0) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return constantType;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function translateMethod(func: FunctionDecl): string {
|
|
178
|
+
const funcType =
|
|
179
|
+
func.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
180
|
+
? func.typeAnnotation.typeAnnotation
|
|
181
|
+
: func.typeAnnotation;
|
|
182
|
+
|
|
183
|
+
return `
|
|
184
|
+
${func.name}(${funcType.params
|
|
185
|
+
.map((param: FunctionParam) => {
|
|
186
|
+
return `${param.name}${optionalSign(param)}: ${translateType(
|
|
187
|
+
param.typeAnnotation,
|
|
188
|
+
)}`;
|
|
189
|
+
})
|
|
190
|
+
.join(', ')})${optionalSign(func)}: ${translateType(
|
|
191
|
+
funcType.returnTypeAnnotation,
|
|
192
|
+
)}${
|
|
193
|
+
funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'
|
|
194
|
+
}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function generateTypeScript(
|
|
198
|
+
_libraryName: string,
|
|
199
|
+
schema: SchemaType,
|
|
200
|
+
_moduleSpecName: string,
|
|
201
|
+
): FilesOutput {
|
|
202
|
+
const files = new Map<string, string>();
|
|
203
|
+
|
|
204
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
205
|
+
const nativeModule = schema.modules[moduleName];
|
|
206
|
+
// from 0.65 facebook's react-native-codegen
|
|
207
|
+
// the module name has the Native prefix comparing to 0.63
|
|
208
|
+
// when reading files we provided
|
|
209
|
+
const nativePrefix = 'Native';
|
|
210
|
+
const preferredModuleName = moduleName.startsWith(nativePrefix)
|
|
211
|
+
? moduleName.substr(nativePrefix.length)
|
|
212
|
+
: moduleName;
|
|
213
|
+
|
|
214
|
+
if (nativeModule.type === 'NativeModule') {
|
|
215
|
+
console.log(`Generating ${preferredModuleName}Spec.g.ts`);
|
|
216
|
+
|
|
217
|
+
const aliasCode = Object.keys(nativeModule.aliases)
|
|
218
|
+
.map(name => translateAlias(name, nativeModule.aliases[name]))
|
|
219
|
+
.join('');
|
|
220
|
+
|
|
221
|
+
const constantType = tryGetConstantType(nativeModule);
|
|
222
|
+
const constantCode =
|
|
223
|
+
constantType === undefined
|
|
224
|
+
? ''
|
|
225
|
+
: ` getConstants(): ${translateType(constantType)}`;
|
|
226
|
+
|
|
227
|
+
const methods = nativeModule.spec.properties.filter(
|
|
228
|
+
prop => prop.name !== 'getConstants',
|
|
229
|
+
);
|
|
230
|
+
const membersCode = methods.map(translateMethod).join('');
|
|
231
|
+
|
|
232
|
+
files.set(
|
|
233
|
+
`${preferredModuleName}Spec.g.ts`,
|
|
234
|
+
moduleTemplate
|
|
235
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)
|
|
236
|
+
.replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)
|
|
237
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
238
|
+
.replace(
|
|
239
|
+
/::_MODULE_GETTER_::/g,
|
|
240
|
+
getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing',
|
|
241
|
+
),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return files;
|
|
247
|
+
}
|
|
@@ -194,7 +194,7 @@ export function translateSpecArgs(
|
|
|
194
194
|
aliases: AliasMap,
|
|
195
195
|
baseAliasName: string,
|
|
196
196
|
) {
|
|
197
|
-
return params.map(
|
|
197
|
+
return params.map(param => {
|
|
198
198
|
const translatedParam = translateSpecFunctionParam(
|
|
199
199
|
param,
|
|
200
200
|
aliases,
|
|
@@ -209,7 +209,7 @@ export function translateArgs(
|
|
|
209
209
|
aliases: AliasMap,
|
|
210
210
|
baseAliasName: string,
|
|
211
211
|
) {
|
|
212
|
-
return params.map(
|
|
212
|
+
return params.map(param => {
|
|
213
213
|
const translatedParam = translateFunctionParam(
|
|
214
214
|
param,
|
|
215
215
|
aliases,
|
|
@@ -14,7 +14,7 @@ export function generateValidateConstants(
|
|
|
14
14
|
aliases: AliasMap,
|
|
15
15
|
): [string, string] | undefined {
|
|
16
16
|
const candidates = nativeModule.spec.properties.filter(
|
|
17
|
-
|
|
17
|
+
prop => prop.name === 'getConstants',
|
|
18
18
|
);
|
|
19
19
|
if (candidates.length === 0) {
|
|
20
20
|
return undefined;
|
|
@@ -65,7 +65,7 @@ function translatePossibleMethodSignatures(
|
|
|
65
65
|
baseAliasName: string,
|
|
66
66
|
): string {
|
|
67
67
|
return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
|
|
68
|
-
.map(
|
|
68
|
+
.map(sig => `" ${sig}\\n"`)
|
|
69
69
|
.join('\n ');
|
|
70
70
|
}
|
|
71
71
|
|
|
@@ -76,7 +76,7 @@ function renderProperties(
|
|
|
76
76
|
): string {
|
|
77
77
|
// TODO: generate code for constants
|
|
78
78
|
return properties
|
|
79
|
-
.filter(
|
|
79
|
+
.filter(prop => prop.name !== 'getConstants')
|
|
80
80
|
.map((prop, index) => {
|
|
81
81
|
// TODO: prop.optional === true
|
|
82
82
|
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|