Skip to content

Commit 633498f

Browse files
youeddfacebook-github-bot
authored andcommitted
refactor module's platform verification (#34961)
Summary: Part of #34872 > [Assigned to youedd] Extract the nameToValidate logic ([Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L704-L713), [TypeScript](https://github.com/facebook/react-native/blob/f353119113d6fc85491765ba1e90ac83cb00fd61/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L738-L747)) into a shared function into the parser/utils.js file. Notice that you may need a callback to update the cxx boolean. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Changed] [Internal] - refactor parser module platform verification Pull Request resolved: #34961 Test Plan: `yarn jest react-native-codegen` ![image](https://user-images.githubusercontent.com/19575877/195425654-46f9e560-efd3-4945-b913-c6d9f6bb7ec2.png) Reviewed By: cipolleschi Differential Revision: D40319245 Pulled By: skinsshark fbshipit-source-id: bc36cdcfa06047e1acee0d638f5756b6462d76d1
1 parent a63a4fa commit 633498f

File tree

4 files changed

+113
-26
lines changed

4 files changed

+113
-26
lines changed

packages/react-native-codegen/src/parsers/__tests__/utils-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
const {
1515
extractNativeModuleName,
1616
createParserErrorCapturer,
17+
verifyPlatforms,
1718
visit,
1819
} = require('../utils.js');
1920
const {ParserError} = require('../errors');
@@ -117,6 +118,71 @@ describe('createParserErrorCapturer', () => {
117118
});
118119
});
119120

121+
describe('verifyPlatforms', () => {
122+
it('exclude android given an iOS only module', () => {
123+
let result = verifyPlatforms('NativeSampleTurboModule', [
124+
'SampleTurboModuleIOS',
125+
]);
126+
127+
expect(result.cxxOnly).toBe(false);
128+
expect(result.excludedPlatforms).toEqual(['android']);
129+
130+
result = verifyPlatforms('NativeSampleTurboModuleIOS', [
131+
'SampleTurboModule',
132+
]);
133+
expect(result.cxxOnly).toBe(false);
134+
expect(result.excludedPlatforms).toEqual(['android']);
135+
136+
result = verifyPlatforms('NativeSampleTurboModuleIOS', [
137+
'SampleTurboModuleIOS',
138+
]);
139+
expect(result.cxxOnly).toBe(false);
140+
expect(result.excludedPlatforms).toEqual(['android']);
141+
});
142+
143+
it('exclude iOS given an android only module', () => {
144+
let result = verifyPlatforms('NativeSampleTurboModule', [
145+
'SampleTurboModuleAndroid',
146+
]);
147+
148+
expect(result.cxxOnly).toBe(false);
149+
expect(result.excludedPlatforms).toEqual(['iOS']);
150+
151+
result = verifyPlatforms('NativeSampleTurboModuleAndroid', [
152+
'SampleTurboModule',
153+
]);
154+
expect(result.cxxOnly).toBe(false);
155+
expect(result.excludedPlatforms).toEqual(['iOS']);
156+
157+
result = verifyPlatforms('NativeSampleTurboModuleAndroid', [
158+
'SampleTurboModuleAndroid',
159+
]);
160+
expect(result.cxxOnly).toBe(false);
161+
expect(result.excludedPlatforms).toEqual(['iOS']);
162+
});
163+
164+
it('exclude iOS and android given a Cxx only module', () => {
165+
let result = verifyPlatforms('NativeSampleTurboModule', [
166+
'SampleTurboModuleCxx',
167+
]);
168+
169+
expect(result.cxxOnly).toBe(true);
170+
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
171+
172+
result = verifyPlatforms('NativeSampleTurboModuleCxx', [
173+
'SampleTurboModule',
174+
]);
175+
expect(result.cxxOnly).toBe(true);
176+
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
177+
178+
result = verifyPlatforms('NativeSampleTurboModuleCxx', [
179+
'SampleTurboModuleCxx',
180+
]);
181+
expect(result.cxxOnly).toBe(true);
182+
expect(result.excludedPlatforms).toEqual(['iOS', 'android']);
183+
});
184+
});
185+
120186
describe('visit', () => {
121187
describe('when the astNode is null', () => {
122188
it("doesn't call the visitor function", () => {

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const {
6666
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6767
IncorrectModuleRegistryCallArgumentTypeParserError,
6868
} = require('../../errors.js');
69+
const {verifyPlatforms} = require('../../utils');
6970

7071
const {
7172
throwIfModuleInterfaceNotFound,
@@ -691,19 +692,10 @@ function buildModuleSchema(
691692
// Eventually this should be made explicit in the Flow type itself.
692693
// Also check the hasteModuleName for platform suffix.
693694
// Note: this shape is consistent with ComponentSchema.
694-
let cxxOnly = false;
695-
const excludedPlatforms = [];
696-
const namesToValidate = [...moduleNames, hasteModuleName];
697-
namesToValidate.forEach(name => {
698-
if (name.endsWith('Android')) {
699-
excludedPlatforms.push('iOS');
700-
} else if (name.endsWith('IOS')) {
701-
excludedPlatforms.push('android');
702-
} else if (name.endsWith('Cxx')) {
703-
cxxOnly = true;
704-
excludedPlatforms.push('iOS', 'android');
705-
}
706-
});
695+
const {cxxOnly, excludedPlatforms} = verifyPlatforms(
696+
hasteModuleName,
697+
moduleNames,
698+
);
707699

708700
// $FlowFixMe[missing-type-arg]
709701
return (moduleSpec.body.properties: $ReadOnlyArray<$FlowFixMe>)

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const {
6666
UnsupportedObjectPropertyValueTypeAnnotationParserError,
6767
IncorrectModuleRegistryCallArgumentTypeParserError,
6868
} = require('../../errors.js');
69+
const {verifyPlatforms} = require('../../utils');
6970

7071
const {
7172
throwIfUntypedModule,
@@ -705,19 +706,10 @@ function buildModuleSchema(
705706
// Eventually this should be made explicit in the Flow type itself.
706707
// Also check the hasteModuleName for platform suffix.
707708
// Note: this shape is consistent with ComponentSchema.
708-
let cxxOnly = false;
709-
const excludedPlatforms = [];
710-
const namesToValidate = [...moduleNames, hasteModuleName];
711-
namesToValidate.forEach(name => {
712-
if (name.endsWith('Android')) {
713-
excludedPlatforms.push('iOS');
714-
} else if (name.endsWith('IOS')) {
715-
excludedPlatforms.push('android');
716-
} else if (name.endsWith('Cxx')) {
717-
cxxOnly = true;
718-
excludedPlatforms.push('iOS', 'android');
719-
}
720-
});
709+
const {cxxOnly, excludedPlatforms} = verifyPlatforms(
710+
hasteModuleName,
711+
moduleNames,
712+
);
721713

722714
// $FlowFixMe[missing-type-arg]
723715
return (moduleSpec.body.body: $ReadOnlyArray<$FlowFixMe>)

packages/react-native-codegen/src/parsers/utils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,42 @@ function createParserErrorCapturer(): [
5454
return [errors, guard];
5555
}
5656

57+
function verifyPlatforms(
58+
hasteModuleName: string,
59+
moduleNames: string[],
60+
): $ReadOnly<{
61+
cxxOnly: boolean,
62+
excludedPlatforms: Array<'iOS' | 'android'>,
63+
}> {
64+
let cxxOnly = false;
65+
const excludedPlatforms = new Set<'iOS' | 'android'>();
66+
const namesToValidate = [...moduleNames, hasteModuleName];
67+
68+
namesToValidate.forEach(name => {
69+
if (name.endsWith('Android')) {
70+
excludedPlatforms.add('iOS');
71+
return;
72+
}
73+
74+
if (name.endsWith('IOS')) {
75+
excludedPlatforms.add('android');
76+
return;
77+
}
78+
79+
if (name.endsWith('Cxx')) {
80+
cxxOnly = true;
81+
excludedPlatforms.add('iOS');
82+
excludedPlatforms.add('android');
83+
return;
84+
}
85+
});
86+
87+
return {
88+
cxxOnly,
89+
excludedPlatforms: Array.from(excludedPlatforms),
90+
};
91+
}
92+
5793
// TODO(T108222691): Use flow-types for @babel/parser
5894
function visit(
5995
astNode: $FlowFixMe,
@@ -86,5 +122,6 @@ function visit(
86122
module.exports = {
87123
extractNativeModuleName,
88124
createParserErrorCapturer,
125+
verifyPlatforms,
89126
visit,
90127
};

0 commit comments

Comments
 (0)