Skip to content

Commit

Permalink
Add getResolvedTypeAnnotation to Parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
siddarthkay committed May 11, 2023
1 parent cceef57 commit 3cdaf7f
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const flowParser = new FlowParser();

const {flowTranslateTypeAnnotation} = require('../flow/modules/index');
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
const {resolveTypeAnnotation} = require('../flow/utils');

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -409,7 +408,7 @@ describe('buildSchemaFromConfigType', () => {
(_ast, _parser) => componentSchemaMock,
);
const buildModuleSchemaMock = jest.fn(
(_0, _1, _2, _3, _4, _5) => moduleSchemaMock,
(_0, _1, _2, _3, _4) => moduleSchemaMock,
);

const buildSchemaFromConfigTypeHelper = (
Expand All @@ -424,7 +423,6 @@ describe('buildSchemaFromConfigType', () => {
buildComponentSchemaMock,
buildModuleSchemaMock,
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -507,7 +505,6 @@ describe('buildSchemaFromConfigType', () => {
astMock,
expect.any(Function),
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -679,7 +676,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
parser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -713,7 +709,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -768,7 +763,6 @@ describe('buildSchema', () => {
buildModuleSchema,
Visitor,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down Expand Up @@ -1064,7 +1058,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1079,7 +1072,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1116,7 +1108,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1131,7 +1122,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1171,7 +1161,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).toThrow(expected);
Expand All @@ -1186,7 +1175,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
),
).not.toThrow();
Expand Down Expand Up @@ -1229,7 +1217,6 @@ describe('buildModuleSchema', () => {
ast,
tryParse,
flowParser,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type {
import type {Parser} from '../../parser';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {resolveTypeAnnotation} = require('../utils');
const {
unwrapNullable,
wrapNullable,
Expand Down Expand Up @@ -60,7 +59,7 @@ function translateTypeAnnotation(
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
const {nullable, typeAnnotation, typeResolutionStatus} =
resolveTypeAnnotation(flowTypeAnnotation, types);
parser.getResolvedTypeAnnotation(flowTypeAnnotation, types);

switch (typeAnnotation.type) {
case 'GenericTypeAnnotation': {
Expand Down
79 changes: 76 additions & 3 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import type {
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';
import type {
ParserErrorCapturer,
TypeDeclarationMap,
PropAST,
TypeResolutionStatus,
} from '../utils';
const invariant = require('invariant');

const {flowTranslateTypeAnnotation} = require('./modules');

Expand All @@ -35,7 +41,6 @@ const {Visitor} = require('../parsers-primitives');
const {buildComponentSchema} = require('./components');
const {wrapComponentSchema} = require('../schema.js');
const {buildModuleSchema} = require('../parsers-commons.js');
const {resolveTypeAnnotation} = require('./utils');

const fs = require('fs');

Expand Down Expand Up @@ -106,7 +111,6 @@ class FlowParser implements Parser {
buildModuleSchema,
Visitor,
this,
resolveTypeAnnotation,
flowTranslateTypeAnnotation,
);
}
Expand Down Expand Up @@ -343,6 +347,75 @@ class FlowParser implements Parser {
property.value.type === 'NullableTypeAnnotation' || property.optional
);
}

getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
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 'TypeAlias': {
typeResolutionStatus = {
successful: true,
type: 'alias',
name: node.id.name,
};
node = resolvedTypeAnnotation.right;
break;
}
case '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 ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
}

return {
nullable: nullable,
typeAnnotation: node,
typeResolutionStatus,
};
}
}

module.exports = {
Expand Down
75 changes: 1 addition & 74 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,11 @@

'use strict';

import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
import type {TypeDeclarationMap} from '../utils';

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
export type ASTNode = Object;

const invariant = require('invariant');

function resolveTypeAnnotation(
// TODO(T71778680): This is an Flow TypeAnnotation. Flow-type this
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
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 'TypeAlias': {
typeResolutionStatus = {
successful: true,
type: 'alias',
name: node.id.name,
};
node = resolvedTypeAnnotation.right;
break;
}
case '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 ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
}

return {
nullable: nullable,
typeAnnotation: node,
typeResolutionStatus,
};
}

function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) {
return getValueFromTypes(types[value.id.name].right, types);
Expand All @@ -96,5 +24,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {

module.exports = {
getValueFromTypes,
resolveTypeAnnotation,
};
16 changes: 15 additions & 1 deletion packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import type {
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
import type {
ParserErrorCapturer,
TypeDeclarationMap,
PropAST,
TypeResolutionStatus,
} from './utils';

/**
* This is the main interface for Parsers of various languages.
Expand Down Expand Up @@ -276,4 +281,13 @@ export interface Parser {
* @returns: a boolean specifying if the Property is optional
*/
isOptionalProperty(property: $FlowFixMe): boolean;

getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
typeResolutionStatus: TypeResolutionStatus,
};
}
Loading

0 comments on commit 3cdaf7f

Please sign in to comment.