Skip to content

Commit

Permalink
Remove duplicated code that resulted after a merge conflict. (faceboo…
Browse files Browse the repository at this point in the history
…k#37477)

Summary:
Pull Request resolved: facebook#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: 7a9dde2c9c0dfeb24f45d6a5f96c384ad1423108
  • Loading branch information
cipolleschi authored and facebook-github-bot committed May 22, 2023
1 parent 0412279 commit aa80a45
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type {
} from '../../../CodegenSchema';

import type {Parser} from '../../parser';
const {resolveTypeAnnotation} = require('../utils');
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {
Expand Down Expand Up @@ -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': {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import type {
TypeResolutionStatus,
} from '../utils';

const {resolveTypeAnnotation} = require('./utils');
const invariant = require('invariant');

const {
Expand Down Expand Up @@ -65,7 +64,6 @@ class FlowParser implements Parser {
typeAlias: string = 'TypeAlias';
enumDeclaration: string = 'EnumDeclaration';
interfaceDeclaration: string = 'InterfaceDeclaration';

nullLiteralTypeAnnotation: string = 'NullLiteralTypeAnnotation';

isProperty(property: $FlowFixMe): boolean {
Expand Down Expand Up @@ -376,6 +374,7 @@ class FlowParser implements Parser {
getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
Expand Down Expand Up @@ -409,7 +408,7 @@ class FlowParser implements Parser {
}

switch (resolvedTypeAnnotation.type) {
case 'TypeAlias': {
case parser.typeAlias: {
typeResolutionStatus = {
successful: true,
type: 'alias',
Expand All @@ -418,7 +417,7 @@ class FlowParser implements Parser {
node = resolvedTypeAnnotation.right;
break;
}
case 'EnumDeclaration': {
case parser.enumDeclaration: {
typeResolutionStatus = {
successful: true,
type: 'enum',
Expand All @@ -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}.`,
);
}
}
Expand All @@ -443,7 +442,8 @@ class FlowParser implements Parser {
}

getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN {
return resolveTypeAnnotation;
return (typeAnnotation, types, parser) =>
this.getResolvedTypeAnnotation(typeAnnotation, types, parser);
}
}

Expand Down
77 changes: 1 addition & 76 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -95,5 +21,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {

module.exports = {
getValueFromTypes,
resolveTypeAnnotation,
};
1 change: 1 addition & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export interface Parser {
getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const {flattenIntersectionType} = require('../parseTopLevelType');
const {flattenProperties} = require('../components/componentsUtils');

const {parseObjectProperty} = require('../../parsers-commons');
const {resolveTypeAnnotation} = require('../utils');

const {
emitArrayType,
Expand Down Expand Up @@ -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': {
Expand Down
18 changes: 12 additions & 6 deletions packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const {
getSchemaInfo,
getTypeAnnotation,
} = require('./components/componentsUtils');
const {resolveTypeAnnotation} = require('./utils');
const fs = require('fs');

const {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -406,7 +406,7 @@ class TypeScriptParser implements Parser {
}

switch (resolvedTypeAnnotation.type) {
case 'TSTypeAliasDeclaration': {
case parser.typeAlias: {
typeResolutionStatus = {
successful: true,
type: 'alias',
Expand All @@ -415,7 +415,7 @@ class TypeScriptParser implements Parser {
node = resolvedTypeAnnotation.typeAnnotation;
break;
}
case 'TSInterfaceDeclaration': {
case parser.interfaceDeclaration: {
typeResolutionStatus = {
successful: true,
type: 'alias',
Expand All @@ -424,7 +424,7 @@ class TypeScriptParser implements Parser {
node = resolvedTypeAnnotation;
break;
}
case 'TSEnumDeclaration': {
case parser.enumDeclaration: {
typeResolutionStatus = {
successful: true,
type: 'enum',
Expand All @@ -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}.`,
);
}
}
Expand All @@ -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);
};
}
}

Expand Down
105 changes: 0 additions & 105 deletions packages/react-native-codegen/src/parsers/typescript/utils.js

This file was deleted.

0 comments on commit aa80a45

Please sign in to comment.