Skip to content

Commit dad3800

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 dad3800

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
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/getNameOrValue.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default function getNameOrValue(path: NodePath, raw?: boolean): string {
2525
return node.name;
2626
case types.Literal.name:
2727
return raw ? node.raw : node.value;
28+
case types.FunctionTypeParam.name:
29+
return '';
2830
default:
2931
throw new TypeError('Argument must be an Identifier or a Literal');
3032
}

0 commit comments

Comments
 (0)