Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
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
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', () => {
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() {},
};
"
`);
});
});
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
8 changes: 6 additions & 2 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 Down Expand Up @@ -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));
Expand Down
Loading