Skip to content

Commit 0b4455a

Browse files
committed
address feedback
1 parent 95e6b4c commit 0b4455a

File tree

13 files changed

+68
-30
lines changed

13 files changed

+68
-30
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/extends.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212

1313
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
1414
import type {TypeDeclarationMap} from '../../utils';
15+
import type {Parser} from '../../parser';
1516
const {extendsForProp} = require('../../parsers-commons');
1617

1718
function removeKnownExtends(
1819
typeDefinition: $ReadOnlyArray<PropsAST>,
1920
types: TypeDeclarationMap,
21+
parser: Parser,
2022
): $ReadOnlyArray<PropsAST> {
2123
return typeDefinition.filter(
2224
prop =>
2325
prop.type !== 'ObjectTypeSpreadProperty' ||
24-
extendsForProp(prop, types, 'argument') === null,
26+
extendsForProp(prop, types, parser) === null,
2527
);
2628
}
2729

@@ -31,10 +33,11 @@ type PropsAST = Object;
3133
function getExtendsProps(
3234
typeDefinition: $ReadOnlyArray<PropsAST>,
3335
types: TypeDeclarationMap,
36+
parser: Parser,
3437
): $ReadOnlyArray<ExtendsPropsShape> {
3538
return typeDefinition
3639
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
37-
.map(prop => extendsForProp(prop, types, 'argument'))
40+
.map(prop => extendsForProp(prop, types, parser))
3841
.filter(Boolean);
3942
}
4043

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ function buildComponentSchema(
129129

130130
const commandProperties = getCommandProperties(ast, parser);
131131

132-
const extendsProps = getExtendsProps(propProperties, types);
132+
const extendsProps = getExtendsProps(propProperties, types, parser);
133133
const options = getOptions(optionsExpression);
134134

135-
const nonExtendsProps = removeKnownExtends(propProperties, types);
135+
const nonExtendsProps = removeKnownExtends(propProperties, types, parser);
136136
const props = getProps(nonExtendsProps, types);
137137
const events = getEvents(propProperties, types);
138138
const commands = getCommands(commandProperties, types);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ const {
1717
} = require('./componentsUtils.js');
1818

1919
import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
20-
import type {TypeDeclarationMap} from '../../utils';
21-
22-
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
23-
type PropAST = Object;
20+
import type {TypeDeclarationMap, PropAST} from '../../utils';
2421

2522
function buildPropSchema(
2623
property: PropAST,

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import type {
2424
import type {ParserType} from './errors';
2525
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
2626

27+
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
28+
type PropAST = Object;
29+
2730
/**
2831
* This is the main interface for Parsers of various languages.
2932
* It exposes all the methods that contain language-specific logic.
@@ -255,4 +258,18 @@ export interface Parser {
255258
* @returns: converted TypeAnnotation to Keywords
256259
*/
257260
convertKeywordToTypeAnnotation(keyword: string): string;
261+
262+
/**
263+
* Given a prop return its arguments.
264+
* @parameter prop
265+
* @returns: Arguments of the prop
266+
*/
267+
argumentForProp(prop: PropAST): $FlowFixMe;
268+
269+
/**
270+
* Given a prop return its name.
271+
* @parameter prop
272+
* @returns: name property
273+
*/
274+
nameForArgument(prop: PropAST): $FlowFixMe;
258275
}

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -828,15 +828,15 @@ function propertyNames(
828828
function extendsForProp(
829829
prop: PropAST,
830830
types: TypeDeclarationMap,
831-
propName: 'argument' | 'expression',
831+
parser: Parser,
832832
): ExtendedPropResult {
833-
if (!prop[propName]) {
833+
const argument = parser.argumentForProp(prop);
834+
835+
if (!argument) {
834836
console.log('null', prop);
835837
}
836838

837-
const name = prop[propName]?.id
838-
? prop[propName].id.name
839-
: prop[propName]?.name;
839+
const name = parser.nameForArgument(prop);
840840

841841
if (types[name] != null) {
842842
// This type is locally defined in the file

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);
140140
const commands = getCommands(commandProperties, types);
141141

0 commit comments

Comments
 (0)