diff --git a/scripts/build/build-types/transforms/__tests__/stripPrivateProperties-test.js b/scripts/build/build-types/transforms/__tests__/stripPrivateProperties-test.js new file mode 100644 index 00000000000000..df97f5719a02c1 --- /dev/null +++ b/scripts/build/build-types/transforms/__tests__/stripPrivateProperties-test.js @@ -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 { + 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() {}, + }; + " + `); + }); +}); diff --git a/scripts/build/build-types/transforms/stripPrivateProperties.js b/scripts/build/build-types/transforms/stripPrivateProperties.js index e7ceb7e232e7cf..f5f38601d36798 100644 --- a/scripts/build/build-types/transforms/stripPrivateProperties.js +++ b/scripts/build/build-types/transforms/stripPrivateProperties.js @@ -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( diff --git a/scripts/build/build-types/translateSourceFile.js b/scripts/build/build-types/translateSourceFile.js index ef1ad061e33ad6..6a98d5e525b5df 100644 --- a/scripts/build/build-types/translateSourceFile.js +++ b/scripts/build/build-types/translateSourceFile.js @@ -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; @@ -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));