Skip to content

Commit 55b3c56

Browse files
coadofacebook-github-bot
authored andcommitted
Add transform that strips private properties in build types script
Summary: ## Changelog: [Internal] - Added transform that strips private properties in build-types script Differential Revision: D68892853
1 parent 252294b commit 55b3c56

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
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+
it('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

+7-3
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>;
@@ -40,7 +40,6 @@ async function translateSourceFile(
4040

4141
// Apply pre-transforms
4242
const preTransformResult = await applyTransforms(parsed, preTransforms);
43-
4443
// Translate to TypeScript defs
4544
return await translate.translateFlowToTSDef(
4645
preTransformResult.code,
@@ -55,10 +54,15 @@ async function applyTransforms(
5554
return transforms.reduce((input, transform) => {
5655
return input.then(async result => {
5756
const transformed = await transform(result);
57+
const printed = await print(
58+
transformed.ast,
59+
transformed.mutatedCode,
60+
prettierOptions,
61+
);
5862
return {
5963
...result,
6064
ast: transformed.ast,
61-
code: transformed.mutatedCode,
65+
code: printed,
6266
};
6367
});
6468
}, Promise.resolve(source));

0 commit comments

Comments
 (0)