Skip to content

Commit

Permalink
chore: Export codegen parseFile function (#35000)
Browse files Browse the repository at this point in the history
Summary:
This PR export the content of the `parseFile` into a parseFile function accepting a callback to buildSchema properly in `parsers/utils.js` as requested on #34872.

## Changelog

[Internal] [Changed] - Export ` parseFile` in to a `parseFile` function in `parsers/utils.js`

Pull Request resolved: #35000

Test Plan:
Run `yarn jest react-native-codegen` and ensure CI is green

![image](https://user-images.githubusercontent.com/11707729/196051689-1b61838c-477c-4be5-8df0-9f5969fcf90d.png)

Reviewed By: cortinico

Differential Revision: D40424857

Pulled By: cipolleschi

fbshipit-source-id: a700033d674b8be8e1af942dedf73155ea3ca025
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Oct 19, 2022
1 parent 8f484c3 commit 376ffac
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateComponentDescriptorH');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateComponentHObjCpp');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateComponentHObjCpp can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateEventEmitterCpp');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateEventEmitterH');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateEventEmitterH can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GeneratePropsCpp');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GeneratePropsCpp can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GeneratePropsH');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GeneratePropsH can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GeneratePropsJavaDelegate');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GeneratePropsJavaDelegate can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GeneratePropsJavaInterface');
const fs = require('fs');

Expand All @@ -21,7 +22,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GeneratePropsJavaInterface can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema, undefined, false);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateShadowNodeCpp');
const fs = require('fs');

Expand All @@ -21,7 +22,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema, undefined, false);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateShadowNodeH');
const fs = require('fs');

Expand All @@ -21,7 +22,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateShadowNodeH can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema, undefined, false);
expect(Object.fromEntries(output)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/components/GenerateViewConfigJs');
const fs = require('fs');

Expand All @@ -22,7 +23,10 @@ const fixtures = fs.readdirSync(FIXTURE_DIR);
fixtures.forEach(fixture => {
it(`GenerateViewConfigJs can generate for '${fixture}'`, () => {
const libName = 'RNCodegenModuleFixtures';
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
const schema = parseFile(
`${FIXTURE_DIR}/${fixture}`,
FlowParser.buildSchema,
);
const output = generator.generate(libName, schema);
expect(output).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

'use strict';

const parser = require('../../../src/parsers/flow');
const {parseFile} = require('../../../src/parsers/utils');
const FlowParser = require('../../../src/parsers/flow');
const generator = require('../../../src/generators/modules/GenerateModuleObjCpp');
const fs = require('fs');

Expand All @@ -23,7 +24,10 @@ function getModules(): SchemaType {
const filenames: Array<string> = fs.readdirSync(FIXTURE_DIR);
return filenames.reduce<SchemaType>(
(accumulator, file) => {
const schema = parser.parseFile(`${FIXTURE_DIR}/${file}`);
const schema = parseFile(
`${FIXTURE_DIR}/${file}`,
FlowParser.buildSchema,
);
return {
modules: {
...accumulator.modules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';
import type {SchemaType} from '../../CodegenSchema.js';

const {parseFile} = require('../../parsers/utils');
const FlowParser = require('../../parsers/flow');
const TypeScriptParser = require('../../parsers/typescript');
const fs = require('fs');
Expand All @@ -29,9 +30,10 @@ function combineSchemas(files: Array<string>): SchemaType {
const isTypeScript =
path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';

const schema = isTypeScript
? TypeScriptParser.parseFile(filename)
: FlowParser.parseFile(filename);
const schema = parseFile(
filename,
isTypeScript ? TypeScriptParser.buildSchema : FlowParser.buildSchema,
);

if (schema && schema.modules) {
merged.modules = {...merged.modules, ...schema.modules};
Expand Down
8 changes: 5 additions & 3 deletions packages/react-native-codegen/src/cli/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const path = require('path');
const {parseFile} = require('../../parsers/utils');
const FlowParser = require('../../parsers/flow');
const TypeScriptParser = require('../../parsers/typescript');

Expand All @@ -22,9 +23,10 @@ function parseFiles(files: Array<string>) {
console.log(
filename,
JSON.stringify(
isTypeScript
? TypeScriptParser.parseFile(filename)
: FlowParser.parseFile(filename),
parseFile(
filename,
isTypeScript ? TypeScriptParser.buildSchema : FlowParser.buildSchema,
),
null,
2,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

const FlowParser = require('../../index.js');
const {parseFile} = require('../../../utils.js');
const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');
jest.mock('fs', () => ({
Expand All @@ -30,7 +31,7 @@ describe('RN Codegen Flow Parser', () => {
.sort()
.forEach(fixtureName => {
it(`can generate fixture ${fixtureName}`, () => {
const schema = FlowParser.parseFile(fixtureName);
const schema = parseFile(fixtureName, FlowParser.buildSchema);
const serializedSchema = JSON.stringify(schema, null, 2).replace(
/"/g,
"'",
Expand All @@ -44,7 +45,7 @@ describe('RN Codegen Flow Parser', () => {
.forEach(fixtureName => {
it(`Fails with error message ${fixtureName}`, () => {
expect(() => {
FlowParser.parseFile(fixtureName);
parseFile(fixtureName, FlowParser.buildSchema);
}).toThrowErrorMatchingSnapshot();
});
});
Expand Down
8 changes: 1 addition & 7 deletions packages/react-native-codegen/src/parsers/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ function buildSchema(contents: string, filename: ?string): SchemaType {
);
}

function parseFile(filename: string): SchemaType {
const contents = fs.readFileSync(filename, 'utf8');

return buildSchema(contents, filename);
}

function parseModuleFixture(filename: string): SchemaType {
const contents = fs.readFileSync(filename, 'utf8');

Expand All @@ -102,7 +96,7 @@ function parseString(contents: string, filename: ?string): SchemaType {
}

module.exports = {
parseFile,
buildSchema,
parseModuleFixture,
parseString,
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

const TypeScriptParser = require('../../index.js');
const {parseFile} = require('../../../utils.js');
const fixtures = require('../__test_fixtures__/fixtures.js');
const failureFixtures = require('../__test_fixtures__/failures.js');
jest.mock('fs', () => ({
Expand All @@ -30,7 +31,7 @@ describe('RN Codegen TypeScript Parser', () => {
.sort()
.forEach(fixtureName => {
it(`can generate fixture ${fixtureName}`, () => {
const schema = TypeScriptParser.parseFile(fixtureName);
const schema = parseFile(fixtureName, TypeScriptParser.buildSchema);
const serializedSchema = JSON.stringify(schema, null, 2).replace(
/"/g,
"'",
Expand All @@ -44,7 +45,7 @@ describe('RN Codegen TypeScript Parser', () => {
.forEach(fixtureName => {
it(`Fails with error message ${fixtureName}`, () => {
expect(() => {
TypeScriptParser.parseFile(fixtureName);
parseFile(fixtureName, TypeScriptParser.buildSchema);
}).toThrowErrorMatchingSnapshot();
});
});
Expand Down
Loading

0 comments on commit 376ffac

Please sign in to comment.