@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.
Files changed (43) hide show
  1. package/CHANGELOG.json +305 -10
  2. package/CHANGELOG.md +121 -8
  3. package/lib-commonjs/Cli.d.ts +7 -0
  4. package/lib-commonjs/Cli.js +203 -0
  5. package/lib-commonjs/Cli.js.map +1 -0
  6. package/lib-commonjs/generators/AliasGen.d.ts +11 -0
  7. package/lib-commonjs/generators/AliasGen.js +72 -0
  8. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  9. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  10. package/lib-commonjs/generators/AliasManaging.js +49 -0
  11. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  12. package/lib-commonjs/generators/GenerateNM2.d.ts +11 -0
  13. package/lib-commonjs/generators/GenerateNM2.js +94 -0
  14. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  15. package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
  16. package/lib-commonjs/generators/ObjectTypes.js +53 -0
  17. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  18. package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
  19. package/lib-commonjs/generators/ParamTypes.js +114 -0
  20. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  21. package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
  22. package/lib-commonjs/generators/ReturnTypes.js +63 -0
  23. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  24. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  25. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  26. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  27. package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
  28. package/lib-commonjs/generators/ValidateMethods.js +70 -0
  29. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  30. package/package.json +21 -11
  31. package/src/Cli.ts +102 -54
  32. package/src/generators/AliasGen.ts +105 -0
  33. package/src/generators/AliasManaging.ts +75 -0
  34. package/src/generators/GenerateNM2.ts +58 -290
  35. package/src/generators/ObjectTypes.ts +70 -0
  36. package/src/generators/ParamTypes.ts +220 -0
  37. package/src/generators/ReturnTypes.ts +92 -0
  38. package/src/generators/ValidateConstants.ts +50 -0
  39. package/src/generators/ValidateMethods.ts +135 -0
  40. package/.eslintrc.js +0 -4
  41. package/.vscode/launch.json +0 -23
  42. package/jest.config.js +0 -1
  43. package/tsconfig.json +0 -5
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import {
10
+ NativeModuleReturnTypeAnnotation,
11
+ Nullable,
12
+ } from 'react-native-tscodegen';
13
+ import {
14
+ AliasMap,
15
+ getAliasCppName,
16
+ getAnonymousAliasCppName,
17
+ } from './AliasManaging';
18
+
19
+ function translateReturnType(
20
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
21
+ aliases: AliasMap,
22
+ baseAliasName: string,
23
+ ): string {
24
+ // avoid: Property 'type' does not exist on type 'never'
25
+ const returnType = type.type;
26
+ switch (type.type) {
27
+ case 'VoidTypeAnnotation':
28
+ case 'PromiseTypeAnnotation':
29
+ return 'void';
30
+ case 'StringTypeAnnotation':
31
+ return 'std::string';
32
+ case 'NumberTypeAnnotation':
33
+ case 'FloatTypeAnnotation':
34
+ case 'DoubleTypeAnnotation':
35
+ return 'double';
36
+ case 'Int32TypeAnnotation':
37
+ return 'int';
38
+ case 'BooleanTypeAnnotation':
39
+ return 'bool';
40
+ case 'ArrayTypeAnnotation':
41
+ if (type.elementType) {
42
+ return `std::vector<${translateReturnType(
43
+ type.elementType,
44
+ aliases,
45
+ `${baseAliasName}_element`,
46
+ )}>`;
47
+ } else {
48
+ return 'React::JSValueArray';
49
+ }
50
+ case 'GenericObjectTypeAnnotation':
51
+ return 'React::JSValue';
52
+ case 'ObjectTypeAnnotation':
53
+ return getAnonymousAliasCppName(aliases, baseAliasName, type);
54
+ case 'ReservedTypeAnnotation': {
55
+ // avoid: Property 'name' does not exist on type 'never'
56
+ const name = type.name;
57
+ // (#6597)
58
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
59
+ if (name !== 'RootTag')
60
+ throw new Error(
61
+ `Unknown reserved function: ${name} in translateReturnType`,
62
+ );
63
+ return 'double';
64
+ }
65
+ case 'TypeAliasTypeAnnotation':
66
+ return getAliasCppName(type.name);
67
+ case 'NullableTypeAnnotation':
68
+ return `std::optional<${translateReturnType(
69
+ type.typeAnnotation,
70
+ aliases,
71
+ baseAliasName,
72
+ )}>`;
73
+ default:
74
+ throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
75
+ }
76
+ }
77
+
78
+ export function translateSpecReturnType(
79
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
80
+ aliases: AliasMap,
81
+ baseAliasName: string,
82
+ ) {
83
+ return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
84
+ }
85
+
86
+ export function translateImplReturnType(
87
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
88
+ aliases: AliasMap,
89
+ baseAliasName: string,
90
+ ) {
91
+ return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
92
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import {NativeModuleSchema} from 'react-native-tscodegen';
10
+ import {AliasMap, getAnonymousAliasCppName} from './AliasManaging';
11
+
12
+ export function generateValidateConstants(
13
+ nativeModule: NativeModuleSchema,
14
+ aliases: AliasMap,
15
+ ): [string, string] | undefined {
16
+ const candidates = nativeModule.spec.properties.filter(
17
+ prop => prop.name === 'getConstants',
18
+ );
19
+ if (candidates.length === 0) {
20
+ return undefined;
21
+ }
22
+
23
+ const getConstant = candidates[0];
24
+ const funcType =
25
+ getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
26
+ ? getConstant.typeAnnotation.typeAnnotation
27
+ : getConstant.typeAnnotation;
28
+ if (
29
+ funcType.params.length > 0 ||
30
+ funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'
31
+ ) {
32
+ return undefined;
33
+ }
34
+
35
+ const constantType = funcType.returnTypeAnnotation;
36
+ if (constantType.properties.length === 0) {
37
+ return undefined;
38
+ }
39
+
40
+ const cppName = getAnonymousAliasCppName(aliases, 'Constants', constantType);
41
+
42
+ return [
43
+ ` TypedConstant<${cppName}>{0},`,
44
+ ` REACT_SHOW_CONSTANT_SPEC_ERRORS(
45
+ 0,
46
+ "${cppName}",
47
+ " REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\n"
48
+ " REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\n");`,
49
+ ];
50
+ }
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import {
10
+ NativeModuleFunctionTypeAnnotation,
11
+ NativeModulePropertyShape,
12
+ NativeModuleSchema,
13
+ } from 'react-native-tscodegen';
14
+ import {AliasMap} from './AliasManaging';
15
+ import {translateArgs, translateSpecArgs} from './ParamTypes';
16
+ import {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';
17
+
18
+ function isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {
19
+ return (
20
+ funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
21
+ funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation'
22
+ );
23
+ }
24
+
25
+ function isMethodReturnPromise(funcType: NativeModuleFunctionTypeAnnotation) {
26
+ return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';
27
+ }
28
+
29
+ function getPossibleMethodSignatures(
30
+ prop: NativeModulePropertyShape,
31
+ funcType: NativeModuleFunctionTypeAnnotation,
32
+ aliases: AliasMap,
33
+ baseAliasName: string,
34
+ ): string[] {
35
+ const args = translateArgs(funcType.params, aliases, baseAliasName);
36
+ if (isMethodReturnPromise(funcType)) {
37
+ // TODO: type of the promise could be provided in the future
38
+ args.push('React::ReactPromise<React::JSValue> &&result');
39
+ }
40
+
41
+ // TODO: be much more exhastive on the possible method signatures that can be used..
42
+ const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
43
+ prop.name
44
+ }) ${translateImplReturnType(
45
+ funcType.returnTypeAnnotation,
46
+ aliases,
47
+ baseAliasName,
48
+ )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
49
+
50
+ const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
51
+ prop.name
52
+ }) static ${translateImplReturnType(
53
+ funcType.returnTypeAnnotation,
54
+ aliases,
55
+ baseAliasName,
56
+ )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
57
+
58
+ return [sig, staticsig];
59
+ }
60
+
61
+ function translatePossibleMethodSignatures(
62
+ prop: NativeModulePropertyShape,
63
+ funcType: NativeModuleFunctionTypeAnnotation,
64
+ aliases: AliasMap,
65
+ baseAliasName: string,
66
+ ): string {
67
+ return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
68
+ .map(sig => `" ${sig}\\n"`)
69
+ .join('\n ');
70
+ }
71
+
72
+ function renderProperties(
73
+ properties: ReadonlyArray<NativeModulePropertyShape>,
74
+ aliases: AliasMap,
75
+ tuple: boolean,
76
+ ): string {
77
+ // TODO: generate code for constants
78
+ return properties
79
+ .filter(prop => prop.name !== 'getConstants')
80
+ .map((prop, index) => {
81
+ // TODO: prop.optional === true
82
+ // TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
83
+ const propAliasName = prop.name;
84
+ const funcType =
85
+ prop.typeAnnotation.type === 'NullableTypeAnnotation'
86
+ ? prop.typeAnnotation.typeAnnotation
87
+ : prop.typeAnnotation;
88
+
89
+ const traversedArgs = translateSpecArgs(
90
+ funcType.params,
91
+ aliases,
92
+ propAliasName,
93
+ );
94
+
95
+ const translatedReturnParam = translateSpecReturnType(
96
+ funcType.returnTypeAnnotation,
97
+ aliases,
98
+ propAliasName,
99
+ );
100
+
101
+ if (isMethodReturnPromise(funcType)) {
102
+ // TODO: type of the promise could be provided in the future
103
+ traversedArgs.push('Promise<React::JSValue>');
104
+ }
105
+
106
+ if (tuple) {
107
+ return ` ${
108
+ isMethodSync(funcType) ? 'Sync' : ''
109
+ }Method<${translatedReturnParam}(${traversedArgs.join(
110
+ ', ',
111
+ )}) noexcept>{${index}, L"${prop.name}"},`;
112
+ } else {
113
+ return ` REACT_SHOW_METHOD_SPEC_ERRORS(
114
+ ${index},
115
+ "${prop.name}",
116
+ ${translatePossibleMethodSignatures(
117
+ prop,
118
+ funcType,
119
+ aliases,
120
+ propAliasName,
121
+ )});`;
122
+ }
123
+ })
124
+ .join('\n');
125
+ }
126
+
127
+ export function generateValidateMethods(
128
+ nativeModule: NativeModuleSchema,
129
+ aliases: AliasMap,
130
+ ): [string, string] {
131
+ const properties = nativeModule.spec.properties;
132
+ const traversedProperties = renderProperties(properties, aliases, false);
133
+ const traversedPropertyTuples = renderProperties(properties, aliases, true);
134
+ return [traversedPropertyTuples, traversedProperties];
135
+ }
package/.eslintrc.js DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- extends: ['@rnw-scripts'],
3
- parserOptions: {tsconfigRootDir : __dirname},
4
- };
@@ -1,23 +0,0 @@
1
- {
2
- // Use IntelliSense to learn about possible attributes.
3
- // Hover to view descriptions of existing attributes.
4
- // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
- "version": "0.2.0",
6
- "configurations": [
7
- {
8
- "name": "vscode-jest-tests",
9
- "type": "node",
10
- "request": "launch",
11
- "runtimeArgs": [
12
- "--inspect-brk",
13
- "../../node_modules/jest/bin/jest.js",
14
- "--runInBand",
15
- "--config",
16
- "../@rnw-scripts/jest-debug-config/jest.debug.config.js",
17
- ],
18
- "console": "integratedTerminal",
19
- "internalConsoleOptions": "neverOpen",
20
- "port": 9229,
21
- },
22
- ]
23
- }
package/jest.config.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('@rnw-scripts/jest-unittest-config');
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "@rnw-scripts/ts-config",
3
- "include": ["src"],
4
- "exclude": ["node_modules"]
5
- }