Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export declare function keys<T extends object>(): Array<keyof T>;
import { Property } from './transformer';

export declare function keys<T extends object>(): Array<Property>;
10 changes: 9 additions & 1 deletion test/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { Foo } from './interface';
describe('Test transformer.', () => {
test('Should get keys of interface which contains simple key types.', () => {
interface Foo {
/**
* Note on parameter a
* Note line 2
* @description somethings
* @returns somethings2
*/
a: string;
b: number;
c: boolean;
Expand All @@ -17,7 +23,9 @@ describe('Test transformer.', () => {
"name": "a",
"modifiers": [],
"optional": false,
"type": "string"
"type": "string",
"title": "Note on parameter a,Note line 2",
"args": { description: "somethings", returns: "somethings2" }
},
{
"name": "b",
Expand Down
29 changes: 28 additions & 1 deletion transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ export default (program: ts.Program): ts.TransformerFactory<ts.SourceFile> => {
};
}

interface Property {
export interface Property {
name: string;
modifiers: string[];
optional: boolean;
type: string;
elementKeys?: string[];
elementType?: any;
title?: string;
args?: {
[key: string]: string;
};
}

const symbolMap = new Map<string, ts.Symbol>();
Expand Down Expand Up @@ -106,6 +110,29 @@ const getSymbolProperties = (symbol: ts.Symbol, outerLayerProperties: Property[]
return getSymbolProperties(member.symbol, [], symbolMap);
}));
}

if (symbol.valueDeclaration) {
const fullText = symbol.valueDeclaration.getFullText();
const title_reg = /(?<=\* )[^@](.*)+[^\n]/g;
const title = fullText.match(title_reg);
if (title) {
property.title = title.toString();
}

const args_reg = /(?<=\* @).*/g;
const args = fullText.match(args_reg);
if (args) {
property.args = args.reduce((p, c) => {
const key = c.match(/[^\s]+/);
const value = c.match(/(?<=\s).*/);
if (key) {
p[key[0]] = value ? value[0] : ''
}
return p;
}, {});
}
}

properties.push(property);

const propertiesOfSymbol = _getPropertiesOfSymbol(symbol, propertyPathElements, symbolMap);
Expand Down