Skip to content

Commit

Permalink
Merge pull request #639 from kkoomen/feature/jsdoc-class-prop
Browse files Browse the repository at this point in the history
Add support for class prop documentation
  • Loading branch information
kkoomen authored Oct 27, 2023
2 parents 3c9bb77 + 17c0041 commit 6b7ce96
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
- run: pip install vim-vint

# https://github.com/Vimjas/vint/issues/329#issuecomment-1029628054
- run: pip install git+https://github.com/Vimjas/vint.git

- run: vint -s ./autoload ./plugin
3 changes: 2 additions & 1 deletion autoload/doge.vim
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ function! doge#generate(arg) abort
let l:Indent = function('doge#indent#add', [l:comment_indent])

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

" Write the comment.
call append(l:comment_lnum_insert_position, l:comment)
Expand Down
11 changes: 11 additions & 0 deletions helper/src/typescript/docs/jsdoc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ templates:
* @returns {% if show_types %}{{ "{" }}{{ return_type | default(value="[TODO:type]") }}{{ "}" }}{% endif %} [TODO:description]
{% endif %}
*/
class_property:
node_types:
- public_field_definition
template: |
/**
* [TODO:description]
{% if show_types %}
* @type {{ "{" }}{{ type | default(value="[TODO:type]") }}{{ "}" }}
{% endif %}
*/
18 changes: 18 additions & 0 deletions helper/src/typescript/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ impl<'a> TypescriptParser<'a> {
}
},

"public_field_definition" => Some(self.parse_class_property(&child_node)),

"class" | "class_declaration" => Some(self.parse_class(&child_node)),

_ => None,
Expand All @@ -99,6 +101,22 @@ impl<'a> TypescriptParser<'a> {
None
}

fn parse_class_property(&self, node: &Node) -> Result<Map<String, Value>, String> {
let mut tokens = Map::new();

for child_node in node.children(&mut node.walk()) {
match child_node.kind() {
"type_annotation" => {
let type_node = child_node.children(&mut child_node.walk()).last().unwrap();
tokens.insert("type".to_string(), Value::String(self.get_node_text(&type_node)));
},
_ => {},
}
}

Ok(tokens)
}

fn parse_class(&self, node: &Node) -> Result<Map<String, Value>, String> {
let mut tokens = Map::new();

Expand Down
86 changes: 86 additions & 0 deletions test/filetypes/typescript/class-properties.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# ==============================================================================
# Class with multiple properties
# ==============================================================================
Given typescript (class with multiple properties):
class TestClass {
foo
private bar;
baz: int
private bax: str = "test";
}

Do (run doge):
:2\<CR>
\<C-d>
:7\<CR>
\<C-d>
:12\<CR>
\<C-d>
:17\<CR>
\<C-d>

Expect typescript (each class prop has a docblock):
class TestClass {
/**
* [TODO:description]
* @type {[TODO:type]}
*/
foo
/**
* [TODO:description]
* @type {[TODO:type]}
*/
private bar;
/**
* [TODO:description]
* @type {int}
*/
baz: int
/**
* [TODO:description]
* @type {str}
*/
private bax: str = "test";
}

# ------------------------------------------------------------------------------

Given typescript (class with multiple properties with omit_redundant_param_types=1):
class TestClass {
foo
private bar;
baz: int
private bax: str = "test";
}

Do (run doge):
:let g:doge_javascript_settings['omit_redundant_param_types'] = 1\<CR>
:2\<CR>
\<C-d>
:6\<CR>
\<C-d>
:10\<CR>
\<C-d>
:14\<CR>
\<C-d>
:let g:doge_javascript_settings['omit_redundant_param_types'] = 0\<CR>

Expect typescript (each class prop has a docblock):
class TestClass {
/**
* [TODO:description]
*/
foo
/**
* [TODO:description]
*/
private bar;
/**
* [TODO:description]
*/
baz: int
/**
* [TODO:description]
*/
private bax: str = "test";
}

0 comments on commit 6b7ce96

Please sign in to comment.