mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
chore: Export codegen parseFile function (#35000)
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 https://github.com/facebook/react-native/issues/34872. ## Changelog [Internal] [Changed] - Export ` parseFile` in to a `parseFile` function in `parsers/utils.js` Pull Request resolved: https://github.com/facebook/react-native/pull/35000 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green  Reviewed By: cortinico Differential Revision: D40424857 Pulled By: cipolleschi fbshipit-source-id: a700033d674b8be8e1af942dedf73155ea3ca025
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8f484c3a62
commit
376ffac759
Vendored
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
+6
-2
@@ -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');
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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');
|
||||
@@ -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};
|
||||
|
||||
+5
-3
@@ -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');
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
|
||||
Vendored
+3
-2
@@ -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', () => ({
|
||||
@@ -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,
|
||||
"'",
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
+1
-7
@@ -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');
|
||||
|
||||
@@ -102,7 +96,7 @@ function parseString(contents: string, filename: ?string): SchemaType {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseFile,
|
||||
buildSchema,
|
||||
parseModuleFixture,
|
||||
parseString,
|
||||
};
|
||||
|
||||
+3
-2
@@ -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', () => ({
|
||||
@@ -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,
|
||||
"'",
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -95,12 +95,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');
|
||||
|
||||
@@ -112,7 +106,7 @@ function parseString(contents: string, filename: ?string): SchemaType {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseFile,
|
||||
buildSchema,
|
||||
parseModuleFixture,
|
||||
parseString,
|
||||
};
|
||||
|
||||
+11
-1
@@ -12,10 +12,10 @@
|
||||
|
||||
import type {ComponentSchemaBuilderConfig} from './flow/components/schema';
|
||||
import type {NativeModuleSchema, SchemaType} from '../CodegenSchema';
|
||||
|
||||
const {ParserError} = require('./errors');
|
||||
const {wrapModuleSchema} = require('./parsers-commons');
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const invariant = require('invariant');
|
||||
|
||||
@@ -95,6 +95,15 @@ function verifyPlatforms(
|
||||
};
|
||||
}
|
||||
|
||||
function parseFile(
|
||||
filename: string,
|
||||
callback: (contents: string, filename: string) => SchemaType,
|
||||
): SchemaType {
|
||||
const contents = fs.readFileSync(filename, 'utf8');
|
||||
|
||||
return callback(contents, filename);
|
||||
}
|
||||
|
||||
// TODO(T108222691): Use flow-types for @babel/parser
|
||||
function visit(
|
||||
astNode: $FlowFixMe,
|
||||
@@ -180,6 +189,7 @@ module.exports = {
|
||||
extractNativeModuleName,
|
||||
createParserErrorCapturer,
|
||||
verifyPlatforms,
|
||||
parseFile,
|
||||
visit,
|
||||
buildSchemaFromConfigType,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user