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 @@ -305,6 +305,12 @@ describe('FlowParser', () => {
expect(parser.isOptionalProperty(property)).toEqual(true);
});
});

describe('typeAlias', () => {
it('returns typeAlias Property', () => {
expect(parser.typeAlias).toEqual('TypeAlias');
});
});
});

describe('TypeScriptParser', () => {
Expand Down Expand Up @@ -582,4 +588,10 @@ describe('TypeScriptParser', () => {
expect(parser.isOptionalProperty(property)).toEqual(false);
});
});

describe('typeAlias', () => {
it('returns typeAlias Property', () => {
expect(parser.typeAlias).toEqual('TSTypeAliasDeclaration');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function translateTypeAnnotation(
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
const {nullable, typeAnnotation, typeResolutionStatus} =
resolveTypeAnnotation(flowTypeAnnotation, types);
resolveTypeAnnotation(flowTypeAnnotation, types, parser);

switch (typeAnnotation.type) {
case 'GenericTypeAnnotation': {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {

class FlowParser implements Parser {
typeParameterInstantiation: string = 'TypeParameterInstantiation';
typeAlias: string = 'TypeAlias';

isProperty(property: $FlowFixMe): boolean {
return property.type === 'ObjectTypeProperty';
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
'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,
Expand Down Expand Up @@ -51,7 +53,7 @@ function resolveTypeAnnotation(
}

switch (resolvedTypeAnnotation.type) {
case 'TypeAlias': {
case parser.typeAlias: {
typeResolutionStatus = {
successful: true,
type: 'alias',
Expand All @@ -71,7 +73,7 @@ function resolveTypeAnnotation(
}
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 ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface Parser {
*/
typeParameterInstantiation: string;

/**
* TypeAlias property of the Parser
*/
typeAlias: string;

/**
* Given a declaration, it returns true if it is a property
*/
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const schemaMock = {

export class MockedParser implements Parser {
typeParameterInstantiation: string = 'TypeParameterInstantiation';
typeAlias: string = 'TypeAlias';

isProperty(property: $FlowFixMe): boolean {
return property.type === 'ObjectTypeProperty';
Expand Down
6 changes: 5 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ function buildPropertySchema(
: property.typeAnnotation;
}

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));
({nullable, typeAnnotation: value} = resolveTypeAnnotation(
value,
types,
parser,
));

throwIfModuleTypeIsUnsupported(
hasteModuleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function translateTypeAnnotation(
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
const {nullable, typeAnnotation, typeResolutionStatus} =
resolveTypeAnnotation(typeScriptTypeAnnotation, types);
resolveTypeAnnotation(typeScriptTypeAnnotation, types, parser);

switch (typeAnnotation.type) {
case 'TSArrayType': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {

class TypeScriptParser implements Parser {
typeParameterInstantiation: string = 'TSTypeParameterInstantiation';
typeAlias: string = 'TSTypeAliasDeclaration';

isProperty(property: $FlowFixMe): boolean {
return property.type === 'TSPropertySignature';
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

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

const {parseTopLevelType} = require('./parseTopLevelType');

Expand All @@ -20,6 +21,7 @@ function resolveTypeAnnotation(
// TODO(T108222691): Use flow-types for @babel/parser
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
parser: Parser,
): {
nullable: boolean,
typeAnnotation: $FlowFixMe,
Expand Down Expand Up @@ -54,7 +56,7 @@ function resolveTypeAnnotation(
}

switch (resolvedTypeAnnotation.type) {
case 'TSTypeAliasDeclaration': {
case parser.typeAlias: {
typeResolutionStatus = {
successful: true,
type: 'alias',
Expand Down Expand Up @@ -83,7 +85,7 @@ function resolveTypeAnnotation(
}
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 ('TSInterfaceDeclaration'), or enum ('TSEnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
);
}
}
Expand Down