Skip to content

Commit b4e6454

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Add a post transform to rename the default export names (#50397)
Summary: Pull Request resolved: #50397 Changelog: [Internal] Reviewed By: huntie Differential Revision: D72170157 fbshipit-source-id: 9067a83c5e941ef8ade7b846799d9a09f7dc4e62
1 parent 0615b05 commit b4e6454

File tree

5 files changed

+86
-12
lines changed

5 files changed

+86
-12
lines changed

packages/react-native/Libraries/Animated/nodes/AnimatedValue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
1212
import type {PlatformConfig} from '../AnimatedPlatformConfig';
13-
import type Animation, {EndCallback} from '../animations/Animation';
13+
import type Animation from '../animations/Animation';
14+
import type {EndCallback} from '../animations/Animation';
1415
import type {InterpolationConfigType} from './AnimatedInterpolation';
1516
import type AnimatedNode from './AnimatedNode';
1617
import type {AnimatedNodeConfig} from './AnimatedNode';

packages/virtualized-lists/Lists/VirtualizeUtils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
'use strict';
1212

13-
import type ListMetricsAggregator, {
14-
CellMetricProps,
15-
} from './ListMetricsAggregator';
13+
import type ListMetricsAggregator from './ListMetricsAggregator';
14+
import type {CellMetricProps} from './ListMetricsAggregator';
1615

1716
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
1817

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
const createReplaceDefaultExportName = require('../replaceDefaultExportName.js');
13+
const babel = require('@babel/core');
14+
const flowApiTranslator = require('flow-api-translator');
15+
16+
const prettierOptions = {parser: 'babel'};
17+
18+
async function translate(code: string, fileName: string): Promise<string> {
19+
const tsDef = await flowApiTranslator.translateFlowToTSDef(
20+
code,
21+
prettierOptions,
22+
);
23+
const result = await babel.transformAsync(tsDef, {
24+
plugins: [
25+
'@babel/plugin-syntax-typescript',
26+
createReplaceDefaultExportName(fileName),
27+
],
28+
});
29+
30+
return result.code;
31+
}
32+
33+
describe('replaceDefaultExportName', () => {
34+
test('should replace default export name with unambiguous one', async () => {
35+
const code = `export default Foo;`;
36+
const result = await translate(code, 'mock/path/to/module/Foo.js');
37+
expect(result).toMatchInlineSnapshot(`
38+
"declare const Foo_DEFAULT: typeof Foo;
39+
export default Foo_DEFAULT;"
40+
`);
41+
});
42+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import type {PluginObj} from '@babel/core';
13+
14+
function createReplaceDefaultExportName(filePath: string): PluginObj<mixed> {
15+
return {
16+
visitor: {
17+
Identifier(node) {
18+
const fileName = filePath.split('/').pop() ?? '';
19+
const moduleName = fileName.split('.')[0];
20+
21+
if (node.node.name === '$$EXPORT_DEFAULT_DECLARATION$$') {
22+
node.node.name = `${moduleName}_DEFAULT`;
23+
}
24+
},
25+
},
26+
};
27+
}
28+
29+
module.exports = createReplaceDefaultExportName;

scripts/build/build-types/translateSourceFile.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse';
1414
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
1515

1616
const getDependencies = require('./resolution/getDependencies');
17+
const createReplaceDefaultExportName = require('./transforms/replaceDefaultExportName');
1718
const babel = require('@babel/core');
1819
const translate = require('flow-api-translator');
1920
const {parse, print} = require('hermes-transform');
@@ -77,7 +78,7 @@ async function translateSourceFile(
7778
}
7879

7980
// Apply post-transforms
80-
const result = await applyPostTransforms(tsDefResult);
81+
const result = await applyPostTransforms(tsDefResult, filePath);
8182

8283
return {
8384
result,
@@ -101,14 +102,16 @@ async function applyPreTransforms(source: ParseResult): Promise<ParseResult> {
101102
/**
102103
* Apply post-transforms to .d.ts source code containing @build-types directives.
103104
*/
104-
async function applyPostTransforms(source: string): Promise<string> {
105-
if (!source.includes('@build-types')) {
106-
// Exiting early, as there are no @build-types directives
107-
return source;
108-
}
109-
105+
async function applyPostTransforms(
106+
source: string,
107+
filePath: string,
108+
): Promise<string> {
110109
const result = await babel.transformAsync(source, {
111-
plugins: ['@babel/plugin-syntax-typescript', ...postTransforms],
110+
plugins: [
111+
'@babel/plugin-syntax-typescript',
112+
...postTransforms,
113+
createReplaceDefaultExportName(filePath),
114+
],
112115
});
113116

114117
return result.code;

0 commit comments

Comments
 (0)