Add support for primitive returning type

Summary:
In this I add support for primitive types as return
- String
- Number
- Boolean
- Any
- Void

Reviewed By: TheSavior

Differential Revision: D16108273

fbshipit-source-id: 2dbd1d5a852a8cf5baf378a73d860492fe2f87cb
This commit is contained in:
Michał Osadnik 2019-07-05 13:11:53 -07:00 коммит произвёл Facebook Github Bot
Родитель 3a5bdc17bb
Коммит e8c5495961
4 изменённых файлов: 74 добавлений и 3 удалений

Просмотреть файл

@ -137,6 +137,14 @@ export type FunctionTypeAnnotationParamTypeAnnotation = $ReadOnly<{|
| 'BooleanTypeAnnotation', | 'BooleanTypeAnnotation',
|}>; |}>;
export type FunctionTypeAnnotationReturn = $ReadOnly<{|
type:
| 'StringTypeAnnotation'
| 'NumberTypeAnnotation'
| 'BooleanTypeAnnotation'
| 'VoidTypeAnnotation',
|}>;
export type FunctionTypeAnnotationParam = $ReadOnly<{| export type FunctionTypeAnnotationParam = $ReadOnly<{|
name: string, name: string,
typeAnnotation: FunctionTypeAnnotationParamTypeAnnotation, typeAnnotation: FunctionTypeAnnotationParamTypeAnnotation,
@ -145,6 +153,7 @@ export type FunctionTypeAnnotationParam = $ReadOnly<{|
export type FunctionTypeAnnotation = $ReadOnly<{| export type FunctionTypeAnnotation = $ReadOnly<{|
type: 'FunctionTypeAnnotation', type: 'FunctionTypeAnnotation',
params: $ReadOnlyArray<FunctionTypeAnnotationParam>, params: $ReadOnlyArray<FunctionTypeAnnotationParam>,
returnTypeAnnotation: FunctionTypeAnnotationReturn,
|}>; |}>;
export type MethodTypeShape = $ReadOnly<{| export type MethodTypeShape = $ReadOnly<{|

Просмотреть файл

@ -10,7 +10,31 @@
'use strict'; 'use strict';
const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE = ` const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from '../RCTExport';
import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
getString: (arg: NotString) => string;
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;
const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN = `
/** /**
* Copyright (c) Facebook, Inc. and its affiliates. * Copyright (c) Facebook, Inc. and its affiliates.
* *
@ -253,7 +277,8 @@ export default codegenNativeComponent<ModuleProps>('Module');
module.exports = { module.exports = {
TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT, TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT,
NATIVE_MODULES_WITH_NOT_EXISTING_TYPE, NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM,
NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN,
NATIVE_MODULES_WITH_NOT_ONLY_METHODS, NATIVE_MODULES_WITH_NOT_ONLY_METHODS,
INCORRECT_NATIVE_MODULES, INCORRECT_NATIVE_MODULES,
COMMANDS_DEFINED_INLINE, COMMANDS_DEFINED_INLINE,

Просмотреть файл

@ -8,7 +8,9 @@ exports[`RN Codegen Flow Parser Fails with error message COMMANDS_DEFINED_WITHOU
exports[`RN Codegen Flow Parser Fails with error message INCORRECT_NATIVE_MODULES 1`] = `"Interface properties for \\"SpecWithTypo has been specified incorrectly.\\""`; exports[`RN Codegen Flow Parser Fails with error message INCORRECT_NATIVE_MODULES 1`] = `"Interface properties for \\"SpecWithTypo has been specified incorrectly.\\""`;
exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`; exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`;
exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found GenericTypeAnnotation"`;
exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_ONLY_METHODS 1`] = `"Only methods are supported as module properties. Found BooleanTypeAnnotation in sampleBool"`; exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_ONLY_METHODS 1`] = `"Only methods are supported as module properties. Found BooleanTypeAnnotation in sampleBool"`;
@ -3336,6 +3338,9 @@ Object {
}, },
}, },
], ],
"returnTypeAnnotation": Object {
"type": "VoidTypeAnnotation",
},
"type": "FunctionTypeAnnotation", "type": "FunctionTypeAnnotation",
}, },
}, },
@ -3350,6 +3355,9 @@ Object {
}, },
}, },
], ],
"returnTypeAnnotation": Object {
"type": "VoidTypeAnnotation",
},
"type": "FunctionTypeAnnotation", "type": "FunctionTypeAnnotation",
}, },
}, },
@ -3364,6 +3372,9 @@ Object {
}, },
}, },
], ],
"returnTypeAnnotation": Object {
"type": "VoidTypeAnnotation",
},
"type": "FunctionTypeAnnotation", "type": "FunctionTypeAnnotation",
}, },
}, },

Просмотреть файл

@ -14,6 +14,7 @@ import type {
MethodTypeShape, MethodTypeShape,
FunctionTypeAnnotationParam, FunctionTypeAnnotationParam,
FunctionTypeAnnotationParamTypeAnnotation, FunctionTypeAnnotationParamTypeAnnotation,
FunctionTypeAnnotationReturn,
} from '../../CodegenSchema.js'; } from '../../CodegenSchema.js';
function wrapPrimitiveIntoTypeAnnotation( function wrapPrimitiveIntoTypeAnnotation(
@ -53,6 +54,25 @@ function getTypeAnnotationForParam(
}; };
} }
function getReturnTypeAnnotation(
methodName: string,
type,
): FunctionTypeAnnotationReturn {
switch (type) {
case 'BooleanTypeAnnotation':
case 'NumberTypeAnnotation':
case 'StringTypeAnnotation':
case 'VoidTypeAnnotation':
return {
type,
};
default:
(type: empty);
throw new Error(
`Unsupported return type for method "${methodName}", Found ${type}`,
);
}
}
function buildMethodSchema(property: MethodAST): MethodTypeShape { function buildMethodSchema(property: MethodAST): MethodTypeShape {
const name: string = property.key.name; const name: string = property.key.name;
const value = property.value; const value = property.value;
@ -67,10 +87,16 @@ function buildMethodSchema(property: MethodAST): MethodTypeShape {
const params = value.params.map(param => const params = value.params.map(param =>
getTypeAnnotationForParam(name, param), getTypeAnnotationForParam(name, param),
); );
const returnTypeAnnotation = getReturnTypeAnnotation(
name,
value.returnType.type,
);
return { return {
name, name,
typeAnnotation: { typeAnnotation: {
type: 'FunctionTypeAnnotation', type: 'FunctionTypeAnnotation',
returnTypeAnnotation,
params, params,
}, },
}; };