Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions packages/react-native-codegen/src/parsers/__tests__/parsers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,61 @@ describe('FlowParser', () => {
expect(parser.callExpressionTypeParameters(node)).toBe(null);
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'a',
},
value: {
type: 'StringTypeAnnotation',
range: [],
},
},
{
type: 'ObjectTypeProperty',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
value: {
type: 'BooleanTypeAnnotation',
range: [],
},
},
];

const expected = [
{
name: 'a',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'BooleanTypeAnnotation'},
},
];

expect(
parser.computePartialProperties(
properties,
'hasteModuleName',
{},
{},
{},
() => null,
false,
),
).toEqual(expected);
});
});
});

describe('TypeScriptParser', () => {
Expand Down Expand Up @@ -202,4 +257,73 @@ describe('TypeScriptParser', () => {
expect(parser.callExpressionTypeParameters(node)).toBe(null);
});
});

describe('computePartialProperties', () => {
it('returns partial properties', () => {
const properties = [
{
type: 'TSPropertySignature',
key: {
type: 'Identifier',
name: 'a',
},
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: {
type: 'TSTypeLiteral',
key: {
type: 'Identifier',
name: 'a',
},
members: [],
},
},
},
{
type: 'TSPropertySignature',
key: {
type: 'Identifier',
name: 'b',
},
optional: true,
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: {
type: 'TSStringKeyword',
key: {
type: 'Identifier',
name: 'b',
},
members: [],
},
},
},
];

const expected = [
{
name: 'a',
optional: true,
typeAnnotation: {properties: [], type: 'ObjectTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
];

expect(
parser.computePartialProperties(
properties,
'hasteModuleName',
{},
{},
{},
() => null,
false,
),
).toEqual(expected);
});
});
});
25 changes: 9 additions & 16 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,15 @@ function translateTypeAnnotation(
parser,
);

const properties = annotatedElement.right.properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: translateTypeAnnotation(
hasteModuleName,
prop.value,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
),
};
});
const properties = parser.computePartialProperties(
annotatedElement.right.properties,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
);

return emitObject(nullable, properties);
}
Expand Down
33 changes: 32 additions & 1 deletion packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {TypeDeclarationMap} from '../utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';

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

// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
Expand Down Expand Up @@ -257,6 +261,33 @@ class FlowParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeArguments || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: flowTranslateTypeAnnotation(
hasteModuleName,
prop.value,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
this,
),
};
});
}
}

module.exports = {
Expand Down
25 changes: 24 additions & 1 deletion packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeDeclarationMap} from './utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

/**
* This is the main interface for Parsers of various languages.
Expand Down Expand Up @@ -181,4 +183,25 @@ export interface Parser {
* @returns: the typeParameters of the callExpression or null if it does not exist.
*/
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null;

/**
* Given an array of properties from a Partial type, it returns an array of remaped properties.
* @paramater properties: properties from a Partial types.
* @parameter hasteModuleName: a string with the native module name.
* @paramater types: a map of type declarations.
* @paramater aliasMap: a map of type aliases.
* @paramater enumMap: a map of type enums.
* @paramater tryParse: a parser error capturer.
* @paramater cxxOnly: a boolean specifying if the module is Cxx only.
Comment on lines +189 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"parameter"?.. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups sorry for the typo!

I see that you fixed the typo and merged the PR, thank you! :)

* @returns: an array of remaped properties
*/
computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe>;
}
27 changes: 26 additions & 1 deletion packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {TypeDeclarationMap} from './utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

// $FlowFixMe[untyped-import] there's no flowtype flow-parser
const flowParser = require('flow-parser');
Expand Down Expand Up @@ -184,4 +186,27 @@ export class MockedParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeArguments || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return [
{
name: 'a',
optional: true,
typeAnnotation: {type: 'StringTypeAnnotation'},
},
{
name: 'b',
optional: true,
typeAnnotation: {type: 'BooleanTypeAnnotation'},
},
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,14 @@ function translateTypeAnnotation(
parser,
);

const properties = annotatedElement.typeAnnotation.members.map(
member => {
return {
name: member.key.name,
optional: true,
typeAnnotation: translateTypeAnnotation(
hasteModuleName,
member.typeAnnotation.typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
),
};
},
const properties = parser.computePartialProperties(
annotatedElement.typeAnnotation.members,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
);

return emitObject(nullable, properties);
Expand Down
34 changes: 33 additions & 1 deletion packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import type {
NativeModuleParamTypeAnnotation,
NativeModuleEnumMembers,
NativeModuleEnumMemberType,
NativeModuleAliasMap,
NativeModuleEnumMap,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {TypeDeclarationMap} from '../utils';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../utils';

const {typeScriptTranslateTypeAnnotation} = require('./modules');

// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
const babelParser = require('@babel/parser');
Expand Down Expand Up @@ -243,7 +247,35 @@ class TypeScriptParser implements Parser {
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
return callExpression.typeParameters || null;
}

computePartialProperties(
properties: Array<$FlowFixMe>,
hasteModuleName: string,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): Array<$FlowFixMe> {
return properties.map(prop => {
return {
name: prop.key.name,
optional: true,
typeAnnotation: typeScriptTranslateTypeAnnotation(
hasteModuleName,
prop.typeAnnotation.typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
this,
),
};
});
}
}

module.exports = {
TypeScriptParser,
};