Skip to content

Commit 6cede0d

Browse files
committed
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
1 parent 7453fca commit 6cede0d

File tree

8 files changed

+170
-139
lines changed

8 files changed

+170
-139
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {SchemaType} from '../../CodegenSchema';
14+
import type {Parser} from '../parser';
15+
16+
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
17+
const flowParser = require('flow-parser');
18+
const {
19+
getConfigType,
20+
buildSchemaFromConfigType,
21+
isModuleRegistryCall,
22+
} = require('../utils');
23+
const {buildComponentSchema} = require('./components');
24+
const {wrapComponentSchema} = require('./components/schema');
25+
const {buildModuleSchema} = require('./modules');
26+
27+
function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
28+
return {
29+
CallExpression(node: $FlowFixMe) {
30+
if (
31+
node.callee.type === 'Identifier' &&
32+
node.callee.name === 'codegenNativeComponent'
33+
) {
34+
infoMap.isComponent = true;
35+
}
36+
37+
if (isModuleRegistryCall(node)) {
38+
infoMap.isModule = true;
39+
}
40+
},
41+
InterfaceExtends(node: $FlowFixMe) {
42+
if (node.id.name === 'TurboModule') {
43+
infoMap.isModule = true;
44+
}
45+
},
46+
};
47+
}
48+
49+
function buildSchema(
50+
contents: string,
51+
filename: ?string,
52+
parser: Parser,
53+
): SchemaType {
54+
// Early return for non-Spec JavaScript files
55+
if (
56+
!contents.includes('codegenNativeComponent') &&
57+
!contents.includes('TurboModule')
58+
) {
59+
return {modules: {}};
60+
}
61+
62+
const ast = flowParser.parse(contents, {enums: true});
63+
const configType = getConfigType(ast, Visitor);
64+
65+
return buildSchemaFromConfigType(
66+
configType,
67+
filename,
68+
ast,
69+
wrapComponentSchema,
70+
buildComponentSchema,
71+
buildModuleSchema,
72+
parser,
73+
);
74+
}
75+
76+
module.exports = {
77+
buildSchema,
78+
};

packages/react-native-codegen/src/parsers/flow/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import type {SchemaType} from '../../CodegenSchema.js';
1414

1515
const fs = require('fs');
16-
const {buildSchema} = require('./utils');
16+
const {buildSchema} = require('./buildSchema');
1717
const {FlowParser} = require('./parser');
1818

1919
const parser = new FlowParser();

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import type {ParserType} from '../errors';
1818
import type {Parser} from '../parser';
1919

20-
const {buildSchema} = require('./utils');
20+
const {buildSchema} = require('./buildSchema');
2121

2222
const fs = require('fs');
2323

packages/react-native-codegen/src/parsers/flow/utils.js

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,7 @@
1010

1111
'use strict';
1212

13-
import type {SchemaType} from '../../CodegenSchema';
14-
import type {Parser} from '../parser';
1513
import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils';
16-
const {buildComponentSchema} = require('./components');
17-
const {wrapComponentSchema} = require('./components/schema');
18-
const {buildModuleSchema} = require('./modules');
19-
20-
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
21-
const flowParser = require('flow-parser');
22-
const {
23-
getConfigType,
24-
buildSchemaFromConfigType,
25-
isModuleRegistryCall,
26-
} = require('../utils');
27-
28-
function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
29-
return {
30-
CallExpression(node: $FlowFixMe) {
31-
if (
32-
node.callee.type === 'Identifier' &&
33-
node.callee.name === 'codegenNativeComponent'
34-
) {
35-
infoMap.isComponent = true;
36-
}
37-
38-
if (isModuleRegistryCall(node)) {
39-
infoMap.isModule = true;
40-
}
41-
},
42-
InterfaceExtends(node: $FlowFixMe) {
43-
if (node.id.name === 'TurboModule') {
44-
infoMap.isModule = true;
45-
}
46-
},
47-
};
48-
}
49-
50-
function buildSchema(
51-
contents: string,
52-
filename: ?string,
53-
parser: Parser,
54-
): SchemaType {
55-
// Early return for non-Spec JavaScript files
56-
if (
57-
!contents.includes('codegenNativeComponent') &&
58-
!contents.includes('TurboModule')
59-
) {
60-
return {modules: {}};
61-
}
62-
63-
const ast = flowParser.parse(contents, {enums: true});
64-
const configType = getConfigType(ast, Visitor);
65-
66-
return buildSchemaFromConfigType(
67-
configType,
68-
filename,
69-
ast,
70-
wrapComponentSchema,
71-
buildComponentSchema,
72-
buildModuleSchema,
73-
parser,
74-
);
75-
}
7614

7715
/**
7816
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
@@ -179,7 +117,6 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
179117
}
180118

181119
module.exports = {
182-
buildSchema,
183120
getValueFromTypes,
184121
resolveTypeAnnotation,
185122
getTypes,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {SchemaType} from '../../CodegenSchema';
14+
import type {Parser} from '../parser';
15+
16+
// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
17+
const babelParser = require('@babel/parser');
18+
const {
19+
buildSchemaFromConfigType,
20+
getConfigType,
21+
isModuleRegistryCall,
22+
} = require('../utils');
23+
const {buildComponentSchema} = require('./components');
24+
const {wrapComponentSchema} = require('./components/schema');
25+
const {buildModuleSchema} = require('./modules');
26+
27+
function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
28+
return {
29+
CallExpression(node: $FlowFixMe) {
30+
if (
31+
node.callee.type === 'Identifier' &&
32+
node.callee.name === 'codegenNativeComponent'
33+
) {
34+
infoMap.isComponent = true;
35+
}
36+
37+
if (isModuleRegistryCall(node)) {
38+
infoMap.isModule = true;
39+
}
40+
},
41+
42+
TSInterfaceDeclaration(node: $FlowFixMe) {
43+
if (
44+
Array.isArray(node.extends) &&
45+
node.extends.some(
46+
extension => extension.expression.name === 'TurboModule',
47+
)
48+
) {
49+
infoMap.isModule = true;
50+
}
51+
},
52+
};
53+
}
54+
55+
function buildSchema(
56+
contents: string,
57+
filename: ?string,
58+
parser: Parser,
59+
): SchemaType {
60+
// Early return for non-Spec JavaScript files
61+
if (
62+
!contents.includes('codegenNativeComponent') &&
63+
!contents.includes('TurboModule')
64+
) {
65+
return {modules: {}};
66+
}
67+
68+
const ast = babelParser.parse(contents, {
69+
sourceType: 'module',
70+
plugins: ['typescript'],
71+
}).program;
72+
73+
const configType = getConfigType(ast, Visitor);
74+
75+
return buildSchemaFromConfigType(
76+
configType,
77+
filename,
78+
ast,
79+
wrapComponentSchema,
80+
buildComponentSchema,
81+
buildModuleSchema,
82+
parser,
83+
);
84+
}
85+
86+
module.exports = {
87+
buildSchema,
88+
};

packages/react-native-codegen/src/parsers/typescript/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import type {SchemaType} from '../../CodegenSchema.js';
1414

1515
const fs = require('fs');
16-
const {buildSchema} = require('./utils');
16+
const {buildSchema} = require('./buildSchema');
1717
const {TypeScriptParser} = require('./parser');
1818

1919
const parser = new TypeScriptParser();

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import type {ParserType} from '../errors';
1818
import type {Parser} from '../parser';
1919

20-
const {buildSchema} = require('./utils');
20+
const {buildSchema} = require('./buildSchema');
2121

2222
const fs = require('fs');
2323

packages/react-native-codegen/src/parsers/typescript/utils.js

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,79 +11,8 @@
1111
'use strict';
1212

1313
import type {TypeAliasResolutionStatus, TypeDeclarationMap} from '../utils';
14-
import type {Parser} from '../parser';
1514

16-
// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
17-
const babelParser = require('@babel/parser');
18-
const {
19-
buildSchemaFromConfigType,
20-
getConfigType,
21-
isModuleRegistryCall,
22-
} = require('../utils');
23-
const {buildComponentSchema} = require('./components');
24-
const {wrapComponentSchema} = require('./components/schema');
25-
const {buildModuleSchema} = require('./modules');
2615
const {parseTopLevelType} = require('./parseTopLevelType');
27-
import type {SchemaType} from '../../CodegenSchema';
28-
29-
function Visitor(infoMap: {isComponent: boolean, isModule: boolean}) {
30-
return {
31-
CallExpression(node: $FlowFixMe) {
32-
if (
33-
node.callee.type === 'Identifier' &&
34-
node.callee.name === 'codegenNativeComponent'
35-
) {
36-
infoMap.isComponent = true;
37-
}
38-
39-
if (isModuleRegistryCall(node)) {
40-
infoMap.isModule = true;
41-
}
42-
},
43-
44-
TSInterfaceDeclaration(node: $FlowFixMe) {
45-
if (
46-
Array.isArray(node.extends) &&
47-
node.extends.some(
48-
extension => extension.expression.name === 'TurboModule',
49-
)
50-
) {
51-
infoMap.isModule = true;
52-
}
53-
},
54-
};
55-
}
56-
57-
function buildSchema(
58-
contents: string,
59-
filename: ?string,
60-
parser: Parser,
61-
): SchemaType {
62-
// Early return for non-Spec JavaScript files
63-
if (
64-
!contents.includes('codegenNativeComponent') &&
65-
!contents.includes('TurboModule')
66-
) {
67-
return {modules: {}};
68-
}
69-
70-
const ast = babelParser.parse(contents, {
71-
sourceType: 'module',
72-
plugins: ['typescript'],
73-
}).program;
74-
75-
const configType = getConfigType(ast, Visitor);
76-
77-
return buildSchemaFromConfigType(
78-
configType,
79-
filename,
80-
ast,
81-
wrapComponentSchema,
82-
buildComponentSchema,
83-
buildModuleSchema,
84-
parser,
85-
);
86-
}
8716

8817
/**
8918
* TODO(T108222691): Use flow-types for @babel/parser
@@ -180,7 +109,6 @@ function resolveTypeAnnotation(
180109
}
181110

182111
module.exports = {
183-
buildSchema,
184112
resolveTypeAnnotation,
185113
getTypes,
186114
};

0 commit comments

Comments
 (0)