@react-native-windows/codegen 0.68.1 → 0.70.0-preview.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 +103 -20
- package/lib-commonjs/Cli.js +50 -10
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.d.ts +2 -1
- package/lib-commonjs/generators/GenerateNM2.js +4 -4
- package/lib-commonjs/generators/GenerateNM2.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 +11 -11
- package/src/Cli.ts +59 -10
- package/src/generators/GenerateNM2.ts +10 -4
- 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
|
@@ -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.70.0-preview.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,28 +18,28 @@
|
|
|
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.70.0-preview.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.13",
|
|
33
|
+
"@rnw-scripts/jest-unittest-config": "1.2.8",
|
|
34
|
+
"@rnw-scripts/just-task": "2.2.5",
|
|
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",
|
|
39
40
|
"babel-jest": "^26.3.0",
|
|
40
41
|
"eslint": "^7.32.0",
|
|
41
42
|
"jest": "^26.6.3",
|
|
42
|
-
"just-scripts": "^1.3.3",
|
|
43
43
|
"prettier": "^2.4.1",
|
|
44
44
|
"typescript": "^4.4.4"
|
|
45
45
|
},
|
|
@@ -51,11 +51,11 @@
|
|
|
51
51
|
"src"
|
|
52
52
|
],
|
|
53
53
|
"beachball": {
|
|
54
|
-
"defaultNpmTag": "
|
|
54
|
+
"defaultNpmTag": "preview",
|
|
55
55
|
"disallowedChangeTypes": [
|
|
56
56
|
"major",
|
|
57
57
|
"minor",
|
|
58
|
-
"
|
|
58
|
+
"patch"
|
|
59
59
|
]
|
|
60
60
|
},
|
|
61
61
|
"promoteRelease": true,
|
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,21 @@ 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
|
+
methodonly: {
|
|
37
|
+
type: 'boolean',
|
|
38
|
+
describe: 'generate only method metadata in C++ turbo module spec',
|
|
39
|
+
default: false,
|
|
40
|
+
},
|
|
41
|
+
outdir: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
describe: 'output directory',
|
|
44
|
+
default: 'codegen',
|
|
45
|
+
},
|
|
27
46
|
test: {
|
|
28
47
|
type: 'boolean',
|
|
29
48
|
describe: 'Verify that the generated output is unchanged',
|
|
@@ -98,11 +117,9 @@ function checkFilesForChanges(
|
|
|
98
117
|
|
|
99
118
|
const allExistingFiles = globby
|
|
100
119
|
.sync(`${outputDir}/**`)
|
|
101
|
-
.map(
|
|
102
|
-
.sort();
|
|
103
|
-
const allGeneratedFiles = [...map.keys()]
|
|
104
|
-
.map((_) => path.normalize(_))
|
|
120
|
+
.map(_ => path.normalize(_))
|
|
105
121
|
.sort();
|
|
122
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
106
123
|
|
|
107
124
|
if (
|
|
108
125
|
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
@@ -132,7 +149,7 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
|
132
149
|
|
|
133
150
|
// This ensures that we delete any generated files from modules that have been deleted
|
|
134
151
|
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
135
|
-
allExistingFiles.forEach(
|
|
152
|
+
allExistingFiles.forEach(existingFile => {
|
|
136
153
|
if (!map.has(path.normalize(existingFile))) {
|
|
137
154
|
fs.unlinkSync(existingFile);
|
|
138
155
|
}
|
|
@@ -162,7 +179,24 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
|
162
179
|
|
|
163
180
|
function parseFlowFile(filename: string): SchemaType {
|
|
164
181
|
try {
|
|
165
|
-
|
|
182
|
+
const schema = parseFile(filename);
|
|
183
|
+
// there will be at most one turbo module per file
|
|
184
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
185
|
+
if (moduleName) {
|
|
186
|
+
const spec = schema.modules[moduleName];
|
|
187
|
+
if (spec.type === 'NativeModule') {
|
|
188
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
189
|
+
if (contents) {
|
|
190
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
191
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
192
|
+
setOptionalTurboModule(spec, true);
|
|
193
|
+
} else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
194
|
+
setOptionalTurboModule(spec, false);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return schema;
|
|
166
200
|
} catch (e) {
|
|
167
201
|
if (e instanceof Error) {
|
|
168
202
|
e.message = `(${filename}): ${e.message}`;
|
|
@@ -208,7 +242,11 @@ function generate(
|
|
|
208
242
|
'DisableFormat: true\nSortIncludes: false',
|
|
209
243
|
);
|
|
210
244
|
|
|
211
|
-
const generateNM2 = createNM2Generator({
|
|
245
|
+
const generateNM2 = createNM2Generator({
|
|
246
|
+
namespace: argv.namespace,
|
|
247
|
+
methodonly: argv.methodonly,
|
|
248
|
+
});
|
|
249
|
+
|
|
212
250
|
const generatorPropsH =
|
|
213
251
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;
|
|
214
252
|
const generatorPropsCPP =
|
|
@@ -221,6 +259,8 @@ function generate(
|
|
|
221
259
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;
|
|
222
260
|
const generatorEventEmitterH =
|
|
223
261
|
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;
|
|
262
|
+
const generatorEventEmitterCPP =
|
|
263
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;
|
|
224
264
|
|
|
225
265
|
normalizeFileMap(
|
|
226
266
|
generateNM2(libraryName, schema, moduleSpecName),
|
|
@@ -228,9 +268,17 @@ function generate(
|
|
|
228
268
|
generatedFiles,
|
|
229
269
|
);
|
|
230
270
|
|
|
271
|
+
if (argv.ts) {
|
|
272
|
+
normalizeFileMap(
|
|
273
|
+
generateTypeScript(libraryName, schema, moduleSpecName),
|
|
274
|
+
outputDirectory,
|
|
275
|
+
generatedFiles,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
231
279
|
if (
|
|
232
280
|
Object.keys(schema.modules).some(
|
|
233
|
-
|
|
281
|
+
moduleName => schema.modules[moduleName].type === 'Component',
|
|
234
282
|
)
|
|
235
283
|
) {
|
|
236
284
|
const componentGenerators = [
|
|
@@ -240,9 +288,10 @@ function generate(
|
|
|
240
288
|
generatorShadowNodeCPP,
|
|
241
289
|
generatorComponentDescriptorH,
|
|
242
290
|
generatorEventEmitterH,
|
|
291
|
+
generatorEventEmitterCPP,
|
|
243
292
|
];
|
|
244
293
|
|
|
245
|
-
componentGenerators.forEach(
|
|
294
|
+
componentGenerators.forEach(generator => {
|
|
246
295
|
const generated: Map<string, string> = generator(
|
|
247
296
|
libraryName,
|
|
248
297
|
schema,
|
|
@@ -273,7 +322,7 @@ if (argv.file) {
|
|
|
273
322
|
|
|
274
323
|
const libraryName = argv.libraryName;
|
|
275
324
|
const moduleSpecName = 'moduleSpecName';
|
|
276
|
-
const outputDirectory =
|
|
325
|
+
const outputDirectory = argv.outdir;
|
|
277
326
|
generate(
|
|
278
327
|
{libraryName, schema, outputDirectory, moduleSpecName},
|
|
279
328
|
{generators: [], test: false},
|
|
@@ -43,7 +43,13 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
|
43
43
|
} // namespace ::_NAMESPACE_::
|
|
44
44
|
`;
|
|
45
45
|
|
|
46
|
-
export function createNM2Generator({
|
|
46
|
+
export function createNM2Generator({
|
|
47
|
+
namespace,
|
|
48
|
+
methodonly,
|
|
49
|
+
}: {
|
|
50
|
+
namespace: string;
|
|
51
|
+
methodonly: boolean;
|
|
52
|
+
}) {
|
|
47
53
|
return (
|
|
48
54
|
_libraryName: string,
|
|
49
55
|
schema: SchemaType,
|
|
@@ -79,7 +85,7 @@ ${methods[0]}
|
|
|
79
85
|
|
|
80
86
|
// prepare constants
|
|
81
87
|
const constants = generateValidateConstants(nativeModule, aliases);
|
|
82
|
-
if (constants !== undefined) {
|
|
88
|
+
if (constants !== undefined && !methodonly) {
|
|
83
89
|
tuples = `
|
|
84
90
|
static constexpr auto constants = std::tuple{
|
|
85
91
|
${constants[0]}
|
|
@@ -98,8 +104,8 @@ ${errors}`;
|
|
|
98
104
|
`Native${preferredModuleName}Spec.g.h`,
|
|
99
105
|
moduleTemplate
|
|
100
106
|
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
|
|
101
|
-
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.
|
|
102
|
-
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.
|
|
107
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
108
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
103
109
|
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
104
110
|
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
105
111
|
.replace(/::_NAMESPACE_::/g, namespace),
|
|
@@ -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'
|