Skip to content

Commit 4628150

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Extract parseString and parseModuleFixture functions in typescript and flow parsers (#35928)
Summary: This PR aims to extract parseString and parseModuleFixture functions into the typescript and flow parsers. This task was proposed in #35158 and helps #34872. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Extract parseString and parseModuleFixture functions in typescript and flow parsers Pull Request resolved: #35928 Test Plan: yarn test: <img width="386" alt="image" src="https://user-images.githubusercontent.com/40902940/213889984-f0cadaff-4472-42d6-b55b-4901023aad1e.png"> yarn flow: <img width="166" alt="image" src="https://user-images.githubusercontent.com/40902940/213889974-21ac2483-2731-4cb1-a2b5-195d98619649.png"> yarn lint: <img width="514" alt="image" src="https://user-images.githubusercontent.com/40902940/213889980-090af354-346f-4a9c-90bc-7006899f0819.png"> Reviewed By: jacdebug Differential Revision: D42673866 Pulled By: cipolleschi fbshipit-source-id: f1b5f8a7b3944e7e8223b25c0fb9bf4e8b512aa7
1 parent 6f7428e commit 4628150

File tree

11 files changed

+96
-129
lines changed

11 files changed

+96
-129
lines changed

packages/babel-plugin-codegen/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@
99

1010
'use strict';
1111

12-
let flowParser, typeScriptParser, RNCodegen;
12+
let FlowParser, TypeScriptParser, RNCodegen;
1313

1414
const {basename} = require('path');
1515

1616
try {
17-
flowParser = require('@react-native/codegen/src/parsers/flow');
18-
typeScriptParser = require('@react-native/codegen/src/parsers/typescript');
17+
FlowParser =
18+
require('@react-native/codegen/src/parsers/flow/parser').FlowParser;
19+
TypeScriptParser =
20+
require('@react-native/codegen/src/parsers/typescript/parser').TypeScriptParser;
1921
RNCodegen = require('@react-native/codegen/src/generators/RNCodegen');
2022
} catch (e) {
2123
// Fallback to lib when source doesn't exit (e.g. when installed as a dev dependency)
22-
flowParser = require('@react-native/codegen/lib/parsers/flow');
23-
typeScriptParser = require('@react-native/codegen/lib/parsers/typescript');
24+
FlowParser =
25+
require('@react-native/codegen/lib/parsers/flow/parser').FlowParser;
26+
TypeScriptParser =
27+
require('@react-native/codegen/lib/parsers/typescript/parser').TypeScriptParser;
2428
RNCodegen = require('@react-native/codegen/lib/generators/RNCodegen');
2529
}
2630

31+
const flowParser = new FlowParser();
32+
const typeScriptParser = new TypeScriptParser();
33+
2734
function parseFile(filename, code) {
2835
if (filename.endsWith('js')) {
2936
return flowParser.parseString(code);

packages/react-native-codegen/src/parsers/flow/index.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-e2e-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import type {
1515
NativeModuleParamTypeAnnotation,
1616
} from '../../../../CodegenSchema';
1717

18-
const {parseString} = require('../../index.js');
18+
const invariant = require('invariant');
19+
1920
const {unwrapNullable} = require('../../../parsers-commons');
2021
const {
2122
UnsupportedGenericParserError,
2223
UnsupportedTypeAnnotationParserError,
2324
UnnamedFunctionParamParserError,
2425
MissingTypeParameterGenericParserError,
2526
} = require('../../../errors');
26-
const invariant = require('invariant');
27+
const {FlowParser} = require('../../parser');
28+
29+
const flowParser = new FlowParser();
2730

2831
type PrimitiveTypeAnnotationType =
2932
| 'StringTypeAnnotation'
@@ -1229,7 +1232,7 @@ describe('Flow Module Parser', () => {
12291232
});
12301233

12311234
function parseModule(source: string) {
1232-
const schema = parseString(source, `${MODULE_NAME}.js`);
1235+
const schema = flowParser.parseString(source, `${MODULE_NAME}.js`);
12331236
const module = schema.modules.NativeFoo;
12341237
invariant(
12351238
module.type === 'NativeModule',

packages/react-native-codegen/src/parsers/flow/modules/__tests__/module-parser-snapshot-test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
'use strict';
1313

14-
const FlowParser = require('../../index.js');
14+
const {FlowParser} = require('../../parser');
1515

1616
const fixtures = require('../__test_fixtures__/fixtures.js');
1717
const failureFixtures = require('../__test_fixtures__/failures.js');
1818

19+
const flowParser = new FlowParser();
20+
1921
jest.mock('fs', () => ({
2022
readFileSync: filename => {
2123
// Jest in the OSS does not allow to capture variables in closures.
@@ -32,7 +34,7 @@ describe('RN Codegen Flow Parser', () => {
3234
.sort()
3335
.forEach(fixtureName => {
3436
it(`can generate fixture ${fixtureName}`, () => {
35-
const schema = FlowParser.parseModuleFixture(fixtureName);
37+
const schema = flowParser.parseModuleFixture(fixtureName);
3638
const serializedSchema = JSON.stringify(schema, null, 2).replace(
3739
/"/g,
3840
"'",
@@ -47,7 +49,7 @@ describe('RN Codegen Flow Parser', () => {
4749
.forEach(fixtureName => {
4850
it(`Fails with error message ${fixtureName}`, () => {
4951
expect(() => {
50-
FlowParser.parseModuleFixture(fixtureName);
52+
flowParser.parseModuleFixture(fixtureName);
5153
}).toThrowErrorMatchingSnapshot();
5254
});
5355
});

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class FlowParser implements Parser {
9696
parseFile(filename: string): SchemaType {
9797
const contents = fs.readFileSync(filename, 'utf8');
9898

99+
return this.parseString(contents, filename);
100+
}
101+
102+
parseString(contents: string, filename: ?string): SchemaType {
99103
return buildSchema(
100104
contents,
101105
filename,
@@ -107,6 +111,12 @@ class FlowParser implements Parser {
107111
);
108112
}
109113

114+
parseModuleFixture(filename: string): SchemaType {
115+
const contents = fs.readFileSync(filename, 'utf8');
116+
117+
return this.parseString(contents, 'path/NativeSampleTurboModule.js');
118+
}
119+
110120
getAst(contents: string): $FlowFixMe {
111121
return flowParser.parse(contents, {
112122
enums: true,

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,24 @@ export interface Parser {
7878
types: $FlowFixMe,
7979
): UnionTypeAnnotationMemberType[];
8080
/**
81-
* Given the content of a file and options, it returns an AST.
82-
* @parameter contents: the content of the file.
83-
* @returns: the AST of the file (given in program property for typescript).
81+
* Given the name of a file, it returns a Schema.
82+
* @parameter filename: the name of the file.
83+
* @returns: the Schema of the file.
8484
*/
8585
parseFile(filename: string): SchemaType;
86+
/**
87+
* Given the content of a file, it returns a Schema.
88+
* @parameter contents: the content of the file.
89+
* @parameter filename: the name of the file.
90+
* @returns: the Schema of the file.
91+
*/
92+
parseString(contents: string, filename: ?string): SchemaType;
93+
/**
94+
* Given the name of a file, it returns a Schema.
95+
* @parameter filename: the name of the file.
96+
* @returns: the Schema of the file.
97+
*/
98+
parseModuleFixture(filename: string): SchemaType;
8699

87100
/**
88101
* Given the content of a file, it returns an AST.

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ const {
2626
UnsupportedObjectPropertyTypeAnnotationParserError,
2727
} = require('./errors');
2828

29+
const schemaMock = {
30+
modules: {
31+
StringPropNativeComponentView: {
32+
type: 'Component',
33+
components: {
34+
StringPropNativeComponentView: {
35+
extendsProps: [],
36+
events: [],
37+
props: [],
38+
commands: [],
39+
},
40+
},
41+
},
42+
},
43+
};
44+
2945
export class MockedParser implements Parser {
3046
typeParameterInstantiation: string = 'TypeParameterInstantiation';
3147

@@ -74,21 +90,15 @@ export class MockedParser implements Parser {
7490
}
7591

7692
parseFile(filename: string): SchemaType {
77-
return {
78-
modules: {
79-
StringPropNativeComponentView: {
80-
type: 'Component',
81-
components: {
82-
StringPropNativeComponentView: {
83-
extendsProps: [],
84-
events: [],
85-
props: [],
86-
commands: [],
87-
},
88-
},
89-
},
90-
},
91-
};
93+
return schemaMock;
94+
}
95+
96+
parseString(contents: string, filename: ?string): SchemaType {
97+
return schemaMock;
98+
}
99+
100+
parseModuleFixture(filename: string): SchemaType {
101+
return schemaMock;
92102
}
93103

94104
getAst(contents: string): $FlowFixMe {

packages/react-native-codegen/src/parsers/typescript/index.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-e2e-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import type {
1515
NativeModuleParamTypeAnnotation,
1616
} from '../../../../CodegenSchema';
1717

18-
const {parseString} = require('../../index.js');
18+
const invariant = require('invariant');
19+
1920
const {unwrapNullable} = require('../../../parsers-commons');
2021
const {
2122
UnsupportedGenericParserError,
2223
UnsupportedTypeAnnotationParserError,
2324
UnnamedFunctionParamParserError,
2425
MissingTypeParameterGenericParserError,
2526
} = require('../../../errors');
26-
const invariant = require('invariant');
27+
const {TypeScriptParser} = require('../../parser');
28+
29+
const typescriptParser = new TypeScriptParser();
2730

2831
type PrimitiveTypeAnnotationType =
2932
| 'StringTypeAnnotation'
@@ -1228,7 +1231,7 @@ describe('TypeScript Module Parser', () => {
12281231
});
12291232

12301233
function parseModule(source: string) {
1231-
const schema = parseString(source, `${MODULE_NAME}.ts`);
1234+
const schema = typescriptParser.parseString(source, `${MODULE_NAME}.ts`);
12321235
const module = schema.modules.NativeFoo;
12331236
invariant(
12341237
module.type === 'NativeModule',

packages/react-native-codegen/src/parsers/typescript/modules/__tests__/typescript-module-parser-snapshot-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
'use strict';
1313

14-
const TypeScriptParser = require('../../index.js');
14+
const {TypeScriptParser} = require('../../parser');
15+
1516
const fixtures = require('../__test_fixtures__/fixtures.js');
1617
const failureFixtures = require('../__test_fixtures__/failures.js');
1718

19+
const typeScriptParser = new TypeScriptParser();
20+
1821
jest.mock('fs', () => ({
1922
readFileSync: filename => {
2023
// Jest in the OSS does not allow to capture variables in closures.
@@ -31,7 +34,7 @@ describe('RN Codegen TypeScript Parser', () => {
3134
.sort()
3235
.forEach(fixtureName => {
3336
it(`can generate fixture ${fixtureName}`, () => {
34-
const schema = TypeScriptParser.parseModuleFixture(fixtureName);
37+
const schema = typeScriptParser.parseModuleFixture(fixtureName);
3538
const serializedSchema = JSON.stringify(schema, null, 2).replace(
3639
/"/g,
3740
"'",
@@ -46,7 +49,7 @@ describe('RN Codegen TypeScript Parser', () => {
4649
.forEach(fixtureName => {
4750
it(`Fails with error message ${fixtureName}`, () => {
4851
expect(() => {
49-
TypeScriptParser.parseModuleFixture(fixtureName);
52+
typeScriptParser.parseModuleFixture(fixtureName);
5053
}).toThrowErrorMatchingSnapshot();
5154
});
5255
});

0 commit comments

Comments
 (0)