Skip to content

Commit e85302d

Browse files
committed
Add support for function paramets without names
Add support for flow functions with parameters that doesn't have a name. Eg. `value: string => void`. It gives the argument a name equal to an empty string: ''
1 parent c9b36f3 commit e85302d

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ type Props = {
224224
literalsAndUnion: 'string' | 'otherstring' | number,
225225
arr: Array<any>,
226226
func?: (value: string) => void,
227+
noParameterName?: string => void,
227228
obj?: { subvalue: ?boolean },
228229
};
229230

@@ -288,6 +289,20 @@ we are getting this output:
288289
},
289290
"required":false
290291
},
292+
"noParameterName":{
293+
"flowType":{
294+
"name":"signature",
295+
"type":"function",
296+
"raw":"string => void",
297+
"signature":{
298+
"arguments":[
299+
{ "name":"", "type":{ "name":"string" } }
300+
],
301+
"return":{ "name":"void" }
302+
}
303+
},
304+
"required":false
305+
},
291306
"obj":{
292307
"flowType":{
293308
"name":"signature",

src/utils/__tests__/getFlowType-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@ describe('getFlowType', () => {
139139
}, raw: '(p1: number, p2: ?string) => boolean'});
140140
});
141141

142+
143+
it('detects function signature types without parameter names', () => {
144+
var typePath = expression('x: (number, ?string) => boolean').get('typeAnnotation').get('typeAnnotation');
145+
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'function', signature: {
146+
arguments: [
147+
{ name: '', type: { name: 'number' }},
148+
{ name: '', type: { name: 'string', nullable: true }},
149+
],
150+
return: { name: 'boolean' },
151+
}, raw: '(number, ?string) => boolean'});
152+
});
153+
154+
it('detects function signature type with single parmeter without name', () => {
155+
var typePath = expression('x: string => boolean').get('typeAnnotation').get('typeAnnotation');
156+
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'function', signature: {
157+
arguments: [
158+
{ name: '', type: { name: 'string' }},
159+
],
160+
return: { name: 'boolean' },
161+
}, raw: 'string => boolean'});
162+
});
142163
it('detects callable signature type', () => {
143164
var typePath = expression('x: { (str: string): string, token: string }').get('typeAnnotation').get('typeAnnotation');
144165
expect(getFlowType(typePath)).toEqual({name: 'signature', type: 'object', signature: {

src/utils/getFlowType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function handleFunctionTypeAnnotation(path: NodePath) {
148148
if (!typeAnnotation) return null;
149149

150150
type.signature.arguments.push({
151-
name: getPropertyName(param.get('name')),
151+
name: param.node.name ? param.node.name.name : '',
152152
type: getFlowType(typeAnnotation),
153153
});
154154
});

0 commit comments

Comments
 (0)