Skip to content

Commit

Permalink
fix(okidoc-md): #66 handle optional method for interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kupriyanenko committed Apr 22, 2019
1 parent b82775b commit 0048af6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ exports[`buildDocumentation should build documentation for functions 1`] = `
"
`;

exports[`buildDocumentation should handle optional method in interface, issue #66 1`] = `
"<!-- Generated automatically. Update this documentation by updating the source code. -->
# Documentation
"
`;

exports[`buildDocumentation should show readable error from \`documentation.js\` 1`] = `
[SyntaxError: "Documentation" documentation source - Unexpected token, expected , (2:19)
1 | /** */
Expand Down
16 changes: 16 additions & 0 deletions packages/okidoc-md/src/buildDocumentation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ describe('buildDocumentation', () => {
).toMatchSnapshot();
});

it('should handle optional method in interface, issue #66', async () => {
const sourceCode = `
interface IRootContainerAPI {
getWidth?(): number;
}
`;

expect(
await buildDocumentation({
source: sourceCode,
tag: 'UI',
title: 'Documentation',
}),
).toMatchSnapshot();
});

it('should show readable error from `documentation.js`', async () => {
// NOTE: https://github.com/niieani/typescript-vs-flowtype#bounded-polymorphism
const sourceCode = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ interface IView {
}"
`;

exports[`buildDocumentationSource for API class should cleanup typescript vs flow difference interface with optional method, issue #66 1`] = `
"/** */
interface IView {
show(): void;
}"
`;

exports[`buildDocumentationSource for API class with @doc tag on class declaration should extract class 1`] = `
"/** */
class MyComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ describe('buildDocumentationSource', () => {
).toMatchSnapshot();
});

it('interface with optional method, issue #66', () => {
const sourceCode = `
interface IView {
show?();
}
`;

expect(
buildDocumentationSource({
source: sourceCode,
tag: 'UI',
}),
).toMatchSnapshot();
});

it('class public method', () => {
const sourceCode = `
/**
Expand Down
7 changes: 5 additions & 2 deletions packages/okidoc-md/src/utils/nodeAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function cleanUpClassMethod(node, { identifierName, JSDocCommentValue } = {}) {
// https://github.com/niieani/typescript-vs-flowtype
// https://github.com/babel/babel/blob/v7.0.0-beta.44/packages/babel-plugin-transform-typescript/src/index.js#L89
node.accessibility = null;
node.params = node.params.map(
param => (param.type === 'TSParameterProperty' ? param.parameter : param),
node.params = node.params.map(param =>
param.type === 'TSParameterProperty' ? param.parameter : param,
);

node.decorators = [];
Expand Down Expand Up @@ -117,6 +117,9 @@ function cleanUpInterfaceDeclaration(
}

if (item.type === 'TSMethodSignature') {
// NOTE: fix issue #66 with ts vs flow optional
item.optional = false;

// convert code like `show();` to `show(): void;`
if (!item.typeAnnotation) {
item.typeAnnotation = t.tsTypeAnnotation(t.tsVoidKeyword());
Expand Down

0 comments on commit 0048af6

Please sign in to comment.