Add transform that strips private properties in build types script (#49060)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49060

We want to hide private properties from JS public API interface. The stripPrivateProperties transform removes all private nodes of type ObjectTypeProperty, Property, PropertyDefinition and MethodDefinition. There is also a change in transforms reducer that incorporates `print` function from hermes-transform which modifies the code base on the transformed ast (transformed.mutatedCode seems to be a code before the transform operation).

## Changelog:
[Internal] -  Added transform that strips private properties in build-types script

Reviewed By: huntie

Differential Revision: D68892853

fbshipit-source-id: 5035fd4339aa6294d972e7aff0eb563f48d4c3d2
This commit is contained in:
Dawid Małecki
2025-01-31 02:02:08 -08:00
committed by Facebook GitHub Bot
parent 64c2a52ca9
commit 4ccb2f2aa2
3 changed files with 66 additions and 3 deletions
@@ -0,0 +1,40 @@
/**
* 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 stripPrivateProperties = require('../stripPrivateProperties.js');
const {parse, print} = require('hermes-transform');
const prettierOptions = {parser: 'babel'};
async function translate(code: string): Promise<string> {
const parsed = await parse(code);
const result = await stripPrivateProperties(parsed);
return print(result.ast, result.mutatedCode, prettierOptions);
}
describe('stripPrivateProperties', () => {
test('should strip private properties', async () => {
const code = `const Foo = {
foo: 'foo',
bar() {},
_privateFoo: 'privateFoo',
_privateBar() {},
}`;
const result = await translate(code);
expect(result).toMatchInlineSnapshot(`
"const Foo = {
foo: \\"foo\\",
bar() {},
};
"
`);
});
});
@@ -18,7 +18,26 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse';
const {transformAST} = require('hermes-transform/dist/transform/transformAST');
const visitors /*: TransformVisitor */ = context => ({
// TODO
ObjectTypeProperty(node) /*: void */ {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
Property(node) /*: void */ {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
PropertyDefinition(node) /*: void */ {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
MethodDefinition(node) /*: void */ {
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
context.removeNode(node);
}
},
});
async function stripPrivateProperties(
@@ -15,7 +15,7 @@ import type {TransformASTResult} from 'hermes-transform/dist/transform/transform
*/
const translate = require('flow-api-translator');
const {parse} = require('hermes-transform');
const {parse, print} = require('hermes-transform');
/*::
type TransformFn = (ParseResult) => Promise<TransformASTResult>;
@@ -55,10 +55,14 @@ async function applyTransforms(
return transforms.reduce((input, transform) => {
return input.then(async result => {
const transformed = await transform(result);
const code = transformed.astWasMutated
? await print(transformed.ast, transformed.mutatedCode, prettierOptions)
: transformed.mutatedCode;
return {
...result,
ast: transformed.ast,
code: transformed.mutatedCode,
code,
};
});
}, Promise.resolve(source));