diff --git a/helper/src/typescript/docs/jsdoc.yaml b/helper/src/typescript/docs/jsdoc.yaml index 8db8bf36..7239510c 100644 --- a/helper/src/typescript/docs/jsdoc.yaml +++ b/helper/src/typescript/docs/jsdoc.yaml @@ -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 %} + */ diff --git a/helper/src/typescript/parser.rs b/helper/src/typescript/parser.rs index dc606521..0f0427f9 100644 --- a/helper/src/typescript/parser.rs +++ b/helper/src/typescript/parser.rs @@ -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, @@ -99,6 +101,22 @@ impl<'a> TypescriptParser<'a> { None } + fn parse_class_property(&self, node: &Node) -> Result, 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, String> { let mut tokens = Map::new(); diff --git a/test/filetypes/typescript/class-properties.vader b/test/filetypes/typescript/class-properties.vader new file mode 100644 index 00000000..aede2951 --- /dev/null +++ b/test/filetypes/typescript/class-properties.vader @@ -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\ + \ + :7\ + \ + :12\ + \ + :17\ + \ + :let g:doge_javascript_settings['omit_redundant_param_types'] = 1\ + :22\ + \ + :let g:doge_javascript_settings['omit_redundant_param_types'] = 0\ + +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"; + }