From c23c3d71c1a544bb089e9eff0be1bb580a8e273e Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 24 Jun 2025 07:36:17 -0700 Subject: [PATCH 1/2] Subfolder build-types transforms by language Summary: Reorganisation/refactoring. Changelog: [Internal] Differential Revision: D77148446 --- .../Libraries/__tests__/public-api-test.js | 2 +- scripts/build-types/BuildApiSnapshot.js | 12 ++++---- .../__tests__/ensureNoUnprefixedProps-test.js | 2 +- .../__tests__/reattachDocComments-test.js | 2 +- .../__tests__/replaceEmptyWithNever-test.js | 2 +- ...aceNullablePropertiesWithUndefined-test.js | 2 +- .../replaceRequiresWithImports-test.js | 2 +- .../replaceStringishWithString-test.js | 2 +- .../__tests__/stripPrivateProperties-test.js | 2 +- .../{ => flow}/ensureNoUnprefixedProps.js | 0 .../{ => flow}/reattachDocComments.js | 0 .../{ => flow}/replaceEmptyWithNever.js | 0 .../replaceNullablePropertiesWithUndefined.js | 0 .../{ => flow}/replaceRequiresWithImports.js | 0 .../{ => flow}/replaceStringishWithString.js | 0 .../{ => flow}/stripPrivateProperties.js | 0 .../__fixtures__/sortProperties.d.ts | 0 .../__fixtures__/sortTypeDefinitions.d.ts | 0 .../__fixtures__/sortUnions.d.ts | 0 .../__snapshots__/sortProperties-test.js.snap | 0 .../sortTypeDefinitions-test.js.snap | 0 .../__snapshots__/sortUnions-test.js.snap | 0 .../renameDefaultExportedIdentifiers-test.js | 2 +- .../replaceDefaultExportName-test.js | 2 +- .../__tests__/sortProperties-test.js | 2 +- .../__tests__/sortTypeDefinitions-test.js | 2 +- .../__tests__/sortUnions-test.js | 2 +- .../__tests__/stripUnstableApis-test.js | 2 +- .../renameDefaultExportedIdentifiers.js | 0 .../replaceDefaultExportName.js | 0 .../{ => typescript}/sortProperties.js | 0 .../{ => typescript}/sortTypeDefinitions.js | 0 .../transforms/{ => typescript}/sortUnions.js | 0 .../{ => typescript}/stripUnstableApis.js | 0 scripts/build-types/translateSourceFile.js | 28 ++++++++----------- .../resolveCyclicImportsInDefinition-test.js | 2 +- .../resolveCyclicImportsInDefinition.js | 0 37 files changed, 32 insertions(+), 38 deletions(-) rename scripts/build-types/transforms/{ => flow}/__tests__/ensureNoUnprefixedProps-test.js (99%) rename scripts/build-types/transforms/{ => flow}/__tests__/reattachDocComments-test.js (96%) rename scripts/build-types/transforms/{ => flow}/__tests__/replaceEmptyWithNever-test.js (95%) rename scripts/build-types/transforms/{ => flow}/__tests__/replaceNullablePropertiesWithUndefined-test.js (97%) rename scripts/build-types/transforms/{ => flow}/__tests__/replaceRequiresWithImports-test.js (99%) rename scripts/build-types/transforms/{ => flow}/__tests__/replaceStringishWithString-test.js (98%) rename scripts/build-types/transforms/{ => flow}/__tests__/stripPrivateProperties-test.js (99%) rename scripts/build-types/transforms/{ => flow}/ensureNoUnprefixedProps.js (100%) rename scripts/build-types/transforms/{ => flow}/reattachDocComments.js (100%) rename scripts/build-types/transforms/{ => flow}/replaceEmptyWithNever.js (100%) rename scripts/build-types/transforms/{ => flow}/replaceNullablePropertiesWithUndefined.js (100%) rename scripts/build-types/transforms/{ => flow}/replaceRequiresWithImports.js (100%) rename scripts/build-types/transforms/{ => flow}/replaceStringishWithString.js (100%) rename scripts/build-types/transforms/{ => flow}/stripPrivateProperties.js (100%) rename scripts/build-types/transforms/{ => typescript}/__fixtures__/sortProperties.d.ts (100%) rename scripts/build-types/transforms/{ => typescript}/__fixtures__/sortTypeDefinitions.d.ts (100%) rename scripts/build-types/transforms/{ => typescript}/__fixtures__/sortUnions.d.ts (100%) rename scripts/build-types/transforms/{ => typescript}/__tests__/__snapshots__/sortProperties-test.js.snap (100%) rename scripts/build-types/transforms/{ => typescript}/__tests__/__snapshots__/sortTypeDefinitions-test.js.snap (100%) rename scripts/build-types/transforms/{ => typescript}/__tests__/__snapshots__/sortUnions-test.js.snap (100%) rename scripts/build-types/transforms/{ => typescript}/__tests__/renameDefaultExportedIdentifiers-test.js (98%) rename scripts/build-types/transforms/{ => typescript}/__tests__/replaceDefaultExportName-test.js (98%) rename scripts/build-types/transforms/{ => typescript}/__tests__/sortProperties-test.js (93%) rename scripts/build-types/transforms/{ => typescript}/__tests__/sortTypeDefinitions-test.js (99%) rename scripts/build-types/transforms/{ => typescript}/__tests__/sortUnions-test.js (93%) rename scripts/build-types/transforms/{ => typescript}/__tests__/stripUnstableApis-test.js (98%) rename scripts/build-types/transforms/{ => typescript}/renameDefaultExportedIdentifiers.js (100%) rename scripts/build-types/transforms/{ => typescript}/replaceDefaultExportName.js (100%) rename scripts/build-types/transforms/{ => typescript}/sortProperties.js (100%) rename scripts/build-types/transforms/{ => typescript}/sortTypeDefinitions.js (100%) rename scripts/build-types/transforms/{ => typescript}/sortUnions.js (100%) rename scripts/build-types/transforms/{ => typescript}/stripUnstableApis.js (100%) rename scripts/build-types/{transforms => utils}/__tests__/resolveCyclicImportsInDefinition-test.js (99%) rename scripts/build-types/{transforms => utils}/resolveCyclicImportsInDefinition.js (100%) diff --git a/packages/react-native/Libraries/__tests__/public-api-test.js b/packages/react-native/Libraries/__tests__/public-api-test.js index c5c8825e472c..88df509ec242 100644 --- a/packages/react-native/Libraries/__tests__/public-api-test.js +++ b/packages/react-native/Libraries/__tests__/public-api-test.js @@ -12,7 +12,7 @@ import type {TransformVisitor} from 'hermes-transform'; const { visitors: stripPrivateProperties, -} = require('../../../../scripts/build-types/transforms/stripPrivateProperties'); +} = require('../../../../scripts/build-types/transforms/flow/stripPrivateProperties'); const translate = require('flow-api-translator'); const {existsSync, promises: fs} = require('fs'); const glob = require('glob'); diff --git a/scripts/build-types/BuildApiSnapshot.js b/scripts/build-types/BuildApiSnapshot.js index ddf30af047da..dcff93ef05b9 100644 --- a/scripts/build-types/BuildApiSnapshot.js +++ b/scripts/build-types/BuildApiSnapshot.js @@ -15,7 +15,7 @@ const {PACKAGES_DIR, REACT_NATIVE_PACKAGE_DIR} = require('../consts'); const {isGitRepo} = require('../scm-utils'); const {API_EXTRACTOR_CONFIG_FILE, TYPES_OUTPUT_DIR} = require('./config'); const apiSnapshotTemplate = require('./templates/ReactNativeApi.d.ts-template.js'); -const resolveCyclicImportsInDefinition = require('./transforms/resolveCyclicImportsInDefinition'); +const resolveCyclicImportsInDefinition = require('./utils/resolveCyclicImportsInDefinition'); const babel = require('@babel/core'); const { Extractor, @@ -31,14 +31,14 @@ const osTempDir = require('temp-dir'); const {styleText} = require('util'); const inputFilesPostTransforms: $ReadOnlyArray> = [ - require('./transforms/renameDefaultExportedIdentifiers'), + require('./transforms/typescript/renameDefaultExportedIdentifiers'), ]; const postTransforms: $ReadOnlyArray> = [ - require('./transforms/stripUnstableApis'), - require('./transforms/sortTypeDefinitions'), - require('./transforms/sortProperties'), - require('./transforms/sortUnions'), + require('./transforms/typescript/stripUnstableApis'), + require('./transforms/typescript/sortTypeDefinitions'), + require('./transforms/typescript/sortProperties'), + require('./transforms/typescript/sortUnions'), require('./transforms/removeUndefinedFromOptionalMembers'), ]; diff --git a/scripts/build-types/transforms/__tests__/ensureNoUnprefixedProps-test.js b/scripts/build-types/transforms/flow/__tests__/ensureNoUnprefixedProps-test.js similarity index 99% rename from scripts/build-types/transforms/__tests__/ensureNoUnprefixedProps-test.js rename to scripts/build-types/transforms/flow/__tests__/ensureNoUnprefixedProps-test.js index 8cf27a94bc12..25b71c633d29 100644 --- a/scripts/build-types/transforms/__tests__/ensureNoUnprefixedProps-test.js +++ b/scripts/build-types/transforms/flow/__tests__/ensureNoUnprefixedProps-test.js @@ -8,7 +8,7 @@ * @format */ -const ensureNoUnprefixedProps = require('../ensureNoUnprefixedProps.js'); +const ensureNoUnprefixedProps = require('../ensureNoUnprefixedProps'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/reattachDocComments-test.js b/scripts/build-types/transforms/flow/__tests__/reattachDocComments-test.js similarity index 96% rename from scripts/build-types/transforms/__tests__/reattachDocComments-test.js rename to scripts/build-types/transforms/flow/__tests__/reattachDocComments-test.js index 4da3899e329d..dc289f67a317 100644 --- a/scripts/build-types/transforms/__tests__/reattachDocComments-test.js +++ b/scripts/build-types/transforms/flow/__tests__/reattachDocComments-test.js @@ -8,7 +8,7 @@ * @format */ -const reattachDocComments = require('../reattachDocComments.js'); +const reattachDocComments = require('../reattachDocComments'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/replaceEmptyWithNever-test.js b/scripts/build-types/transforms/flow/__tests__/replaceEmptyWithNever-test.js similarity index 95% rename from scripts/build-types/transforms/__tests__/replaceEmptyWithNever-test.js rename to scripts/build-types/transforms/flow/__tests__/replaceEmptyWithNever-test.js index aaf59e5c6fd9..95649fa3b944 100644 --- a/scripts/build-types/transforms/__tests__/replaceEmptyWithNever-test.js +++ b/scripts/build-types/transforms/flow/__tests__/replaceEmptyWithNever-test.js @@ -8,7 +8,7 @@ * @format */ -const replaceEmptyWithNever = require('../replaceEmptyWithNever.js'); +const replaceEmptyWithNever = require('../replaceEmptyWithNever'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/replaceNullablePropertiesWithUndefined-test.js b/scripts/build-types/transforms/flow/__tests__/replaceNullablePropertiesWithUndefined-test.js similarity index 97% rename from scripts/build-types/transforms/__tests__/replaceNullablePropertiesWithUndefined-test.js rename to scripts/build-types/transforms/flow/__tests__/replaceNullablePropertiesWithUndefined-test.js index c9854e9edd12..7cc1f113ebf9 100644 --- a/scripts/build-types/transforms/__tests__/replaceNullablePropertiesWithUndefined-test.js +++ b/scripts/build-types/transforms/flow/__tests__/replaceNullablePropertiesWithUndefined-test.js @@ -8,7 +8,7 @@ * @format */ -const replaceNullablePropertiesWithUndefined = require('../replaceNullablePropertiesWithUndefined.js'); +const replaceNullablePropertiesWithUndefined = require('../replaceNullablePropertiesWithUndefined'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/replaceRequiresWithImports-test.js b/scripts/build-types/transforms/flow/__tests__/replaceRequiresWithImports-test.js similarity index 99% rename from scripts/build-types/transforms/__tests__/replaceRequiresWithImports-test.js rename to scripts/build-types/transforms/flow/__tests__/replaceRequiresWithImports-test.js index 35214d642e5f..87951c324f0e 100644 --- a/scripts/build-types/transforms/__tests__/replaceRequiresWithImports-test.js +++ b/scripts/build-types/transforms/flow/__tests__/replaceRequiresWithImports-test.js @@ -8,7 +8,7 @@ * @format */ -const replaceRequiresWithImports = require('../replaceRequiresWithImports.js'); +const replaceRequiresWithImports = require('../replaceRequiresWithImports'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/replaceStringishWithString-test.js b/scripts/build-types/transforms/flow/__tests__/replaceStringishWithString-test.js similarity index 98% rename from scripts/build-types/transforms/__tests__/replaceStringishWithString-test.js rename to scripts/build-types/transforms/flow/__tests__/replaceStringishWithString-test.js index 8b683d9e6a07..fbd9e31a33e3 100644 --- a/scripts/build-types/transforms/__tests__/replaceStringishWithString-test.js +++ b/scripts/build-types/transforms/flow/__tests__/replaceStringishWithString-test.js @@ -8,7 +8,7 @@ * @format */ -const replaceStringishWithString = require('../replaceStringishWithString.js'); +const replaceStringishWithString = require('../replaceStringishWithString'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/__tests__/stripPrivateProperties-test.js b/scripts/build-types/transforms/flow/__tests__/stripPrivateProperties-test.js similarity index 99% rename from scripts/build-types/transforms/__tests__/stripPrivateProperties-test.js rename to scripts/build-types/transforms/flow/__tests__/stripPrivateProperties-test.js index 31f876b169ef..70945fa8abdc 100644 --- a/scripts/build-types/transforms/__tests__/stripPrivateProperties-test.js +++ b/scripts/build-types/transforms/flow/__tests__/stripPrivateProperties-test.js @@ -8,7 +8,7 @@ * @format */ -const stripPrivateProperties = require('../stripPrivateProperties.js'); +const stripPrivateProperties = require('../stripPrivateProperties'); const {parse, print} = require('hermes-transform'); const prettierOptions = {parser: 'babel'}; diff --git a/scripts/build-types/transforms/ensureNoUnprefixedProps.js b/scripts/build-types/transforms/flow/ensureNoUnprefixedProps.js similarity index 100% rename from scripts/build-types/transforms/ensureNoUnprefixedProps.js rename to scripts/build-types/transforms/flow/ensureNoUnprefixedProps.js diff --git a/scripts/build-types/transforms/reattachDocComments.js b/scripts/build-types/transforms/flow/reattachDocComments.js similarity index 100% rename from scripts/build-types/transforms/reattachDocComments.js rename to scripts/build-types/transforms/flow/reattachDocComments.js diff --git a/scripts/build-types/transforms/replaceEmptyWithNever.js b/scripts/build-types/transforms/flow/replaceEmptyWithNever.js similarity index 100% rename from scripts/build-types/transforms/replaceEmptyWithNever.js rename to scripts/build-types/transforms/flow/replaceEmptyWithNever.js diff --git a/scripts/build-types/transforms/replaceNullablePropertiesWithUndefined.js b/scripts/build-types/transforms/flow/replaceNullablePropertiesWithUndefined.js similarity index 100% rename from scripts/build-types/transforms/replaceNullablePropertiesWithUndefined.js rename to scripts/build-types/transforms/flow/replaceNullablePropertiesWithUndefined.js diff --git a/scripts/build-types/transforms/replaceRequiresWithImports.js b/scripts/build-types/transforms/flow/replaceRequiresWithImports.js similarity index 100% rename from scripts/build-types/transforms/replaceRequiresWithImports.js rename to scripts/build-types/transforms/flow/replaceRequiresWithImports.js diff --git a/scripts/build-types/transforms/replaceStringishWithString.js b/scripts/build-types/transforms/flow/replaceStringishWithString.js similarity index 100% rename from scripts/build-types/transforms/replaceStringishWithString.js rename to scripts/build-types/transforms/flow/replaceStringishWithString.js diff --git a/scripts/build-types/transforms/stripPrivateProperties.js b/scripts/build-types/transforms/flow/stripPrivateProperties.js similarity index 100% rename from scripts/build-types/transforms/stripPrivateProperties.js rename to scripts/build-types/transforms/flow/stripPrivateProperties.js diff --git a/scripts/build-types/transforms/__fixtures__/sortProperties.d.ts b/scripts/build-types/transforms/typescript/__fixtures__/sortProperties.d.ts similarity index 100% rename from scripts/build-types/transforms/__fixtures__/sortProperties.d.ts rename to scripts/build-types/transforms/typescript/__fixtures__/sortProperties.d.ts diff --git a/scripts/build-types/transforms/__fixtures__/sortTypeDefinitions.d.ts b/scripts/build-types/transforms/typescript/__fixtures__/sortTypeDefinitions.d.ts similarity index 100% rename from scripts/build-types/transforms/__fixtures__/sortTypeDefinitions.d.ts rename to scripts/build-types/transforms/typescript/__fixtures__/sortTypeDefinitions.d.ts diff --git a/scripts/build-types/transforms/__fixtures__/sortUnions.d.ts b/scripts/build-types/transforms/typescript/__fixtures__/sortUnions.d.ts similarity index 100% rename from scripts/build-types/transforms/__fixtures__/sortUnions.d.ts rename to scripts/build-types/transforms/typescript/__fixtures__/sortUnions.d.ts diff --git a/scripts/build-types/transforms/__tests__/__snapshots__/sortProperties-test.js.snap b/scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortProperties-test.js.snap similarity index 100% rename from scripts/build-types/transforms/__tests__/__snapshots__/sortProperties-test.js.snap rename to scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortProperties-test.js.snap diff --git a/scripts/build-types/transforms/__tests__/__snapshots__/sortTypeDefinitions-test.js.snap b/scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortTypeDefinitions-test.js.snap similarity index 100% rename from scripts/build-types/transforms/__tests__/__snapshots__/sortTypeDefinitions-test.js.snap rename to scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortTypeDefinitions-test.js.snap diff --git a/scripts/build-types/transforms/__tests__/__snapshots__/sortUnions-test.js.snap b/scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortUnions-test.js.snap similarity index 100% rename from scripts/build-types/transforms/__tests__/__snapshots__/sortUnions-test.js.snap rename to scripts/build-types/transforms/typescript/__tests__/__snapshots__/sortUnions-test.js.snap diff --git a/scripts/build-types/transforms/__tests__/renameDefaultExportedIdentifiers-test.js b/scripts/build-types/transforms/typescript/__tests__/renameDefaultExportedIdentifiers-test.js similarity index 98% rename from scripts/build-types/transforms/__tests__/renameDefaultExportedIdentifiers-test.js rename to scripts/build-types/transforms/typescript/__tests__/renameDefaultExportedIdentifiers-test.js index 532a4d5ed103..14104009f20c 100644 --- a/scripts/build-types/transforms/__tests__/renameDefaultExportedIdentifiers-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/renameDefaultExportedIdentifiers-test.js @@ -8,7 +8,7 @@ * @format */ -const renameDefaultExportedIdentifiersVisitor = require('../renameDefaultExportedIdentifiers.js'); +const renameDefaultExportedIdentifiersVisitor = require('../renameDefaultExportedIdentifiers'); const babel = require('@babel/core'); async function translate(code: string): Promise { diff --git a/scripts/build-types/transforms/__tests__/replaceDefaultExportName-test.js b/scripts/build-types/transforms/typescript/__tests__/replaceDefaultExportName-test.js similarity index 98% rename from scripts/build-types/transforms/__tests__/replaceDefaultExportName-test.js rename to scripts/build-types/transforms/typescript/__tests__/replaceDefaultExportName-test.js index 6dcf1667a342..394ac52b8dad 100644 --- a/scripts/build-types/transforms/__tests__/replaceDefaultExportName-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/replaceDefaultExportName-test.js @@ -8,7 +8,7 @@ * @format */ -const createReplaceDefaultExportName = require('../replaceDefaultExportName.js'); +const createReplaceDefaultExportName = require('../replaceDefaultExportName'); const babel = require('@babel/core'); const flowApiTranslator = require('flow-api-translator'); diff --git a/scripts/build-types/transforms/__tests__/sortProperties-test.js b/scripts/build-types/transforms/typescript/__tests__/sortProperties-test.js similarity index 93% rename from scripts/build-types/transforms/__tests__/sortProperties-test.js rename to scripts/build-types/transforms/typescript/__tests__/sortProperties-test.js index 56438795f30a..7d618be5199a 100644 --- a/scripts/build-types/transforms/__tests__/sortProperties-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/sortProperties-test.js @@ -8,7 +8,7 @@ * @format */ -const sortPropertiesVisitor = require('../sortProperties.js'); +const sortPropertiesVisitor = require('../sortProperties'); const babel = require('@babel/core'); const {promises: fs} = require('fs'); const path = require('path'); diff --git a/scripts/build-types/transforms/__tests__/sortTypeDefinitions-test.js b/scripts/build-types/transforms/typescript/__tests__/sortTypeDefinitions-test.js similarity index 99% rename from scripts/build-types/transforms/__tests__/sortTypeDefinitions-test.js rename to scripts/build-types/transforms/typescript/__tests__/sortTypeDefinitions-test.js index ba0bc32454d1..4bb40ed36286 100644 --- a/scripts/build-types/transforms/__tests__/sortTypeDefinitions-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/sortTypeDefinitions-test.js @@ -8,7 +8,7 @@ * @format */ -const sortTypeDefinitionsVisitor = require('../sortTypeDefinitions.js'); +const sortTypeDefinitionsVisitor = require('../sortTypeDefinitions'); const babel = require('@babel/core'); const {promises: fs} = require('fs'); const path = require('path'); diff --git a/scripts/build-types/transforms/__tests__/sortUnions-test.js b/scripts/build-types/transforms/typescript/__tests__/sortUnions-test.js similarity index 93% rename from scripts/build-types/transforms/__tests__/sortUnions-test.js rename to scripts/build-types/transforms/typescript/__tests__/sortUnions-test.js index b3522b8e8ffb..3c9da61064f4 100644 --- a/scripts/build-types/transforms/__tests__/sortUnions-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/sortUnions-test.js @@ -8,7 +8,7 @@ * @format */ -const sortUnionsVisitor = require('../sortUnions.js'); +const sortUnionsVisitor = require('../sortUnions'); const babel = require('@babel/core'); const {promises: fs} = require('fs'); const path = require('path'); diff --git a/scripts/build-types/transforms/__tests__/stripUnstableApis-test.js b/scripts/build-types/transforms/typescript/__tests__/stripUnstableApis-test.js similarity index 98% rename from scripts/build-types/transforms/__tests__/stripUnstableApis-test.js rename to scripts/build-types/transforms/typescript/__tests__/stripUnstableApis-test.js index f900502f1490..9ab88855c5b3 100644 --- a/scripts/build-types/transforms/__tests__/stripUnstableApis-test.js +++ b/scripts/build-types/transforms/typescript/__tests__/stripUnstableApis-test.js @@ -8,7 +8,7 @@ * @format */ -const stripUnstableApisVisitor = require('../stripUnstableApis.js'); +const stripUnstableApisVisitor = require('../stripUnstableApis'); const babel = require('@babel/core'); async function translate(code: string): Promise { diff --git a/scripts/build-types/transforms/renameDefaultExportedIdentifiers.js b/scripts/build-types/transforms/typescript/renameDefaultExportedIdentifiers.js similarity index 100% rename from scripts/build-types/transforms/renameDefaultExportedIdentifiers.js rename to scripts/build-types/transforms/typescript/renameDefaultExportedIdentifiers.js diff --git a/scripts/build-types/transforms/replaceDefaultExportName.js b/scripts/build-types/transforms/typescript/replaceDefaultExportName.js similarity index 100% rename from scripts/build-types/transforms/replaceDefaultExportName.js rename to scripts/build-types/transforms/typescript/replaceDefaultExportName.js diff --git a/scripts/build-types/transforms/sortProperties.js b/scripts/build-types/transforms/typescript/sortProperties.js similarity index 100% rename from scripts/build-types/transforms/sortProperties.js rename to scripts/build-types/transforms/typescript/sortProperties.js diff --git a/scripts/build-types/transforms/sortTypeDefinitions.js b/scripts/build-types/transforms/typescript/sortTypeDefinitions.js similarity index 100% rename from scripts/build-types/transforms/sortTypeDefinitions.js rename to scripts/build-types/transforms/typescript/sortTypeDefinitions.js diff --git a/scripts/build-types/transforms/sortUnions.js b/scripts/build-types/transforms/typescript/sortUnions.js similarity index 100% rename from scripts/build-types/transforms/sortUnions.js rename to scripts/build-types/transforms/typescript/sortUnions.js diff --git a/scripts/build-types/transforms/stripUnstableApis.js b/scripts/build-types/transforms/typescript/stripUnstableApis.js similarity index 100% rename from scripts/build-types/transforms/stripUnstableApis.js rename to scripts/build-types/transforms/typescript/stripUnstableApis.js diff --git a/scripts/build-types/translateSourceFile.js b/scripts/build-types/translateSourceFile.js index 877d457b09dc..fe9b3e34e3e8 100644 --- a/scripts/build-types/translateSourceFile.js +++ b/scripts/build-types/translateSourceFile.js @@ -13,7 +13,6 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse'; import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST'; const getDependencies = require('./resolution/getDependencies'); -const createReplaceDefaultExportName = require('./transforms/replaceDefaultExportName'); const babel = require('@babel/core'); const translate = require('flow-api-translator'); const {parse, print} = require('hermes-transform'); @@ -21,15 +20,17 @@ const {parse, print} = require('hermes-transform'); type PreTransformFn = ParseResult => Promise; const preTransforms: Array = [ - require('./transforms/stripPrivateProperties'), - require('./transforms/replaceRequiresWithImports'), - require('./transforms/replaceEmptyWithNever'), - require('./transforms/replaceStringishWithString'), - require('./transforms/replaceNullablePropertiesWithUndefined'), - require('./transforms/reattachDocComments'), - require('./transforms/ensureNoUnprefixedProps'), + require('./transforms/flow/stripPrivateProperties'), + require('./transforms/flow/replaceRequiresWithImports'), + require('./transforms/flow/replaceEmptyWithNever'), + require('./transforms/flow/replaceStringishWithString'), + require('./transforms/flow/replaceNullablePropertiesWithUndefined'), + require('./transforms/flow/reattachDocComments'), + require('./transforms/flow/ensureNoUnprefixedProps'), +]; +const postTransforms = (filePath: string): Array> => [ + require('./transforms/typescript/replaceDefaultExportName')(filePath), ]; -const postTransforms: Array> = []; const prettierOptions = {parser: 'babel'}; const unsupportedFeatureRegex = /Unsupported feature: Translating ".*" is currently not supported/; @@ -100,19 +101,12 @@ async function applyPreTransforms(source: ParseResult): Promise { }, Promise.resolve(source)); } -/** - * Apply post-transforms to .d.ts source code containing @build-types directives. - */ async function applyPostTransforms( source: string, filePath: string, ): Promise { const result = await babel.transformAsync(source, { - plugins: [ - '@babel/plugin-syntax-typescript', - ...postTransforms, - createReplaceDefaultExportName(filePath), - ], + plugins: ['@babel/plugin-syntax-typescript', ...postTransforms(filePath)], }); return result.code; diff --git a/scripts/build-types/transforms/__tests__/resolveCyclicImportsInDefinition-test.js b/scripts/build-types/utils/__tests__/resolveCyclicImportsInDefinition-test.js similarity index 99% rename from scripts/build-types/transforms/__tests__/resolveCyclicImportsInDefinition-test.js rename to scripts/build-types/utils/__tests__/resolveCyclicImportsInDefinition-test.js index 34cbaececf72..bcf353258ecb 100644 --- a/scripts/build-types/transforms/__tests__/resolveCyclicImportsInDefinition-test.js +++ b/scripts/build-types/utils/__tests__/resolveCyclicImportsInDefinition-test.js @@ -8,7 +8,7 @@ * @format */ -const resolveCyclicImportsInDefinition = require('../resolveCyclicImportsInDefinition.js'); +const resolveCyclicImportsInDefinition = require('../resolveCyclicImportsInDefinition'); const path = require('path'); const packagesPath = '/path/to/package/definition/files'; diff --git a/scripts/build-types/transforms/resolveCyclicImportsInDefinition.js b/scripts/build-types/utils/resolveCyclicImportsInDefinition.js similarity index 100% rename from scripts/build-types/transforms/resolveCyclicImportsInDefinition.js rename to scripts/build-types/utils/resolveCyclicImportsInDefinition.js From 929e486ba790e84dbf51cc4faa0940373c885998 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 24 Jun 2025 08:38:00 -0700 Subject: [PATCH 2/2] Update all transforms to apply sequentially Summary: To avoid unexpected behaviour, apply all Babel transforms within `build-types` sequentially, so that each transform plugin has an accurate starting AST. Changelog: [Internal] Reviewed By: j-piasecki Differential Revision: D77148444 --- scripts/build-types/BuildApiSnapshot.js | 17 ++----- scripts/build-types/translateSourceFile.js | 19 ++----- .../utils/applyBabelTransformsSeq.js | 49 +++++++++++++++++++ 3 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 scripts/build-types/utils/applyBabelTransformsSeq.js diff --git a/scripts/build-types/BuildApiSnapshot.js b/scripts/build-types/BuildApiSnapshot.js index dcff93ef05b9..92d14a68ab69 100644 --- a/scripts/build-types/BuildApiSnapshot.js +++ b/scripts/build-types/BuildApiSnapshot.js @@ -15,8 +15,8 @@ const {PACKAGES_DIR, REACT_NATIVE_PACKAGE_DIR} = require('../consts'); const {isGitRepo} = require('../scm-utils'); const {API_EXTRACTOR_CONFIG_FILE, TYPES_OUTPUT_DIR} = require('./config'); const apiSnapshotTemplate = require('./templates/ReactNativeApi.d.ts-template.js'); +const applyBabelTransformsSeq = require('./utils/applyBabelTransformsSeq'); const resolveCyclicImportsInDefinition = require('./utils/resolveCyclicImportsInDefinition'); -const babel = require('@babel/core'); const { Extractor, ExtractorConfig, @@ -193,7 +193,7 @@ async function preparePackagesInTempDir( await Promise.all( typeDefs.map(async file => { const source = await fs.readFile(file, 'utf-8'); - const transformed = await applyPostTransforms( + const transformed = await applyBabelTransformsSeq( source, inputFilesPostTransforms, ); @@ -241,7 +241,7 @@ async function getCleanedUpRollup(tempDirectory: string) { .replace(/^\s+$/gm, '') // Clear whitespace-only lines .replace(/\n+/gm, '\n'); // Collapse empty lines - const transformedRollup = await applyPostTransforms( + const transformedRollup = await applyBabelTransformsSeq( cleanedRollup, postTransforms, ); @@ -255,17 +255,6 @@ async function getCleanedUpRollup(tempDirectory: string) { return formattedRollup; } -async function applyPostTransforms( - inSrc: string, - transforms: $ReadOnlyArray>, -): Promise { - const result = await babel.transformAsync(inSrc, { - plugins: ['@babel/plugin-syntax-typescript', ...transforms], - }); - - return result.code; -} - async function generateConfigFiles(tempDirectory: string) { // generate tsconfig const tsConfig = { diff --git a/scripts/build-types/translateSourceFile.js b/scripts/build-types/translateSourceFile.js index fe9b3e34e3e8..16cd92a81b78 100644 --- a/scripts/build-types/translateSourceFile.js +++ b/scripts/build-types/translateSourceFile.js @@ -13,7 +13,7 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse'; import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST'; const getDependencies = require('./resolution/getDependencies'); -const babel = require('@babel/core'); +const applyBabelTransformsSeq = require('./utils/applyBabelTransformsSeq'); const translate = require('flow-api-translator'); const {parse, print} = require('hermes-transform'); @@ -80,7 +80,10 @@ async function translateSourceFile( } // Apply post-transforms - const result = await applyPostTransforms(tsDefResult, filePath); + const result = await applyBabelTransformsSeq( + tsDefResult, + postTransforms(filePath), + ); return { result, @@ -95,21 +98,9 @@ async function applyPreTransforms(source: ParseResult): Promise { const code = transformed.astWasMutated ? await print(transformed.ast, transformed.mutatedCode, prettierOptions) : transformed.mutatedCode; - return parse(code); }); }, Promise.resolve(source)); } -async function applyPostTransforms( - source: string, - filePath: string, -): Promise { - const result = await babel.transformAsync(source, { - plugins: ['@babel/plugin-syntax-typescript', ...postTransforms(filePath)], - }); - - return result.code; -} - module.exports = translateSourceFile; diff --git a/scripts/build-types/utils/applyBabelTransformsSeq.js b/scripts/build-types/utils/applyBabelTransformsSeq.js new file mode 100644 index 000000000000..f7933fa422bb --- /dev/null +++ b/scripts/build-types/utils/applyBabelTransformsSeq.js @@ -0,0 +1,49 @@ +/** + * 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 + */ + +import type {PluginObj} from '@babel/core'; + +import * as babel from '@babel/core'; + +/** + * Apply an array of Babel transforms to a TypeScript source in order. + */ +async function applyBabelTransformsSeq( + source: string, + transforms: $ReadOnlyArray>, +): Promise { + const parsed = await babel.parseAsync(source, { + plugins: ['@babel/plugin-syntax-typescript'], + }); + + const finalAST = await transforms.reduce((input, transform) => { + return input.then(async ast => { + const result = await babel.transformFromAstAsync(ast, source, { + plugins: [transform], + ast: true, + code: false, + }); + if (result == null) { + throw new Error('Unexpected null result from Babel transform'); + } + // $FlowIgnore[incompatible-cast] + return result.ast as BabelNodeFile; + }); + }, Promise.resolve(parsed)); + + const result = await babel.transformFromAstAsync(finalAST, source, { + code: true, + ast: false, + }); + + return result.code; +} + +module.exports = applyBabelTransformsSeq;