From 7227bdec8e75a7593cb1815b97106583f0efc2d8 Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Sun, 9 Oct 2022 03:17:28 -0700 Subject: [PATCH] chore: Extract codegen ParserError from flow and typescript into a common file (#34874) Summary: This PR extracts the `ParserError` class from the `error.js` files from the flow and typescript folders into a single file in their parent folder in order to reduce the amount of duplicated code as requested on https://github.com/facebook/react-native/issues/34872 ## Changelog [Internal] [Changed] - Extract codegen `ParserError` from flow and typescript folders into a common file Pull Request resolved: https://github.com/facebook/react-native/pull/34874 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/194135137-bf0fc82d-220b-4509-b1bb-1e7dae925efe.png) Reviewed By: cipolleschi Differential Revision: D40133301 Pulled By: cipolleschi fbshipit-source-id: 060571cf993672a9d03003e1a1a7c2aa2f381123 --- .../src/parsers/{flow => }/errors.js | 0 .../src/parsers/flow/modules/errors.js | 2 +- .../src/parsers/flow/utils.js | 2 +- .../src/parsers/typescript/errors.js | 36 ------------------- .../src/parsers/typescript/modules/errors.js | 2 +- .../src/parsers/typescript/utils.js | 2 +- 6 files changed, 4 insertions(+), 40 deletions(-) rename packages/react-native-codegen/src/parsers/{flow => }/errors.js (100%) delete mode 100644 packages/react-native-codegen/src/parsers/typescript/errors.js diff --git a/packages/react-native-codegen/src/parsers/flow/errors.js b/packages/react-native-codegen/src/parsers/errors.js similarity index 100% rename from packages/react-native-codegen/src/parsers/flow/errors.js rename to packages/react-native-codegen/src/parsers/errors.js diff --git a/packages/react-native-codegen/src/parsers/flow/modules/errors.js b/packages/react-native-codegen/src/parsers/flow/modules/errors.js index 9314565c32fc16..d0929f3689618c 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/errors.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/errors.js @@ -11,7 +11,7 @@ 'use strict'; const invariant = require('invariant'); -const {ParserError} = require('../errors'); +const {ParserError} = require('../../errors'); class MisnamedModuleFlowInterfaceParserError extends ParserError { constructor(hasteModuleName: string, id: $FlowFixMe) { diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index e2ca547678ad0d..6051302979148f 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -10,7 +10,7 @@ 'use strict'; -const {ParserError} = require('./errors'); +const {ParserError} = require('../errors'); /** * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias diff --git a/packages/react-native-codegen/src/parsers/typescript/errors.js b/packages/react-native-codegen/src/parsers/typescript/errors.js deleted file mode 100644 index 41fc7f8ca4eed5..00000000000000 --- a/packages/react-native-codegen/src/parsers/typescript/errors.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * 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-local - * @format - */ - -'use strict'; - -class ParserError extends Error { - nodes: $ReadOnlyArray<$FlowFixMe>; - constructor( - hasteModuleName: string, - astNodeOrNodes: $FlowFixMe, - message: string, - ) { - super(`Module ${hasteModuleName}: ${message}`); - - this.nodes = Array.isArray(astNodeOrNodes) - ? astNodeOrNodes - : [astNodeOrNodes]; - - // assign the error class name in your custom error (as a shortcut) - this.name = this.constructor.name; - - // capturing the stack trace keeps the reference to your error class - Error.captureStackTrace(this, this.constructor); - } -} - -module.exports = { - ParserError, -}; diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/errors.js b/packages/react-native-codegen/src/parsers/typescript/modules/errors.js index 9cc1470c9d8802..a6de07a86400f9 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/errors.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/errors.js @@ -11,7 +11,7 @@ 'use strict'; const invariant = require('invariant'); -const {ParserError} = require('../errors'); +const {ParserError} = require('../../errors'); class MisnamedModuleTypeScriptInterfaceParserError extends ParserError { constructor(hasteModuleName: string, id: $FlowFixMe) { diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 11fb8a4d5b3f30..034efdbce3a1af 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -10,7 +10,7 @@ 'use strict'; -const {ParserError} = require('./errors'); +const {ParserError} = require('../errors'); const {parseTopLevelType} = require('./parseTopLevelType'); /**