Skip to content

Commit 3f2691c

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Extract the parseFile function in the typescript and flow parsers (#35318)
Summary: This PR aims to extract the parseFile function in the typescript and flow parsers. This is to solve the problem described [here](#35158 (comment)) and help with the work done in #34872. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Extract the parseFile function in the typescript and flow parsers Pull Request resolved: #35318 Test Plan: yarn flow: <img width="496" alt="image" src="https://user-images.githubusercontent.com/40902940/206518024-83084c3d-ab0d-4a04-810a-d40270add4b0.png"> yarn lint: <img width="495" alt="image" src="https://user-images.githubusercontent.com/40902940/206518076-9e07eafe-db61-4c6e-8aaa-f92f190cf4f3.png"> yarn test: <img width="389" alt="image" src="https://user-images.githubusercontent.com/40902940/206518118-5633b28c-b79b-4421-80f7-de1e03fb8ff2.png"> Reviewed By: cortinico Differential Revision: D41248581 Pulled By: cipolleschi fbshipit-source-id: f5b878a28a7de612fcdd1528f064b44f668503af
1 parent 2344860 commit 3f2691c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+382
-289
lines changed

packages/eslint-plugin-specs/react-native-modules.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ const ERRORS = {
2323

2424
let RNModuleParser;
2525
let RNParserUtils;
26+
let RNFlowParser;
2627

2728
function requireModuleParser() {
28-
if (RNModuleParser == null || RNParserUtils == null) {
29+
if (RNModuleParser == null || RNParserUtils == null || RNFlowParser == null) {
2930
// If using this externally, we leverage @react-native/codegen as published form
3031
if (!PACKAGE_USAGE) {
3132
const config = {
@@ -36,6 +37,7 @@ function requireModuleParser() {
3637
withBabelRegister(config, () => {
3738
RNModuleParser = require('@react-native/codegen/src/parsers/flow/modules');
3839
RNParserUtils = require('@react-native/codegen/src/parsers/utils');
40+
RNFlowParser = require('@react-native/codegen/src/parsers/flow/parser');
3941
});
4042
} else {
4143
const config = {
@@ -46,13 +48,15 @@ function requireModuleParser() {
4648
withBabelRegister(config, () => {
4749
RNModuleParser = require('@react-native/codegen/lib/parsers/flow/modules');
4850
RNParserUtils = require('@react-native/codegen/lib/parsers/flow/utils');
51+
RNFlowParser = require('@react-native/codegen/lib/parsers/flow/parser');
4952
});
5053
}
5154
}
5255

5356
return {
5457
buildModuleSchema: RNModuleParser.buildModuleSchema,
5558
createParserErrorCapturer: RNParserUtils.createParserErrorCapturer,
59+
parser: new RNFlowParser.FlowParser(),
5660
};
5761
}
5862

@@ -127,7 +131,7 @@ function rule(context) {
127131
});
128132
}
129133

130-
const {buildModuleSchema, createParserErrorCapturer} =
134+
const {buildModuleSchema, createParserErrorCapturer, parser} =
131135
requireModuleParser();
132136
const flowParser = require('flow-parser');
133137

@@ -137,7 +141,7 @@ function rule(context) {
137141
const ast = flowParser.parse(sourceCode, {enums: true});
138142

139143
tryParse(() => {
140-
buildModuleSchema(hasteModuleName, ast, tryParse);
144+
buildModuleSchema(hasteModuleName, ast, tryParse, parser);
141145
});
142146

143147
parsingErrors.forEach(error => {

packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GenerateComponentDescriptorH');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GenerateComponentHObjCpp');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GenerateComponentHObjCpp can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GenerateEventEmitterCpp');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GenerateEventEmitterH');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GenerateEventEmitterH can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GeneratePropsCpp');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GeneratePropsCpp can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GeneratePropsH');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GeneratePropsH can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GeneratePropsJavaDelegate');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019

2120
const fixtures = fs.readdirSync(FIXTURE_DIR);
2221

22+
const parser = new FlowParser();
23+
2324
fixtures.forEach(fixture => {
2425
it(`GeneratePropsJavaDelegate can generate for '${fixture}'`, () => {
2526
const libName = 'RNCodegenModuleFixtures';
26-
const schema = parseFile(
27-
`${FIXTURE_DIR}/${fixture}`,
28-
FlowParser.buildSchema,
29-
);
27+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
3028
const output = generator.generate(libName, schema);
3129
expect(Object.fromEntries(output)).toMatchSnapshot();
3230
});

packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GeneratePropsJavaInterface');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019
const fixtures = fs.readdirSync(FIXTURE_DIR);
2120

21+
const parser = new FlowParser();
22+
2223
fixtures.forEach(fixture => {
2324
it(`GeneratePropsJavaInterface can generate for '${fixture}'`, () => {
2425
const libName = 'RNCodegenModuleFixtures';
25-
const schema = parseFile(
26-
`${FIXTURE_DIR}/${fixture}`,
27-
FlowParser.buildSchema,
28-
);
26+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
2927
const output = generator.generate(libName, schema, undefined, false);
3028
expect(Object.fromEntries(output)).toMatchSnapshot();
3129
});

packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@
1111

1212
'use strict';
1313

14-
const {parseFile} = require('../../../src/parsers/utils');
15-
const FlowParser = require('../../../src/parsers/flow');
14+
const {FlowParser} = require('../../../src/parsers/flow/parser');
1615
const generator = require('../../../src/generators/components/GenerateShadowNodeCpp');
1716
const fs = require('fs');
1817

1918
const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`;
2019
const fixtures = fs.readdirSync(FIXTURE_DIR);
2120

21+
const parser = new FlowParser();
22+
2223
fixtures.forEach(fixture => {
2324
it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => {
2425
const libName = 'RNCodegenModuleFixtures';
25-
const schema = parseFile(
26-
`${FIXTURE_DIR}/${fixture}`,
27-
FlowParser.buildSchema,
28-
);
26+
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
2927
const output = generator.generate(libName, schema, undefined, false);
3028
expect(Object.fromEntries(output)).toMatchSnapshot();
3129
});

0 commit comments

Comments
 (0)