Skip to content

Commit

Permalink
Move verifyProprsNotAlreadyDefined Function To parsers-commons.js (#3…
Browse files Browse the repository at this point in the history
…7963)

Summary:
Move the `verifyProprsNotAlreadyDefined` functions [from Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js#L220-L229) and [from TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js#LL486-L495) to the parsers-commons.js file. Use the new function in place of the others.

## Changelog:

[Internal] [Changed] - Moved `verifyProprsNotAlreadyDefined` to `parsers-commons.js`

Pull Request resolved: #37963

Reviewed By: cipolleschi

Differential Revision: D46841711

Pulled By: rshest

fbshipit-source-id: 4c7e85d8e184126d16c520b0e56b4c291babff06
  • Loading branch information
branaust authored and facebook-github-bot committed Jun 20, 2023
1 parent 02771ec commit a108dcb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const {getValueFromTypes} = require('../utils.js');
const {verifyPropNotAlreadyDefined} = require('../../parsers-commons');
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
import type {BuildSchemaFN, Parser} from '../../parser';

Expand Down Expand Up @@ -206,17 +207,6 @@ function flattenProperties(
.filter(Boolean);
}

function verifyPropNotAlreadyDefined(
props: $ReadOnlyArray<PropAST>,
needleProp: PropAST,
) {
const propName = needleProp.key.name;
const foundProp = props.some(prop => prop.key.name === propName);
if (foundProp) {
throw new Error(`A prop was already defined with the name ${propName}`);
}
}

function getTypeAnnotation<+T>(
name: string,
annotation: $FlowFixMe | ASTNode,
Expand Down
12 changes: 12 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,17 @@ function buildPropertiesForEvent(
return getPropertyType(name, optional, typeAnnotation, parser);
}

function verifyPropNotAlreadyDefined(
props: $ReadOnlyArray<PropAST>,
needleProp: PropAST,
) {
const propName = needleProp.key.name;
const foundProp = props.some(prop => prop.key.name === propName);
if (foundProp) {
throw new Error(`A prop was already defined with the name ${propName}`);
}
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand Down Expand Up @@ -1099,4 +1110,5 @@ module.exports = {
handleGenericTypeAnnotation,
getTypeResolutionStatus,
buildPropertiesForEvent,
verifyPropNotAlreadyDefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
parseTopLevelType,
flattenIntersectionType,
} = require('../parseTopLevelType');
const {verifyPropNotAlreadyDefined} = require('../../parsers-commons');
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
import type {BuildSchemaFN, Parser} from '../../parser';

Expand Down Expand Up @@ -453,17 +454,6 @@ function getSchemaInfo(
};
}

function verifyPropNotAlreadyDefined(
props: $ReadOnlyArray<PropAST>,
needleProp: PropAST,
) {
const propName = needleProp.key.name;
const foundProp = props.some(prop => prop.key.name === propName);
if (foundProp) {
throw new Error(`A prop was already defined with the name ${propName}`);
}
}

function flattenProperties(
typeDefinition: $ReadOnlyArray<PropAST>,
types: TypeDeclarationMap,
Expand Down

0 comments on commit a108dcb

Please sign in to comment.