Skip to content

Commit c937162

Browse files
siddarthkayfacebook-github-bot
authored andcommitted
move extendsForProp into parsers-commons (#37052)
Summary: [Codegen 94] This PR attempts to extracts the logic of `extendsForProp` function from the following locations : - `parsers/flow/components/extends.js` - `parsers/typescript/components/props.js` since they are the same and move the function to `parsers/parsers-commons.js` as requested on #34872 ## Changelog: [Internal] [Changed] - Move `extendsForProp` to parser-commons and update usages. Pull Request resolved: #37052 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green Reviewed By: cipolleschi Differential Revision: D45225880 Pulled By: rshest fbshipit-source-id: 45199089746d58d9e9494b28040b34c2a0eb31fe
1 parent 5ff19f0 commit c937162

File tree

12 files changed

+105
-53
lines changed

12 files changed

+105
-53
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import type {ASTNode} from '../utils';
1414
import type {NamedShape} from '../../../CodegenSchema.js';
1515
const {getValueFromTypes} = require('../utils.js');
16-
import type {TypeDeclarationMap} from '../../utils';
16+
import type {TypeDeclarationMap, PropAST} from '../../utils';
1717

1818
function getProperties(
1919
typeName: string,
@@ -499,9 +499,6 @@ function getSchemaInfo(
499499
};
500500
}
501501

502-
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
503-
type PropAST = Object;
504-
505502
module.exports = {
506503
getProperties,
507504
getSchemaInfo,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function buildComponentSchema(
126126

127127
const propProperties = getProperties(propsTypeName, types);
128128
const commandProperties = getCommandProperties(ast, parser);
129-
const {extendsProps, props} = getProps(propProperties, types);
129+
const {extendsProps, props} = getProps(propProperties, types, parser);
130130

131131
const options = getOptions(optionsExpression);
132132
const events = getEvents(propProperties, types, parser);

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ import type {
1515
NamedShape,
1616
PropTypeAnnotation,
1717
} from '../../../CodegenSchema.js';
18-
import type {TypeDeclarationMap} from '../../utils';
18+
import type {TypeDeclarationMap, PropAST} from '../../utils';
19+
import type {Parser} from '../../parser';
1920

2021
const {
2122
flattenProperties,
2223
getSchemaInfo,
2324
getTypeAnnotation,
2425
} = require('./componentsUtils.js');
2526

26-
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
27-
type PropAST = Object;
28-
2927
type ExtendsForProp = null | {
3028
type: 'ReactNativeBuiltInType',
3129
knownTypeName: 'ReactNativeCoreViewProps',
@@ -58,11 +56,13 @@ function buildPropSchema(
5856
function extendsForProp(
5957
prop: PropAST,
6058
types: TypeDeclarationMap,
59+
parser: Parser,
6160
): ExtendsForProp {
62-
if (!prop.argument) {
61+
const argument = parser.argumentForProp(prop);
62+
if (!argument) {
6363
console.log('null', prop);
6464
}
65-
const name = prop.argument.id.name;
65+
const name = parser.nameForArgument(prop);
6666

6767
if (types[name] != null) {
6868
// This type is locally defined in the file
@@ -84,21 +84,23 @@ function extendsForProp(
8484
function removeKnownExtends(
8585
typeDefinition: $ReadOnlyArray<PropAST>,
8686
types: TypeDeclarationMap,
87+
parser: Parser,
8788
): $ReadOnlyArray<PropAST> {
8889
return typeDefinition.filter(
8990
prop =>
9091
prop.type !== 'ObjectTypeSpreadProperty' ||
91-
extendsForProp(prop, types) === null,
92+
extendsForProp(prop, types, parser) === null,
9293
);
9394
}
9495

9596
function getExtendsProps(
9697
typeDefinition: $ReadOnlyArray<PropAST>,
9798
types: TypeDeclarationMap,
99+
parser: Parser,
98100
): $ReadOnlyArray<ExtendsPropsShape> {
99101
return typeDefinition
100102
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
101-
.map(prop => extendsForProp(prop, types))
103+
.map(prop => extendsForProp(prop, types, parser))
102104
.filter(Boolean);
103105
}
104106
/**
@@ -108,18 +110,19 @@ function getExtendsProps(
108110
function getProps(
109111
typeDefinition: $ReadOnlyArray<PropAST>,
110112
types: TypeDeclarationMap,
113+
parser: Parser,
111114
): {
112115
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
113116
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
114117
} {
115-
const nonExtendsProps = removeKnownExtends(typeDefinition, types);
118+
const nonExtendsProps = removeKnownExtends(typeDefinition, types, parser);
116119
const props = flattenProperties(nonExtendsProps, types)
117120
.map(property => buildPropSchema(property, types))
118121
.filter(Boolean);
119122

120123
return {
121124
props,
122-
extendsProps: getExtendsProps(typeDefinition, types),
125+
extendsProps: getExtendsProps(typeDefinition, types, parser),
123126
};
124127
}
125128

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
} from '../../CodegenSchema';
2424
import type {ParserType} from '../errors';
2525
import type {Parser} from '../parser';
26-
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';
26+
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';
2727

2828
const {flowTranslateTypeAnnotation} = require('./modules');
2929

@@ -329,6 +329,14 @@ class FlowParser implements Parser {
329329
convertKeywordToTypeAnnotation(keyword: string): string {
330330
return keyword;
331331
}
332+
333+
argumentForProp(prop: PropAST): $FlowFixMe {
334+
return prop.argument;
335+
}
336+
337+
nameForArgument(prop: PropAST): $FlowFixMe {
338+
return prop.argument.id.name;
339+
}
332340
}
333341

334342
module.exports = {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222
NativeModuleEnumMap,
2323
} from '../CodegenSchema';
2424
import type {ParserType} from './errors';
25-
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
25+
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
2626

2727
/**
2828
* This is the main interface for Parsers of various languages.
@@ -255,4 +255,18 @@ export interface Parser {
255255
* @returns: converted TypeAnnotation to Keywords
256256
*/
257257
convertKeywordToTypeAnnotation(keyword: string): string;
258+
259+
/**
260+
* Given a prop return its arguments.
261+
* @parameter prop
262+
* @returns: Arguments of the prop
263+
*/
264+
argumentForProp(prop: PropAST): $FlowFixMe;
265+
266+
/**
267+
* Given a prop return its name.
268+
* @parameter prop
269+
* @returns: name property
270+
*/
271+
nameForArgument(prop: PropAST): $FlowFixMe;
258272
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
NativeModuleAliasMap,
2424
NativeModuleEnumMap,
2525
} from '../CodegenSchema';
26-
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
26+
import type {ParserErrorCapturer, PropAST, TypeDeclarationMap} from './utils';
2727

2828
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
2929
const flowParser = require('flow-parser');
@@ -243,4 +243,12 @@ export class MockedParser implements Parser {
243243
convertKeywordToTypeAnnotation(keyword: string): string {
244244
return keyword;
245245
}
246+
247+
argumentForProp(prop: PropAST): $FlowFixMe {
248+
return prop.expression;
249+
}
250+
251+
nameForArgument(prop: PropAST): $FlowFixMe {
252+
return prop.expression.name;
253+
}
246254
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727

2828
import type {Parser} from './parser';
2929
import type {ParserType} from './errors';
30-
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
30+
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
3131
import type {ComponentSchemaBuilderConfig} from './schema.js';
3232

3333
const {
@@ -69,6 +69,11 @@ export type CommandOptions = $ReadOnly<{
6969
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
7070
type OptionsAST = Object;
7171

72+
type ExtendedPropResult = {
73+
type: 'ReactNativeBuiltInType',
74+
knownTypeName: 'ReactNativeCoreViewProps',
75+
} | null;
76+
7277
function wrapModuleSchema(
7378
nativeModuleSchema: NativeModuleSchema,
7479
hasteModuleName: string,
@@ -817,6 +822,36 @@ function propertyNames(
817822
.filter(Boolean);
818823
}
819824

825+
function extendsForProp(
826+
prop: PropAST,
827+
types: TypeDeclarationMap,
828+
parser: Parser,
829+
): ExtendedPropResult {
830+
const argument = parser.argumentForProp(prop);
831+
832+
if (!argument) {
833+
console.log('null', prop);
834+
}
835+
836+
const name = parser.nameForArgument(prop);
837+
838+
if (types[name] != null) {
839+
// This type is locally defined in the file
840+
return null;
841+
}
842+
843+
switch (name) {
844+
case 'ViewProps':
845+
return {
846+
type: 'ReactNativeBuiltInType',
847+
knownTypeName: 'ReactNativeCoreViewProps',
848+
};
849+
default: {
850+
throw new Error(`Unable to handle prop spread: ${name}`);
851+
}
852+
}
853+
}
854+
820855
module.exports = {
821856
wrapModuleSchema,
822857
unwrapNullable,
@@ -836,4 +871,5 @@ module.exports = {
836871
getCommandOptions,
837872
getOptions,
838873
getCommandTypeNameAndOptionsExpression,
874+
extendsForProp,
839875
};

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
parseTopLevelType,
1616
flattenIntersectionType,
1717
} = require('../parseTopLevelType');
18-
import type {TypeDeclarationMap} from '../../utils';
18+
import type {TypeDeclarationMap, PropAST} from '../../utils';
1919

2020
function getProperties(
2121
typeName: string,
@@ -463,9 +463,6 @@ function getSchemaInfo(
463463
};
464464
}
465465

466-
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
467-
type PropAST = Object;
468-
469466
function verifyPropNotAlreadyDefined(
470467
props: $ReadOnlyArray<PropAST>,
471468
needleProp: PropAST,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function buildComponentSchema(
135135

136136
const componentEventAsts: Array<PropsAST> = [];
137137
categorizeProps(propProperties, types, componentEventAsts);
138-
const {props, extendsProps} = getProps(propProperties, types);
138+
const {props, extendsProps} = getProps(propProperties, types, parser);
139139
const events = getEvents(componentEventAsts, types, parser);
140140
const commands = getCommands(commandProperties, types);
141141

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

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

1111
'use strict';
12+
1213
const {getSchemaInfo, getTypeAnnotation} = require('./componentsUtils.js');
1314

1415
import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
15-
import type {TypeDeclarationMap} from '../../utils';
16+
import type {TypeDeclarationMap, PropAST} from '../../utils';
1617
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
18+
import type {Parser} from '../../parser';
1719

1820
const {flattenProperties} = require('./componentsUtils.js');
1921
const {parseTopLevelType} = require('../parseTopLevelType');
20-
21-
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
22-
type PropAST = Object;
22+
const {extendsForProp} = require('../../parsers-commons');
2323

2424
function buildPropSchema(
2525
property: PropAST,
@@ -59,32 +59,10 @@ function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
5959
return !isStyle;
6060
}
6161

62-
function extendsForProp(prop: PropAST, types: TypeDeclarationMap) {
63-
if (!prop.expression) {
64-
console.log('null', prop);
65-
}
66-
const name = prop.expression.name;
67-
68-
if (types[name] != null) {
69-
// This type is locally defined in the file
70-
return null;
71-
}
72-
73-
switch (name) {
74-
case 'ViewProps':
75-
return {
76-
type: 'ReactNativeBuiltInType',
77-
knownTypeName: 'ReactNativeCoreViewProps',
78-
};
79-
default: {
80-
throw new Error(`Unable to handle prop spread: ${name}`);
81-
}
82-
}
83-
}
84-
8562
function getProps(
8663
typeDefinition: $ReadOnlyArray<PropAST>,
8764
types: TypeDeclarationMap,
65+
parser: Parser,
8866
): {
8967
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
9068
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
@@ -96,7 +74,7 @@ function getProps(
9674
for (const prop of typeDefinition) {
9775
// find extends
9876
if (prop.type === 'TSExpressionWithTypeArguments') {
99-
const extend = extendsForProp(prop, types);
77+
const extend = extendsForProp(prop, types, parser);
10078
if (extend) {
10179
extendsProps.push(extend);
10280
continue;

0 commit comments

Comments
 (0)