Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ describe('FlowParser', () => {
});
});

describe('interfaceDelcaration', () => {
it('returns interfaceDelcaration Property', () => {
expect(parser.interfaceDelcaration).toEqual('InterfaceDeclaration');
describe('interfaceDeclaration', () => {
it('returns interfaceDeclaration Property', () => {
expect(parser.interfaceDeclaration).toEqual('InterfaceDeclaration');
});
});
});
Expand Down Expand Up @@ -613,9 +613,9 @@ describe('TypeScriptParser', () => {
});
});

describe('interfaceDelcaration', () => {
it('returns interfaceDelcaration Property', () => {
expect(parser.interfaceDelcaration).toEqual('TSInterfaceDeclaration');
describe('interfaceDeclaration', () => {
it('returns interfaceDeclaration Property', () => {
expect(parser.interfaceDeclaration).toEqual('TSInterfaceDeclaration');
});
});
});
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
14 changes: 7 additions & 7 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 @@ -64,8 +63,7 @@ class FlowParser implements Parser {
typeParameterInstantiation: string = 'TypeParameterInstantiation';
typeAlias: string = 'TypeAlias';
enumDeclaration: string = 'EnumDeclaration';
interfaceDelcaration: string = 'InterfaceDeclaration';

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,
};
5 changes: 3 additions & 2 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export interface Parser {
enumDeclaration: string;

/**
* InterfaceDelcaration property of the Parser
* InterfaceDeclaration property of the Parser
*/
interfaceDelcaration: string;
interfaceDeclaration: string;

/**
* This is the NullLiteralTypeAnnotation value
Expand Down Expand Up @@ -353,6 +353,7 @@ export interface Parser {
getResolvedTypeAnnotation(
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class MockedParser implements Parser {
typeParameterInstantiation: string = 'TypeParameterInstantiation';
typeAlias: string = 'TypeAlias';
enumDeclaration: string = 'EnumDeclaration';
interfaceDelcaration: string = 'InterfaceDelcaration';
interfaceDeclaration: string = 'InterfaceDeclaration';

nullLiteralTypeAnnotation: string = 'NullLiteralTypeAnnotation';

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
20 changes: 13 additions & 7 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 All @@ -62,7 +61,7 @@ class TypeScriptParser implements Parser {
typeParameterInstantiation: string = 'TSTypeParameterInstantiation';
typeAlias: string = 'TSTypeAliasDeclaration';
enumDeclaration: string = 'TSEnumDeclaration';
interfaceDelcaration: string = 'TSInterfaceDeclaration';
interfaceDeclaration: string = 'TSInterfaceDeclaration';

nullLiteralTypeAnnotation: string = 'TSNullKeyword';

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
Loading