Skip to content

Commit

Permalink
Support method parameters in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Apr 28, 2019
1 parent 4cb1f3b commit f2193db
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Expand Up @@ -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",
},
},
},
}
`;
29 changes: 29 additions & 0 deletions 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<Props> {
render() {
return <h1>Hello world</h1>;
}

/**
* This is a method
*/
method(a: string): string {
return a;
}
}
12 changes: 10 additions & 2 deletions src/utils/getMethodDocumentation.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand All @@ -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) };
}
}

Expand Down

0 comments on commit f2193db

Please sign in to comment.