mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Optional post-transform pipeline for .d.ts files generated by build-types script (#49634)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49634 Changelog: [Internal] Reviewed By: huntie Differential Revision: D69992512 fbshipit-source-id: 7fc004e9460498de236a833aea5265a5fac5f490
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ac57ec4d0b
commit
9d92b277bf
@@ -45,6 +45,7 @@
|
||||
"@babel/core": "^7.25.2",
|
||||
"@babel/eslint-parser": "^7.25.1",
|
||||
"@babel/generator": "^7.25.0",
|
||||
"@babel/plugin-syntax-typescript": "^7.25.4",
|
||||
"@babel/plugin-transform-regenerator": "^7.24.7",
|
||||
"@babel/preset-env": "^7.25.3",
|
||||
"@babel/preset-flow": "^7.24.7",
|
||||
|
||||
@@ -9,21 +9,24 @@
|
||||
* @oncall react_native
|
||||
*/
|
||||
|
||||
import type {PluginObj} from '@babel/core';
|
||||
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 translate = require('flow-api-translator');
|
||||
const {parse, print} = require('hermes-transform');
|
||||
|
||||
type TransformFn = ParseResult => Promise<TransformASTResult>;
|
||||
type PreTransformFn = ParseResult => Promise<TransformASTResult>;
|
||||
|
||||
const preTransforms: Array<TransformFn> = [
|
||||
const preTransforms: Array<PreTransformFn> = [
|
||||
require('./transforms/stripPrivateProperties'),
|
||||
require('./transforms/replaceRequiresWithImports'),
|
||||
require('./transforms/replaceEmptyWithNever'),
|
||||
require('./transforms/replaceStringishWithString'),
|
||||
];
|
||||
const postTransforms: Array<PluginObj<mixed>> = [];
|
||||
const prettierOptions = {parser: 'babel'};
|
||||
const unsupportedFeatureRegex =
|
||||
/Unsupported feature: Translating ".*" is currently not supported/;
|
||||
@@ -47,7 +50,7 @@ async function translateSourceFile(
|
||||
const parsed = await parse(source);
|
||||
|
||||
// Apply pre-transforms
|
||||
const preTransformResult = await applyTransforms(parsed, preTransforms);
|
||||
const preTransformResult = await applyPreTransforms(parsed);
|
||||
|
||||
// Translate to Flow defs (prunes non-type imports)
|
||||
const flowDefResult = await translate.translateFlowToFlowDef(
|
||||
@@ -62,27 +65,27 @@ async function translateSourceFile(
|
||||
);
|
||||
|
||||
// Translate to TypeScript defs
|
||||
const result = await translate.translateFlowToTSDef(
|
||||
const tsDefResult = await translate.translateFlowToTSDef(
|
||||
flowDefResult,
|
||||
prettierOptions,
|
||||
);
|
||||
|
||||
const unsupportedFeatureMatch = result.match(unsupportedFeatureRegex);
|
||||
const unsupportedFeatureMatch = tsDefResult.match(unsupportedFeatureRegex);
|
||||
if (unsupportedFeatureMatch != null) {
|
||||
throw new Error(`Error: ${unsupportedFeatureMatch[0]}`);
|
||||
}
|
||||
|
||||
// Apply post-transforms
|
||||
const result = await applyPostTransforms(tsDefResult);
|
||||
|
||||
return {
|
||||
result,
|
||||
dependencies,
|
||||
};
|
||||
}
|
||||
|
||||
async function applyTransforms(
|
||||
source: ParseResult,
|
||||
transforms: $ReadOnlyArray<TransformFn>,
|
||||
): Promise<ParseResult> {
|
||||
return transforms.reduce((input, transform) => {
|
||||
async function applyPreTransforms(source: ParseResult): Promise<ParseResult> {
|
||||
return preTransforms.reduce((input, transform) => {
|
||||
return input.then(async result => {
|
||||
const transformed = await transform(result);
|
||||
const code = transformed.astWasMutated
|
||||
@@ -94,4 +97,20 @@ async function applyTransforms(
|
||||
}, Promise.resolve(source));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
const result = await babel.transformAsync(source, {
|
||||
plugins: ['@babel/plugin-syntax-typescript', ...postTransforms],
|
||||
});
|
||||
|
||||
return result.code;
|
||||
}
|
||||
|
||||
module.exports = translateSourceFile;
|
||||
|
||||
@@ -160,6 +160,11 @@
|
||||
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.24.8.tgz#94ee67e8ec0e5d44ea7baeb51e571bd26af07878"
|
||||
integrity sha512-FFWx5142D8h2Mgr/iPVGH5G7w6jDn4jUSpZTyDnQO0Yn7Ks2Kuz6Pci8H6MPCoUJegd/UZQ3tAvfLCxQSnWWwg==
|
||||
|
||||
"@babel/helper-plugin-utils@^7.25.9":
|
||||
version "7.26.5"
|
||||
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.26.5.tgz#18580d00c9934117ad719392c4f6585c9333cc35"
|
||||
integrity sha512-RS+jZcRdZdRFzMyr+wcsaqOmld1/EqTghfaBGQQd/WnRdzdlvSZ//kF7U8VQTxf1ynZ4cjUcYgjVGx13ewNPMg==
|
||||
|
||||
"@babel/helper-remap-async-to-generator@^7.24.7", "@babel/helper-remap-async-to-generator@^7.25.0":
|
||||
version "7.25.0"
|
||||
resolved "https://registry.yarnpkg.com/@babel/helper-remap-async-to-generator/-/helper-remap-async-to-generator-7.25.0.tgz#d2f0fbba059a42d68e5e378feaf181ef6055365e"
|
||||
@@ -482,6 +487,13 @@
|
||||
dependencies:
|
||||
"@babel/helper-plugin-utils" "^7.24.8"
|
||||
|
||||
"@babel/plugin-syntax-typescript@^7.25.4":
|
||||
version "7.25.9"
|
||||
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.25.9.tgz#67dda2b74da43727cf21d46cf9afef23f4365399"
|
||||
integrity sha512-hjMgRy5hb8uJJjUcdWunWVcoi9bGpJp8p5Ol1229PoN6aytsLwNMgmdftO23wnCLMfVmTwZDWMPNq/D1SY60JQ==
|
||||
dependencies:
|
||||
"@babel/helper-plugin-utils" "^7.25.9"
|
||||
|
||||
"@babel/plugin-syntax-unicode-sets-regex@^7.18.6":
|
||||
version "7.18.6"
|
||||
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-unicode-sets-regex/-/plugin-syntax-unicode-sets-regex-7.18.6.tgz#d49a3b3e6b52e5be6740022317580234a6a47357"
|
||||
|
||||
Reference in New Issue
Block a user