Skip to content

Commit 1520738

Browse files
coadofacebook-github-bot
authored andcommitted
Add transform that strips private properties in build types script (#49060)
Summary: 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
1 parent 82cc465 commit 1520738

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
const stripPrivateProperties = require('../stripPrivateProperties.js');
13+
const {parse, print} = require('hermes-transform');
14+
15+
const prettierOptions = {parser: 'babel'};
16+
17+
async function translate(code: string): Promise<string> {
18+
const parsed = await parse(code);
19+
const result = await stripPrivateProperties(parsed);
20+
return print(result.ast, result.mutatedCode, prettierOptions);
21+
}
22+
23+
describe('stripPrivateProperties', () => {
24+
test('should strip private properties', async () => {
25+
const code = `const Foo = {
26+
foo: 'foo',
27+
bar() {},
28+
_privateFoo: 'privateFoo',
29+
_privateBar() {},
30+
}`;
31+
const result = await translate(code);
32+
expect(result).toMatchInlineSnapshot(`
33+
"const Foo = {
34+
foo: \\"foo\\",
35+
bar() {},
36+
};
37+
"
38+
`);
39+
});
40+
});

scripts/build/build-types/transforms/stripPrivateProperties.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,26 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse';
1818
const {transformAST} = require('hermes-transform/dist/transform/transformAST');
1919

2020
const visitors /*: TransformVisitor */ = context => ({
21-
// TODO
21+
ObjectTypeProperty(node) /*: void */ {
22+
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
23+
context.removeNode(node);
24+
}
25+
},
26+
Property(node) /*: void */ {
27+
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
28+
context.removeNode(node);
29+
}
30+
},
31+
PropertyDefinition(node) /*: void */ {
32+
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
33+
context.removeNode(node);
34+
}
35+
},
36+
MethodDefinition(node) /*: void */ {
37+
if (node.key.type === 'Identifier' && node.key.name.startsWith('_')) {
38+
context.removeNode(node);
39+
}
40+
},
2241
});
2342

2443
async function stripPrivateProperties(

scripts/build/build-types/translateSourceFile.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {TransformASTResult} from 'hermes-transform/dist/transform/transform
1515
*/
1616

1717
const translate = require('flow-api-translator');
18-
const {parse} = require('hermes-transform');
18+
const {parse, print} = require('hermes-transform');
1919

2020
/*::
2121
type TransformFn = (ParseResult) => Promise<TransformASTResult>;
@@ -55,10 +55,14 @@ async function applyTransforms(
5555
return transforms.reduce((input, transform) => {
5656
return input.then(async result => {
5757
const transformed = await transform(result);
58+
const code = transformed.astWasMutated
59+
? await print(transformed.ast, transformed.mutatedCode, prettierOptions)
60+
: transformed.mutatedCode;
61+
5862
return {
5963
...result,
6064
ast: transformed.ast,
61-
code: transformed.mutatedCode,
65+
code,
6266
};
6367
});
6468
}, Promise.resolve(source));

0 commit comments

Comments
 (0)