Convert build scripts to regular Flow syntax (#49103)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49103

Changelog: [Internal]

Reviewed By: j-piasecki

Differential Revision: D68960540

fbshipit-source-id: 0ac01529eaea97db98b85b6021532092997d633b
This commit is contained in:
Alex Hunt
2025-02-03 03:49:23 -08:00
committed by Facebook GitHub Bot
parent e1575857dd
commit 3cf400a51b
8 changed files with 63 additions and 43 deletions
@@ -9,31 +9,29 @@
* @oncall react_native
*/
/*::
import type {TransformVisitor} from 'hermes-transform';
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
import type {ParseResult} from 'hermes-transform/dist/transform/parse';
*/
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
const {transformAST} = require('hermes-transform/dist/transform/transformAST');
const visitors /*: TransformVisitor */ = context => ({
ObjectTypeProperty(node) /*: void */ {
const visitors: TransformVisitor = context => ({
ObjectTypeProperty(node): void {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
Property(node) /*: void */ {
Property(node): void {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
PropertyDefinition(node) /*: void */ {
PropertyDefinition(node): void {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
MethodDefinition(node) /*: void */ {
MethodDefinition(node): void {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
@@ -41,8 +39,8 @@ const visitors /*: TransformVisitor */ = context => ({
});
async function stripPrivateProperties(
source /*: ParseResult */,
) /*: Promise<TransformASTResult> */ {
source: ParseResult,
): Promise<TransformASTResult> {
return transformAST(source, visitors);
}
@@ -9,19 +9,15 @@
* @oncall react_native
*/
/*::
import type {ParseResult} from 'hermes-transform/dist/transform/parse';
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
*/
const translate = require('flow-api-translator');
const {parse, print} = require('hermes-transform');
/*::
type TransformFn = (ParseResult) => Promise<TransformASTResult>;
*/
type TransformFn = ParseResult => Promise<TransformASTResult>;
const preTransforms /*: Array<TransformFn> */ = [
const preTransforms: Array<TransformFn> = [
require('./transforms/stripPrivateProperties'),
];
const prettierOptions = {parser: 'babel'};
@@ -32,9 +28,7 @@ const prettierOptions = {parser: 'babel'};
* This uses [flow-api-translator](https://www.npmjs.com/package/flow-api-translator),
* and applies extra transformations such as stripping private properties.
*/
async function translateSourceFile(
source /*: string */,
) /*: Promise<string> */ {
async function translateSourceFile(source: string): Promise<string> {
// Parse Flow source
const parsed = await parse(source);
@@ -49,9 +43,9 @@ async function translateSourceFile(
}
async function applyTransforms(
source /*: ParseResult */,
transforms /*: $ReadOnlyArray<TransformFn> */,
) /*: Promise<ParseResult> */ {
source: ParseResult,
transforms: $ReadOnlyArray<TransformFn>,
): Promise<ParseResult> {
return transforms.reduce((input, transform) => {
return input.then(async result => {
const transformed = await transform(result);