File tree 3 files changed +67
-4
lines changed
scripts/build/build-types
3 files changed +67
-4
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -18,7 +18,26 @@ import type {ParseResult} from 'hermes-transform/dist/transform/parse';
18
18
const { transformAST} = require ( 'hermes-transform/dist/transform/transformAST' ) ;
19
19
20
20
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
+ } ,
22
41
} ) ;
23
42
24
43
async function stripPrivateProperties (
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ import type {TransformASTResult} from 'hermes-transform/dist/transform/transform
15
15
*/
16
16
17
17
const translate = require ( 'flow-api-translator' ) ;
18
- const { parse} = require ( 'hermes-transform' ) ;
18
+ const { parse, print } = require ( 'hermes-transform' ) ;
19
19
20
20
/*::
21
21
type TransformFn = (ParseResult) => Promise<TransformASTResult>;
@@ -40,7 +40,6 @@ async function translateSourceFile(
40
40
41
41
// Apply pre-transforms
42
42
const preTransformResult = await applyTransforms ( parsed , preTransforms ) ;
43
-
44
43
// Translate to TypeScript defs
45
44
return await translate . translateFlowToTSDef (
46
45
preTransformResult . code ,
@@ -55,10 +54,15 @@ async function applyTransforms(
55
54
return transforms . reduce ( ( input , transform ) => {
56
55
return input . then ( async result => {
57
56
const transformed = await transform ( result ) ;
57
+ const printed = await print (
58
+ transformed . ast ,
59
+ transformed . mutatedCode ,
60
+ prettierOptions ,
61
+ ) ;
58
62
return {
59
63
...result ,
60
64
ast : transformed . ast ,
61
- code : transformed . mutatedCode ,
65
+ code : printed ,
62
66
} ;
63
67
} ) ;
64
68
} , Promise . resolve ( source ) ) ;
You can’t perform that action at this time.
0 commit comments