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
This commit is contained in:
Gabriel Donadel Dall'Agnol 2022-10-09 03:17:28 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 24efebf83d
Коммит 7227bdec8e
6 изменённых файлов: 4 добавлений и 40 удалений

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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

Просмотреть файл

@ -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,
};

Просмотреть файл

@ -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) {

Просмотреть файл

@ -10,7 +10,7 @@
'use strict';
const {ParserError} = require('./errors');
const {ParserError} = require('../errors');
const {parseTopLevelType} = require('./parseTopLevelType');
/**