From 913c7f49234fd9680125a13ffed26f70424997b3 Mon Sep 17 00:00:00 2001 From: dohard Date: Tue, 29 Dec 2020 14:44:52 +0800 Subject: [PATCH 1/2] Parsing field notes --- test/transformer.test.ts | 10 +++++++++- transformer.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) 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..b7466c2 100644 --- a/transformer.ts +++ b/transformer.ts @@ -20,6 +20,10 @@ interface Property { 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); From 47b654116f16e0e938f223a8ed88214b8f59772f Mon Sep 17 00:00:00 2001 From: dohard Date: Tue, 29 Dec 2020 14:55:29 +0800 Subject: [PATCH 2/2] fix declared function type --- index.ts | 4 +++- transformer.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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/transformer.ts b/transformer.ts index b7466c2..ffa6a68 100644 --- a/transformer.ts +++ b/transformer.ts @@ -13,7 +13,7 @@ export default (program: ts.Program): ts.TransformerFactory => { }; } -interface Property { +export interface Property { name: string; modifiers: string[]; optional: boolean;