Skip to content

Commit

Permalink
refactor: move buildSchema into its own file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MaeIg committed Dec 8, 2022
1 parent 1672d29 commit 29505f7
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 139 deletions.
78 changes: 78 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/buildSchema.js
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/parsers/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
63 changes: 0 additions & 63 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,7 +117,6 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
}

module.exports = {
buildSchema,
getValueFromTypes,
resolveTypeAnnotation,
getTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
72 changes: 0 additions & 72 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -180,7 +109,6 @@ function resolveTypeAnnotation(
}

module.exports = {
buildSchema,
resolveTypeAnnotation,
getTypes,
};

0 comments on commit 29505f7

Please sign in to comment.