Remove duplicated code that resulted after a merge conflict. (#37477)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37477

When creating a stack of several codegen fixes, a conflit generated some duplicated code we moved to the parsers from the utils.

This change removes that duplicated cone.

## Changelog:
[Genearal][Fixed] - Remove duplicated code.

Reviewed By: cortinico

Differential Revision: D45979013

fbshipit-source-id: 78cb89df81305221258e283ba5924135d498e800
This commit is contained in:
Riccardo Cipolleschi
2023-05-22 03:35:46 -07:00
committed by Facebook GitHub Bot
parent 718582d3bb
commit 8dcaa4cc3b
7 changed files with 25 additions and 198 deletions
@@ -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<NativeModuleTypeAnnotation> {
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
const {nullable, typeAnnotation, typeResolutionStatus} =
resolveTypeAnnotation(flowTypeAnnotation, types, parser);
resolveTypeAnnotationFN(flowTypeAnnotation, types, parser);
switch (typeAnnotation.type) {
case 'GenericTypeAnnotation': {
+6 -6
View File
@@ -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);
}
}
+1 -76
View File
@@ -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,
};
+1
View File
@@ -353,6 +353,7 @@ export interface Parser {
getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
@@ -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<NativeModuleTypeAnnotation> {
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': {
@@ -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);
};
}
}
@@ -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,
};