Skip to content

Commit

Permalink
Merge pull request #652 from kkoomen/feature/js-member-expression-param
Browse files Browse the repository at this point in the history
Ignore member expressions as function param in JS/TS
  • Loading branch information
kkoomen authored Jan 26, 2024
2 parents aa449a3 + c875221 commit 32af2cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions helper/src/typescript/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ impl<'a> TypescriptParser<'a> {
fn parse_member_expression(&self, node: &Node) -> Result<Map<String, Value>, String> {
let mut tokens = Map::new();

// If a member expression is part of a function parameter, ignore it.
if let Some(parent_node) = node.parent() {
if ["required_parameter", "rest_parameter", "optional_parameter"].contains(&parent_node.kind()) {
return Ok(tokens);
}
}

for child_node in node.children(&mut node.walk()) {
match child_node.kind() {
"member_expression" => {
Expand Down Expand Up @@ -301,7 +308,7 @@ impl<'a> TypescriptParser<'a> {
tokens.insert("params".to_string(), Value::Array(params));
}
}
}
},
_ => {},
}
}
Expand Down Expand Up @@ -372,7 +379,7 @@ impl<'a> TypescriptParser<'a> {
subparam.insert("name".to_string(), Value::String(subparam_name));
subparam.insert("optional".to_string(), Value::Bool(true));
},
"pair_pattern" =>{
"pair_pattern" => {
let subparam_name = node
.children(&mut node.walk())
.next()
Expand Down
22 changes: 22 additions & 0 deletions test/filetypes/javascript/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ Expect javascript (generated comments with a description, @param and @returns ta
*/
function foo({ a, b: str, c = 3, d: int = 5 }) {}

# ==============================================================================
# Test function references as default values
# ==============================================================================
Given javascript (functions with destructuring_props):
function foo(bar = import.meta.url) {
return true;
}

Do (trigger doge):
\<C-d>

Expect javascript (generated comments with a description, @param and @returns tags):
/**
* [TODO:description]
*
* @param {[TODO:type]} [bar] - [TODO:description]
* @returns {[TODO:type]} [TODO:description]
*/
function foo(bar = import.meta.url) {
return true;
}

# ==============================================================================
# Test the 'omit_redundant_param_types' option
# ==============================================================================
Expand Down

0 comments on commit 32af2cb

Please sign in to comment.