Skip to content

Commit e41085e

Browse files
committed
move extendsForProp into parsers-commons.js
1 parent cff4bc8 commit e41085e

File tree

3 files changed

+53
-49
lines changed

3 files changed

+53
-49
lines changed

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,7 @@
1212

1313
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
1414
import type {TypeDeclarationMap} from '../../utils';
15-
16-
function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) {
17-
if (!prop.argument) {
18-
console.log('null', prop);
19-
}
20-
const name = prop.argument.id.name;
21-
22-
if (types[name] != null) {
23-
// This type is locally defined in the file
24-
return null;
25-
}
26-
27-
switch (name) {
28-
case 'ViewProps':
29-
return {
30-
type: 'ReactNativeBuiltInType',
31-
knownTypeName: 'ReactNativeCoreViewProps',
32-
};
33-
default: {
34-
throw new Error(`Unable to handle prop spread: ${name}`);
35-
}
36-
}
37-
}
15+
const {extendsForProp} = require('../../parsers-commons');
3816

3917
function removeKnownExtends(
4018
typeDefinition: $ReadOnlyArray<PropsAST>,
@@ -43,7 +21,7 @@ function removeKnownExtends(
4321
return typeDefinition.filter(
4422
prop =>
4523
prop.type !== 'ObjectTypeSpreadProperty' ||
46-
extendsForProp(prop, types) === null,
24+
extendsForProp(prop, types, 'argument') === null,
4725
);
4826
}
4927

@@ -56,7 +34,7 @@ function getExtendsProps(
5634
): $ReadOnlyArray<ExtendsPropsShape> {
5735
return typeDefinition
5836
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
59-
.map(prop => extendsForProp(prop, types))
37+
.map(prop => extendsForProp(prop, types, 'argument'))
6038
.filter(Boolean);
6139
}
6240

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ export type CommandOptions = $ReadOnly<{
6969
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
7070
type OptionsAST = Object;
7171

72+
type Prop = {
73+
argument?: {
74+
id?: {
75+
name: string,
76+
},
77+
name?: string,
78+
},
79+
expression?: {
80+
id?: {
81+
name: string,
82+
},
83+
name?: string,
84+
},
85+
};
86+
87+
type ExtendedPropResult = {
88+
type: TypeDeclarationMap,
89+
knownTypeName: 'ReactNativeCoreViewProps',
90+
} | null;
91+
7292
function wrapModuleSchema(
7393
nativeModuleSchema: NativeModuleSchema,
7494
hasteModuleName: string,
@@ -817,6 +837,33 @@ function propertyNames(
817837
.filter(Boolean);
818838
}
819839

840+
function extendsForProp(
841+
prop: Prop,
842+
types: TypeDeclarationMap,
843+
propName: 'argument' | 'expression',
844+
): ExtendedPropResult {
845+
if (!prop[propName]) {
846+
console.log('null', prop);
847+
}
848+
const name = prop[propName].id ? prop[propName].id.name : prop[propName].name;
849+
850+
if (types[name] != null) {
851+
// This type is locally defined in the file
852+
return null;
853+
}
854+
855+
switch (name) {
856+
case 'ViewProps':
857+
return {
858+
type: 'ReactNativeBuiltInType',
859+
knownTypeName: 'ReactNativeCoreViewProps',
860+
};
861+
default: {
862+
throw new Error(`Unable to handle prop spread: ${name}`);
863+
}
864+
}
865+
}
866+
820867
module.exports = {
821868
wrapModuleSchema,
822869
unwrapNullable,
@@ -836,4 +883,5 @@ module.exports = {
836883
getCommandOptions,
837884
getOptions,
838885
getCommandTypeNameAndOptionsExpression,
886+
extendsForProp,
839887
};

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
1717

1818
const {flattenProperties} = require('./componentsUtils.js');
1919
const {parseTopLevelType} = require('../parseTopLevelType');
20+
const {extendsForProp} = require('../../parsers-commons');
2021

2122
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
2223
type PropAST = Object;
@@ -59,29 +60,6 @@ function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
5960
return !isStyle;
6061
}
6162

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-
8563
function getProps(
8664
typeDefinition: $ReadOnlyArray<PropAST>,
8765
types: TypeDeclarationMap,
@@ -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, 'expression');
10078
if (extend) {
10179
extendsProps.push(extend);
10280
continue;

0 commit comments

Comments
 (0)