Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 1a40858

Browse files
committed
feat: Added jsdoc to d.ts files
1 parent 66e5792 commit 1a40858

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

Diff for: index.ts

+31-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface IProp {
2727
optional: boolean;
2828
importType?: string;
2929
importPath?: string;
30+
documentation?: string;
3031
}
3132

3233
export interface IPropTypes {
@@ -121,7 +122,9 @@ function parseAst(ast: any, instanceOfResolver: InstanceOfResolver): IParsingRes
121122
propTypes = {};
122123
walk(attributeNode.value, {
123124
'ObjectProperty': (propertyNode: any): void => {
124-
propTypes[propertyNode.key.name] = getTypeFromPropType(propertyNode.value, instanceOfResolver);
125+
const prop: IProp = getTypeFromPropType(propertyNode.value, instanceOfResolver);
126+
prop.documentation = getOptionalDocumentation(propertyNode);
127+
propTypes[propertyNode.key.name] = prop;
125128
}
126129
});
127130
}
@@ -135,6 +138,12 @@ function parseAst(ast: any, instanceOfResolver: InstanceOfResolver): IParsingRes
135138
};
136139
}
137140

141+
function getOptionalDocumentation(propertyNode: any): string {
142+
return (((propertyNode.leadingComments || []) as any[])
143+
.filter((comment: any) => comment.type == 'CommentBlock')[0] || {})
144+
.value;
145+
}
146+
138147
interface IAstWalkHandlers {
139148
[type: string]: (node: IASTNode) => void;
140149
}
@@ -293,26 +302,41 @@ export class Writer {
293302
}
294303
}
295304

296-
public props(name: string, props: any, fn?: () => void): void {
305+
public props(name: string, props: IPropTypes, fn?: () => void): void {
297306
this.interface(`${name}Props`, () => {
298307
this.prop('key', 'any', true);
299308
Object.keys(props).forEach((propName: any) => {
300309
const prop: IProp = props[propName];
301-
this.prop(propName, prop.type, prop.optional);
310+
this.prop(propName, prop.type, prop.optional, prop.documentation);
302311
});
303312
});
304313
if (fn) {
305314
fn();
306315
}
307316
}
308317

309-
public prop(name: string, type: string, optional: boolean, fn?: () => void): void {
318+
public prop(name: string, type: string, optional: boolean, documentation?: string): void {
310319
this.indent();
320+
if (documentation) {
321+
this.comment(documentation);
322+
}
311323
this.code += `${name}${optional ? '?' : ''}: ${type};`;
312324
this.nl();
313-
if (fn) {
314-
fn();
315-
}
325+
}
326+
327+
public comment(comment: string): void {
328+
this.code += '/*';
329+
const lines: string[] = (comment || '').replace(/\t/g, '').split(/\n/g);
330+
lines.forEach((line: string, index: number) => {
331+
this.code += line;
332+
if (index < lines.length - 1) {
333+
this.nl();
334+
this.indent();
335+
}
336+
});
337+
this.code += '*/';
338+
this.nl();
339+
this.indent();
316340
}
317341

318342
public interface(name: string, fn: () => void): void {

Diff for: tests/simple-component.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ declare module 'simple-component' {
44

55
interface SimpleComponentProps {
66
key?: any;
7+
/**
8+
* This is a jsdoc comment for optionalAny.
9+
*/
710
optionalAny?: any;
811
optionalArray?: any[];
912
optionalBool?: boolean;

Diff for: tests/simple-component.jsx

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as React from 'react';
33
export default class SimpleComponent extends React.Component {
44

55
static propTypes = {
6+
/**
7+
* This is a jsdoc comment for optionalAny.
8+
*/
69
optionalAny: React.PropTypes.any,
710
optionalArray: React.PropTypes.array,
811
optionalBool: React.PropTypes.bool,

Diff for: tests/writer-test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ describe('The Writer', () => {
3636
writer.class('Name', false);
3737
assert.equal(writer.toString(), 'class Name extends React.Component<any, any> {\n}\n');
3838
});
39+
it('should write an indented block comment', () => {
40+
writer.comment('* yada\n\t\t\t\tyada\n ');
41+
assert.equal(writer.toString(), '/** yada\nyada\n */\n');
42+
});
3943
});

0 commit comments

Comments
 (0)