diff --git a/src/utils/__tests__/getFlowType-test.js b/src/utils/__tests__/getFlowType-test.js index 3704cc3fd77..98b34f359ac 100644 --- a/src/utils/__tests__/getFlowType-test.js +++ b/src/utils/__tests__/getFlowType-test.js @@ -68,6 +68,11 @@ describe('getFlowType', () => { expect(getFlowType(typePath)).toEqual({ name: 'xyz', nullable: true }); }); + it('detects array type shorthand', () => { + var typePath = expression('x: number[]').get('typeAnnotation').get('typeAnnotation'); + expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number' }], raw: 'number[]' }); + }); + it('detects array type', () => { var typePath = expression('x: Array').get('typeAnnotation').get('typeAnnotation'); expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number' }], raw: 'Array' }); diff --git a/src/utils/getFlowType.js b/src/utils/getFlowType.js index 91fa773571d..8a62070805f 100644 --- a/src/utils/getFlowType.js +++ b/src/utils/getFlowType.js @@ -37,6 +37,7 @@ const flowLiteralTypes = { }; const namedTypes = { + ArrayTypeAnnotation: handleArrayTypeAnnotation, GenericTypeAnnotation: handleGenericTypeAnnotation, ObjectTypeAnnotation: handleObjectTypeAnnotation, UnionTypeAnnotation: handleUnionTypeAnnotation, @@ -76,6 +77,14 @@ function handleKeysHelper(path: NodePath) { } } +function handleArrayTypeAnnotation(path: NodePath) { + return { + name: 'Array', + elements: [getFlowType(path.get('elementType'))], + raw: printValue(path), + }; +} + function handleGenericTypeAnnotation(path: NodePath) { if (path.node.id.name === '$Keys' && path.node.typeParameters) { return handleKeysHelper(path);