From a9e7751caf43c5f4cd6b9c4cb72c2e8fa45b622c Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Thu, 20 Oct 2022 03:45:27 -0700 Subject: [PATCH] Create the Parser interface Summary: This diff is the base to create a polimorphic behavior for TypeScript and Flow parser. This type will allow to share a lot of code between the parsers and also to keep their differences a part. It will be the base diff/PR for further tasks in the Codegen umbrella Issue ## Changelog: [General][Added] - Parser interface to divide parser logic. Differential Revision: D40548707 fbshipit-source-id: 4f6b5e56d74b9b2d92b87dfcf36abab6cb222606 --- .../src/parsers/errors.js | 11 ++++----- .../src/parsers/flow/modules/index.js | 5 +++- .../src/parsers/flow/parser.js | 23 +++++++++++++++++++ .../src/parsers/parser.js | 15 ++++++++++++ .../src/parsers/typescript/modules/index.js | 7 ++++-- .../src/parsers/typescript/parser.js | 22 ++++++++++++++++++ 6 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 packages/react-native-codegen/src/parsers/flow/parser.js create mode 100644 packages/react-native-codegen/src/parsers/parser.js create mode 100644 packages/react-native-codegen/src/parsers/typescript/parser.js diff --git a/packages/react-native-codegen/src/parsers/errors.js b/packages/react-native-codegen/src/parsers/errors.js index 4882a9bdb43a..c4aa8c1fd8eb 100644 --- a/packages/react-native-codegen/src/parsers/errors.js +++ b/packages/react-native-codegen/src/parsers/errors.js @@ -11,7 +11,7 @@ 'use strict'; const invariant = require('invariant'); - +import type {Parser} from './parser'; export type ParserType = 'Flow' | 'TypeScript'; class ParserError extends Error { @@ -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, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 9e9f3e6abe3d..914bb27128bc 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -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, @@ -276,7 +279,7 @@ function translateTypeAnnotation( throw new UnsupportedGenericParserError( hasteModuleName, typeAnnotation, - language, + parser, ); } } diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js new file mode 100644 index 000000000000..47481c903783 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -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, +}; diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js new file mode 100644 index 000000000000..6699e9603120 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -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; +} diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 448ff4388ab1..3c26d7ab84d0 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -80,7 +80,10 @@ const { throwIfUnsupportedFunctionReturnTypeAnnotationParserError, } = require('../../error-utils'); +const {TypeScriptParser} = require('../parser'); + const language = 'TypeScript'; +const parser = new TypeScriptParser(); function translateArrayTypeAnnotation( hasteModuleName: string, @@ -205,7 +208,7 @@ function translateTypeAnnotation( throw new UnsupportedGenericParserError( hasteModuleName, typeAnnotation, - language, + parser, ); } } @@ -290,7 +293,7 @@ function translateTypeAnnotation( throw new UnsupportedGenericParserError( hasteModuleName, typeAnnotation, - language, + parser, ); } } diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js new file mode 100644 index 000000000000..1c9db6d8d175 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -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, +};