diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js index 3b901fe3f2c..1727b9f463b 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js @@ -10,7 +10,8 @@ import type {EventSubscription} from '../../vendor/emitter/EventEmitter'; import type {PlatformConfig} from '../AnimatedPlatformConfig'; -import type Animation, {EndCallback} from '../animations/Animation'; +import type Animation from '../animations/Animation'; +import type {EndCallback} from '../animations/Animation'; import type {InterpolationConfigType} from './AnimatedInterpolation'; import type AnimatedNode from './AnimatedNode'; import type {AnimatedNodeConfig} from './AnimatedNode'; diff --git a/packages/virtualized-lists/Lists/VirtualizeUtils.js b/packages/virtualized-lists/Lists/VirtualizeUtils.js index 6697d479691..2264507c3a2 100644 --- a/packages/virtualized-lists/Lists/VirtualizeUtils.js +++ b/packages/virtualized-lists/Lists/VirtualizeUtils.js @@ -10,9 +10,8 @@ 'use strict'; -import type ListMetricsAggregator, { - CellMetricProps, -} from './ListMetricsAggregator'; +import type ListMetricsAggregator from './ListMetricsAggregator'; +import type {CellMetricProps} from './ListMetricsAggregator'; import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags'; diff --git a/scripts/build/build-types/transforms/__tests__/replaceDefaultExportName-test.js b/scripts/build/build-types/transforms/__tests__/replaceDefaultExportName-test.js new file mode 100644 index 00000000000..9b82301719d --- /dev/null +++ b/scripts/build/build-types/transforms/__tests__/replaceDefaultExportName-test.js @@ -0,0 +1,42 @@ +/** + * 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 + * @oncall react_native + */ + +const createReplaceDefaultExportName = require('../replaceDefaultExportName.js'); +const babel = require('@babel/core'); +const flowApiTranslator = require('flow-api-translator'); + +const prettierOptions = {parser: 'babel'}; + +async function translate(code: string, fileName: string): Promise { + const tsDef = await flowApiTranslator.translateFlowToTSDef( + code, + prettierOptions, + ); + const result = await babel.transformAsync(tsDef, { + plugins: [ + '@babel/plugin-syntax-typescript', + createReplaceDefaultExportName(fileName), + ], + }); + + return result.code; +} + +describe('replaceDefaultExportName', () => { + test('should replace default export name with unambiguous one', async () => { + const code = `export default Foo;`; + const result = await translate(code, 'mock/path/to/module/Foo.js'); + expect(result).toMatchInlineSnapshot(` + "declare const Foo_DEFAULT: typeof Foo; + export default Foo_DEFAULT;" + `); + }); +}); diff --git a/scripts/build/build-types/transforms/replaceDefaultExportName.js b/scripts/build/build-types/transforms/replaceDefaultExportName.js new file mode 100644 index 00000000000..b1c3e4e863e --- /dev/null +++ b/scripts/build/build-types/transforms/replaceDefaultExportName.js @@ -0,0 +1,29 @@ +/** + * 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 + * @oncall react_native + */ + +import type {PluginObj} from '@babel/core'; + +function createReplaceDefaultExportName(filePath: string): PluginObj { + return { + visitor: { + Identifier(node) { + const fileName = filePath.split('/').pop() ?? ''; + const moduleName = fileName.split('.')[0]; + + if (node.node.name === '$$EXPORT_DEFAULT_DECLARATION$$') { + node.node.name = `${moduleName}_DEFAULT`; + } + }, + }, + }; +} + +module.exports = createReplaceDefaultExportName; diff --git a/scripts/build/build-types/translateSourceFile.js b/scripts/build/build-types/translateSourceFile.js index f353b70e860..cad01225d39 100644 --- a/scripts/build/build-types/translateSourceFile.js +++ b/scripts/build/build-types/translateSourceFile.js @@ -14,6 +14,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 createReplaceDefaultExportName = require('./transforms/replaceDefaultExportName'); const babel = require('@babel/core'); const translate = require('flow-api-translator'); const {parse, print} = require('hermes-transform'); @@ -77,7 +78,7 @@ async function translateSourceFile( } // Apply post-transforms - const result = await applyPostTransforms(tsDefResult); + const result = await applyPostTransforms(tsDefResult, filePath); return { result, @@ -101,14 +102,16 @@ async function applyPreTransforms(source: ParseResult): Promise { /** * Apply post-transforms to .d.ts source code containing @build-types directives. */ -async function applyPostTransforms(source: string): Promise { - if (!source.includes('@build-types')) { - // Exiting early, as there are no @build-types directives - return source; - } - +async function applyPostTransforms( + source: string, + filePath: string, +): Promise { const result = await babel.transformAsync(source, { - plugins: ['@babel/plugin-syntax-typescript', ...postTransforms], + plugins: [ + '@babel/plugin-syntax-typescript', + ...postTransforms, + createReplaceDefaultExportName(filePath), + ], }); return result.code;