Skip to content

Commit 6b7ce96

Browse files
authored
Merge pull request #639 from kkoomen/feature/jsdoc-class-prop
Add support for class prop documentation
2 parents 3c9bb77 + 17c0041 commit 6b7ce96

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,8 @@ jobs:
135135
steps:
136136
- uses: actions/checkout@v2
137137
- uses: actions/setup-python@v1
138-
- run: pip install vim-vint
138+
139+
# https://github.com/Vimjas/vint/issues/329#issuecomment-1029628054
140+
- run: pip install git+https://github.com/Vimjas/vint.git
141+
139142
- run: vint -s ./autoload ./plugin

autoload/doge.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function! doge#generate(arg) abort
131131
let l:Indent = function('doge#indent#add', [l:comment_indent])
132132

133133
" Indent the comment.
134-
let l:comment = map(l:comment, { k, line -> l:Indent(line) })
134+
" vint: next-line -ProhibitUnusedVariable
135+
let l:comment = map(l:comment, { index, line -> l:Indent(line) })
135136

136137
" Write the comment.
137138
call append(l:comment_lnum_insert_position, l:comment)

helper/src/typescript/docs/jsdoc.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,14 @@ templates:
9191
* @returns {% if show_types %}{{ "{" }}{{ return_type | default(value="[TODO:type]") }}{{ "}" }}{% endif %} [TODO:description]
9292
{% endif %}
9393
*/
94+
95+
class_property:
96+
node_types:
97+
- public_field_definition
98+
template: |
99+
/**
100+
* [TODO:description]
101+
{% if show_types %}
102+
* @type {{ "{" }}{{ type | default(value="[TODO:type]") }}{{ "}" }}
103+
{% endif %}
104+
*/

helper/src/typescript/parser.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ impl<'a> TypescriptParser<'a> {
8989
}
9090
},
9191

92+
"public_field_definition" => Some(self.parse_class_property(&child_node)),
93+
9294
"class" | "class_declaration" => Some(self.parse_class(&child_node)),
9395

9496
_ => None,
@@ -99,6 +101,22 @@ impl<'a> TypescriptParser<'a> {
99101
None
100102
}
101103

104+
fn parse_class_property(&self, node: &Node) -> Result<Map<String, Value>, String> {
105+
let mut tokens = Map::new();
106+
107+
for child_node in node.children(&mut node.walk()) {
108+
match child_node.kind() {
109+
"type_annotation" => {
110+
let type_node = child_node.children(&mut child_node.walk()).last().unwrap();
111+
tokens.insert("type".to_string(), Value::String(self.get_node_text(&type_node)));
112+
},
113+
_ => {},
114+
}
115+
}
116+
117+
Ok(tokens)
118+
}
119+
102120
fn parse_class(&self, node: &Node) -> Result<Map<String, Value>, String> {
103121
let mut tokens = Map::new();
104122

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# ==============================================================================
2+
# Class with multiple properties
3+
# ==============================================================================
4+
Given typescript (class with multiple properties):
5+
class TestClass {
6+
foo
7+
private bar;
8+
baz: int
9+
private bax: str = "test";
10+
}
11+
12+
Do (run doge):
13+
:2\<CR>
14+
\<C-d>
15+
:7\<CR>
16+
\<C-d>
17+
:12\<CR>
18+
\<C-d>
19+
:17\<CR>
20+
\<C-d>
21+
22+
Expect typescript (each class prop has a docblock):
23+
class TestClass {
24+
/**
25+
* [TODO:description]
26+
* @type {[TODO:type]}
27+
*/
28+
foo
29+
/**
30+
* [TODO:description]
31+
* @type {[TODO:type]}
32+
*/
33+
private bar;
34+
/**
35+
* [TODO:description]
36+
* @type {int}
37+
*/
38+
baz: int
39+
/**
40+
* [TODO:description]
41+
* @type {str}
42+
*/
43+
private bax: str = "test";
44+
}
45+
46+
# ------------------------------------------------------------------------------
47+
48+
Given typescript (class with multiple properties with omit_redundant_param_types=1):
49+
class TestClass {
50+
foo
51+
private bar;
52+
baz: int
53+
private bax: str = "test";
54+
}
55+
56+
Do (run doge):
57+
:let g:doge_javascript_settings['omit_redundant_param_types'] = 1\<CR>
58+
:2\<CR>
59+
\<C-d>
60+
:6\<CR>
61+
\<C-d>
62+
:10\<CR>
63+
\<C-d>
64+
:14\<CR>
65+
\<C-d>
66+
:let g:doge_javascript_settings['omit_redundant_param_types'] = 0\<CR>
67+
68+
Expect typescript (each class prop has a docblock):
69+
class TestClass {
70+
/**
71+
* [TODO:description]
72+
*/
73+
foo
74+
/**
75+
* [TODO:description]
76+
*/
77+
private bar;
78+
/**
79+
* [TODO:description]
80+
*/
81+
baz: int
82+
/**
83+
* [TODO:description]
84+
*/
85+
private bax: str = "test";
86+
}

0 commit comments

Comments
 (0)