diff --git a/src/__tests__/__snapshots__/main-test.js.snap b/src/__tests__/__snapshots__/main-test.js.snap index ce4e05761c7..3652dd82b4f 100644 --- a/src/__tests__/__snapshots__/main-test.js.snap +++ b/src/__tests__/__snapshots__/main-test.js.snap @@ -1384,3 +1384,40 @@ Object { }, } `; + +exports[`main fixtures processes component "component_27.tsx" without errors 1`] = ` +Object { + "description": "This is a typescript class component", + "displayName": "TSComponent", + "methods": Array [ + Object { + "description": "This is a method", + "docblock": "This is a method", + "modifiers": Array [], + "name": "method", + "params": Array [ + Object { + "name": "a", + "type": Object { + "name": "string", + }, + }, + ], + "returns": Object { + "type": Object { + "name": "string", + }, + }, + }, + ], + "props": Object { + "foo": Object { + "description": "", + "required": true, + "tsType": Object { + "name": "string", + }, + }, + }, +} +`; diff --git a/src/__tests__/fixtures/component_27.tsx b/src/__tests__/fixtures/component_27.tsx new file mode 100644 index 00000000000..9cd7c4187ae --- /dev/null +++ b/src/__tests__/fixtures/component_27.tsx @@ -0,0 +1,29 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +import React, { Component } from 'react'; + +interface Props { + foo: string +} + +/** + * This is a typescript class component + */ +export default class TSComponent extends Component { + render() { + return

Hello world

; + } + + /** + * This is a method + */ + method(a: string): string { + return a; + } +} diff --git a/src/utils/getMethodDocumentation.js b/src/utils/getMethodDocumentation.js index 1b901ed7932..163a74b73a1 100644 --- a/src/utils/getMethodDocumentation.js +++ b/src/utils/getMethodDocumentation.js @@ -9,6 +9,7 @@ import { getDocblock } from './docblock'; import getFlowType from './getFlowType'; +import getTSType from './getTSType'; import getParameterName from './getParameterName'; import getPropertyName from './getPropertyName'; import getTypeAnnotation from './getTypeAnnotation'; @@ -45,11 +46,16 @@ function getMethodParamsDoc(methodPath) { functionExpression.get('params').each(paramPath => { let type = null; const typePath = getTypeAnnotation(paramPath); - if (typePath) { + if (typePath && types.Flow.check(typePath.node)) { type = getFlowType(typePath); if (types.GenericTypeAnnotation.check(typePath.node)) { type.alias = typePath.node.id.name; } + } else if (typePath) { + type = getTSType(typePath); + if (types.TSTypeReference.check(typePath.node)) { + type.alias = typePath.node.typeName.name; + } } const param = { @@ -70,8 +76,10 @@ function getMethodReturnDoc(methodPath) { if (functionExpression.node.returnType) { const returnType = getTypeAnnotation(functionExpression.get('returnType')); - if (returnType) { + if (returnType && types.Flow.check(returnType.node)) { return { type: getFlowType(returnType) }; + } else if (returnType) { + return { type: getTSType(returnType) }; } }