mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add a post transform to rename the default export names (#50397)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50397 Changelog: [Internal] Reviewed By: huntie Differential Revision: D72170157 fbshipit-source-id: 9067a83c5e941ef8ade7b846799d9a09f7dc4e62
This commit is contained in:
committed by
Facebook GitHub Bot
parent
0615b056e6
commit
b4e64546fd
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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<string> {
|
||||
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;"
|
||||
`);
|
||||
});
|
||||
});
|
||||
@@ -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<mixed> {
|
||||
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;
|
||||
@@ -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<ParseResult> {
|
||||
/**
|
||||
* Apply post-transforms to .d.ts source code containing @build-types directives.
|
||||
*/
|
||||
async function applyPostTransforms(source: string): Promise<string> {
|
||||
if (!source.includes('@build-types')) {
|
||||
// Exiting early, as there are no @build-types directives
|
||||
return source;
|
||||
}
|
||||
|
||||
async function applyPostTransforms(
|
||||
source: string,
|
||||
filePath: string,
|
||||
): Promise<string> {
|
||||
const result = await babel.transformAsync(source, {
|
||||
plugins: ['@babel/plugin-syntax-typescript', ...postTransforms],
|
||||
plugins: [
|
||||
'@babel/plugin-syntax-typescript',
|
||||
...postTransforms,
|
||||
createReplaceDefaultExportName(filePath),
|
||||
],
|
||||
});
|
||||
|
||||
return result.code;
|
||||
|
||||
Reference in New Issue
Block a user