Skip to content

Commit f23f7f4

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
Replace getTypes functions with parser specific methods (#36225)
Summary: This PR is task 74 from #34872: > Move getTypes functions from utils.js to specific Parsers. Right now we have two Parser classes that takes care of the language specific details and two utils files that contains similar logic. We would like to move everything under the Parsers classes for better OOP architecture and to encourage code-reuse. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Replace getTypes functions with parser specific methods Pull Request resolved: #36225 Test Plan: I tested using Jest and Flow commands. Reviewed By: rshest Differential Revision: D43453454 Pulled By: cipolleschi fbshipit-source-id: 0eebcb55e1af3319e2c35bb462980046329a2c09
1 parent a1f6b4d commit f23f7f4

File tree

12 files changed

+113
-91
lines changed

12 files changed

+113
-91
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ const {
3333
} = require('../errors');
3434

3535
import {MockedParser} from '../parserMock';
36+
import {FlowParser} from '../flow/parser';
3637

3738
const parser = new MockedParser();
3839

40+
const flowParser = new FlowParser();
41+
3942
const flowTranslateTypeAnnotation = require('../flow/modules/index');
4043
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
4144

@@ -384,7 +387,9 @@ describe('buildSchemaFromConfigType', () => {
384387
};
385388

386389
const wrapComponentSchemaMock = jest.fn();
387-
const buildComponentSchemaMock = jest.fn(_ => componentSchemaMock);
390+
const buildComponentSchemaMock = jest.fn(
391+
(_ast, _parser) => componentSchemaMock,
392+
);
388393
const buildModuleSchemaMock = jest.fn((_0, _1, _2, _3) => moduleSchemaMock);
389394

390395
const buildSchemaFromConfigTypeHelper = (
@@ -414,7 +419,7 @@ describe('buildSchemaFromConfigType', () => {
414419
buildSchemaFromConfigTypeHelper('component');
415420

416421
expect(buildComponentSchemaMock).toHaveBeenCalledTimes(1);
417-
expect(buildComponentSchemaMock).toHaveBeenCalledWith(astMock);
422+
expect(buildComponentSchemaMock).toHaveBeenCalledWith(astMock, parser);
418423
expect(wrapComponentSchemaMock).toHaveBeenCalledTimes(1);
419424
expect(wrapComponentSchemaMock).toHaveBeenCalledWith(componentSchemaMock);
420425

@@ -681,7 +686,7 @@ describe('buildSchema', () => {
681686
buildComponentSchema,
682687
buildModuleSchema,
683688
Visitor,
684-
parser,
689+
flowParser,
685690
);
686691

687692
expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);
@@ -734,7 +739,7 @@ describe('buildSchema', () => {
734739
buildComponentSchema,
735740
buildModuleSchema,
736741
Visitor,
737-
parser,
742+
flowParser,
738743
);
739744

740745
expect(getConfigTypeSpy).toHaveBeenCalledTimes(1);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010

1111
'use strict';
12+
import type {Parser} from '../../parser';
1213
import type {TypeDeclarationMap} from '../../utils';
1314
import type {CommandOptions} from './options';
1415
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
1516

16-
const {getTypes} = require('../utils');
1717
const {getCommands} = require('./commands');
1818
const {getEvents} = require('./events');
1919
const {getExtendsProps, removeKnownExtends} = require('./extends');
@@ -180,9 +180,10 @@ function getCommandProperties(
180180
}
181181

182182
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
183-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
184-
* LTI update could not be added via codemod */
185-
function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
183+
function buildComponentSchema(
184+
ast: $FlowFixMe,
185+
parser: Parser,
186+
): ComponentSchemaBuilderConfig {
186187
const {
187188
componentName,
188189
propsTypeName,
@@ -191,7 +192,7 @@ function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
191192
optionsExpression,
192193
} = findComponentConfig(ast);
193194

194-
const types = getTypes(ast);
195+
const types = parser.getTypes(ast);
195196

196197
const propProperties = getProperties(propsTypeName, types);
197198
const commandOptions = getCommandOptions(commandOptionsExpression);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {Parser} from '../../parser';
2525
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2626

2727
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
28-
const {resolveTypeAnnotation, getTypes} = require('../utils');
28+
const {resolveTypeAnnotation} = require('../utils');
2929
const {
3030
unwrapNullable,
3131
wrapNullable,
@@ -339,7 +339,7 @@ function buildModuleSchema(
339339
tryParse: ParserErrorCapturer,
340340
parser: Parser,
341341
): NativeModuleSchema {
342-
const types = getTypes(ast);
342+
const types = parser.getTypes(ast);
343343
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
344344
t => parser.isModuleInterface(t),
345345
);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,46 @@ class FlowParser implements Parser {
213213
): $FlowFixMe {
214214
return types[typeAnnotation.typeParameters.params[0].id.name];
215215
}
216+
217+
/**
218+
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
219+
* declaration type. Unfortunately, we don't have those types, because flow-parser
220+
* generates them, and flow-parser is not type-safe. In the future, we should find
221+
* a way to get these types from our flow parser library.
222+
*
223+
* TODO(T71778680): Flow type AST Nodes
224+
*/
225+
226+
getTypes(ast: $FlowFixMe): TypeDeclarationMap {
227+
return ast.body.reduce((types, node) => {
228+
if (
229+
node.type === 'ExportNamedDeclaration' &&
230+
node.exportKind === 'type'
231+
) {
232+
if (
233+
node.declaration != null &&
234+
(node.declaration.type === 'TypeAlias' ||
235+
node.declaration.type === 'InterfaceDeclaration')
236+
) {
237+
types[node.declaration.id.name] = node.declaration;
238+
}
239+
} else if (
240+
node.type === 'ExportNamedDeclaration' &&
241+
node.exportKind === 'value' &&
242+
node.declaration &&
243+
node.declaration.type === 'EnumDeclaration'
244+
) {
245+
types[node.declaration.id.name] = node.declaration;
246+
} else if (
247+
node.type === 'TypeAlias' ||
248+
node.type === 'InterfaceDeclaration' ||
249+
node.type === 'EnumDeclaration'
250+
) {
251+
types[node.id.name] = node;
252+
}
253+
return types;
254+
}, {});
255+
}
216256
}
217257

218258
module.exports = {

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,6 @@
1212

1313
import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
1414

15-
/**
16-
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
17-
* declaration type. Unfortunately, we don't have those types, because flow-parser
18-
* generates them, and flow-parser is not type-safe. In the future, we should find
19-
* a way to get these types from our flow parser library.
20-
*
21-
* TODO(T71778680): Flow type AST Nodes
22-
*/
23-
24-
function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
25-
return ast.body.reduce((types, node) => {
26-
if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') {
27-
if (
28-
node.declaration != null &&
29-
(node.declaration.type === 'TypeAlias' ||
30-
node.declaration.type === 'InterfaceDeclaration')
31-
) {
32-
types[node.declaration.id.name] = node.declaration;
33-
}
34-
} else if (
35-
node.type === 'ExportNamedDeclaration' &&
36-
node.exportKind === 'value' &&
37-
node.declaration &&
38-
node.declaration.type === 'EnumDeclaration'
39-
) {
40-
types[node.declaration.id.name] = node.declaration;
41-
} else if (
42-
node.type === 'TypeAlias' ||
43-
node.type === 'InterfaceDeclaration' ||
44-
node.type === 'EnumDeclaration'
45-
) {
46-
types[node.id.name] = node;
47-
}
48-
return types;
49-
}, {});
50-
}
51-
5215
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
5316
export type ASTNode = Object;
5417

@@ -134,5 +97,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
13497
module.exports = {
13598
getValueFromTypes,
13699
resolveTypeAnnotation,
137-
getTypes,
138100
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,9 @@ export interface Parser {
169169
typeAnnotation: $FlowFixMe,
170170
types: TypeDeclarationMap,
171171
): $FlowFixMe;
172+
173+
/**
174+
* Given the AST, returns the TypeDeclarationMap
175+
*/
176+
getTypes(ast: $FlowFixMe): TypeDeclarationMap;
172177
}

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type {
2121
NativeModuleEnumMemberType,
2222
NativeModuleEnumMembers,
2323
} from '../CodegenSchema';
24-
2524
import type {TypeDeclarationMap} from './utils';
2625

2726
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
@@ -177,4 +176,8 @@ export class MockedParser implements Parser {
177176
): $FlowFixMe {
178177
return types[typeAnnotation.typeParameters.params[0].id.name];
179178
}
179+
180+
getTypes(ast: $FlowFixMe): TypeDeclarationMap {
181+
return {};
182+
}
180183
}

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,10 @@ function buildSchemaFromConfigType(
345345
filename: ?string,
346346
ast: $FlowFixMe,
347347
wrapComponentSchema: (config: ComponentSchemaBuilderConfig) => SchemaType,
348-
buildComponentSchema: (ast: $FlowFixMe) => ComponentSchemaBuilderConfig,
348+
buildComponentSchema: (
349+
ast: $FlowFixMe,
350+
parser: Parser,
351+
) => ComponentSchemaBuilderConfig,
349352
buildModuleSchema: (
350353
hasteModuleName: string,
351354
ast: $FlowFixMe,
@@ -356,7 +359,7 @@ function buildSchemaFromConfigType(
356359
): SchemaType {
357360
switch (configType) {
358361
case 'component': {
359-
return wrapComponentSchema(buildComponentSchema(ast));
362+
return wrapComponentSchema(buildComponentSchema(ast, parser));
360363
}
361364
case 'module': {
362365
if (filename === undefined || filename === null) {
@@ -398,7 +401,10 @@ function buildSchema(
398401
contents: string,
399402
filename: ?string,
400403
wrapComponentSchema: (config: ComponentSchemaBuilderConfig) => SchemaType,
401-
buildComponentSchema: (ast: $FlowFixMe) => ComponentSchemaBuilderConfig,
404+
buildComponentSchema: (
405+
ast: $FlowFixMe,
406+
parser: Parser,
407+
) => ComponentSchemaBuilderConfig,
402408
buildModuleSchema: (
403409
hasteModuleName: string,
404410
ast: $FlowFixMe,

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
'use strict';
1212
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
13+
import type {Parser} from '../../parser';
1314
import type {TypeDeclarationMap} from '../../utils';
1415
import type {CommandOptions} from './options';
1516
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
1617

17-
const {getTypes} = require('../utils');
1818
const {getCommands} = require('./commands');
1919
const {getEvents} = require('./events');
2020
const {categorizeProps} = require('./extends');
@@ -185,9 +185,10 @@ function getCommandProperties(
185185
type PropsAST = Object;
186186

187187
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
188-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
189-
* LTI update could not be added via codemod */
190-
function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
188+
function buildComponentSchema(
189+
ast: $FlowFixMe,
190+
parser: Parser,
191+
): ComponentSchemaBuilderConfig {
191192
const {
192193
componentName,
193194
propsTypeName,
@@ -196,7 +197,7 @@ function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
196197
optionsExpression,
197198
} = findComponentConfig(ast);
198199

199-
const types = getTypes(ast);
200+
const types = parser.getTypes(ast);
200201

201202
const propProperties = getProperties(propsTypeName, types);
202203
const commandOptions = getCommandOptions(commandOptionsExpression);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {flattenIntersectionType} = require('../parseTopLevelType');
3131
const {flattenProperties} = require('../components/componentsUtils');
3232

3333
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
34-
const {resolveTypeAnnotation, getTypes} = require('../utils');
34+
const {resolveTypeAnnotation} = require('../utils');
3535

3636
const {
3737
parseObjectProperty,
@@ -445,7 +445,7 @@ function buildModuleSchema(
445445
tryParse: ParserErrorCapturer,
446446
parser: Parser,
447447
): NativeModuleSchema {
448-
const types = getTypes(ast);
448+
const types = parser.getTypes(ast);
449449
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
450450
t => parser.isModuleInterface(t),
451451
);

0 commit comments

Comments
 (0)