Skip to content

Add support for function paramets without names #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2017
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type Props = {
literalsAndUnion: 'string' | 'otherstring' | number,
arr: Array<any>,
func?: (value: string) => void,
noParameterName?: string => void,
obj?: { subvalue: ?boolean },
};

Expand Down Expand Up @@ -288,6 +289,20 @@ we are getting this output:
},
"required":false
},
"noParameterName":{
"flowType":{
"name":"signature",
"type":"function",
"raw":"string => void",
"signature":{
"arguments":[
{ "name":"", "type":{ "name":"string" } }
],
"return":{ "name":"void" }
}
},
"required":false
},
"obj":{
"flowType":{
"name":"signature",
Expand Down
21 changes: 21 additions & 0 deletions src/utils/__tests__/getFlowType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ describe('getFlowType', () => {
}, raw: '(p1: number, p2: ?string) => boolean'});
});


it('detects function signature types without parameter names', () => {
var typePath = expression('x: (number, ?string) => boolean').get('typeAnnotation').get('typeAnnotation');
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'function', signature: {
arguments: [
{ name: '', type: { name: 'number' }},
{ name: '', type: { name: 'string', nullable: true }},
],
return: { name: 'boolean' },
}, raw: '(number, ?string) => boolean'});
});

it('detects function signature type with single parmeter without name', () => {
var typePath = expression('x: string => boolean').get('typeAnnotation').get('typeAnnotation');
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'function', signature: {
arguments: [
{ name: '', type: { name: 'string' }},
],
return: { name: 'boolean' },
}, raw: 'string => boolean'});
});
it('detects callable signature type', () => {
var typePath = expression('x: { (str: string): string, token: string }').get('typeAnnotation').get('typeAnnotation');
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'object', signature: {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function handleFunctionTypeAnnotation(path: NodePath) {
if (!typeAnnotation) return null;

type.signature.arguments.push({
name: getPropertyName(param.get('name')),
name: param.node.name ? param.node.name.name : '',
type: getFlowType(typeAnnotation),
});
});
Expand Down