Skip to content

Commit

Permalink
chore: Add functionTypeAnnotation to codegen Parser class (#36468)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Mar 15, 2023
1 parent b893582 commit 43986e8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -554,73 +554,71 @@ describe('throwIfUntypedModule', () => {
describe('throwIfModuleTypeIsUnsupported', () => {
const hasteModuleName = 'moduleName';
const property = {value: 'value', key: {name: 'name'}};
const flowParser = new FlowParser();
const typescriptParser = new TypeScriptParser();

it("don't throw error if module type is FunctionTypeAnnotation in Flow", () => {
const value = {type: 'FunctionTypeAnnotation'};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
flowParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in Flow', () => {
const value = {type: ''};
const language = 'Flow';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
flowParser,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSFunctionType in TypeScript", () => {
const value = {type: 'TSFunctionType'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it("don't throw error if module type is TSMethodSignature in TypeScript", () => {
const value = {type: 'TSMethodSignature'};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).not.toThrow(UnsupportedModulePropertyParserError);
});
it('throw error if module type is unsupported in TypeScript', () => {
const value = {type: ''};
const language = 'TypeScript';

expect(() => {
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
typescriptParser,
);
}).toThrow(UnsupportedModulePropertyParserError);
});
Expand Down
18 changes: 3 additions & 15 deletions packages/react-native-codegen/src/parsers/error-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,15 @@ function throwIfModuleTypeIsUnsupported(
propertyValue: $FlowFixMe,
propertyName: string,
propertyValueType: string,
language: ParserType,
parser: Parser,
) {
if (language === 'Flow' && propertyValueType !== 'FunctionTypeAnnotation') {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
);
} else if (
language === 'TypeScript' &&
propertyValueType !== 'TSFunctionType' &&
propertyValueType !== 'TSMethodSignature'
) {
if (!parser.functionTypeAnnotation(propertyValueType)) {
throw new UnsupportedModulePropertyParserError(
nativeModuleName,
propertyValue,
propertyName,
propertyValueType,
language,
parser.language(),
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ class FlowParser implements Parser {
};
});
}

functionTypeAnnotation(propertyValueType: string): boolean {
return propertyValueType === 'FunctionTypeAnnotation';
}
}

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ export interface Parser {
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe>;

/**
* Given a propertyValueType, it returns a boolean specifying if the property is a function type annotation.
* @parameter propertyValueType: the propertyValueType.
* @returns: a boolean specifying if the property is a function type annotation.
*/
functionTypeAnnotation(propertyValueType: string): boolean;
}
4 changes: 4 additions & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,8 @@ export class MockedParser implements Parser {
},
];
}

functionTypeAnnotation(propertyValueType: string): boolean {
return propertyValueType === 'FunctionTypeAnnotation';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function buildPropertySchema(
property.value,
key.name,
value.type,
parser.language(),
parser,
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ class TypeScriptParser implements Parser {
};
});
}

functionTypeAnnotation(propertyValueType: string): boolean {
return (
propertyValueType === 'TSFunctionType' ||
propertyValueType === 'TSMethodSignature'
);
}
}

module.exports = {
Expand Down

0 comments on commit 43986e8

Please sign in to comment.