From 2934399eddfbc9f2b5c881ee6822698bd20d0fd3 Mon Sep 17 00:00:00 2001 From: Vinay Harwani Date: Thu, 13 Oct 2022 02:41:28 -0700 Subject: [PATCH] Extracted TypeDeclarationMap from flow and typescript to a common file parsers/utils.js (#34951) Summary: This PR is a part of https://github.com/facebook/react-native/issues/34872. This PR extracts TypeDeclarationMap type from flow and typescript to a common file parsers/utils.js and updates all imports. ## Changelog [Internal] [Changed] - Extracted TypeDeclarationMap from flow and typescript to a common file parsers/utils.js. Pull Request resolved: https://github.com/facebook/react-native/pull/34951 Test Plan: yarn jest react-native-codegen Screenshot 2022-10-12 at 12 13 48 PM Reviewed By: NickGerleman Differential Revision: D40296917 Pulled By: cipolleschi fbshipit-source-id: d073daf0aadc291d5f9d00c003f0161af9e11319 --- .../src/parsers/flow/components/commands.js | 2 +- .../src/parsers/flow/components/componentsUtils.js | 2 +- .../src/parsers/flow/components/extends.js | 2 +- .../react-native-codegen/src/parsers/flow/components/index.js | 2 +- .../react-native-codegen/src/parsers/flow/components/props.js | 2 +- .../react-native-codegen/src/parsers/flow/components/states.js | 2 +- .../react-native-codegen/src/parsers/flow/modules/index.js | 3 +-- packages/react-native-codegen/src/parsers/flow/utils.js | 3 +-- .../src/parsers/typescript/components/commands.js | 2 +- .../src/parsers/typescript/components/componentsUtils.js | 2 +- .../src/parsers/typescript/components/events.js | 2 +- .../src/parsers/typescript/components/extends.js | 2 +- .../src/parsers/typescript/components/index.js | 2 +- .../src/parsers/typescript/components/props.js | 2 +- .../src/parsers/typescript/components/states.js | 2 +- .../src/parsers/typescript/modules/index.js | 3 +-- .../src/parsers/typescript/parseTopLevelType.js | 2 +- packages/react-native-codegen/src/parsers/typescript/utils.js | 3 +-- packages/react-native-codegen/src/parsers/utils.js | 2 ++ 19 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/components/commands.js b/packages/react-native-codegen/src/parsers/flow/components/commands.js index 163b5feb5e6714..e829582ce3116d 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/commands.js +++ b/packages/react-native-codegen/src/parsers/flow/components/commands.js @@ -14,7 +14,7 @@ import type { NamedShape, CommandTypeAnnotation, } from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; const {getValueFromTypes} = require('../utils.js'); diff --git a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js index 49de49da57ff7c..c448decc42d9fc 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js +++ b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js @@ -11,9 +11,9 @@ 'use strict'; import type {ASTNode} from '../utils'; -import type {TypeDeclarationMap} from '../utils.js'; import type {NamedShape} from '../../../CodegenSchema.js'; const {getValueFromTypes} = require('../utils.js'); +import type {TypeDeclarationMap} from '../../utils'; function getProperties( typeName: string, diff --git a/packages/react-native-codegen/src/parsers/flow/components/extends.js b/packages/react-native-codegen/src/parsers/flow/components/extends.js index 5cd021512f1d77..6f81f7b454080f 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/extends.js +++ b/packages/react-native-codegen/src/parsers/flow/components/extends.js @@ -11,7 +11,7 @@ 'use strict'; import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) { if (!prop.argument) { 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 b46c3b95fff02e..364a279d8f8f9a 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -9,7 +9,7 @@ */ 'use strict'; -import type {TypeDeclarationMap} from '../utils'; +import type {TypeDeclarationMap} from '../../utils'; import type {CommandOptions} from './options'; import type {ComponentSchemaBuilderConfig} from './schema.js'; 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 26d29216f4b67d..05cdf06b02230c 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/props.js +++ b/packages/react-native-codegen/src/parsers/flow/components/props.js @@ -17,7 +17,7 @@ const { } = require('./componentsUtils.js'); import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; // $FlowFixMe[unclear-type] there's no flowtype for ASTs type PropAST = Object; diff --git a/packages/react-native-codegen/src/parsers/flow/components/states.js b/packages/react-native-codegen/src/parsers/flow/components/states.js index c80b3809374b6d..936aa3f11521cc 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/states.js +++ b/packages/react-native-codegen/src/parsers/flow/components/states.js @@ -17,7 +17,7 @@ const { } = require('./componentsUtils.js'); import type {StateTypeAnnotation, NamedShape} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; // $FlowFixMe[unclear-type] there's no flowtype for ASTs type PropAST = Object; diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 90ab99c4025285..169a3aeb9f1a96 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -22,8 +22,7 @@ import type { Nullable, } from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; -import type {ParserErrorCapturer} from '../../utils'; +import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js'; const { diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index 26272346f8305d..1954a9bd99da12 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -10,7 +10,7 @@ 'use strict'; -import type {TypeAliasResolutionStatus} from '../utils'; +import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; /** * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias @@ -20,7 +20,6 @@ import type {TypeAliasResolutionStatus} from '../utils'; * * TODO(T71778680): Flow type AST Nodes */ -export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe}; function getTypes(ast: $FlowFixMe): TypeDeclarationMap { return ast.body.reduce((types, node) => { diff --git a/packages/react-native-codegen/src/parsers/typescript/components/commands.js b/packages/react-native-codegen/src/parsers/typescript/components/commands.js index fb827d2cbc98c1..23e300c39e2f06 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/commands.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/commands.js @@ -14,7 +14,7 @@ import type { NamedShape, CommandTypeAnnotation, } from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; const {parseTopLevelType} = require('../parseTopLevelType'); type EventTypeAST = Object; diff --git a/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js b/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js index a0b21bd8186a20..ff8661b28a69b1 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js @@ -10,9 +10,9 @@ 'use strict'; import type {ASTNode} from '../utils'; -import type {TypeDeclarationMap} from '../utils.js'; import type {NamedShape} from '../../../CodegenSchema.js'; const {parseTopLevelType} = require('../parseTopLevelType'); +import type {TypeDeclarationMap} from '../../utils'; function getProperties( typeName: string, diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 4ac105455570dc..94114362255e38 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -15,9 +15,9 @@ import type { NamedShape, EventTypeAnnotation, } from '../../../CodegenSchema.js'; +import type {TypeDeclarationMap} from '../../utils'; const {flattenProperties} = require('./componentsUtils'); const {parseTopLevelType} = require('../parseTopLevelType'); -import type {TypeDeclarationMap} from '../utils.js'; function getPropertyType( /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's diff --git a/packages/react-native-codegen/src/parsers/typescript/components/extends.js b/packages/react-native-codegen/src/parsers/typescript/components/extends.js index 2b16a91a5d1476..84ad505dd1fcb9 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/extends.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/extends.js @@ -11,7 +11,7 @@ 'use strict'; import type {ExtendsPropsShape} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) { if (!prop.expression) { diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index e1838570baaf93..060063453ca64e 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -9,7 +9,7 @@ */ 'use strict'; -import type {TypeDeclarationMap} from '../utils'; +import type {TypeDeclarationMap} from '../../utils'; import type {CommandOptions} from './options'; import type {ComponentSchemaBuilderConfig} from './schema.js'; diff --git a/packages/react-native-codegen/src/parsers/typescript/components/props.js b/packages/react-native-codegen/src/parsers/typescript/components/props.js index a612296bc9fbef..b4a201816325d1 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/props.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/props.js @@ -16,7 +16,7 @@ const { } = require('./componentsUtils.js'); import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; // $FlowFixMe[unclear-type] there's no flowtype for ASTs type PropAST = Object; diff --git a/packages/react-native-codegen/src/parsers/typescript/components/states.js b/packages/react-native-codegen/src/parsers/typescript/components/states.js index 5b1b01551c4aa2..739548ceade831 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/states.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/states.js @@ -16,7 +16,7 @@ const { } = require('./componentsUtils.js'); import type {StateTypeAnnotation, NamedShape} from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; +import type {TypeDeclarationMap} from '../../utils'; // $FlowFixMe[unclear-type] there's no flowtype for ASTs type PropAST = Object; diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 8b4da89cf5a6b3..522eb134b36143 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -22,8 +22,7 @@ import type { Nullable, } from '../../../CodegenSchema.js'; -import type {TypeDeclarationMap} from '../utils.js'; -import type {ParserErrorCapturer} from '../../utils'; +import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js'; const { diff --git a/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js b/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js index b51da605f0b991..8ec8580e66c91a 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js +++ b/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js @@ -10,7 +10,7 @@ 'use strict'; -import type {TypeDeclarationMap} from './utils.js'; +import type {TypeDeclarationMap} from '../utils'; export type LegalDefaultValues = string | number | boolean | null; diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 82fefa66dcfd1e..af88bcb4411568 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -10,14 +10,13 @@ 'use strict'; -import type {TypeAliasResolutionStatus} from '../utils'; +import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; const {parseTopLevelType} = require('./parseTopLevelType'); /** * TODO(T108222691): Use flow-types for @babel/parser */ -export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe}; function getTypes(ast: $FlowFixMe): TypeDeclarationMap { return ast.body.reduce((types, node) => { diff --git a/packages/react-native-codegen/src/parsers/utils.js b/packages/react-native-codegen/src/parsers/utils.js index 39b6e2ce5b1f34..83f374d8252313 100644 --- a/packages/react-native-codegen/src/parsers/utils.js +++ b/packages/react-native-codegen/src/parsers/utils.js @@ -14,6 +14,8 @@ const {ParserError} = require('./errors'); const path = require('path'); +export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe}; + export type TypeAliasResolutionStatus = | $ReadOnly<{ successful: true,