Skip to content

Commit 43986e8

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
chore: Add functionTypeAnnotation to codegen Parser class (#36468)
Summary: This PR adds a `functionTypeAnnotation` function to the codegen Parser class and implements it in the Flow and TypeScript parsers as requested on #34872, refactoring the `throwIfModuleTypeIsUnsupported` function to accept a `Parser` instead of a `ParserType` and use the newly created function instead of the `if (language)` logic. ## Changelog [Internal] [Added] - Add `functionTypeAnnotation` to codegen Parser class Pull Request resolved: #36468 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/224882017-2c31f326-047d-4c21-91e0-3396203ce1c5.png) Reviewed By: cipolleschi Differential Revision: D44056853 Pulled By: rshest fbshipit-source-id: f22b9efe53df16f7916eb1a9cea8b9531f1194f5
1 parent b893582 commit 43986e8

File tree

7 files changed

+34
-26
lines changed

7 files changed

+34
-26
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,73 +554,71 @@ describe('throwIfUntypedModule', () => {
554554
describe('throwIfModuleTypeIsUnsupported', () => {
555555
const hasteModuleName = 'moduleName';
556556
const property = {value: 'value', key: {name: 'name'}};
557+
const flowParser = new FlowParser();
558+
const typescriptParser = new TypeScriptParser();
559+
557560
it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
558561
const value = {type: 'FunctionTypeAnnotation'};
559-
const language = 'Flow';
560562

561563
expect(() => {
562564
throwIfModuleTypeIsUnsupported(
563565
hasteModuleName,
564566
property.value,
565567
property.key.name,
566568
value.type,
567-
language,
569+
flowParser,
568570
);
569571
}).not.toThrow(UnsupportedModulePropertyParserError);
570572
});
571573
it('throw error if module type is unsupported in Flow', () => {
572574
const value = {type: ''};
573-
const language = 'Flow';
574575

575576
expect(() => {
576577
throwIfModuleTypeIsUnsupported(
577578
hasteModuleName,
578579
property.value,
579580
property.key.name,
580581
value.type,
581-
language,
582+
flowParser,
582583
);
583584
}).toThrow(UnsupportedModulePropertyParserError);
584585
});
585586
it("don't throw error if module type is TSFunctionType in TypeScript", () => {
586587
const value = {type: 'TSFunctionType'};
587-
const language = 'TypeScript';
588588

589589
expect(() => {
590590
throwIfModuleTypeIsUnsupported(
591591
hasteModuleName,
592592
property.value,
593593
property.key.name,
594594
value.type,
595-
language,
595+
typescriptParser,
596596
);
597597
}).not.toThrow(UnsupportedModulePropertyParserError);
598598
});
599599
it("don't throw error if module type is TSMethodSignature in TypeScript", () => {
600600
const value = {type: 'TSMethodSignature'};
601-
const language = 'TypeScript';
602601

603602
expect(() => {
604603
throwIfModuleTypeIsUnsupported(
605604
hasteModuleName,
606605
property.value,
607606
property.key.name,
608607
value.type,
609-
language,
608+
typescriptParser,
610609
);
611610
}).not.toThrow(UnsupportedModulePropertyParserError);
612611
});
613612
it('throw error if module type is unsupported in TypeScript', () => {
614613
const value = {type: ''};
615-
const language = 'TypeScript';
616614

617615
expect(() => {
618616
throwIfModuleTypeIsUnsupported(
619617
hasteModuleName,
620618
property.value,
621619
property.key.name,
622620
value.type,
623-
language,
621+
typescriptParser,
624622
);
625623
}).toThrow(UnsupportedModulePropertyParserError);
626624
});

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,15 @@ function throwIfModuleTypeIsUnsupported(
160160
propertyValue: $FlowFixMe,
161161
propertyName: string,
162162
propertyValueType: string,
163-
language: ParserType,
163+
parser: Parser,
164164
) {
165-
if (language === 'Flow' && propertyValueType !== 'FunctionTypeAnnotation') {
166-
throw new UnsupportedModulePropertyParserError(
167-
nativeModuleName,
168-
propertyValue,
169-
propertyName,
170-
propertyValueType,
171-
language,
172-
);
173-
} else if (
174-
language === 'TypeScript' &&
175-
propertyValueType !== 'TSFunctionType' &&
176-
propertyValueType !== 'TSMethodSignature'
177-
) {
165+
if (!parser.functionTypeAnnotation(propertyValueType)) {
178166
throw new UnsupportedModulePropertyParserError(
179167
nativeModuleName,
180168
propertyValue,
181169
propertyName,
182170
propertyValueType,
183-
language,
171+
parser.language(),
184172
);
185173
}
186174
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ class FlowParser implements Parser {
291291
};
292292
});
293293
}
294+
295+
functionTypeAnnotation(propertyValueType: string): boolean {
296+
return propertyValueType === 'FunctionTypeAnnotation';
297+
}
294298
}
295299

296300
module.exports = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,11 @@ export interface Parser {
204204
tryParse: ParserErrorCapturer,
205205
cxxOnly: boolean,
206206
): Array<$FlowFixMe>;
207+
208+
/**
209+
* Given a propertyValueType, it returns a boolean specifying if the property is a function type annotation.
210+
* @parameter propertyValueType: the propertyValueType.
211+
* @returns: a boolean specifying if the property is a function type annotation.
212+
*/
213+
functionTypeAnnotation(propertyValueType: string): boolean;
207214
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,8 @@ export class MockedParser implements Parser {
209209
},
210210
];
211211
}
212+
213+
functionTypeAnnotation(propertyValueType: string): boolean {
214+
return propertyValueType === 'FunctionTypeAnnotation';
215+
}
212216
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function buildPropertySchema(
328328
property.value,
329329
key.name,
330330
value.type,
331-
parser.language(),
331+
parser,
332332
);
333333

334334
return {

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ class TypeScriptParser implements Parser {
277277
};
278278
});
279279
}
280+
281+
functionTypeAnnotation(propertyValueType: string): boolean {
282+
return (
283+
propertyValueType === 'TSFunctionType' ||
284+
propertyValueType === 'TSMethodSignature'
285+
);
286+
}
280287
}
281288

282289
module.exports = {

0 commit comments

Comments
 (0)