Skip to content

Commit e27941e

Browse files
committed
fix(flow): Handle array shorthand notation
1 parent 719317c commit e27941e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/utils/__tests__/getFlowType-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ describe('getFlowType', () => {
6868
expect(getFlowType(typePath)).toEqual({ name: 'xyz', nullable: true });
6969
});
7070

71+
it('detects array type shorthand', () => {
72+
var typePath = expression('x: number[]').get('typeAnnotation').get('typeAnnotation');
73+
expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number' }], raw: 'number[]' });
74+
});
75+
7176
it('detects array type', () => {
7277
var typePath = expression('x: Array<number>').get('typeAnnotation').get('typeAnnotation');
7378
expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number' }], raw: 'Array<number>' });

src/utils/getFlowType.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const flowLiteralTypes = {
3737
};
3838

3939
const namedTypes = {
40+
ArrayTypeAnnotation: handleArrayTypeAnnotation,
4041
GenericTypeAnnotation: handleGenericTypeAnnotation,
4142
ObjectTypeAnnotation: handleObjectTypeAnnotation,
4243
UnionTypeAnnotation: handleUnionTypeAnnotation,
@@ -76,6 +77,14 @@ function handleKeysHelper(path: NodePath) {
7677
}
7778
}
7879

80+
function handleArrayTypeAnnotation(path: NodePath) {
81+
return {
82+
name: 'Array',
83+
elements: [getFlowType(path.get('elementType'))],
84+
raw: printValue(path),
85+
};
86+
}
87+
7988
function handleGenericTypeAnnotation(path: NodePath) {
8089
if (path.node.id.name === '$Keys' && path.node.typeParameters) {
8190
return handleKeysHelper(path);

0 commit comments

Comments
 (0)