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
11 changes: 5 additions & 6 deletions packages/react-native-codegen/src/parsers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';

const invariant = require('invariant');

import type {Parser} from './parser';
export type ParserType = 'Flow' | 'TypeScript';

class ParserError extends Error {
Expand Down Expand Up @@ -114,12 +114,11 @@ class UnsupportedGenericParserError extends ParserError {
constructor(
nativeModuleName: string,
genericTypeAnnotation: $FlowFixMe,
language: ParserType,
parser: Parser,
) {
const genericName =
language === 'TypeScript'
? genericTypeAnnotation.typeName.name
: genericTypeAnnotation.id.name;
const genericName = parser.nameForGenericTypeAnnotation(
genericTypeAnnotation,
);
super(
nativeModuleName,
genericTypeAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const {
throwIfMoreThanOneModuleInterfaceParserError,
} = require('../../error-utils');

const {FlowParser} = require('../parser.js');

const language = 'Flow';
const parser = new FlowParser();

function translateArrayTypeAnnotation(
hasteModuleName: string,
Expand Down Expand Up @@ -276,7 +279,7 @@ function translateTypeAnnotation(
throw new UnsupportedGenericParserError(
hasteModuleName,
typeAnnotation,
language,
parser,
);
}
}
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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';

import type {Parser} from '../parser';

class FlowParser implements Parser {
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
return typeAnnotation.id.name;
}
}

module.exports = {
FlowParser,
};
15 changes: 15 additions & 0 deletions packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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';

export interface Parser {
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ const {
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
} = require('../../error-utils');

const {TypeScriptParser} = require('../parser');

const language = 'TypeScript';
const parser = new TypeScriptParser();

function translateArrayTypeAnnotation(
hasteModuleName: string,
Expand Down Expand Up @@ -205,7 +208,7 @@ function translateTypeAnnotation(
throw new UnsupportedGenericParserError(
hasteModuleName,
typeAnnotation,
language,
parser,
);
}
}
Expand Down Expand Up @@ -290,7 +293,7 @@ function translateTypeAnnotation(
throw new UnsupportedGenericParserError(
hasteModuleName,
typeAnnotation,
language,
parser,
);
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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';

import type {Parser} from '../parser';

class TypeScriptParser implements Parser {
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
return typeAnnotation.typeName.name;
}
}
module.exports = {
TypeScriptParser,
};