@react-native-windows/codegen 0.65.0 → 0.67.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.json +305 -10
- package/CHANGELOG.md +121 -8
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +203 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +11 -0
- package/lib-commonjs/generators/AliasGen.js +72 -0
- package/lib-commonjs/generators/AliasGen.js.map +1 -0
- package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
- package/lib-commonjs/generators/AliasManaging.js +49 -0
- package/lib-commonjs/generators/AliasManaging.js.map +1 -0
- package/lib-commonjs/generators/GenerateNM2.d.ts +11 -0
- package/lib-commonjs/generators/GenerateNM2.js +94 -0
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
- package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
- package/lib-commonjs/generators/ObjectTypes.js +53 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
- package/lib-commonjs/generators/ParamTypes.js +114 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
- package/lib-commonjs/generators/ReturnTypes.js +63 -0
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
- package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
- package/lib-commonjs/generators/ValidateConstants.js +38 -0
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
- package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
- package/lib-commonjs/generators/ValidateMethods.js +70 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/package.json +21 -11
- package/src/Cli.ts +102 -54
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +58 -290
- package/src/generators/ObjectTypes.ts +70 -0
- package/src/generators/ParamTypes.ts +220 -0
- package/src/generators/ReturnTypes.ts +92 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +135 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReturnTypes.js","sourceRoot":"","sources":["../../src/generators/ReturnTypes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAMb,mDAIyB;AAEzB,SAAS,mBAAmB,CAC1B,IAAgD,EAChD,OAAiB,EACjB,aAAqB;IAErB,wDAAwD;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;IAC7B,QAAQ,IAAI,CAAC,IAAI,EAAE;QACjB,KAAK,oBAAoB,CAAC;QAC1B,KAAK,uBAAuB;YAC1B,OAAO,MAAM,CAAC;QAChB,KAAK,sBAAsB;YACzB,OAAO,aAAa,CAAC;QACvB,KAAK,sBAAsB,CAAC;QAC5B,KAAK,qBAAqB,CAAC;QAC3B,KAAK,sBAAsB;YACzB,OAAO,QAAQ,CAAC;QAClB,KAAK,qBAAqB;YACxB,OAAO,KAAK,CAAC;QACf,KAAK,uBAAuB;YAC1B,OAAO,MAAM,CAAC;QAChB,KAAK,qBAAqB;YACxB,IAAI,IAAI,CAAC,WAAW,EAAE;gBACpB,OAAO,eAAe,mBAAmB,CACvC,IAAI,CAAC,WAAW,EAChB,OAAO,EACP,GAAG,aAAa,UAAU,CAC3B,GAAG,CAAC;aACN;iBAAM;gBACL,OAAO,qBAAqB,CAAC;aAC9B;QACH,KAAK,6BAA6B;YAChC,OAAO,gBAAgB,CAAC;QAC1B,KAAK,sBAAsB;YACzB,OAAO,IAAA,wCAAwB,EAAC,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,CAAC;QAChE,KAAK,wBAAwB,CAAC,CAAC;YAC7B,wDAAwD;YACxD,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACvB,UAAU;YACV,uEAAuE;YACvE,IAAI,IAAI,KAAK,SAAS;gBACpB,MAAM,IAAI,KAAK,CACb,8BAA8B,IAAI,yBAAyB,CAC5D,CAAC;YACJ,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,yBAAyB;YAC5B,OAAO,IAAA,+BAAe,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK,wBAAwB;YAC3B,OAAO,iBAAiB,mBAAmB,CACzC,IAAI,CAAC,cAAc,EACnB,OAAO,EACP,aAAa,CACd,GAAG,CAAC;QACP;YACE,MAAM,IAAI,KAAK,CAAC,0CAA0C,UAAU,EAAE,CAAC,CAAC;KAC3E;AACH,CAAC;AAED,SAAgB,uBAAuB,CACrC,IAAgD,EAChD,OAAiB,EACjB,aAAqB;IAErB,OAAO,mBAAmB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,aAAa,aAAa,CAAC,CAAC;AAC3E,CAAC;AAND,0DAMC;AAED,SAAgB,uBAAuB,CACrC,IAAgD,EAChD,OAAiB,EACjB,aAAqB;IAErB,OAAO,mBAAmB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,aAAa,aAAa,CAAC,CAAC;AAC3E,CAAC;AAND,0DAMC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {\n NativeModuleReturnTypeAnnotation,\n Nullable,\n} from 'react-native-tscodegen';\nimport {\n AliasMap,\n getAliasCppName,\n getAnonymousAliasCppName,\n} from './AliasManaging';\n\nfunction translateReturnType(\n type: Nullable<NativeModuleReturnTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n): string {\n // avoid: Property 'type' does not exist on type 'never'\n const returnType = type.type;\n switch (type.type) {\n case 'VoidTypeAnnotation':\n case 'PromiseTypeAnnotation':\n return 'void';\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 'ArrayTypeAnnotation':\n if (type.elementType) {\n return `std::vector<${translateReturnType(\n type.elementType,\n aliases,\n `${baseAliasName}_element`,\n )}>`;\n } else {\n return 'React::JSValueArray';\n }\n case 'GenericObjectTypeAnnotation':\n return 'React::JSValue';\n case 'ObjectTypeAnnotation':\n return getAnonymousAliasCppName(aliases, baseAliasName, type);\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 'double';\n }\n case 'TypeAliasTypeAnnotation':\n return getAliasCppName(type.name);\n case 'NullableTypeAnnotation':\n return `std::optional<${translateReturnType(\n type.typeAnnotation,\n aliases,\n baseAliasName,\n )}>`;\n default:\n throw new Error(`Unhandled type in translateReturnType: ${returnType}`);\n }\n}\n\nexport function translateSpecReturnType(\n type: Nullable<NativeModuleReturnTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n) {\n return translateReturnType(type, aliases, `${baseAliasName}_returnType`);\n}\n\nexport function translateImplReturnType(\n type: Nullable<NativeModuleReturnTypeAnnotation>,\n aliases: AliasMap,\n baseAliasName: string,\n) {\n return translateReturnType(type, aliases, `${baseAliasName}_returnType`);\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModuleSchema } from 'react-native-tscodegen';
|
|
7
|
+
import { AliasMap } from './AliasManaging';
|
|
8
|
+
export declare function generateValidateConstants(nativeModule: NativeModuleSchema, aliases: AliasMap): [string, string] | undefined;
|
|
@@ -0,0 +1,38 @@
|
|
|
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.generateValidateConstants = void 0;
|
|
9
|
+
const AliasManaging_1 = require("./AliasManaging");
|
|
10
|
+
function generateValidateConstants(nativeModule, aliases) {
|
|
11
|
+
const candidates = nativeModule.spec.properties.filter(prop => prop.name === 'getConstants');
|
|
12
|
+
if (candidates.length === 0) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
const getConstant = candidates[0];
|
|
16
|
+
const funcType = getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
17
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
18
|
+
: getConstant.typeAnnotation;
|
|
19
|
+
if (funcType.params.length > 0 ||
|
|
20
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation') {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
24
|
+
if (constantType.properties.length === 0) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const cppName = (0, AliasManaging_1.getAnonymousAliasCppName)(aliases, 'Constants', constantType);
|
|
28
|
+
return [
|
|
29
|
+
` TypedConstant<${cppName}>{0},`,
|
|
30
|
+
` REACT_SHOW_CONSTANT_SPEC_ERRORS(
|
|
31
|
+
0,
|
|
32
|
+
"${cppName}",
|
|
33
|
+
" REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\n"
|
|
34
|
+
" REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\n");`,
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
exports.generateValidateConstants = generateValidateConstants;
|
|
38
|
+
//# sourceMappingURL=ValidateConstants.js.map
|
|
@@ -0,0 +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,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"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModuleSchema } from 'react-native-tscodegen';
|
|
7
|
+
import { AliasMap } from './AliasManaging';
|
|
8
|
+
export declare function generateValidateMethods(nativeModule: NativeModuleSchema, aliases: AliasMap): [string, string];
|
|
@@ -0,0 +1,70 @@
|
|
|
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.generateValidateMethods = void 0;
|
|
9
|
+
const ParamTypes_1 = require("./ParamTypes");
|
|
10
|
+
const ReturnTypes_1 = require("./ReturnTypes");
|
|
11
|
+
function isMethodSync(funcType) {
|
|
12
|
+
return (funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
|
|
13
|
+
funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation');
|
|
14
|
+
}
|
|
15
|
+
function isMethodReturnPromise(funcType) {
|
|
16
|
+
return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';
|
|
17
|
+
}
|
|
18
|
+
function getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
19
|
+
const args = (0, ParamTypes_1.translateArgs)(funcType.params, aliases, baseAliasName);
|
|
20
|
+
if (isMethodReturnPromise(funcType)) {
|
|
21
|
+
// TODO: type of the promise could be provided in the future
|
|
22
|
+
args.push('React::ReactPromise<React::JSValue> &&result');
|
|
23
|
+
}
|
|
24
|
+
// TODO: be much more exhastive on the possible method signatures that can be used..
|
|
25
|
+
const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
26
|
+
const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) static ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
27
|
+
return [sig, staticsig];
|
|
28
|
+
}
|
|
29
|
+
function translatePossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
30
|
+
return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
|
|
31
|
+
.map(sig => `" ${sig}\\n"`)
|
|
32
|
+
.join('\n ');
|
|
33
|
+
}
|
|
34
|
+
function renderProperties(properties, aliases, tuple) {
|
|
35
|
+
// TODO: generate code for constants
|
|
36
|
+
return properties
|
|
37
|
+
.filter(prop => prop.name !== 'getConstants')
|
|
38
|
+
.map((prop, index) => {
|
|
39
|
+
// TODO: prop.optional === true
|
|
40
|
+
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
41
|
+
const propAliasName = prop.name;
|
|
42
|
+
const funcType = prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
43
|
+
? prop.typeAnnotation.typeAnnotation
|
|
44
|
+
: prop.typeAnnotation;
|
|
45
|
+
const traversedArgs = (0, ParamTypes_1.translateSpecArgs)(funcType.params, aliases, propAliasName);
|
|
46
|
+
const translatedReturnParam = (0, ReturnTypes_1.translateSpecReturnType)(funcType.returnTypeAnnotation, aliases, propAliasName);
|
|
47
|
+
if (isMethodReturnPromise(funcType)) {
|
|
48
|
+
// TODO: type of the promise could be provided in the future
|
|
49
|
+
traversedArgs.push('Promise<React::JSValue>');
|
|
50
|
+
}
|
|
51
|
+
if (tuple) {
|
|
52
|
+
return ` ${isMethodSync(funcType) ? 'Sync' : ''}Method<${translatedReturnParam}(${traversedArgs.join(', ')}) noexcept>{${index}, L"${prop.name}"},`;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return ` REACT_SHOW_METHOD_SPEC_ERRORS(
|
|
56
|
+
${index},
|
|
57
|
+
"${prop.name}",
|
|
58
|
+
${translatePossibleMethodSignatures(prop, funcType, aliases, propAliasName)});`;
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.join('\n');
|
|
62
|
+
}
|
|
63
|
+
function generateValidateMethods(nativeModule, aliases) {
|
|
64
|
+
const properties = nativeModule.spec.properties;
|
|
65
|
+
const traversedProperties = renderProperties(properties, aliases, false);
|
|
66
|
+
const traversedPropertyTuples = renderProperties(properties, aliases, true);
|
|
67
|
+
return [traversedPropertyTuples, traversedProperties];
|
|
68
|
+
}
|
|
69
|
+
exports.generateValidateMethods = generateValidateMethods;
|
|
70
|
+
//# sourceMappingURL=ValidateMethods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidateMethods.js","sourceRoot":"","sources":["../../src/generators/ValidateMethods.ts"],"names":[],"mappings":"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","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.67.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",
|
|
@@ -15,21 +15,21 @@
|
|
|
15
15
|
"watch": "rnw-scripts watch"
|
|
16
16
|
},
|
|
17
17
|
"bin": {
|
|
18
|
-
"codegen": "./bin.js"
|
|
18
|
+
"react-native-windows-codegen": "./bin.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"chalk": "^4.1.0",
|
|
22
22
|
"globby": "^9.2.0",
|
|
23
23
|
"mustache": "^4.0.1",
|
|
24
|
-
"react-native-tscodegen": "0.
|
|
24
|
+
"react-native-tscodegen": "0.68.4",
|
|
25
25
|
"source-map-support": "^0.5.19",
|
|
26
26
|
"yargs": "^16.2.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@rnw-scripts/eslint-config": "1.1.
|
|
30
|
-
"@rnw-scripts/jest-unittest-config": "1.2.
|
|
31
|
-
"@rnw-scripts/just-task": "2.
|
|
32
|
-
"@rnw-scripts/ts-config": "
|
|
29
|
+
"@rnw-scripts/eslint-config": "1.1.8",
|
|
30
|
+
"@rnw-scripts/jest-unittest-config": "1.2.4",
|
|
31
|
+
"@rnw-scripts/just-task": "2.2.1",
|
|
32
|
+
"@rnw-scripts/ts-config": "2.0.1",
|
|
33
33
|
"@types/chalk": "^2.2.0",
|
|
34
34
|
"@types/globby": "^9.1.0",
|
|
35
35
|
"@types/jest": "^26.0.20",
|
|
@@ -40,15 +40,25 @@
|
|
|
40
40
|
"jest": "^26.6.3",
|
|
41
41
|
"just-scripts": "^1.3.3",
|
|
42
42
|
"prettier": "1.19.1",
|
|
43
|
-
"typescript": "^
|
|
43
|
+
"typescript": "^4.4.4"
|
|
44
44
|
},
|
|
45
|
+
"files": [
|
|
46
|
+
"bin.js",
|
|
47
|
+
"CHANGELOG.md",
|
|
48
|
+
"README.md",
|
|
49
|
+
"lib-commonjs",
|
|
50
|
+
"src"
|
|
51
|
+
],
|
|
45
52
|
"beachball": {
|
|
46
|
-
"defaultNpmTag": "
|
|
53
|
+
"defaultNpmTag": "preview",
|
|
47
54
|
"disallowedChangeTypes": [
|
|
48
55
|
"major",
|
|
49
56
|
"minor",
|
|
50
|
-
"
|
|
57
|
+
"patch"
|
|
51
58
|
]
|
|
52
59
|
},
|
|
53
|
-
"promoteRelease": true
|
|
60
|
+
"promoteRelease": true,
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">= 12.0.0"
|
|
63
|
+
}
|
|
54
64
|
}
|
package/src/Cli.ts
CHANGED
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @format
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
8
|
+
import yargs from 'yargs';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import globby from 'globby';
|
|
12
12
|
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
13
|
// @ts-ignore
|
|
14
14
|
import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
|
|
15
15
|
// @ts-ignore
|
|
16
|
-
import
|
|
16
|
+
import schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
|
|
17
17
|
|
|
18
18
|
const argv = yargs.options({
|
|
19
19
|
file: {
|
|
@@ -79,20 +79,42 @@ const GENERATORS = {
|
|
|
79
79
|
};
|
|
80
80
|
*/
|
|
81
81
|
|
|
82
|
+
function normalizeFileMap(
|
|
83
|
+
map: Map<string, string>,
|
|
84
|
+
outputDir: string,
|
|
85
|
+
outMap: Map<string, string>,
|
|
86
|
+
): void {
|
|
87
|
+
for (const [fileName, contents] of map) {
|
|
88
|
+
const location = path.join(outputDir, fileName);
|
|
89
|
+
outMap.set(path.normalize(location), contents);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
82
93
|
function checkFilesForChanges(
|
|
83
94
|
map: Map<string, string>,
|
|
84
95
|
outputDir: string,
|
|
85
96
|
): boolean {
|
|
86
97
|
let hasChanges = false;
|
|
87
98
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
99
|
+
const allExistingFiles = globby
|
|
100
|
+
.sync(`${outputDir}/**`)
|
|
101
|
+
.map(_ => path.normalize(_))
|
|
102
|
+
.sort();
|
|
103
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
104
|
+
|
|
105
|
+
if (
|
|
106
|
+
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
107
|
+
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index])
|
|
108
|
+
)
|
|
109
|
+
return true;
|
|
110
|
+
|
|
111
|
+
for (const [fileName, contents] of map) {
|
|
112
|
+
if (!fs.existsSync(fileName)) {
|
|
91
113
|
hasChanges = true;
|
|
92
114
|
continue;
|
|
93
115
|
}
|
|
94
116
|
|
|
95
|
-
const currentContents = fs.readFileSync(
|
|
117
|
+
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
96
118
|
if (currentContents !== contents) {
|
|
97
119
|
console.error(`- ${fileName} has changed`);
|
|
98
120
|
hasChanges = true;
|
|
@@ -105,20 +127,48 @@ function checkFilesForChanges(
|
|
|
105
127
|
|
|
106
128
|
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
107
129
|
let success = true;
|
|
108
|
-
|
|
130
|
+
|
|
131
|
+
// This ensures that we delete any generated files from modules that have been deleted
|
|
132
|
+
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
133
|
+
allExistingFiles.forEach(existingFile => {
|
|
134
|
+
if (!map.has(path.normalize(existingFile))) {
|
|
135
|
+
fs.unlinkSync(existingFile);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
for (const [fileName, contents] of map) {
|
|
109
140
|
try {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
fs.
|
|
141
|
+
fs.mkdirSync(path.dirname(fileName), {recursive: true});
|
|
142
|
+
|
|
143
|
+
if (fs.existsSync(fileName)) {
|
|
144
|
+
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
145
|
+
// Don't update the files if there are no changes as this breaks incremental builds
|
|
146
|
+
if (currentContents === contents) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fs.writeFileSync(fileName, contents);
|
|
113
152
|
} catch (error) {
|
|
114
153
|
success = false;
|
|
115
|
-
console.error(`Failed to write ${fileName} to ${
|
|
154
|
+
console.error(`Failed to write ${fileName} to ${fileName}`, error);
|
|
116
155
|
}
|
|
117
|
-
}
|
|
156
|
+
}
|
|
118
157
|
|
|
119
158
|
return success;
|
|
120
159
|
}
|
|
121
160
|
|
|
161
|
+
function parseFlowFile(filename: string): SchemaType {
|
|
162
|
+
try {
|
|
163
|
+
return parseFile(filename);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (e instanceof Error) {
|
|
166
|
+
e.message = `(${filename}): ${e.message}`;
|
|
167
|
+
}
|
|
168
|
+
throw e;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
122
172
|
function combineSchemas(files: string[]): SchemaType {
|
|
123
173
|
return files.reduce(
|
|
124
174
|
(merged, filename) => {
|
|
@@ -128,11 +178,8 @@ function combineSchemas(files: string[]): SchemaType {
|
|
|
128
178
|
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
129
179
|
contents.includes('extends TurboModule'))
|
|
130
180
|
) {
|
|
131
|
-
const schema =
|
|
132
|
-
|
|
133
|
-
if (schema && schema.modules) {
|
|
134
|
-
merged.modules = {...merged.modules, ...schema.modules};
|
|
135
|
-
}
|
|
181
|
+
const schema = parseFlowFile(filename);
|
|
182
|
+
merged.modules = {...merged.modules, ...schema.modules};
|
|
136
183
|
}
|
|
137
184
|
return merged;
|
|
138
185
|
},
|
|
@@ -152,15 +199,12 @@ function generate(
|
|
|
152
199
|
libraryName,
|
|
153
200
|
);
|
|
154
201
|
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
*/
|
|
202
|
+
const generatedFiles = new Map<string, string>();
|
|
203
|
+
|
|
204
|
+
generatedFiles.set(
|
|
205
|
+
path.join(outputDirectory, '.clang-format'),
|
|
206
|
+
'DisableFormat: true\nSortIncludes: false',
|
|
207
|
+
);
|
|
164
208
|
|
|
165
209
|
const generateNM2 = createNM2Generator({namespace: argv.namespace});
|
|
166
210
|
const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH')
|
|
@@ -176,37 +220,41 @@ function generate(
|
|
|
176
220
|
const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH')
|
|
177
221
|
.generate;
|
|
178
222
|
|
|
179
|
-
|
|
180
|
-
|
|
223
|
+
normalizeFileMap(
|
|
224
|
+
generateNM2(libraryName, schema, moduleSpecName),
|
|
225
|
+
outputDirectory,
|
|
226
|
+
generatedFiles,
|
|
181
227
|
);
|
|
182
228
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
229
|
+
if (
|
|
230
|
+
Object.keys(schema.modules).some(
|
|
231
|
+
moduleName => schema.modules[moduleName].type === 'Component',
|
|
232
|
+
)
|
|
233
|
+
) {
|
|
234
|
+
const componentGenerators = [
|
|
235
|
+
generatorPropsH,
|
|
236
|
+
generatorPropsCPP,
|
|
237
|
+
generatorShadowNodeH,
|
|
238
|
+
generatorShadowNodeCPP,
|
|
239
|
+
generatorComponentDescriptorH,
|
|
240
|
+
generatorEventEmitterH,
|
|
241
|
+
];
|
|
191
242
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
243
|
+
componentGenerators.forEach(generator => {
|
|
244
|
+
const generated: Map<string, string> = generator(
|
|
245
|
+
libraryName,
|
|
246
|
+
schema,
|
|
247
|
+
moduleSpecName,
|
|
248
|
+
);
|
|
249
|
+
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
198
252
|
|
|
199
253
|
if (test === true) {
|
|
200
|
-
return (
|
|
201
|
-
checkFilesForChanges(moduleFilesToUpdate, outputDirectory) &&
|
|
202
|
-
checkFilesForChanges(componentFilesToUpdate, componentOutputdir)
|
|
203
|
-
);
|
|
254
|
+
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
204
255
|
}
|
|
205
256
|
|
|
206
|
-
return (
|
|
207
|
-
writeMapToFiles(moduleFilesToUpdate, outputDirectory) &&
|
|
208
|
-
writeMapToFiles(componentFilesToUpdate, componentOutputdir)
|
|
209
|
-
);
|
|
257
|
+
return writeMapToFiles(generatedFiles, outputDirectory);
|
|
210
258
|
}
|
|
211
259
|
|
|
212
260
|
if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
@@ -216,7 +264,7 @@ if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
|
216
264
|
|
|
217
265
|
let schema: SchemaType;
|
|
218
266
|
if (argv.file) {
|
|
219
|
-
schema =
|
|
267
|
+
schema = parseFlowFile(argv.file);
|
|
220
268
|
} else {
|
|
221
269
|
schema = combineSchemas(globby.sync(argv.files as string[]));
|
|
222
270
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
NativeModuleBaseTypeAnnotation,
|
|
11
|
+
NativeModuleObjectTypeAnnotation,
|
|
12
|
+
NamedShape,
|
|
13
|
+
Nullable,
|
|
14
|
+
} from 'react-native-tscodegen';
|
|
15
|
+
import {AliasMap, getAliasCppName} from './AliasManaging';
|
|
16
|
+
import {translateField} from './ObjectTypes';
|
|
17
|
+
|
|
18
|
+
function translateObjectBody(
|
|
19
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
20
|
+
aliases: AliasMap,
|
|
21
|
+
baseAliasName: string,
|
|
22
|
+
prefix: string,
|
|
23
|
+
) {
|
|
24
|
+
return type.properties
|
|
25
|
+
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
26
|
+
let propType = prop.typeAnnotation;
|
|
27
|
+
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
28
|
+
propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
|
|
29
|
+
}
|
|
30
|
+
const first = `${prefix}REACT_FIELD(${prop.name})`;
|
|
31
|
+
const second = `${prefix}${translateField(
|
|
32
|
+
propType,
|
|
33
|
+
aliases,
|
|
34
|
+
`${baseAliasName}_${prop.name}`,
|
|
35
|
+
)} ${prop.name};`;
|
|
36
|
+
return `${first}\n${second}`;
|
|
37
|
+
})
|
|
38
|
+
.join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAliasMap(nativeModuleAliases: {
|
|
42
|
+
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
43
|
+
}): AliasMap {
|
|
44
|
+
const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
|
|
45
|
+
for (const aliasName of aliases.jobs) {
|
|
46
|
+
aliases.types[aliasName] = nativeModuleAliases[aliasName];
|
|
47
|
+
}
|
|
48
|
+
return aliases;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface AliasCodeMap {
|
|
52
|
+
[name: string]: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function generateSingleAlias(
|
|
56
|
+
aliases: AliasMap,
|
|
57
|
+
aliasName: string,
|
|
58
|
+
aliasCode: AliasCodeMap,
|
|
59
|
+
): void {
|
|
60
|
+
const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
|
|
61
|
+
aliasCode[aliasName] = `
|
|
62
|
+
REACT_STRUCT(${getAliasCppName(aliasName)})
|
|
63
|
+
struct ${getAliasCppName(aliasName)} {
|
|
64
|
+
${translateObjectBody(aliasType, aliases, aliasName, ' ')}
|
|
65
|
+
};
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function generateNestedAliasesInCorrectOrder(
|
|
70
|
+
aliases: AliasMap,
|
|
71
|
+
aliasCode: AliasCodeMap,
|
|
72
|
+
aliasOrder: string[],
|
|
73
|
+
): void {
|
|
74
|
+
// retrieve and clean all ungenerated aliases
|
|
75
|
+
const jobs = aliases.jobs;
|
|
76
|
+
aliases.jobs = [];
|
|
77
|
+
|
|
78
|
+
// generate each one in its found order
|
|
79
|
+
for (const aliasName of jobs) {
|
|
80
|
+
// generate a new struct and all fields will be examined
|
|
81
|
+
// new anonymous objects could be found
|
|
82
|
+
// they will be stored in aliases.jobs
|
|
83
|
+
generateSingleAlias(aliases, aliasName, aliasCode);
|
|
84
|
+
// nested C++ structs must be put before the current C++ struct
|
|
85
|
+
// as they will be used in the current C++ struct
|
|
86
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
87
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
88
|
+
// all referenced C++ structs are generated
|
|
89
|
+
// put the current one following them
|
|
90
|
+
aliasOrder.push(aliasName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function generateAliases(aliases: AliasMap): string {
|
|
95
|
+
const aliasCode: AliasCodeMap = {};
|
|
96
|
+
const aliasOrder: string[] = [];
|
|
97
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
98
|
+
|
|
99
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
100
|
+
let traversedAliasedStructs = '';
|
|
101
|
+
for (const aliasName of aliasOrder) {
|
|
102
|
+
traversedAliasedStructs = `${traversedAliasedStructs}${aliasCode[aliasName]}`;
|
|
103
|
+
}
|
|
104
|
+
return traversedAliasedStructs;
|
|
105
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {NativeModuleObjectTypeAnnotation} from 'react-native-tscodegen';
|
|
10
|
+
|
|
11
|
+
let preferredModuleName: string = '';
|
|
12
|
+
|
|
13
|
+
export function setPreferredModuleName(moduleName: string): void {
|
|
14
|
+
preferredModuleName = moduleName;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getAliasCppName(typeName: string): string {
|
|
18
|
+
return `${preferredModuleName}Spec_${typeName}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AliasMap {
|
|
22
|
+
types: {[name: string]: NativeModuleObjectTypeAnnotation | undefined};
|
|
23
|
+
jobs: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
|
|
27
|
+
interface ExtendedObject extends NativeModuleObjectTypeAnnotation {
|
|
28
|
+
'$RNW-TURBOMODULE-ALIAS'?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function recordAnonymouseAlias(
|
|
32
|
+
aliases: AliasMap,
|
|
33
|
+
baseAliasName: string,
|
|
34
|
+
extended: ExtendedObject,
|
|
35
|
+
): string {
|
|
36
|
+
extended[ExtendedObjectKey] = baseAliasName;
|
|
37
|
+
aliases.types[baseAliasName] = extended;
|
|
38
|
+
aliases.jobs.push(baseAliasName);
|
|
39
|
+
return baseAliasName;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getAnonymousAliasCppName(
|
|
43
|
+
aliases: AliasMap,
|
|
44
|
+
baseAliasName: string,
|
|
45
|
+
objectType: NativeModuleObjectTypeAnnotation,
|
|
46
|
+
): string {
|
|
47
|
+
// someone found an anonymous object literal type
|
|
48
|
+
// if the ExtendedObjectKey flag has been set
|
|
49
|
+
// then it is a known one
|
|
50
|
+
// this happens because method signatures are generate twice in spec and error messages
|
|
51
|
+
const extended = <ExtendedObject>objectType;
|
|
52
|
+
const key = extended[ExtendedObjectKey];
|
|
53
|
+
if (key !== undefined) {
|
|
54
|
+
return getAliasCppName(key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// if the ExtendedObjectKey flag has not been set
|
|
58
|
+
// it means it is a unknown one
|
|
59
|
+
// associate the name with this object literal type and return
|
|
60
|
+
if (aliases.types[baseAliasName] === undefined) {
|
|
61
|
+
return getAliasCppName(
|
|
62
|
+
recordAnonymouseAlias(aliases, baseAliasName, extended),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// sometimes names could be anonymous
|
|
67
|
+
let index = 2;
|
|
68
|
+
while (aliases.types[`${baseAliasName}${index}`] !== undefined) {
|
|
69
|
+
index++;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return getAliasCppName(
|
|
73
|
+
recordAnonymouseAlias(aliases, `${baseAliasName}${index}`, extended),
|
|
74
|
+
);
|
|
75
|
+
}
|