Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix auto-document ext #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions exercize-auto-document/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

### 使用:

```
npm i
node index.js
```
### 效果展示:

![效果展示](./showMd.png)
Binary file added exercize-auto-document/showMd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions exercize-auto-document/src/docs/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##sayHi
say 你好
name: 名字
>sayHi(name: string, age: number, a: boolean):string
#### Parameters:
- name(string)
- age(number)
- a(boolean)

##sayHi2
say 你好,没有返回类型
name: 名字
>sayHi2(name: string, age: number, a: boolean):void
#### Parameters:
- name(string)
- age(number)
- a(boolean)

##Guang
类测试
> new Guang()
#### Properties:
- name:string
#### Methods:
- sayHi


39 changes: 24 additions & 15 deletions exercize-auto-document/src/plugin/auto-document-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,38 @@ function generate(docs, format = 'json') {
}
} else if (format === 'html') {
return {
ext: 'html',
ext: '.html',
content: renderer.html(docs)
}
} else {
return {
ext: 'json',
ext: '.json',
content: renderer.json(docs)
}
}
}
}

function resolveType(tsType) {
const typeAnnotation = tsType.typeAnnotation;
if (!typeAnnotation) {
if (tsType.type) {
return transformType(tsType.type)
}
return;
}
switch (typeAnnotation.type) {
case 'TSStringKeyword':
return transformType(typeAnnotation.type)
}

function transformType(type) {
switch (type) {
case 'TSStringKeyword':
return 'string';
case 'TSNumberKeyword':
return 'number';
case 'TSBooleanKeyword':
return 'boolean';
default:
return;
}
}

Expand All @@ -60,18 +69,18 @@ const autoDocumentPlugin = declare((api, options, dirname) => {
docs.push({
type: 'function',
name: path.get('id').toString(),
params: path.get('params').map(paramPath=> {
params: path.get('params').map(paramPath => {
return {
name: paramPath.toString(),
type: resolveType(paramPath.getTypeAnnotation())
}
}),
return: resolveType(path.get('returnType').getTypeAnnotation()),
doc: path.node.leadingComments && parseComment(path.node.leadingComments[0].value)
return: resolveType(path.get('returnType').getTypeAnnotation()) || 'void',
doc: path.node.leadingComments && parseComment(path.node.leadingComments.at(-1).value)
});
state.file.set('docs', docs);
},
ClassDeclaration (path, state) {
ClassDeclaration(path, state) {
const docs = state.file.get('docs');
const classInfo = {
type: 'class',
Expand All @@ -81,7 +90,7 @@ const autoDocumentPlugin = declare((api, options, dirname) => {
propertiesInfo: []
};
if (path.node.leadingComments) {
classInfo.doc = parseComment(path.node.leadingComments[0].value);
classInfo.doc = parseComment(path.node.leadingComments.at(-1).value);
}
path.traverse({
ClassProperty(path) {
Expand All @@ -96,25 +105,25 @@ const autoDocumentPlugin = declare((api, options, dirname) => {
ClassMethod(path) {
if (path.node.kind === 'constructor') {
classInfo.constructorInfo = {
params: path.get('params').map(paramPath=> {
params: path.get('params').map(paramPath => {
return {
name: paramPath.toString(),
type: resolveType(paramPath.getTypeAnnotation()),
doc: parseComment(path.node.leadingComments[0].value)
doc: parseComment(path.node.leadingComments.at(-1).value)
}
})
}
} else {
classInfo.methodsInfo.push({
name: path.get('key').toString(),
doc: parseComment(path.node.leadingComments[0].value),
params: path.get('params').map(paramPath=> {
doc: parseComment(path.node.leadingComments.at(-1).value),
params: path.get('params').map(paramPath => {
return {
name: paramPath.toString(),
type: resolveType(paramPath.getTypeAnnotation())
}
}),
return: resolveType(path.getTypeAnnotation())
return: resolveType(path.get('returnType').getTypeAnnotation()) || 'void',
})
}
}
Expand Down
68 changes: 42 additions & 26 deletions exercize-auto-document/src/plugin/renderer/markdown.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
module.exports = function(docs) {
module.exports = function (docs) {
let str = '';

docs.forEach(doc => {
if (doc.type === 'function') {
str = transformFunc(str, doc)
} else if (doc.type === 'class') {
str += '##' + doc.name + '\n';
str += doc.doc.description + '\n';
if (doc.doc.tags) {
doc.doc.tags.forEach(tag => {
str += tag.name + ': ' + tag.description + '\n';
})
}
str += '>' + doc.name + '(';
if (doc.params) {
str += doc.params.map(param => {
return param.name + ': ' + param.type;
}).join(', ');
}
str += ')\n';
str += '#### Parameters:\n';
if (doc.params) {
str += doc.params.map(param => {
return '-' + param.name + '(' + param.type + ')';
}).join('\n');
}
str += '\n'
} else if (doc.type === 'class'){
str += '##' + doc.name + '\n';
str += doc.doc.description + '\n';
if (doc.doc.tags) {
doc.doc.tags.forEach(tag => {
str += tag.name + ': ' + tag.description + '\n';
str += tag.name + ': ' + tag.description + '\n';
})
}
str += '> new ' + doc.name + '(';
Expand All @@ -42,13 +22,13 @@ module.exports = function(docs) {
str += '#### Properties:\n';
if (doc.propertiesInfo) {
doc.propertiesInfo.forEach(param => {
str += '-' + param.name + ':' + param.type + '\n';
str += '- ' + param.name + ':' + param.type + '\n';
});
}
str += '#### Methods:\n';
if (doc.methodsInfo) {
doc.methodsInfo.forEach(param => {
str += '-' + param.name + '\n';
str = transformFunc(str, param, 5)
});
}
str += '\n'
Expand All @@ -57,3 +37,39 @@ module.exports = function(docs) {
})
return str;
}
function stringRepeate(params, len) {
if (len <= 0) {
return '';
}
let str = '';
for (let i = 0; i < len; i++) {
str += params;
}
return str;
}
function transformFunc(str, doc, titileDeep = 2) {
str += stringRepeate('#', titileDeep) + ' ' + doc.name + '\n';
str += doc.doc.description + '\n';
if (doc.doc.tags) {
doc.doc.tags.forEach(tag => {
str += (tag.name || tag.title) + ': ' + tag.description.replace(':', '') + '\n';
})
}
str += '>' + doc.name + '(';
if (doc.params) {
str += doc.params.map(param => {
return param.name + ': ' + param.type;
}).join(', ');
}
str += ')' + ':' + doc.return + '\n';
str += stringRepeate('#', (titileDeep + 1) < 4 ? 4 : (titileDeep + 1)) + ' Parameters:\n';
if (doc.params && doc.params.length) {
str += doc.params.map(param => {
return '- ' + param.name + '(' + param.type + ')';
}).join('\n');
} else {
str += 'null\n';
}
str += '\n'
return str
}
15 changes: 15 additions & 0 deletions exercize-auto-document/src/sourceCode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* say 你好
* @param name 名字
Expand All @@ -6,6 +7,13 @@ function sayHi (name: string, age: number, a: boolean):string {
console.log(`hi, ${name}`);
return `hi, ${name}`;
}
/**
* say 你好,没有返回类型
* @param name 名字
*/
function sayHi2 ():void {
console.log(`hi, ${name}`);
}

/**
* 类测试
Expand All @@ -22,4 +30,11 @@ class Guang {
sayHi (): string {
return `hi, I'm ${this.name}`;
}

/**
* 方法测试2
*/
sayHi2 (str:string): string {
return `hi, I'm ${this.name}. ${str}`;
}
}