From 8582e20466fb2805a9be5cf43fb5bdd694bf7c29 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Sat, 12 Nov 2022 14:25:35 +0100 Subject: [PATCH 01/11] refactor: only import buildSchema in tests --- .../components/GenerateComponentDescriptorH-test.js | 7 ++----- .../__tests__/components/GenerateComponentHObjCpp-test.js | 7 ++----- .../__tests__/components/GenerateEventEmitterCpp-test.js | 7 ++----- .../__tests__/components/GenerateEventEmitterH-test.js | 7 ++----- .../e2e/__tests__/components/GeneratePropsCpp-test.js | 7 ++----- .../e2e/__tests__/components/GeneratePropsH-test.js | 7 ++----- .../components/GeneratePropsJavaDelegate-test.js | 7 ++----- .../components/GeneratePropsJavaInterface-test.js | 7 ++----- .../__tests__/components/GenerateShadowNodeCpp-test.js | 7 ++----- .../e2e/__tests__/components/GenerateShadowNodeH-test.js | 7 ++----- .../e2e/__tests__/components/GenerateViewConfigJs-test.js | 7 ++----- .../e2e/__tests__/modules/GenerateModuleObjCpp-test.js | 7 ++----- .../src/cli/combine/combine-js-to-schema.js | 8 +++++--- packages/react-native-codegen/src/cli/parser/parser.js | 8 +++++--- .../flow/components/__tests__/component-parser-test.js | 6 +++--- .../__tests__/typescript-component-parser-test.js | 6 +++--- 16 files changed, 40 insertions(+), 72 deletions(-) diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js index 8d35cca3843e58..5dffc6d3cb087c 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentDescriptorH'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js index 89d14fc74a63ee..8209e9f8a1b941 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentHObjCpp'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateComponentHObjCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js index 603c77b5660705..f7fe22e5747bf0 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterCpp'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js index 05b1082297cc8c..972a43df1a4f21 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterH'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateEventEmitterH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js index 131d4f4fd6688a..2c33698b9089bc 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsCpp'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GeneratePropsCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js index 0600f6bb70d115..52f80baae374a3 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsH'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GeneratePropsH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js index 3f87386c4a0e15..9a609bc57a4218 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaDelegate'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GeneratePropsJavaDelegate can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js index 40fcfe3d9d02b8..8c8de96682d698 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaInterface'); const fs = require('fs'); @@ -22,10 +22,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GeneratePropsJavaInterface can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js index a4fa38bddab7c6..38e53996b28f62 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeCpp'); const fs = require('fs'); @@ -22,10 +22,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js index 0af0ba44f4b2aa..41897e53241ce7 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeH'); const fs = require('fs'); @@ -22,10 +22,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateShadowNodeH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js index fa4d90797ba40d..232a9405b89237 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateViewConfigJs'); const fs = require('fs'); @@ -23,10 +23,7 @@ const fixtures = fs.readdirSync(FIXTURE_DIR); fixtures.forEach(fixture => { it(`GenerateViewConfigJs can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile( - `${FIXTURE_DIR}/${fixture}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(output).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js index a6bb549d056ac2..888ca94618735e 100644 --- a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js @@ -12,7 +12,7 @@ 'use strict'; const {parseFile} = require('../../../src/parsers/utils'); -const FlowParser = require('../../../src/parsers/flow'); +const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/modules/GenerateModuleObjCpp'); const fs = require('fs'); @@ -24,10 +24,7 @@ function getModules(): SchemaType { const filenames: Array = fs.readdirSync(FIXTURE_DIR); return filenames.reduce( (accumulator, file) => { - const schema = parseFile( - `${FIXTURE_DIR}/${file}`, - FlowParser.buildSchema, - ); + const schema = parseFile(`${FIXTURE_DIR}/${file}`, buildSchema); return { modules: { ...accumulator.modules, diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js index e4bc8780db6743..93f6a515039220 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js @@ -12,8 +12,10 @@ import type {SchemaType} from '../../CodegenSchema.js'; const {parseFile} = require('../../parsers/utils'); -const FlowParser = require('../../parsers/flow'); -const TypeScriptParser = require('../../parsers/typescript'); +const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); +const { + buildSchema: buildSchemaForTypescript, +} = require('../../parsers/typescript'); const fs = require('fs'); const path = require('path'); @@ -32,7 +34,7 @@ function combineSchemas(files: Array): SchemaType { const schema = parseFile( filename, - isTypeScript ? TypeScriptParser.buildSchema : FlowParser.buildSchema, + isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, ); if (schema && schema.modules) { diff --git a/packages/react-native-codegen/src/cli/parser/parser.js b/packages/react-native-codegen/src/cli/parser/parser.js index 1afc70035bc0c2..e0fb6cddae7b4f 100644 --- a/packages/react-native-codegen/src/cli/parser/parser.js +++ b/packages/react-native-codegen/src/cli/parser/parser.js @@ -12,8 +12,10 @@ const path = require('path'); const {parseFile} = require('../../parsers/utils'); -const FlowParser = require('../../parsers/flow'); -const TypeScriptParser = require('../../parsers/typescript'); +const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); +const { + buildSchema: buildSchemaForTypescript, +} = require('../../parsers/typescript'); function parseFiles(files: Array) { files.forEach(filename => { @@ -25,7 +27,7 @@ function parseFiles(files: Array) { JSON.stringify( parseFile( filename, - isTypeScript ? TypeScriptParser.buildSchema : FlowParser.buildSchema, + isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, ), null, 2, diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js index 384b0af5b2c86b..b2dd0d4184c243 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js @@ -11,7 +11,7 @@ 'use strict'; -const FlowParser = require('../../index.js'); +const {buildSchema} = require('../../index.js'); const {parseFile} = require('../../../utils.js'); const fixtures = require('../__test_fixtures__/fixtures.js'); const failureFixtures = require('../__test_fixtures__/failures.js'); @@ -31,7 +31,7 @@ describe('RN Codegen Flow Parser', () => { .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parseFile(fixtureName, FlowParser.buildSchema); + const schema = parseFile(fixtureName, buildSchema); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -45,7 +45,7 @@ describe('RN Codegen Flow Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parseFile(fixtureName, FlowParser.buildSchema); + parseFile(fixtureName, buildSchema); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js index 19a49a6b8ad212..967bb409e48f4c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js @@ -11,7 +11,7 @@ 'use strict'; -const TypeScriptParser = require('../../index.js'); +const {buildSchema} = require('../../index.js'); const {parseFile} = require('../../../utils.js'); const fixtures = require('../__test_fixtures__/fixtures.js'); const failureFixtures = require('../__test_fixtures__/failures.js'); @@ -31,7 +31,7 @@ describe('RN Codegen TypeScript Parser', () => { .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parseFile(fixtureName, TypeScriptParser.buildSchema); + const schema = parseFile(fixtureName, buildSchema); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -45,7 +45,7 @@ describe('RN Codegen TypeScript Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parseFile(fixtureName, TypeScriptParser.buildSchema); + parseFile(fixtureName, buildSchema); }).toThrowErrorMatchingSnapshot(); }); }); From 02ffd12781baa6e54e9c9bb68b4bc09a1aac12f7 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Sat, 12 Nov 2022 14:16:45 +0100 Subject: [PATCH 02/11] refactor: move parseFile in typescript and flow parsers --- .../GenerateComponentDescriptorH-test.js | 6 +++-- .../GenerateComponentHObjCpp-test.js | 6 +++-- .../GenerateEventEmitterCpp-test.js | 6 +++-- .../components/GenerateEventEmitterH-test.js | 6 +++-- .../components/GeneratePropsCpp-test.js | 6 +++-- .../components/GeneratePropsH-test.js | 6 +++-- .../GeneratePropsJavaDelegate-test.js | 6 +++-- .../GeneratePropsJavaInterface-test.js | 6 +++-- .../components/GenerateShadowNodeCpp-test.js | 6 +++-- .../components/GenerateShadowNodeH-test.js | 6 +++-- .../components/GenerateViewConfigJs-test.js | 6 +++-- .../modules/GenerateModuleObjCpp-test.js | 6 +++-- .../src/cli/combine/combine-js-to-schema.js | 10 ++++++-- .../src/cli/parser/parser.js | 10 ++++++-- .../__tests__/component-parser-test.js | 8 ++++--- .../src/parsers/flow/parser.js | 16 ++++++++++++- .../src/parsers/parser.js | 11 ++++++++- .../src/parsers/parserMock.js | 23 ++++++++++++++++++- .../typescript-component-parser-test.js | 8 ++++--- .../src/parsers/typescript/parser.js | 16 ++++++++++++- .../react-native-codegen/src/parsers/utils.js | 11 --------- 21 files changed, 136 insertions(+), 49 deletions(-) diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js index 5dffc6d3cb087c..ea3927a89de46d 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentDescriptorH'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js index 8209e9f8a1b941..64bc978fc095a5 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentHObjCpp'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateComponentHObjCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js index f7fe22e5747bf0..86628c76d40e66 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterCpp'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js index 972a43df1a4f21..38c5c89d8f34a0 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterH'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateEventEmitterH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js index 2c33698b9089bc..dced02546d8c43 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsCpp'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GeneratePropsCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js index 52f80baae374a3..281e39487de058 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsH'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GeneratePropsH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js index 9a609bc57a4218..654f3c79047bf2 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaDelegate'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GeneratePropsJavaDelegate can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js index 8c8de96682d698..ad7b89ce10d0dc 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaInterface'); const fs = require('fs'); @@ -19,10 +19,12 @@ const fs = require('fs'); const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GeneratePropsJavaInterface can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js index 38e53996b28f62..aa536c73f56d2d 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeCpp'); const fs = require('fs'); @@ -19,10 +19,12 @@ const fs = require('fs'); const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js index 41897e53241ce7..af0632a666abdc 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeH'); const fs = require('fs'); @@ -19,10 +19,12 @@ const fs = require('fs'); const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateShadowNodeH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js index 232a9405b89237..6ea480bec9143f 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateViewConfigJs'); const fs = require('fs'); @@ -20,10 +20,12 @@ const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/components`; const fixtures = fs.readdirSync(FIXTURE_DIR); +const parser = new FlowParser(); + fixtures.forEach(fixture => { it(`GenerateViewConfigJs can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); const output = generator.generate(libName, schema); expect(output).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js index 888ca94618735e..618316740c7d8c 100644 --- a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js @@ -11,7 +11,7 @@ 'use strict'; -const {parseFile} = require('../../../src/parsers/utils'); +const {FlowParser} = require('../../../src/parsers/flow/parser'); const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/modules/GenerateModuleObjCpp'); const fs = require('fs'); @@ -20,11 +20,13 @@ import type {SchemaType} from '../../../src/CodegenSchema'; const FIXTURE_DIR = `${__dirname}/../../__test_fixtures__/modules`; +const parser = new FlowParser(); + function getModules(): SchemaType { const filenames: Array = fs.readdirSync(FIXTURE_DIR); return filenames.reduce( (accumulator, file) => { - const schema = parseFile(`${FIXTURE_DIR}/${file}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${file}`, buildSchema); return { modules: { ...accumulator.modules, diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js index 93f6a515039220..882f79a370554c 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js @@ -11,7 +11,8 @@ 'use strict'; import type {SchemaType} from '../../CodegenSchema.js'; -const {parseFile} = require('../../parsers/utils'); +const {FlowParser} = require('../../parsers/flow/parser'); +const {TypeScriptParser} = require('../../parsers/typescript/parser'); const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); const { buildSchema: buildSchemaForTypescript, @@ -19,6 +20,9 @@ const { const fs = require('fs'); const path = require('path'); +const flowParser = new FlowParser(); +const typescriptParser = new TypeScriptParser(); + function combineSchemas(files: Array): SchemaType { return files.reduce( (merged, filename) => { @@ -32,7 +36,9 @@ function combineSchemas(files: Array): SchemaType { const isTypeScript = path.extname(filename) === '.ts' || path.extname(filename) === '.tsx'; - const schema = parseFile( + const parser = isTypeScript ? typescriptParser : flowParser; + + const schema = parser.parseFile( filename, isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, ); diff --git a/packages/react-native-codegen/src/cli/parser/parser.js b/packages/react-native-codegen/src/cli/parser/parser.js index e0fb6cddae7b4f..dcecf68c9a5c47 100644 --- a/packages/react-native-codegen/src/cli/parser/parser.js +++ b/packages/react-native-codegen/src/cli/parser/parser.js @@ -11,21 +11,27 @@ 'use strict'; const path = require('path'); -const {parseFile} = require('../../parsers/utils'); +const {FlowParser} = require('../../parsers/flow/parser'); +const {TypeScriptParser} = require('../../parsers/typescript/parser'); const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); const { buildSchema: buildSchemaForTypescript, } = require('../../parsers/typescript'); +const flowParser = new FlowParser(); +const typescriptParser = new TypeScriptParser(); + function parseFiles(files: Array) { files.forEach(filename => { const isTypeScript = path.extname(filename) === '.ts' || path.extname(filename) === '.tsx'; + const parser = isTypeScript ? typescriptParser : flowParser; + console.log( filename, JSON.stringify( - parseFile( + parser.parseFile( filename, isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, ), diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js index b2dd0d4184c243..da51bbb2cb84d6 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js @@ -11,8 +11,8 @@ 'use strict'; +const {FlowParser} = require('../../parser'); const {buildSchema} = 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', () => ({ @@ -26,12 +26,14 @@ jest.mock('fs', () => ({ }, })); +const parser = new FlowParser(); + describe('RN Codegen Flow Parser', () => { Object.keys(fixtures) .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parseFile(fixtureName, buildSchema); + const schema = parser.parseFile(fixtureName, buildSchema); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -45,7 +47,7 @@ describe('RN Codegen Flow Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parseFile(fixtureName, buildSchema); + parser.parseFile(fixtureName, buildSchema); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index b0fd878f9c2cb8..5fab84ad938c3f 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -10,10 +10,15 @@ 'use strict'; -import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js'; +import type { + UnionTypeAnnotationMemberType, + SchemaType, +} from '../../CodegenSchema'; import type {ParserType} from '../errors'; import type {Parser} from '../parser'; +const fs = require('fs'); + const { UnsupportedObjectPropertyTypeAnnotationParserError, } = require('../errors'); @@ -76,6 +81,15 @@ class FlowParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } + + parseFile( + filename: string, + callback: (contents: string, filename: string) => SchemaType, + ): SchemaType { + const contents = fs.readFileSync(filename, 'utf8'); + + return callback(contents, filename); + } } module.exports = { diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index fdb03cd32b8fb6..7218c612bbb9c8 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -10,7 +10,7 @@ 'use strict'; -import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js'; +import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema'; import type {ParserType} from './errors'; /** @@ -67,4 +67,13 @@ export interface Parser { remapUnionTypeAnnotationMemberNames( types: $FlowFixMe, ): UnionTypeAnnotationMemberType[]; + /** + * Given the content of a file and options, it returns an AST. + * @parameter contents: the content of the file. + * @returns: the AST of the file (given in program property for typescript). + */ + parseFile( + filename: string, + callback: (contents: string, filename: string) => SchemaType, + ): SchemaType; } diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index 6f3d16eba978fe..e9375e947f5612 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -10,9 +10,9 @@ 'use strict'; -import type {UnionTypeAnnotationMemberType} from '../CodegenSchema.js'; import type {Parser} from './parser'; import type {ParserType} from './errors'; +import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema'; const { UnsupportedObjectPropertyTypeAnnotationParserError, @@ -65,4 +65,25 @@ export class MockedParser implements Parser { ): UnionTypeAnnotationMemberType[] { return []; } + + parseFile( + filename: string, + callback: (contents: string, filename: string) => SchemaType, + ): SchemaType { + return { + modules: { + StringPropNativeComponentView: { + type: 'Component', + components: { + StringPropNativeComponentView: { + extendsProps: [], + events: [], + props: [], + commands: [], + }, + }, + }, + }, + }; + } } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js index 967bb409e48f4c..46cabc40906f6c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js @@ -11,8 +11,8 @@ 'use strict'; +const {TypeScriptParser} = require('../../parser'); const {buildSchema} = 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', () => ({ @@ -26,12 +26,14 @@ jest.mock('fs', () => ({ }, })); +const parser = new TypeScriptParser(); + describe('RN Codegen TypeScript Parser', () => { Object.keys(fixtures) .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parseFile(fixtureName, buildSchema); + const schema = parser.parseFile(fixtureName, buildSchema); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -45,7 +47,7 @@ describe('RN Codegen TypeScript Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parseFile(fixtureName, buildSchema); + parser.parseFile(fixtureName, buildSchema); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 12d2d5beb06b62..02e5cda9d6fc9d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -10,10 +10,15 @@ 'use strict'; -import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema.js'; +import type { + UnionTypeAnnotationMemberType, + SchemaType, +} from '../../CodegenSchema'; import type {ParserType} from '../errors'; import type {Parser} from '../parser'; +const fs = require('fs'); + const { UnsupportedObjectPropertyTypeAnnotationParserError, } = require('../errors'); @@ -81,6 +86,15 @@ class TypeScriptParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } + + parseFile( + filename: string, + callback: (contents: string, filename: string) => SchemaType, + ): SchemaType { + const contents = fs.readFileSync(filename, 'utf8'); + + return callback(contents, filename); + } } module.exports = { TypeScriptParser, diff --git a/packages/react-native-codegen/src/parsers/utils.js b/packages/react-native-codegen/src/parsers/utils.js index 2d7ca6fae8c760..d6c77d9e239cc2 100644 --- a/packages/react-native-codegen/src/parsers/utils.js +++ b/packages/react-native-codegen/src/parsers/utils.js @@ -15,7 +15,6 @@ 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,15 +94,6 @@ 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, @@ -259,7 +249,6 @@ module.exports = { extractNativeModuleName, createParserErrorCapturer, verifyPlatforms, - parseFile, visit, buildSchemaFromConfigType, isModuleRegistryCall, From c6d43a9981973e72c5990cafdabf864a6bfc1a2f Mon Sep 17 00:00:00 2001 From: MaeIg Date: Sat, 12 Nov 2022 14:46:06 +0100 Subject: [PATCH 03/11] refactor: remove buildSchema from parseFile params and import it in parsers --- .../GenerateComponentDescriptorH-test.js | 3 +-- .../components/GenerateComponentHObjCpp-test.js | 3 +-- .../components/GenerateEventEmitterCpp-test.js | 3 +-- .../components/GenerateEventEmitterH-test.js | 3 +-- .../components/GeneratePropsCpp-test.js | 3 +-- .../__tests__/components/GeneratePropsH-test.js | 3 +-- .../components/GeneratePropsJavaDelegate-test.js | 3 +-- .../GeneratePropsJavaInterface-test.js | 3 +-- .../components/GenerateShadowNodeCpp-test.js | 3 +-- .../components/GenerateShadowNodeH-test.js | 3 +-- .../components/GenerateViewConfigJs-test.js | 3 +-- .../modules/GenerateModuleObjCpp-test.js | 3 +-- .../src/cli/combine/combine-js-to-schema.js | 9 +-------- .../src/cli/parser/parser.js | 16 +--------------- .../__tests__/component-parser-test.js | 5 ++--- .../src/parsers/flow/parser.js | 9 ++++----- .../react-native-codegen/src/parsers/parser.js | 5 +---- .../src/parsers/parserMock.js | 5 +---- .../typescript-component-parser-test.js | 5 ++--- .../src/parsers/typescript/parser.js | 9 ++++----- 20 files changed, 28 insertions(+), 71 deletions(-) diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js index ea3927a89de46d..3e12f2afa956c4 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentDescriptorH'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js index 64bc978fc095a5..50959ea33a805b 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentHObjCpp-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateComponentHObjCpp'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateComponentHObjCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js index 86628c76d40e66..177ec1dccb9eb7 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterCpp'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js index 38c5c89d8f34a0..962c57ef404160 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterH-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateEventEmitterH'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateEventEmitterH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js index dced02546d8c43..b6d0f5e9792bb0 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsCpp'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GeneratePropsCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js index 281e39487de058..6c60a966c3451c 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsH-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsH'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GeneratePropsH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js index 654f3c79047bf2..3a3a2135cb221e 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaDelegate-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaDelegate'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GeneratePropsJavaDelegate can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js index ad7b89ce10d0dc..14a807fd02b4ef 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsJavaInterface-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GeneratePropsJavaInterface'); const fs = require('fs'); @@ -24,7 +23,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GeneratePropsJavaInterface can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js index aa536c73f56d2d..a7a766b1a56b62 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeCpp'); const fs = require('fs'); @@ -24,7 +23,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js index af0632a666abdc..87cc703a17aabd 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateShadowNodeH'); const fs = require('fs'); @@ -24,7 +23,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateShadowNodeH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema, undefined, false); expect(Object.fromEntries(output)).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js index 6ea480bec9143f..833866e473df09 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateViewConfigJs-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/components/GenerateViewConfigJs'); const fs = require('fs'); @@ -25,7 +24,7 @@ const parser = new FlowParser(); fixtures.forEach(fixture => { it(`GenerateViewConfigJs can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; - const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); const output = generator.generate(libName, schema); expect(output).toMatchSnapshot(); }); diff --git a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js index 618316740c7d8c..9e043428607389 100644 --- a/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/modules/GenerateModuleObjCpp-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../../src/parsers/flow/parser'); -const {buildSchema} = require('../../../src/parsers/flow'); const generator = require('../../../src/generators/modules/GenerateModuleObjCpp'); const fs = require('fs'); @@ -26,7 +25,7 @@ function getModules(): SchemaType { const filenames: Array = fs.readdirSync(FIXTURE_DIR); return filenames.reduce( (accumulator, file) => { - const schema = parser.parseFile(`${FIXTURE_DIR}/${file}`, buildSchema); + const schema = parser.parseFile(`${FIXTURE_DIR}/${file}`); return { modules: { ...accumulator.modules, diff --git a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js index 882f79a370554c..a2dcef55c43dab 100644 --- a/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js +++ b/packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js @@ -13,10 +13,6 @@ import type {SchemaType} from '../../CodegenSchema.js'; const {FlowParser} = require('../../parsers/flow/parser'); const {TypeScriptParser} = require('../../parsers/typescript/parser'); -const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); -const { - buildSchema: buildSchemaForTypescript, -} = require('../../parsers/typescript'); const fs = require('fs'); const path = require('path'); @@ -38,10 +34,7 @@ function combineSchemas(files: Array): SchemaType { const parser = isTypeScript ? typescriptParser : flowParser; - const schema = parser.parseFile( - filename, - isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, - ); + const schema = parser.parseFile(filename); if (schema && schema.modules) { merged.modules = {...merged.modules, ...schema.modules}; diff --git a/packages/react-native-codegen/src/cli/parser/parser.js b/packages/react-native-codegen/src/cli/parser/parser.js index dcecf68c9a5c47..40a599c9ff9c41 100644 --- a/packages/react-native-codegen/src/cli/parser/parser.js +++ b/packages/react-native-codegen/src/cli/parser/parser.js @@ -13,10 +13,6 @@ const path = require('path'); const {FlowParser} = require('../../parsers/flow/parser'); const {TypeScriptParser} = require('../../parsers/typescript/parser'); -const {buildSchema: buildSchemaForFlow} = require('../../parsers/flow'); -const { - buildSchema: buildSchemaForTypescript, -} = require('../../parsers/typescript'); const flowParser = new FlowParser(); const typescriptParser = new TypeScriptParser(); @@ -28,17 +24,7 @@ function parseFiles(files: Array) { const parser = isTypeScript ? typescriptParser : flowParser; - console.log( - filename, - JSON.stringify( - parser.parseFile( - filename, - isTypeScript ? buildSchemaForTypescript : buildSchemaForFlow, - ), - null, - 2, - ), - ); + console.log(filename, JSON.stringify(parser.parseFile(filename), null, 2)); }); } diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js index da51bbb2cb84d6..e01d1ade6e1f42 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/component-parser-test.js @@ -12,7 +12,6 @@ 'use strict'; const {FlowParser} = require('../../parser'); -const {buildSchema} = require('../../index.js'); const fixtures = require('../__test_fixtures__/fixtures.js'); const failureFixtures = require('../__test_fixtures__/failures.js'); jest.mock('fs', () => ({ @@ -33,7 +32,7 @@ describe('RN Codegen Flow Parser', () => { .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parser.parseFile(fixtureName, buildSchema); + const schema = parser.parseFile(fixtureName); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -47,7 +46,7 @@ describe('RN Codegen Flow Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parser.parseFile(fixtureName, buildSchema); + parser.parseFile(fixtureName); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 5fab84ad938c3f..1c0b1efa3c18d7 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -17,6 +17,8 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; +const {buildSchema} = require('.'); + const fs = require('fs'); const { @@ -82,13 +84,10 @@ class FlowParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } - parseFile( - filename: string, - callback: (contents: string, filename: string) => SchemaType, - ): SchemaType { + parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return callback(contents, filename); + return buildSchema(contents, filename); } } diff --git a/packages/react-native-codegen/src/parsers/parser.js b/packages/react-native-codegen/src/parsers/parser.js index 7218c612bbb9c8..a78a3b17fb3029 100644 --- a/packages/react-native-codegen/src/parsers/parser.js +++ b/packages/react-native-codegen/src/parsers/parser.js @@ -72,8 +72,5 @@ export interface Parser { * @parameter contents: the content of the file. * @returns: the AST of the file (given in program property for typescript). */ - parseFile( - filename: string, - callback: (contents: string, filename: string) => SchemaType, - ): SchemaType; + parseFile(filename: string): SchemaType; } diff --git a/packages/react-native-codegen/src/parsers/parserMock.js b/packages/react-native-codegen/src/parsers/parserMock.js index e9375e947f5612..54789c8f04c5ba 100644 --- a/packages/react-native-codegen/src/parsers/parserMock.js +++ b/packages/react-native-codegen/src/parsers/parserMock.js @@ -66,10 +66,7 @@ export class MockedParser implements Parser { return []; } - parseFile( - filename: string, - callback: (contents: string, filename: string) => SchemaType, - ): SchemaType { + parseFile(filename: string): SchemaType { return { modules: { StringPropNativeComponentView: { diff --git a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js index 46cabc40906f6c..dd56de0b1351be 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/typescript-component-parser-test.js @@ -12,7 +12,6 @@ 'use strict'; const {TypeScriptParser} = require('../../parser'); -const {buildSchema} = require('../../index.js'); const fixtures = require('../__test_fixtures__/fixtures.js'); const failureFixtures = require('../__test_fixtures__/failures.js'); jest.mock('fs', () => ({ @@ -33,7 +32,7 @@ describe('RN Codegen TypeScript Parser', () => { .sort() .forEach(fixtureName => { it(`can generate fixture ${fixtureName}`, () => { - const schema = parser.parseFile(fixtureName, buildSchema); + const schema = parser.parseFile(fixtureName); const serializedSchema = JSON.stringify(schema, null, 2).replace( /"/g, "'", @@ -47,7 +46,7 @@ describe('RN Codegen TypeScript Parser', () => { .forEach(fixtureName => { it(`Fails with error message ${fixtureName}`, () => { expect(() => { - parser.parseFile(fixtureName, buildSchema); + parser.parseFile(fixtureName); }).toThrowErrorMatchingSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 02e5cda9d6fc9d..b786f7276f89fa 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -17,6 +17,8 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; +const {buildSchema} = require('.'); + const fs = require('fs'); const { @@ -87,13 +89,10 @@ class TypeScriptParser implements Parser { return [...new Set(membersTypes.map(remapLiteral))]; } - parseFile( - filename: string, - callback: (contents: string, filename: string) => SchemaType, - ): SchemaType { + parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return callback(contents, filename); + return buildSchema(contents, filename); } } module.exports = { From 0b72a27be23fb8c2921a935e68b5c04997377955 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Sat, 12 Nov 2022 15:04:12 +0100 Subject: [PATCH 04/11] type: use @flow strict --- .../src/parsers/flow/components/commands.js | 3 ++- .../src/parsers/flow/components/componentsUtils.js | 2 +- .../react-native-codegen/src/parsers/flow/components/events.js | 2 +- .../src/parsers/flow/components/extends.js | 2 +- .../react-native-codegen/src/parsers/flow/components/index.js | 2 +- .../src/parsers/flow/components/options.js | 2 +- .../react-native-codegen/src/parsers/flow/components/props.js | 2 +- packages/react-native-codegen/src/parsers/flow/index.js | 2 +- .../src/parsers/typescript/components/commands.js | 3 ++- .../src/parsers/typescript/components/componentsUtils.js | 2 +- .../src/parsers/typescript/components/events.js | 2 +- .../src/parsers/typescript/components/extends.js | 2 +- .../src/parsers/typescript/components/index.js | 2 +- .../src/parsers/typescript/components/options.js | 2 +- .../src/parsers/typescript/components/props.js | 2 +- packages/react-native-codegen/src/parsers/typescript/index.js | 3 ++- 16 files changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/components/commands.js b/packages/react-native-codegen/src/parsers/flow/components/commands.js index 4d0b8a62fa6010..bf789178d996d1 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/commands.js +++ b/packages/react-native-codegen/src/parsers/flow/components/commands.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict * @format */ @@ -18,6 +18,7 @@ import type {TypeDeclarationMap} from '../../utils'; const {getValueFromTypes} = require('../utils.js'); +// $FlowFixMe[unclear-type] there's no flowtype for ASTs type EventTypeAST = Object; function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { diff --git a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js index be6ec280105555..9faaacab46cf16 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js +++ b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index 8384edd789b8a5..9361f0c005056f 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/extends.js b/packages/react-native-codegen/src/parsers/flow/components/extends.js index 6f81f7b454080f..d53147d9d4f505 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/extends.js +++ b/packages/react-native-codegen/src/parsers/flow/components/extends.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/index.js b/packages/react-native-codegen/src/parsers/flow/components/index.js index a0b1eda6cdaaa9..50026ebf6c1f7d 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/index.js +++ b/packages/react-native-codegen/src/parsers/flow/components/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/options.js b/packages/react-native-codegen/src/parsers/flow/components/options.js index beb032eae320ab..a83ba528b467d8 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/options.js +++ b/packages/react-native-codegen/src/parsers/flow/components/options.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/components/props.js b/packages/react-native-codegen/src/parsers/flow/components/props.js index 05cdf06b02230c..70ff36e8526ea1 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/props.js +++ b/packages/react-native-codegen/src/parsers/flow/components/props.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index 2dfd5ff37ccd49..600d5a82a793d9 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/commands.js b/packages/react-native-codegen/src/parsers/typescript/components/commands.js index 5e6d38de9c5531..390a6d71b9607c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/commands.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/commands.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict * @format */ @@ -17,6 +17,7 @@ import type { import type {TypeDeclarationMap} from '../../utils'; const {parseTopLevelType} = require('../parseTopLevelType'); +// $FlowFixMe[unclear-type] there's no flowtype for ASTs type EventTypeAST = Object; function buildCommandSchemaInternal( diff --git a/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js b/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js index ed8b283192dee7..70e9e33b8b9ac4 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 7da6660752e7e6..dfd295f6ddce5d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/extends.js b/packages/react-native-codegen/src/parsers/typescript/components/extends.js index e9fda8c46ee752..1d4d006f5d7c18 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/extends.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/extends.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/index.js b/packages/react-native-codegen/src/parsers/typescript/components/index.js index 46cbba5dd505e7..d6c66d49a6330f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/options.js b/packages/react-native-codegen/src/parsers/typescript/components/options.js index 4b0f9711172fea..96ea8abc75b1d5 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/options.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/options.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/props.js b/packages/react-native-codegen/src/parsers/typescript/components/props.js index a2b1bcfad482f3..c7a1d3b04e6539 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/props.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/props.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/typescript/index.js b/packages/react-native-codegen/src/parsers/typescript/index.js index 426e4c8eef950a..b1d4a2e1feec52 100644 --- a/packages/react-native-codegen/src/parsers/typescript/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict * @format */ @@ -12,6 +12,7 @@ import type {SchemaType} from '../../CodegenSchema.js'; +// $FlowFixMe[untyped-import] Use flow-types for @babel/parser const babelParser = require('@babel/parser'); const fs = require('fs'); const { From e64de3463f554a192af12cbc700e6d1b2888a1f3 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 19:56:28 +0100 Subject: [PATCH 05/11] refactor(flow): pass parser as a param to translateTypeAnnotation and other functions that consume it --- .../react-native-codegen/src/parsers/flow/modules/index.js | 6 ++++++ .../react-native-codegen/src/parsers/parsers-commons.js | 6 ++++++ .../react-native-codegen/src/parsers/parsers-primitives.js | 5 +++++ .../src/parsers/typescript/modules/index.js | 2 ++ 4 files changed, 19 insertions(+) diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 120d6fa8443fd1..807e014f0a1179 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -19,6 +19,8 @@ import type { NativeModuleSchema, Nullable, } from '../../../CodegenSchema'; + +import type {Parser} from '../../parser'; import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); @@ -82,6 +84,7 @@ function translateTypeAnnotation( aliasMap: {...NativeModuleAliasMap}, tryParse: ParserErrorCapturer, cxxOnly: boolean, + parser: Parser, ): Nullable { const {nullable, typeAnnotation, typeAliasResolutionStatus} = resolveTypeAnnotation(flowTypeAnnotation, types); @@ -133,6 +136,7 @@ function translateTypeAnnotation( aliasMap, tryParse, cxxOnly, + parser, ), ); @@ -223,6 +227,7 @@ function translateTypeAnnotation( cxxOnly, translateTypeAnnotation, language, + parser, ); } case 'UnionTypeAnnotation': { @@ -389,6 +394,7 @@ function buildModuleSchema( language, resolveTypeAnnotation, translateTypeAnnotation, + parser, ), })); }) diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index a7884085f91069..41c2d7a3ec6b06 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -181,6 +181,7 @@ function parseObjectProperty( aliasMap, tryParse, cxxOnly, + parser, ), ); @@ -292,6 +293,7 @@ function translateFunctionTypeAnnotation( cxxOnly: boolean, translateTypeAnnotation: $FlowFixMe, language: ParserType, + parser: Parser, ): NativeModuleFunctionTypeAnnotation { type Param = NamedShape>; const params: Array = []; @@ -317,6 +319,7 @@ function translateFunctionTypeAnnotation( aliasMap, tryParse, cxxOnly, + parser, ), ); @@ -356,6 +359,7 @@ function translateFunctionTypeAnnotation( aliasMap, tryParse, cxxOnly, + parser, ), ); @@ -393,6 +397,7 @@ function buildPropertySchema( language: ParserType, resolveTypeAnnotation: $FlowFixMe, translateTypeAnnotation: $FlowFixMe, + parser: Parser, ): NativeModulePropertyShape { let nullable: boolean = false; let {key, value} = property; @@ -429,6 +434,7 @@ function buildPropertySchema( cxxOnly, translateTypeAnnotation, language, + parser, ), ), }; diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 5d4b41338f676f..e33adf3df1af73 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -106,6 +106,7 @@ function emitFunction( cxxOnly: boolean, translateTypeAnnotation: $FlowFixMe, language: ParserType, + parser: Parser, ): Nullable { const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation = translateFunctionTypeAnnotation( @@ -117,6 +118,7 @@ function emitFunction( cxxOnly, translateTypeAnnotation, language, + parser, ); return wrapNullable(nullable, translateFunctionTypeAnnotationValue); } @@ -225,6 +227,7 @@ function emitPromise( aliasMap, tryParse, cxxOnly, + parser, ), }); } catch { @@ -287,6 +290,7 @@ function translateArrayTypeAnnotation( nullable: boolean, language: ParserType, translateTypeAnnotation: $FlowFixMe, + parser: Parser, ): Nullable { try { /** @@ -310,6 +314,7 @@ function translateArrayTypeAnnotation( */ nullGuard, cxxOnly, + parser, ), ); diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 5991c7c8e3b369..0b8d7aed2ee1b4 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -238,6 +238,7 @@ function translateTypeAnnotation( cxxOnly, translateTypeAnnotation, language, + parser, ); } case 'TSUnionType': { @@ -402,6 +403,7 @@ function buildModuleSchema( language, resolveTypeAnnotation, translateTypeAnnotation, + parser, ), })); }) From 793fcdd818916aee10fadffd17b070b3d26064ca Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 20:03:12 +0100 Subject: [PATCH 06/11] refactor(typescript): pass parser as a param to translateTypeAnnotation and other functions that consume it --- .../react-native-codegen/src/parsers/parsers-primitives.js | 2 +- .../src/parsers/typescript/modules/index.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index e33adf3df1af73..44696cf011907c 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -362,8 +362,8 @@ function emitArrayType( typeAnnotation.type, typeAnnotation.typeParameters.params[0], nullable, - parser.language(), translateTypeAnnotation, + parser, ); } diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 0b8d7aed2ee1b4..0a3a3c70da8d47 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -20,6 +20,7 @@ import type { Nullable, } from '../../../CodegenSchema'; +import type {Parser} from '../../parser'; import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); @@ -83,6 +84,7 @@ function translateTypeAnnotation( aliasMap: {...NativeModuleAliasMap}, tryParse: ParserErrorCapturer, cxxOnly: boolean, + parser: Parser, ): Nullable { const {nullable, typeAnnotation, typeAliasResolutionStatus} = resolveTypeAnnotation(typeScriptTypeAnnotation, types); @@ -99,6 +101,7 @@ function translateTypeAnnotation( nullable, language, translateTypeAnnotation, + parser, ); } case 'TSTypeOperator': { @@ -116,6 +119,7 @@ function translateTypeAnnotation( nullable, language, translateTypeAnnotation, + parser, ); } else { throw new UnsupportedGenericParserError( From 9ba996708149f0260e5c6a3198c18d6cd773d189 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 20:16:09 +0100 Subject: [PATCH 07/11] refactor: remove useless language properties It is already accessible in parser --- .../src/parsers/flow/modules/index.js | 2 -- .../src/parsers/parsers-commons.js | 24 +++++++++---------- .../src/parsers/parsers-primitives.js | 6 +---- .../src/parsers/typescript/modules/index.js | 4 ---- 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 807e014f0a1179..2df5eaa528a334 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -226,7 +226,6 @@ function translateTypeAnnotation( tryParse, cxxOnly, translateTypeAnnotation, - language, parser, ); } @@ -391,7 +390,6 @@ function buildModuleSchema( aliasMap, tryParse, cxxOnly, - language, resolveTypeAnnotation, translateTypeAnnotation, parser, diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index 41c2d7a3ec6b06..db25d052a3c75f 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -292,29 +292,31 @@ function translateFunctionTypeAnnotation( tryParse: ParserErrorCapturer, cxxOnly: boolean, translateTypeAnnotation: $FlowFixMe, - language: ParserType, parser: Parser, ): NativeModuleFunctionTypeAnnotation { type Param = NamedShape>; const params: Array = []; - for (const param of getTypeAnnotationParameters(typeAnnotation, language)) { + for (const param of getTypeAnnotationParameters( + typeAnnotation, + parser.language(), + )) { const parsedParam = tryParse(() => { - if (getFunctionNameFromParameter(param, language) == null) { + if (getFunctionNameFromParameter(param, parser.language()) == null) { throw new UnnamedFunctionParamParserError( param, hasteModuleName, - language, + parser.language(), ); } - const paramName = getParameterName(param, language); + const paramName = getParameterName(param, parser.language()); const [paramTypeAnnotation, isParamTypeAnnotationNullable] = unwrapNullable<$FlowFixMe>( translateTypeAnnotation( hasteModuleName, - getParameterTypeAnnotation(param, language), + getParameterTypeAnnotation(param, parser.language()), types, aliasMap, tryParse, @@ -354,7 +356,7 @@ function translateFunctionTypeAnnotation( unwrapNullable<$FlowFixMe>( translateTypeAnnotation( hasteModuleName, - getTypeAnnotationReturnType(typeAnnotation, language), + getTypeAnnotationReturnType(typeAnnotation, parser.language()), types, aliasMap, tryParse, @@ -367,7 +369,7 @@ function translateFunctionTypeAnnotation( hasteModuleName, typeAnnotation, 'FunctionTypeAnnotation', - language, + parser.language(), cxxOnly, returnTypeAnnotation.type, ); @@ -394,7 +396,6 @@ function buildPropertySchema( aliasMap: {...NativeModuleAliasMap}, tryParse: ParserErrorCapturer, cxxOnly: boolean, - language: ParserType, resolveTypeAnnotation: $FlowFixMe, translateTypeAnnotation: $FlowFixMe, parser: Parser, @@ -403,7 +404,7 @@ function buildPropertySchema( let {key, value} = property; const methodName: string = key.name; - if (language === 'TypeScript') { + if (parser.language() === 'TypeScript') { value = property.type === 'TSMethodSignature' ? property @@ -417,7 +418,7 @@ function buildPropertySchema( property.value, key.name, value.type, - language, + parser.language(), ); return { @@ -433,7 +434,6 @@ function buildPropertySchema( tryParse, cxxOnly, translateTypeAnnotation, - language, parser, ), ), diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 44696cf011907c..d29bac2b443fa8 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -31,7 +31,6 @@ import type { StringTypeAnnotation, VoidTypeAnnotation, } from '../CodegenSchema'; -import type {ParserType} from './errors'; import type {Parser} from './parser'; import type { ParserErrorCapturer, @@ -105,7 +104,6 @@ function emitFunction( tryParse: ParserErrorCapturer, cxxOnly: boolean, translateTypeAnnotation: $FlowFixMe, - language: ParserType, parser: Parser, ): Nullable { const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation = @@ -117,7 +115,6 @@ function emitFunction( tryParse, cxxOnly, translateTypeAnnotation, - language, parser, ); return wrapNullable(nullable, translateFunctionTypeAnnotationValue); @@ -288,7 +285,6 @@ function translateArrayTypeAnnotation( arrayType: 'Array' | 'ReadonlyArray', elementType: $FlowFixMe, nullable: boolean, - language: ParserType, translateTypeAnnotation: $FlowFixMe, parser: Parser, ): Nullable { @@ -323,7 +319,7 @@ function translateArrayTypeAnnotation( elementType, arrayType, _elementType.type, - language, + parser.language(), ); return wrapNullable(nullable, { diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 0a3a3c70da8d47..8156da51ad14da 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -99,7 +99,6 @@ function translateTypeAnnotation( 'Array', typeAnnotation.elementType, nullable, - language, translateTypeAnnotation, parser, ); @@ -117,7 +116,6 @@ function translateTypeAnnotation( 'ReadonlyArray', typeAnnotation.typeAnnotation.elementType, nullable, - language, translateTypeAnnotation, parser, ); @@ -241,7 +239,6 @@ function translateTypeAnnotation( tryParse, cxxOnly, translateTypeAnnotation, - language, parser, ); } @@ -404,7 +401,6 @@ function buildModuleSchema( aliasMap, tryParse, cxxOnly, - language, resolveTypeAnnotation, translateTypeAnnotation, parser, From 7c59203355a5166e425dec4bcf0c0633c5f410a7 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 21:10:09 +0100 Subject: [PATCH 08/11] refactor: pass parser as a param to buildModuleSchema and other functions that consume it --- packages/eslint-plugin-specs/react-native-modules.js | 10 +++++++--- .../src/parsers/__tests__/utils-test.js | 8 +++++++- .../react-native-codegen/src/parsers/flow/index.js | 4 ++++ .../src/parsers/flow/modules/index.js | 4 +--- .../src/parsers/typescript/index.js | 4 ++++ .../src/parsers/typescript/modules/index.js | 4 +--- packages/react-native-codegen/src/parsers/utils.js | 6 +++++- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin-specs/react-native-modules.js b/packages/eslint-plugin-specs/react-native-modules.js index 19584626f95c16..fa0b58440d2f1e 100644 --- a/packages/eslint-plugin-specs/react-native-modules.js +++ b/packages/eslint-plugin-specs/react-native-modules.js @@ -23,9 +23,10 @@ const ERRORS = { let RNModuleParser; let RNParserUtils; +let RNFlowParser; function requireModuleParser() { - if (RNModuleParser == null || RNParserUtils == null) { + if (RNModuleParser == null || RNParserUtils == null || RNFlowParser == null) { // If using this externally, we leverage @react-native/codegen as published form if (!PACKAGE_USAGE) { const config = { @@ -36,6 +37,7 @@ function requireModuleParser() { withBabelRegister(config, () => { RNModuleParser = require('@react-native/codegen/src/parsers/flow/modules'); RNParserUtils = require('@react-native/codegen/src/parsers/utils'); + RNFlowParser = require('@react-native/codegen/src/parsers/flow/parser'); }); } else { const config = { @@ -46,6 +48,7 @@ function requireModuleParser() { withBabelRegister(config, () => { RNModuleParser = require('@react-native/codegen/lib/parsers/flow/modules'); RNParserUtils = require('@react-native/codegen/lib/parsers/flow/utils'); + RNFlowParser = require('@react-native/codegen/lib/parsers/flow/parser'); }); } } @@ -53,6 +56,7 @@ function requireModuleParser() { return { buildModuleSchema: RNModuleParser.buildModuleSchema, createParserErrorCapturer: RNParserUtils.createParserErrorCapturer, + parser: new RNFlowParser.FlowParser(), }; } @@ -127,7 +131,7 @@ function rule(context) { }); } - const {buildModuleSchema, createParserErrorCapturer} = + const {buildModuleSchema, createParserErrorCapturer, parser} = requireModuleParser(); const flowParser = require('flow-parser'); @@ -137,7 +141,7 @@ function rule(context) { const ast = flowParser.parse(sourceCode, {enums: true}); tryParse(() => { - buildModuleSchema(hasteModuleName, ast, tryParse); + buildModuleSchema(hasteModuleName, ast, tryParse, parser); }); parsingErrors.forEach(error => { diff --git a/packages/react-native-codegen/src/parsers/__tests__/utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/utils-test.js index 15e60040324545..0421b65e3514e6 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/utils-test.js @@ -11,6 +11,8 @@ 'use strict'; +const {MockedParser} = require('../parserMock'); + const { extractNativeModuleName, createParserErrorCapturer, @@ -297,6 +299,8 @@ describe('visit', () => { }); describe('buildSchemaFromConfigType', () => { + const parser = new MockedParser(); + const astMock = { type: 'Program', loc: { @@ -332,7 +336,7 @@ describe('buildSchemaFromConfigType', () => { require('../parsers-commons'), 'wrapModuleSchema', ); - const buildModuleSchemaMock = jest.fn((_0, _1, _2) => moduleSchemaMock); + const buildModuleSchemaMock = jest.fn((_0, _1, _2, _3) => moduleSchemaMock); const buildSchemaFromConfigTypeHelper = ( configType: 'module' | 'component' | 'none', @@ -345,6 +349,7 @@ describe('buildSchemaFromConfigType', () => { wrapComponentSchemaMock, buildComponentSchemaMock, buildModuleSchemaMock, + parser, ); describe('when configType is none', () => { @@ -426,6 +431,7 @@ describe('buildSchemaFromConfigType', () => { 'filename', astMock, expect.any(Function), + parser, ); expect(wrapModuleSchemaMock).toHaveBeenCalledTimes(1); expect(wrapModuleSchemaMock).toHaveBeenCalledWith( diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index 600d5a82a793d9..ffd7287399e3ca 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -23,6 +23,9 @@ const { const {buildComponentSchema} = require('./components'); const {wrapComponentSchema} = require('./components/schema'); const {buildModuleSchema} = require('./modules'); +const {FlowParser} = require('./parser'); + +const parser = new FlowParser(); function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { return { @@ -65,6 +68,7 @@ function buildSchema(contents: string, filename: ?string): SchemaType { wrapComponentSchema, buildComponentSchema, buildModuleSchema, + parser, ); } diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 2df5eaa528a334..bed63191d6fbae 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -69,10 +69,7 @@ const { throwIfMoreThanOneModuleInterfaceParserError, } = require('../../error-utils'); -const {FlowParser} = require('../parser'); - const language = 'Flow'; -const parser = new FlowParser(); function translateTypeAnnotation( hasteModuleName: string, @@ -271,6 +268,7 @@ function buildModuleSchema( */ ast: $FlowFixMe, tryParse: ParserErrorCapturer, + parser: Parser, ): NativeModuleSchema { const types = getTypes(ast); const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter( diff --git a/packages/react-native-codegen/src/parsers/typescript/index.js b/packages/react-native-codegen/src/parsers/typescript/index.js index b1d4a2e1feec52..230893f7118f5b 100644 --- a/packages/react-native-codegen/src/parsers/typescript/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/index.js @@ -23,6 +23,9 @@ const { const {buildComponentSchema} = require('./components'); const {wrapComponentSchema} = require('./components/schema'); const {buildModuleSchema} = require('./modules'); +const {TypeScriptParser} = require('./parser'); + +const parser = new TypeScriptParser(); function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { return { @@ -75,6 +78,7 @@ function buildSchema(contents: string, filename: ?string): SchemaType { wrapComponentSchema, buildComponentSchema, buildModuleSchema, + parser, ); } diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index 8156da51ad14da..9051b0a8f89afd 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -69,10 +69,7 @@ const { throwIfIncorrectModuleRegistryCallTypeParameterParserError, } = require('../../error-utils'); -const {TypeScriptParser} = require('../parser'); - const language = 'TypeScript'; -const parser = new TypeScriptParser(); function translateTypeAnnotation( hasteModuleName: string, @@ -277,6 +274,7 @@ function buildModuleSchema( */ ast: $FlowFixMe, tryParse: ParserErrorCapturer, + parser: Parser, ): NativeModuleSchema { const types = getTypes(ast); const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter( diff --git a/packages/react-native-codegen/src/parsers/utils.js b/packages/react-native-codegen/src/parsers/utils.js index d6c77d9e239cc2..1c4362bbf04ea5 100644 --- a/packages/react-native-codegen/src/parsers/utils.js +++ b/packages/react-native-codegen/src/parsers/utils.js @@ -12,6 +12,8 @@ import type {ComponentSchemaBuilderConfig} from './flow/components/schema'; import type {NativeModuleSchema, SchemaType} from '../CodegenSchema'; +import type {Parser} from './parser'; + const {ParserError} = require('./errors'); const {wrapModuleSchema} = require('./parsers-commons'); @@ -133,7 +135,9 @@ function buildSchemaFromConfigType( hasteModuleName: string, ast: $FlowFixMe, tryParse: ParserErrorCapturer, + parser: Parser, ) => NativeModuleSchema, + parser: Parser, ): SchemaType { switch (configType) { case 'component': { @@ -148,7 +152,7 @@ function buildSchemaFromConfigType( const [parsingErrors, tryParse] = createParserErrorCapturer(); const schema = tryParse(() => - buildModuleSchema(nativeModuleName, ast, tryParse), + buildModuleSchema(nativeModuleName, ast, tryParse, parser), ); if (parsingErrors.length > 0) { From f51093478c8491752585cd94a842eb487c05fd01 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 21:24:53 +0100 Subject: [PATCH 09/11] refactor: pass parser as a param to buildSchema and other functions that consume it --- .../react-native-codegen/src/parsers/flow/index.js | 11 ++++++++--- .../react-native-codegen/src/parsers/flow/parser.js | 2 +- .../src/parsers/typescript/index.js | 11 ++++++++--- .../src/parsers/typescript/parser.js | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index ffd7287399e3ca..7d090300b2cfa4 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -11,6 +11,7 @@ 'use strict'; import type {SchemaType} from '../../CodegenSchema.js'; +import type {Parser} from '../parser'; // $FlowFixMe[untyped-import] there's no flowtype flow-parser const flowParser = require('flow-parser'); @@ -49,7 +50,11 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { }; } -function buildSchema(contents: string, filename: ?string): SchemaType { +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { // Early return for non-Spec JavaScript files if ( !contents.includes('codegenNativeComponent') && @@ -75,11 +80,11 @@ function buildSchema(contents: string, filename: ?string): SchemaType { function parseModuleFixture(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return buildSchema(contents, 'path/NativeSampleTurboModule.js'); + return buildSchema(contents, 'path/NativeSampleTurboModule.js', parser); } function parseString(contents: string, filename: ?string): SchemaType { - return buildSchema(contents, filename); + return buildSchema(contents, filename, parser); } module.exports = { diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 1c0b1efa3c18d7..0b6c0be9a5bc72 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -87,7 +87,7 @@ class FlowParser implements Parser { parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return buildSchema(contents, filename); + return buildSchema(contents, filename, this); } } diff --git a/packages/react-native-codegen/src/parsers/typescript/index.js b/packages/react-native-codegen/src/parsers/typescript/index.js index 230893f7118f5b..ae26a8ac9db1b6 100644 --- a/packages/react-native-codegen/src/parsers/typescript/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/index.js @@ -11,6 +11,7 @@ 'use strict'; import type {SchemaType} from '../../CodegenSchema.js'; +import type {Parser} from '../parser'; // $FlowFixMe[untyped-import] Use flow-types for @babel/parser const babelParser = require('@babel/parser'); @@ -55,7 +56,11 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { }; } -function buildSchema(contents: string, filename: ?string): SchemaType { +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { // Early return for non-Spec JavaScript files if ( !contents.includes('codegenNativeComponent') && @@ -85,11 +90,11 @@ function buildSchema(contents: string, filename: ?string): SchemaType { function parseModuleFixture(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return buildSchema(contents, 'path/NativeSampleTurboModule.ts'); + return buildSchema(contents, 'path/NativeSampleTurboModule.ts', parser); } function parseString(contents: string, filename: ?string): SchemaType { - return buildSchema(contents, filename); + return buildSchema(contents, filename, parser); } module.exports = { diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index b786f7276f89fa..068f32883a6525 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -92,7 +92,7 @@ class TypeScriptParser implements Parser { parseFile(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); - return buildSchema(contents, filename); + return buildSchema(contents, filename, this); } } module.exports = { From 1672d290459ad72d32d926b8dc1df685fb9df043 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 21:41:16 +0100 Subject: [PATCH 10/11] refactor: move buildSchema into utils file It avoid circular dependencies and it is temporary It will be moved again in #35158 --- .../src/parsers/flow/index.js | 62 +--------------- .../src/parsers/flow/parser.js | 2 +- .../src/parsers/flow/utils.js | 63 ++++++++++++++++ .../src/parsers/typescript/index.js | 72 +----------------- .../src/parsers/typescript/parser.js | 2 +- .../src/parsers/typescript/utils.js | 73 ++++++++++++++++++- 6 files changed, 139 insertions(+), 135 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index 7d090300b2cfa4..60b3fb5ad575f3 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -11,72 +11,13 @@ 'use strict'; import type {SchemaType} from '../../CodegenSchema.js'; -import type {Parser} from '../parser'; -// $FlowFixMe[untyped-import] there's no flowtype flow-parser -const flowParser = require('flow-parser'); const fs = require('fs'); -const { - buildSchemaFromConfigType, - getConfigType, - isModuleRegistryCall, -} = require('../utils'); -const {buildComponentSchema} = require('./components'); -const {wrapComponentSchema} = require('./components/schema'); -const {buildModuleSchema} = require('./modules'); +const {buildSchema} = require('./utils'); const {FlowParser} = require('./parser'); const parser = new FlowParser(); -function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { - return { - CallExpression(node: $FlowFixMe) { - if ( - node.callee.type === 'Identifier' && - node.callee.name === 'codegenNativeComponent' - ) { - infoMap.isComponent = true; - } - - if (isModuleRegistryCall(node)) { - infoMap.isModule = true; - } - }, - InterfaceExtends(node: $FlowFixMe) { - if (node.id.name === 'TurboModule') { - infoMap.isModule = true; - } - }, - }; -} - -function buildSchema( - contents: string, - filename: ?string, - parser: Parser, -): SchemaType { - // Early return for non-Spec JavaScript files - if ( - !contents.includes('codegenNativeComponent') && - !contents.includes('TurboModule') - ) { - return {modules: {}}; - } - - const ast = flowParser.parse(contents, {enums: true}); - const configType = getConfigType(ast, Visitor); - - return buildSchemaFromConfigType( - configType, - filename, - ast, - wrapComponentSchema, - buildComponentSchema, - buildModuleSchema, - parser, - ); -} - function parseModuleFixture(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); @@ -88,7 +29,6 @@ function parseString(contents: string, filename: ?string): SchemaType { } module.exports = { - buildSchema, parseModuleFixture, parseString, }; diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index 0b6c0be9a5bc72..bdbce6508c5224 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -17,7 +17,7 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; -const {buildSchema} = require('.'); +const {buildSchema} = require('./utils'); const fs = require('fs'); diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index f172d8ec4c8283..b32dfae104751a 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -10,7 +10,69 @@ 'use strict'; +import type {SchemaType} from '../../CodegenSchema'; +import type {Parser} from '../parser'; import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; +const {buildComponentSchema} = require('./components'); +const {wrapComponentSchema} = require('./components/schema'); +const {buildModuleSchema} = require('./modules'); + +// $FlowFixMe[untyped-import] there's no flowtype flow-parser +const flowParser = require('flow-parser'); +const { + getConfigType, + buildSchemaFromConfigType, + isModuleRegistryCall, +} = require('../utils'); + +function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { + return { + CallExpression(node: $FlowFixMe) { + if ( + node.callee.type === 'Identifier' && + node.callee.name === 'codegenNativeComponent' + ) { + infoMap.isComponent = true; + } + + if (isModuleRegistryCall(node)) { + infoMap.isModule = true; + } + }, + InterfaceExtends(node: $FlowFixMe) { + if (node.id.name === 'TurboModule') { + infoMap.isModule = true; + } + }, + }; +} + +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { + // Early return for non-Spec JavaScript files + if ( + !contents.includes('codegenNativeComponent') && + !contents.includes('TurboModule') + ) { + return {modules: {}}; + } + + const ast = flowParser.parse(contents, {enums: true}); + const configType = getConfigType(ast, Visitor); + + return buildSchemaFromConfigType( + configType, + filename, + ast, + wrapComponentSchema, + buildComponentSchema, + buildModuleSchema, + parser, + ); +} /** * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias @@ -117,6 +179,7 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode { } module.exports = { + buildSchema, getValueFromTypes, resolveTypeAnnotation, getTypes, diff --git a/packages/react-native-codegen/src/parsers/typescript/index.js b/packages/react-native-codegen/src/parsers/typescript/index.js index ae26a8ac9db1b6..194d132e98bb99 100644 --- a/packages/react-native-codegen/src/parsers/typescript/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/index.js @@ -11,82 +11,13 @@ 'use strict'; import type {SchemaType} from '../../CodegenSchema.js'; -import type {Parser} from '../parser'; -// $FlowFixMe[untyped-import] Use flow-types for @babel/parser -const babelParser = require('@babel/parser'); const fs = require('fs'); -const { - buildSchemaFromConfigType, - getConfigType, - isModuleRegistryCall, -} = require('../utils'); -const {buildComponentSchema} = require('./components'); -const {wrapComponentSchema} = require('./components/schema'); -const {buildModuleSchema} = require('./modules'); +const {buildSchema} = require('./utils'); const {TypeScriptParser} = require('./parser'); const parser = new TypeScriptParser(); -function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { - return { - CallExpression(node: $FlowFixMe) { - if ( - node.callee.type === 'Identifier' && - node.callee.name === 'codegenNativeComponent' - ) { - infoMap.isComponent = true; - } - - if (isModuleRegistryCall(node)) { - infoMap.isModule = true; - } - }, - - TSInterfaceDeclaration(node: $FlowFixMe) { - if ( - Array.isArray(node.extends) && - node.extends.some( - extension => extension.expression.name === 'TurboModule', - ) - ) { - infoMap.isModule = true; - } - }, - }; -} - -function buildSchema( - contents: string, - filename: ?string, - parser: Parser, -): SchemaType { - // Early return for non-Spec JavaScript files - if ( - !contents.includes('codegenNativeComponent') && - !contents.includes('TurboModule') - ) { - return {modules: {}}; - } - - const ast = babelParser.parse(contents, { - sourceType: 'module', - plugins: ['typescript'], - }).program; - - const configType = getConfigType(ast, Visitor); - - return buildSchemaFromConfigType( - configType, - filename, - ast, - wrapComponentSchema, - buildComponentSchema, - buildModuleSchema, - parser, - ); -} - function parseModuleFixture(filename: string): SchemaType { const contents = fs.readFileSync(filename, 'utf8'); @@ -98,7 +29,6 @@ function parseString(contents: string, filename: ?string): SchemaType { } module.exports = { - buildSchema, parseModuleFixture, parseString, }; diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 068f32883a6525..2ac74e968fa35d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -17,7 +17,7 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; -const {buildSchema} = require('.'); +const {buildSchema} = require('./utils'); const fs = require('fs'); diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 0bc07592f1d252..1283afa1925487 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -11,13 +11,83 @@ 'use strict'; import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; +import type {Parser} from '../parser'; +// $FlowFixMe[untyped-import] Use flow-types for @babel/parser +const babelParser = require('@babel/parser'); +const { + buildSchemaFromConfigType, + getConfigType, + isModuleRegistryCall, +} = require('../utils'); +const {buildComponentSchema} = require('./components'); +const {wrapComponentSchema} = require('./components/schema'); +const {buildModuleSchema} = require('./modules'); const {parseTopLevelType} = require('./parseTopLevelType'); +import type {SchemaType} from '../../CodegenSchema'; + +function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { + return { + CallExpression(node: $FlowFixMe) { + if ( + node.callee.type === 'Identifier' && + node.callee.name === 'codegenNativeComponent' + ) { + infoMap.isComponent = true; + } + + if (isModuleRegistryCall(node)) { + infoMap.isModule = true; + } + }, + + TSInterfaceDeclaration(node: $FlowFixMe) { + if ( + Array.isArray(node.extends) && + node.extends.some( + extension => extension.expression.name === 'TurboModule', + ) + ) { + infoMap.isModule = true; + } + }, + }; +} + +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { + // Early return for non-Spec JavaScript files + if ( + !contents.includes('codegenNativeComponent') && + !contents.includes('TurboModule') + ) { + return {modules: {}}; + } + + const ast = babelParser.parse(contents, { + sourceType: 'module', + plugins: ['typescript'], + }).program; + + const configType = getConfigType(ast, Visitor); + + return buildSchemaFromConfigType( + configType, + filename, + ast, + wrapComponentSchema, + buildComponentSchema, + buildModuleSchema, + parser, + ); +} /** * TODO(T108222691): Use flow-types for @babel/parser */ - function getTypes(ast: $FlowFixMe): TypeDeclarationMap { return ast.body.reduce((types, node) => { switch (node.type) { @@ -110,6 +180,7 @@ function resolveTypeAnnotation( } module.exports = { + buildSchema, resolveTypeAnnotation, getTypes, }; From 29505f788bb3e9fc026830702cc15f606f48b25b Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 25 Nov 2022 21:51:57 +0100 Subject: [PATCH 11/11] refactor: move buildSchema into its own file At least I tried... There were still circular dependencies... (node:58076) Warning: Accessing non-existent property 'getTypes' of module exports inside circular dependency (node:58076) Warning: Accessing non-existent property 'getValueFromTypes' of module exports inside circular dependency (node:58076) Warning: Accessing non-existent property 'getValueFromTypes' of module exports inside circular dependency (node:58076) Warning: Accessing non-existent property 'buildModuleSchema' of module exports inside circular dependency --- .../src/parsers/flow/buildSchema.js | 78 ++++++++++++++++ .../src/parsers/flow/index.js | 2 +- .../src/parsers/flow/parser.js | 2 +- .../src/parsers/flow/utils.js | 63 ------------- .../src/parsers/typescript/buildSchema.js | 88 +++++++++++++++++++ .../src/parsers/typescript/index.js | 2 +- .../src/parsers/typescript/parser.js | 2 +- .../src/parsers/typescript/utils.js | 72 --------------- 8 files changed, 170 insertions(+), 139 deletions(-) create mode 100644 packages/react-native-codegen/src/parsers/flow/buildSchema.js create mode 100644 packages/react-native-codegen/src/parsers/typescript/buildSchema.js diff --git a/packages/react-native-codegen/src/parsers/flow/buildSchema.js b/packages/react-native-codegen/src/parsers/flow/buildSchema.js new file mode 100644 index 00000000000000..db8657da3725a7 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/flow/buildSchema.js @@ -0,0 +1,78 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + * @format + */ + +'use strict'; + +import type {SchemaType} from '../../CodegenSchema'; +import type {Parser} from '../parser'; + +// $FlowFixMe[untyped-import] there's no flowtype flow-parser +const flowParser = require('flow-parser'); +const { + getConfigType, + buildSchemaFromConfigType, + isModuleRegistryCall, +} = require('../utils'); +const {buildComponentSchema} = require('./components'); +const {wrapComponentSchema} = require('./components/schema'); +const {buildModuleSchema} = require('./modules'); + +function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { + return { + CallExpression(node: $FlowFixMe) { + if ( + node.callee.type === 'Identifier' && + node.callee.name === 'codegenNativeComponent' + ) { + infoMap.isComponent = true; + } + + if (isModuleRegistryCall(node)) { + infoMap.isModule = true; + } + }, + InterfaceExtends(node: $FlowFixMe) { + if (node.id.name === 'TurboModule') { + infoMap.isModule = true; + } + }, + }; +} + +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { + // Early return for non-Spec JavaScript files + if ( + !contents.includes('codegenNativeComponent') && + !contents.includes('TurboModule') + ) { + return {modules: {}}; + } + + const ast = flowParser.parse(contents, {enums: true}); + const configType = getConfigType(ast, Visitor); + + return buildSchemaFromConfigType( + configType, + filename, + ast, + wrapComponentSchema, + buildComponentSchema, + buildModuleSchema, + parser, + ); +} + +module.exports = { + buildSchema, +}; diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index 60b3fb5ad575f3..9e2e66a6d99981 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -13,7 +13,7 @@ import type {SchemaType} from '../../CodegenSchema.js'; const fs = require('fs'); -const {buildSchema} = require('./utils'); +const {buildSchema} = require('./buildSchema'); const {FlowParser} = require('./parser'); const parser = new FlowParser(); diff --git a/packages/react-native-codegen/src/parsers/flow/parser.js b/packages/react-native-codegen/src/parsers/flow/parser.js index bdbce6508c5224..835ae8b111d30e 100644 --- a/packages/react-native-codegen/src/parsers/flow/parser.js +++ b/packages/react-native-codegen/src/parsers/flow/parser.js @@ -17,7 +17,7 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; -const {buildSchema} = require('./utils'); +const {buildSchema} = require('./buildSchema'); const fs = require('fs'); diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index b32dfae104751a..f172d8ec4c8283 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -10,69 +10,7 @@ 'use strict'; -import type {SchemaType} from '../../CodegenSchema'; -import type {Parser} from '../parser'; import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; -const {buildComponentSchema} = require('./components'); -const {wrapComponentSchema} = require('./components/schema'); -const {buildModuleSchema} = require('./modules'); - -// $FlowFixMe[untyped-import] there's no flowtype flow-parser -const flowParser = require('flow-parser'); -const { - getConfigType, - buildSchemaFromConfigType, - isModuleRegistryCall, -} = require('../utils'); - -function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { - return { - CallExpression(node: $FlowFixMe) { - if ( - node.callee.type === 'Identifier' && - node.callee.name === 'codegenNativeComponent' - ) { - infoMap.isComponent = true; - } - - if (isModuleRegistryCall(node)) { - infoMap.isModule = true; - } - }, - InterfaceExtends(node: $FlowFixMe) { - if (node.id.name === 'TurboModule') { - infoMap.isModule = true; - } - }, - }; -} - -function buildSchema( - contents: string, - filename: ?string, - parser: Parser, -): SchemaType { - // Early return for non-Spec JavaScript files - if ( - !contents.includes('codegenNativeComponent') && - !contents.includes('TurboModule') - ) { - return {modules: {}}; - } - - const ast = flowParser.parse(contents, {enums: true}); - const configType = getConfigType(ast, Visitor); - - return buildSchemaFromConfigType( - configType, - filename, - ast, - wrapComponentSchema, - buildComponentSchema, - buildModuleSchema, - parser, - ); -} /** * This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias @@ -179,7 +117,6 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode { } module.exports = { - buildSchema, getValueFromTypes, resolveTypeAnnotation, getTypes, diff --git a/packages/react-native-codegen/src/parsers/typescript/buildSchema.js b/packages/react-native-codegen/src/parsers/typescript/buildSchema.js new file mode 100644 index 00000000000000..c3b56c3e535e77 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/typescript/buildSchema.js @@ -0,0 +1,88 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + * @format + */ + +'use strict'; + +import type {SchemaType} from '../../CodegenSchema'; +import type {Parser} from '../parser'; + +// $FlowFixMe[untyped-import] Use flow-types for @babel/parser +const babelParser = require('@babel/parser'); +const { + buildSchemaFromConfigType, + getConfigType, + isModuleRegistryCall, +} = require('../utils'); +const {buildComponentSchema} = require('./components'); +const {wrapComponentSchema} = require('./components/schema'); +const {buildModuleSchema} = require('./modules'); + +function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { + return { + CallExpression(node: $FlowFixMe) { + if ( + node.callee.type === 'Identifier' && + node.callee.name === 'codegenNativeComponent' + ) { + infoMap.isComponent = true; + } + + if (isModuleRegistryCall(node)) { + infoMap.isModule = true; + } + }, + + TSInterfaceDeclaration(node: $FlowFixMe) { + if ( + Array.isArray(node.extends) && + node.extends.some( + extension => extension.expression.name === 'TurboModule', + ) + ) { + infoMap.isModule = true; + } + }, + }; +} + +function buildSchema( + contents: string, + filename: ?string, + parser: Parser, +): SchemaType { + // Early return for non-Spec JavaScript files + if ( + !contents.includes('codegenNativeComponent') && + !contents.includes('TurboModule') + ) { + return {modules: {}}; + } + + const ast = babelParser.parse(contents, { + sourceType: 'module', + plugins: ['typescript'], + }).program; + + const configType = getConfigType(ast, Visitor); + + return buildSchemaFromConfigType( + configType, + filename, + ast, + wrapComponentSchema, + buildComponentSchema, + buildModuleSchema, + parser, + ); +} + +module.exports = { + buildSchema, +}; diff --git a/packages/react-native-codegen/src/parsers/typescript/index.js b/packages/react-native-codegen/src/parsers/typescript/index.js index 194d132e98bb99..d790980331c75a 100644 --- a/packages/react-native-codegen/src/parsers/typescript/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/index.js @@ -13,7 +13,7 @@ import type {SchemaType} from '../../CodegenSchema.js'; const fs = require('fs'); -const {buildSchema} = require('./utils'); +const {buildSchema} = require('./buildSchema'); const {TypeScriptParser} = require('./parser'); const parser = new TypeScriptParser(); diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.js b/packages/react-native-codegen/src/parsers/typescript/parser.js index 2ac74e968fa35d..0c077093ba0e1d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parser.js +++ b/packages/react-native-codegen/src/parsers/typescript/parser.js @@ -17,7 +17,7 @@ import type { import type {ParserType} from '../errors'; import type {Parser} from '../parser'; -const {buildSchema} = require('./utils'); +const {buildSchema} = require('./buildSchema'); const fs = require('fs'); diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 1283afa1925487..83e2f8bd2def75 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -11,79 +11,8 @@ 'use strict'; import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils'; -import type {Parser} from '../parser'; -// $FlowFixMe[untyped-import] Use flow-types for @babel/parser -const babelParser = require('@babel/parser'); -const { - buildSchemaFromConfigType, - getConfigType, - isModuleRegistryCall, -} = require('../utils'); -const {buildComponentSchema} = require('./components'); -const {wrapComponentSchema} = require('./components/schema'); -const {buildModuleSchema} = require('./modules'); const {parseTopLevelType} = require('./parseTopLevelType'); -import type {SchemaType} from '../../CodegenSchema'; - -function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) { - return { - CallExpression(node: $FlowFixMe) { - if ( - node.callee.type === 'Identifier' && - node.callee.name === 'codegenNativeComponent' - ) { - infoMap.isComponent = true; - } - - if (isModuleRegistryCall(node)) { - infoMap.isModule = true; - } - }, - - TSInterfaceDeclaration(node: $FlowFixMe) { - if ( - Array.isArray(node.extends) && - node.extends.some( - extension => extension.expression.name === 'TurboModule', - ) - ) { - infoMap.isModule = true; - } - }, - }; -} - -function buildSchema( - contents: string, - filename: ?string, - parser: Parser, -): SchemaType { - // Early return for non-Spec JavaScript files - if ( - !contents.includes('codegenNativeComponent') && - !contents.includes('TurboModule') - ) { - return {modules: {}}; - } - - const ast = babelParser.parse(contents, { - sourceType: 'module', - plugins: ['typescript'], - }).program; - - const configType = getConfigType(ast, Visitor); - - return buildSchemaFromConfigType( - configType, - filename, - ast, - wrapComponentSchema, - buildComponentSchema, - buildModuleSchema, - parser, - ); -} /** * TODO(T108222691): Use flow-types for @babel/parser @@ -180,7 +109,6 @@ function resolveTypeAnnotation( } module.exports = { - buildSchema, resolveTypeAnnotation, getTypes, };