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 1a21c605b1d407..c05e137af993ab 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -20,7 +20,6 @@ import type { } from '../../../CodegenSchema'; import type {Parser} from '../../parser'; -const {resolveTypeAnnotation} = require('../utils'); import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; const { @@ -59,8 +58,9 @@ function translateTypeAnnotation( cxxOnly: boolean, parser: Parser, ): Nullable { + const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN(); const {nullable, typeAnnotation, typeResolutionStatus} = - resolveTypeAnnotation(flowTypeAnnotation, types, parser); + resolveTypeAnnotationFN(flowTypeAnnotation, types, parser); switch (typeAnnotation.type) { case 'GenericTypeAnnotation': { diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 8ebfd3093f8db6..5e5c36a12f277b 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -35,7 +35,6 @@ import type { TypeResolutionStatus, } from '../utils'; -const {resolveTypeAnnotation} = require('./utils'); const invariant = require('invariant'); const { @@ -65,7 +64,6 @@ class FlowParser implements Parser { typeAlias: string = 'TypeAlias'; enumDeclaration: string = 'EnumDeclaration'; interfaceDeclaration: string = 'InterfaceDeclaration'; - nullLiteralTypeAnnotation: string = 'NullLiteralTypeAnnotation'; isProperty(property: $FlowFixMe): boolean { @@ -376,6 +374,7 @@ class FlowParser implements Parser { getResolvedTypeAnnotation( typeAnnotation: $FlowFixMe, types: TypeDeclarationMap, + parser: Parser, ): { nullable: boolean, typeAnnotation: $FlowFixMe, @@ -409,7 +408,7 @@ class FlowParser implements Parser { } switch (resolvedTypeAnnotation.type) { - case 'TypeAlias': { + case parser.typeAlias: { typeResolutionStatus = { successful: true, type: 'alias', @@ -418,7 +417,7 @@ class FlowParser implements Parser { node = resolvedTypeAnnotation.right; break; } - case 'EnumDeclaration': { + case parser.enumDeclaration: { typeResolutionStatus = { successful: true, type: 'enum', @@ -429,7 +428,7 @@ class FlowParser implements Parser { } default: { throw new TypeError( - `A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, + `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}') or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, ); } } @@ -443,7 +442,8 @@ class FlowParser implements Parser { } getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN { - return resolveTypeAnnotation; + return (typeAnnotation, types, parser) => + this.getResolvedTypeAnnotation(typeAnnotation, types, parser); } } diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index f2e125c3b853f2..73e71760df4423 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -10,81 +10,7 @@ 'use strict'; -import type {TypeResolutionStatus, TypeDeclarationMap, ASTNode} from '../utils'; -import type {Parser} from '../../parsers/parser'; - -const invariant = require('invariant'); - -function resolveTypeAnnotation( - // TODO(T71778680): This is an Flow TypeAnnotation. Flow-type this - typeAnnotation: $FlowFixMe, - types: TypeDeclarationMap, - parser: Parser, -): { - nullable: boolean, - typeAnnotation: $FlowFixMe, - typeResolutionStatus: TypeResolutionStatus, -} { - invariant( - typeAnnotation != null, - 'resolveTypeAnnotation(): typeAnnotation cannot be null', - ); - - let node = typeAnnotation; - let nullable = false; - let typeResolutionStatus: TypeResolutionStatus = { - successful: false, - }; - - for (;;) { - if (node.type === 'NullableTypeAnnotation') { - nullable = true; - node = node.typeAnnotation; - continue; - } - - if (node.type !== 'GenericTypeAnnotation') { - break; - } - - const resolvedTypeAnnotation = types[node.id.name]; - if (resolvedTypeAnnotation == null) { - break; - } - - switch (resolvedTypeAnnotation.type) { - case parser.typeAlias: { - typeResolutionStatus = { - successful: true, - type: 'alias', - name: node.id.name, - }; - node = resolvedTypeAnnotation.right; - break; - } - case parser.enumDeclaration: { - typeResolutionStatus = { - successful: true, - type: 'enum', - name: node.id.name, - }; - node = resolvedTypeAnnotation.body; - break; - } - default: { - throw new TypeError( - `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}') or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, - ); - } - } - } - - return { - nullable: nullable, - typeAnnotation: node, - typeResolutionStatus, - }; -} +import type {TypeDeclarationMap, ASTNode} from '../utils'; function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode { if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) { @@ -95,5 +21,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode { module.exports = { getValueFromTypes, - resolveTypeAnnotation, }; diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index c4fd06ff44d089..080f6e72b74eb7 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -353,6 +353,7 @@ export interface Parser { getResolvedTypeAnnotation( typeAnnotation: $FlowFixMe, types: TypeDeclarationMap, + parser: Parser, ): { nullable: boolean, typeAnnotation: $FlowFixMe, 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 d9cab074e77b40..32c30450443038 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -29,7 +29,6 @@ const {flattenIntersectionType} = require('../parseTopLevelType'); const {flattenProperties} = require('../components/componentsUtils'); const {parseObjectProperty} = require('../../parsers-commons'); -const {resolveTypeAnnotation} = require('../utils'); const { emitArrayType, @@ -189,8 +188,9 @@ function translateTypeAnnotation( parser: Parser, ): Nullable { const {nullable, typeAnnotation, typeResolutionStatus} = - parser.getResolvedTypeAnnotation(typeScriptTypeAnnotation, types); - resolveTypeAnnotation(typeScriptTypeAnnotation, types, parser); + parser.getResolvedTypeAnnotation(typeScriptTypeAnnotation, types, parser); + const resolveTypeaAnnotationFn = parser.getResolveTypeAnnotationFN(); + resolveTypeaAnnotationFn(typeScriptTypeAnnotation, types, parser); switch (typeAnnotation.type) { case 'TSArrayType': { diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 8b886fad9b81e8..dbaa4d11d474af 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -51,7 +51,6 @@ const { getSchemaInfo, getTypeAnnotation, } = require('./components/componentsUtils'); -const {resolveTypeAnnotation} = require('./utils'); const fs = require('fs'); const { @@ -372,6 +371,7 @@ class TypeScriptParser implements Parser { // TODO(T108222691): Use flow-types for @babel/parser typeAnnotation: $FlowFixMe, types: TypeDeclarationMap, + parser: Parser, ): { nullable: boolean, typeAnnotation: $FlowFixMe, @@ -406,7 +406,7 @@ class TypeScriptParser implements Parser { } switch (resolvedTypeAnnotation.type) { - case 'TSTypeAliasDeclaration': { + case parser.typeAlias: { typeResolutionStatus = { successful: true, type: 'alias', @@ -415,7 +415,7 @@ class TypeScriptParser implements Parser { node = resolvedTypeAnnotation.typeAnnotation; break; } - case 'TSInterfaceDeclaration': { + case parser.interfaceDeclaration: { typeResolutionStatus = { successful: true, type: 'alias', @@ -424,7 +424,7 @@ class TypeScriptParser implements Parser { node = resolvedTypeAnnotation; break; } - case 'TSEnumDeclaration': { + case parser.enumDeclaration: { typeResolutionStatus = { successful: true, type: 'enum', @@ -435,7 +435,7 @@ class TypeScriptParser implements Parser { } default: { throw new TypeError( - `A non GenericTypeAnnotation must be a type declaration ('TSTypeAliasDeclaration'), an interface ('TSInterfaceDeclaration'), or enum ('TSEnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, + `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}'), an interface ('${parser.interfaceDeclaration}'), or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, ); } } @@ -449,7 +449,13 @@ class TypeScriptParser implements Parser { } getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN { - return resolveTypeAnnotation; + return ( + typeAnnotation: $FlowFixMe, + types: TypeDeclarationMap, + parser: Parser, + ) => { + return this.getResolvedTypeAnnotation(typeAnnotation, types, parser); + }; } } diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js deleted file mode 100644 index c67a293a5f292c..00000000000000 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ /dev/null @@ -1,105 +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'; - -// $FlowFixMe[unclear-type] Use flow-types for @babel/parser -export type ASTNode = Object; -import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils'; -import type {Parser} from '../../parsers/parser'; - -const {parseTopLevelType} = require('./parseTopLevelType'); - -const invariant = require('invariant'); - -function resolveTypeAnnotation( - // TODO(T108222691): Use flow-types for @babel/parser - typeAnnotation: $FlowFixMe, - types: TypeDeclarationMap, - parser: Parser, -): { - nullable: boolean, - typeAnnotation: $FlowFixMe, - typeResolutionStatus: TypeResolutionStatus, -} { - invariant( - typeAnnotation != null, - 'resolveTypeAnnotation(): typeAnnotation cannot be null', - ); - - let node = - typeAnnotation.type === 'TSTypeAnnotation' - ? typeAnnotation.typeAnnotation - : typeAnnotation; - let nullable = false; - let typeResolutionStatus: TypeResolutionStatus = { - successful: false, - }; - - for (;;) { - const topLevelType = parseTopLevelType(node); - nullable = nullable || topLevelType.optional; - node = topLevelType.type; - - if (node.type !== 'TSTypeReference') { - break; - } - - const resolvedTypeAnnotation = types[node.typeName.name]; - if (resolvedTypeAnnotation == null) { - break; - } - - switch (resolvedTypeAnnotation.type) { - case parser.typeAlias: { - typeResolutionStatus = { - successful: true, - type: 'alias', - name: node.typeName.name, - }; - node = resolvedTypeAnnotation.typeAnnotation; - break; - } - case parser.interfaceDeclaration: { - typeResolutionStatus = { - successful: true, - type: 'alias', - name: node.typeName.name, - }; - node = resolvedTypeAnnotation; - break; - } - case parser.enumDeclaration: { - typeResolutionStatus = { - successful: true, - type: 'enum', - name: node.typeName.name, - }; - node = resolvedTypeAnnotation; - break; - } - default: { - throw new TypeError( - `A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}'), an interface ('${parser.interfaceDeclaration}'), or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`, - ); - } - } - } - - return { - nullable: nullable, - typeAnnotation: node, - typeResolutionStatus, - }; -} - -module.exports = { - resolveTypeAnnotation, -};