Skip to content

Commit

Permalink
feat(js/ts): add support for class prop documentation (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Oct 27, 2023
1 parent 3c9bb77 commit 66896e4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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
53 changes: 53 additions & 0 deletions test/filetypes/typescript/class-properties.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ==============================================================================
# Functions with destructured parameters.
# ==============================================================================
Given typescript (class with multiple properties):
class TestClass {
foo
private bar;
baz: int
private bax: str = "test";
private bax2: str = "test";
}

Do (run doge):
:2\<CR>
\<C-d>
:7\<CR>
\<C-d>
:12\<CR>
\<C-d>
:17\<CR>
\<C-d>
:let g:doge_javascript_settings['omit_redundant_param_types'] = 1\<CR>
:22\<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]
* @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";
/**
* [TODO:description]
*/
private bax2: str = "test";
}

0 comments on commit 66896e4

Please sign in to comment.