-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 Differential Revision: D68892853
- Loading branch information
1 parent
252294b
commit 9b2e46a
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
scripts/build/build-types/transforms/__tests__/stripPrivateProperties-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {}, | ||
}; | ||
" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters