Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions scripts/build/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/

const {PACKAGES_DIR, REPO_ROOT} = require('../consts');
const translateSourceFile = require('./build-types/translateSourceFile');
const chalk = require('chalk');
const translate = require('flow-api-translator');
const {promises: fs} = require('fs');
const glob = require('glob');
const micromatch = require('micromatch');
Expand Down Expand Up @@ -62,38 +62,35 @@ async function main() {
'\n',
);

for (const file of files) {
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
continue;
}

const buildPath = getBuildPath(file);
const source = await fs.readFile(file, 'utf-8');
const prettierConfig = {parser: 'babel'};

await fs.mkdir(path.dirname(buildPath), {recursive: true});

try {
const typescriptDef = await translate.translateFlowToTSDef(
source,
prettierConfig,
);

if (
/Unsupported feature: Translating ".*" is currently not supported/.test(
typescriptDef,
)
) {
throw new Error(
'Syntax unsupported by flow-api-translator used in ' + file,
);
await Promise.all(
files.map(async file => {
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
return;
}

await fs.writeFile(buildPath, typescriptDef);
} catch (e) {
console.error(`Failed to build ${path.relative(REPO_ROOT, file)}`);
}
}
const buildPath = getBuildPath(file);
const source = await fs.readFile(file, 'utf-8');
await fs.mkdir(path.dirname(buildPath), {recursive: true});

try {
const typescriptDef = await translateSourceFile(source);

if (
/Unsupported feature: Translating ".*" is currently not supported/.test(
typescriptDef,
)
) {
throw new Error(
'Syntax unsupported by flow-api-translator used in ' + file,
);
}

await fs.writeFile(buildPath, typescriptDef);
} catch (e) {
console.error(`Failed to build ${path.relative(REPO_ROOT, file)}`);
}
}),
);
}

function getPackageName(file /*: string */) /*: string */ {
Expand Down
30 changes: 30 additions & 0 deletions scripts/build/build-types/transforms/stripPrivateProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 {TransformVisitor} from 'hermes-transform';
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
import type {ParseResult} from 'hermes-transform/dist/transform/parse';
*/

const {transformAST} = require('hermes-transform/dist/transform/transformAST');

const visitors /*: TransformVisitor */ = context => ({
// TODO
});

async function stripPrivateProperties(
source /*: ParseResult */,
) /*: Promise<TransformASTResult> */ {
return transformAST(source, visitors);
}

module.exports = stripPrivateProperties;
67 changes: 67 additions & 0 deletions scripts/build/build-types/translateSourceFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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
* @format
* @oncall react_native
*/

/*::
import type {ParseResult} from 'hermes-transform/dist/transform/parse';
import type {TransformASTResult} from 'hermes-transform/dist/transform/transformAST';
*/

const translate = require('flow-api-translator');
const {parse} = require('hermes-transform');

/*::
type TransformFn = (ParseResult) => Promise<TransformASTResult>;
*/

const preTransforms /*: Array<TransformFn> */ = [
require('./transforms/stripPrivateProperties'),
];
const prettierOptions = {parser: 'babel'};

/**
* Translate the public API of a Flow source file to TypeScript definition.
*
* This uses [flow-api-translator](https://www.npmjs.com/package/flow-api-translator),
* and applies extra transformations such as stripping private properties.
*/
async function translateSourceFile(
source /*: string */,
) /*: Promise<string> */ {
// Parse Flow source
const parsed = await parse(source);

// Apply pre-transforms
const preTransformResult = await applyTransforms(parsed, preTransforms);

// Translate to TypeScript defs
return await translate.translateFlowToTSDef(
preTransformResult.code,
prettierOptions,
);
}

async function applyTransforms(
source /*: ParseResult */,
transforms /*: $ReadOnlyArray<TransformFn> */,
) /*: Promise<ParseResult> */ {
return transforms.reduce((input, transform) => {
return input.then(async result => {
const transformed = await transform(result);
return {
...result,
ast: transformed.ast,
code: transformed.mutatedCode,
};
});
}, Promise.resolve(source));
}

module.exports = translateSourceFile;