From 23740e307951d1fd65f36002f5898313ac26ab5e Mon Sep 17 00:00:00 2001 From: Pranav Yadav Date: Thu, 13 Apr 2023 17:10:57 +0530 Subject: [PATCH] refactor: merge `getExtendsProps` & `getProps` fns - rm `extends.js` since, this diff simplifies it's dependents, - it's fns are moved to `props.js` only; to simplify further. --- .../src/parsers/flow/components/extends.js | 66 ---------------- .../src/parsers/flow/components/index.js | 7 +- .../src/parsers/flow/components/props.js | 78 +++++++++++++++++-- 3 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 packages/react-native-codegen/src/parsers/flow/components/extends.js diff --git a/packages/react-native-codegen/src/parsers/flow/components/extends.js b/packages/react-native-codegen/src/parsers/flow/components/extends.js deleted file mode 100644 index d53147d9d4f5..000000000000 --- a/packages/react-native-codegen/src/parsers/flow/components/extends.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict - * @format - */ - -'use strict'; - -import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../../utils'; - -function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) { - if (!prop.argument) { - console.log('null', prop); - } - const name = prop.argument.id.name; - - if (types[name] != null) { - // This type is locally defined in the file - return null; - } - - switch (name) { - case 'ViewProps': - return { - type: 'ReactNativeBuiltInType', - knownTypeName: 'ReactNativeCoreViewProps', - }; - default: { - throw new Error(`Unable to handle prop spread: ${name}`); - } - } -} - -function removeKnownExtends( - typeDefinition: $ReadOnlyArray, - types: TypeDeclarationMap, -): $ReadOnlyArray { - return typeDefinition.filter( - prop => - prop.type !== 'ObjectTypeSpreadProperty' || - extendsForProp(prop, types) === null, - ); -} - -// $FlowFixMe[unclear-type] there's no flowtype for ASTs -type PropsAST = Object; - -function getExtendsProps( - typeDefinition: $ReadOnlyArray, - types: TypeDeclarationMap, -): $ReadOnlyArray { - return typeDefinition - .filter(prop => prop.type === 'ObjectTypeSpreadProperty') - .map(prop => extendsForProp(prop, types)) - .filter(Boolean); -} - -module.exports = { - getExtendsProps, - removeKnownExtends, -}; diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index a216417f9cbc..ab3aa436de3f 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -14,7 +14,6 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js'; const {getCommands} = require('./commands'); const {getEvents} = require('./events'); -const {getExtendsProps, removeKnownExtends} = require('./extends'); const {getProps} = require('./props'); const {getProperties} = require('./componentsUtils.js'); const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils'); @@ -126,14 +125,10 @@ function buildComponentSchema( const types = parser.getTypes(ast); const propProperties = getProperties(propsTypeName, types); - const commandProperties = getCommandProperties(ast, parser); + const {extendsProps, props} = getProps(propProperties, types); - const extendsProps = getExtendsProps(propProperties, types); const options = getOptions(optionsExpression); - - const nonExtendsProps = removeKnownExtends(propProperties, types); - const props = getProps(nonExtendsProps, types); const events = getEvents(propProperties, types); const commands = getCommands(commandProperties, types); diff --git a/packages/react-native-codegen/src/parsers/flow/components/props.js b/packages/react-native-codegen/src/parsers/flow/components/props.js index 70ff36e8526e..38cba3b62851 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/props.js +++ b/packages/react-native-codegen/src/parsers/flow/components/props.js @@ -10,18 +10,27 @@ 'use strict'; +import type { + ExtendsPropsShape, + NamedShape, + PropTypeAnnotation, +} from '../../../CodegenSchema.js'; +import type {TypeDeclarationMap} from '../../utils'; + const { flattenProperties, getSchemaInfo, getTypeAnnotation, } = require('./componentsUtils.js'); -import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../../utils'; - // $FlowFixMe[unclear-type] there's no flowtype for ASTs type PropAST = Object; +type ExtendsForProp = null | { + type: 'ReactNativeBuiltInType', + knownTypeName: 'ReactNativeCoreViewProps', +}; + function buildPropSchema( property: PropAST, types: TypeDeclarationMap, @@ -46,13 +55,72 @@ function buildPropSchema( }; } +function extendsForProp( + prop: PropAST, + types: TypeDeclarationMap, +): ExtendsForProp { + if (!prop.argument) { + console.log('null', prop); + } + const name = prop.argument.id.name; + + if (types[name] != null) { + // This type is locally defined in the file + return null; + } + + switch (name) { + case 'ViewProps': + return { + type: 'ReactNativeBuiltInType', + knownTypeName: 'ReactNativeCoreViewProps', + }; + default: { + throw new Error(`Unable to handle prop spread: ${name}`); + } + } +} + +function removeKnownExtends( + typeDefinition: $ReadOnlyArray, + types: TypeDeclarationMap, +): $ReadOnlyArray { + return typeDefinition.filter( + prop => + prop.type !== 'ObjectTypeSpreadProperty' || + extendsForProp(prop, types) === null, + ); +} + +function getExtendsProps( + typeDefinition: $ReadOnlyArray, + types: TypeDeclarationMap, +): $ReadOnlyArray { + return typeDefinition + .filter(prop => prop.type === 'ObjectTypeSpreadProperty') + .map(prop => extendsForProp(prop, types)) + .filter(Boolean); +} +/** + * Extracts the `props` and `extendsProps` (props with `extends` syntax) + * from a type definition AST. + */ function getProps( typeDefinition: $ReadOnlyArray, types: TypeDeclarationMap, -): $ReadOnlyArray> { - return flattenProperties(typeDefinition, types) +): { + props: $ReadOnlyArray>, + extendsProps: $ReadOnlyArray, +} { + const nonExtendsProps = removeKnownExtends(typeDefinition, types); + const props = flattenProperties(nonExtendsProps, types) .map(property => buildPropSchema(property, types)) .filter(Boolean); + + return { + props, + extendsProps: getExtendsProps(typeDefinition, types), + }; } module.exports = {