Skip to content

Commit

Permalink
Add transform that strips private properties in build types script
Browse files Browse the repository at this point in the history
Summary:
## Changelog:
[Internal] -  Added transform that strips private properties in build-types script

Differential Revision: D68892853
  • Loading branch information
coado authored and facebook-github-bot committed Jan 30, 2025
1 parent 252294b commit 55b3c56
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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', () => {
it('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() {},
};
"
`);
});
});
21 changes: 20 additions & 1 deletion scripts/build/build-types/transforms/stripPrivateProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions scripts/build/build-types/translateSourceFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -40,7 +40,6 @@ async function translateSourceFile(

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

// Translate to TypeScript defs
return await translate.translateFlowToTSDef(
preTransformResult.code,
Expand All @@ -55,10 +54,15 @@ async function applyTransforms(
return transforms.reduce((input, transform) => {
return input.then(async result => {
const transformed = await transform(result);
const printed = await print(
transformed.ast,
transformed.mutatedCode,
prettierOptions,
);
return {
...result,
ast: transformed.ast,
code: transformed.mutatedCode,
code: printed,
};
});
}, Promise.resolve(source));
Expand Down

0 comments on commit 55b3c56

Please sign in to comment.