diff --git a/index.ts b/index.ts index 78e1c52..aa8f27a 100644 --- a/index.ts +++ b/index.ts @@ -1 +1,3 @@ -export declare function keys(): Array; +import { Property } from './transformer'; + +export declare function keys(): Array; diff --git a/test/transformer.test.ts b/test/transformer.test.ts index 9653845..e75d9b0 100644 --- a/test/transformer.test.ts +++ b/test/transformer.test.ts @@ -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; @@ -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", diff --git a/transformer.ts b/transformer.ts index f9dd5b0..ffa6a68 100644 --- a/transformer.ts +++ b/transformer.ts @@ -13,13 +13,17 @@ export default (program: ts.Program): ts.TransformerFactory => { }; } -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(); @@ -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);