Skip to content

Commit abe3cad

Browse files
author
Antoine Doubovetzky
committed
refactor(codegen): replace getTypes functions with TS and Flow parsers methods
1 parent 032edd8 commit abe3cad

File tree

9 files changed

+34
-92
lines changed

9 files changed

+34
-92
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/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/parserMock.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import type {
2323
} from '../CodegenSchema';
2424
import type {TypeDeclarationMap} from './utils';
2525

26-
import type {TypeDeclarationMap} from './utils';
27-
2826
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
2927
const flowParser = require('flow-parser');
3028
const {

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
);

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,6 @@ import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
1414

1515
const {parseTopLevelType} = require('./parseTopLevelType');
1616

17-
/**
18-
* TODO(T108222691): Use flow-types for @babel/parser
19-
*/
20-
function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
21-
return ast.body.reduce((types, node) => {
22-
switch (node.type) {
23-
case 'ExportNamedDeclaration': {
24-
if (node.declaration) {
25-
switch (node.declaration.type) {
26-
case 'TSTypeAliasDeclaration':
27-
case 'TSInterfaceDeclaration':
28-
case 'TSEnumDeclaration': {
29-
types[node.declaration.id.name] = node.declaration;
30-
break;
31-
}
32-
}
33-
}
34-
break;
35-
}
36-
case 'TSTypeAliasDeclaration':
37-
case 'TSInterfaceDeclaration':
38-
case 'TSEnumDeclaration': {
39-
types[node.id.name] = node;
40-
break;
41-
}
42-
}
43-
return types;
44-
}, {});
45-
}
46-
4717
// $FlowFixMe[unclear-type] Use flow-types for @babel/parser
4818
export type ASTNode = Object;
4919

@@ -131,5 +101,4 @@ function resolveTypeAnnotation(
131101

132102
module.exports = {
133103
resolveTypeAnnotation,
134-
getTypes,
135104
};

0 commit comments

Comments
 (0)