Skip to content

Commit

Permalink
feat(javascript): add new destructuring option, closes #109
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Oct 13, 2020
1 parent 7d7093a commit 72f2432
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ For more information on how to create custom doc standards you can read

Below is a list of language-specific configuration and their default values.

### JavaScript

JavaScript settings automatically apply for all the default filetypes that are
being aliased, see [`g:doge_filetype_aliases`](#gdoge_filetype_aliases).

```vim
let g:doge_javascript_settings = {
\ 'destructuring_props': 1,
\}
```

- `destructuring_props`: Whether or not to generate `@param` tags for the
destructured properties in a function expression.

### PHP

```vim
Expand All @@ -334,6 +348,10 @@ let g:doge_php_settings = {
\}
```

- `resolve_fqn`: Whether or not to resolve the FQN based on the `use` statements
in the current buffer. The FQN will be resolved for type hints in parameters
and the return type.

### Python

```vim
Expand All @@ -342,6 +360,8 @@ let g:doge_python_settings = {
\}
```

- `single_quotes`: Whether or not to use single quotes for the multi-line comments openers and closers

# FAQ

# Help
Expand Down
12 changes: 12 additions & 0 deletions autoload/doge/preprocessors/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ endfunction
" A callback function being called after the tokens have been extracted. This
" function will adjust the input if needed.
function! doge#preprocessors#javascript#tokens(tokens) abort
if has_key(a:tokens, 'parameters') && !empty(a:tokens['parameters'])
if get(g:doge_javascript_settings, 'destructuring_props') == v:false
let l:filtered_params = []
for l:param in a:tokens['parameters']
if has_key(l:param, 'property') == v:false
call add(l:filtered_params, l:param)
endif
endfor
let a:tokens['parameters'] = l:filtered_params
endif
endif

if has_key(a:tokens, 'returnType')
if a:tokens['returnType'] ==# 'void'
let a:tokens['returnType'] = ''
Expand Down
6 changes: 6 additions & 0 deletions ftplugin/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ set cpoptions&vim
let b:doge_parser = 'typescript'
let b:doge_insert = 'above'

if !exists('g:doge_javascript_settings')
let g:doge_javascript_settings = {
\ 'destructuring_props': 1,
\}
endif

let b:doge_supported_doc_standards = doge#buffer#get_supported_doc_standards(['jsdoc'])
let b:doge_doc_standard = doge#buffer#get_doc_standard('javascript')
let b:doge_patterns = doge#buffer#get_patterns()
Expand Down
51 changes: 51 additions & 0 deletions src/parsers/typescript.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export class TypeScriptParserService
default: null,
optional: cn.type === 'optional_parameter',
};
const subparams: Array<Record<string, any>> = [];

cn.children.forEach((pn: SyntaxNode) => {
if (pn.type === 'identifier') {
Expand All @@ -317,9 +318,59 @@ export class TypeScriptParserService
param.default = pn.text;
param.optional = true;
}

// Check for destructuring patterns.
if (pn.type === 'object_pattern') {
pn.children
.filter((spn: SyntaxNode) =>
[
'pair',
'shorthand_property_identifier',
'assignment_pattern',
].includes(spn.type),
)
.forEach((spn: SyntaxNode) => {
const subparam: Record<string, any> = {
property: true,
name: null,
type: null,
default: null,
optional: false,
};

if (spn.type === 'shorthand_property_identifier') {
subparam.name = spn.text;
}

if (spn.type === 'assignment_pattern') {
subparam.name = spn.children.shift()?.text;
subparam.optional = true;
}

if (spn.type === 'pair') {
subparam.name = spn.children.shift()?.text;
const paramTypeChild = spn.children.pop();
if (
paramTypeChild?.type === 'assignment_expression'
) {
subparam.type = paramTypeChild.children.shift()?.text;
subparam.default = paramTypeChild.children.pop()?.text;
subparam.optional = true;
} else {
subparam.type = paramTypeChild?.text;
}
}

subparams.push({
...subparam,
name: `!parentName.${subparam.name}`,
});
});
}
});

this.result.parameters.push(param);
this.result.parameters.push(...subparams);
});
}
break;
Expand Down
4 changes: 3 additions & 1 deletion test/filetypes/javascript/es6.vader
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Given javascript (fat-arrow functions with destructuring):
Do (trigger doge):
:2\<CR>
\<C-d>
:12\<CR>
:13\<CR>
\<C-d>

Expect javascript (generated comments with @static, @param and @return tags):
Expand All @@ -120,6 +120,7 @@ Expect javascript (generated comments with @static, @param and @return tags):
* [TODO:description]
*
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {number} [TODO:parentName].b - [TODO:description]
* @return {[TODO:type]} [TODO:description]
*/
myMethod = ({ b: number }) => {
Expand All @@ -131,6 +132,7 @@ Expect javascript (generated comments with @static, @param and @return tags):
*
* @static
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {number} [TODO:parentName].b - [TODO:description]
* @return {number} [TODO:description]
*/
static myMethod({ b: number }): number {
Expand Down
1 change: 1 addition & 0 deletions test/filetypes/javascript/es7.vader
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Expect javascript (generated comment with @async, @param and @return tags):
*
* @async
* @param {User} [TODO:name] - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].id - [TODO:description]
* @param {Request} req - [TODO:description]
* @return {Promise<User>} [TODO:description]
*/
Expand Down
39 changes: 39 additions & 0 deletions test/filetypes/javascript/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,42 @@ Expect javascript (generated comments with a description, @param, @throws and @r
throw err;
throw new CustomException();
}

# ==============================================================================
# Test the 'destructuring_props' option
# ==============================================================================
Given javascript (functions with destructuring_props):
function foo({ a, b: str, c = 3, d: int = 5 }) {}

function foo({ a, b: str, c = 3, d: int = 5 }) {}


Do (trigger doge):
:let g:doge_javascript_settings = {'destructuring_props': 1}\<CR>
:1\<CR>
\<C-d>
:let g:doge_javascript_settings = {'destructuring_props': 0}\<CR>
:13\<CR>
\<C-d>
:let g:doge_javascript_settings = {'destructuring_props': 1}\<CR>

Expect javascript (generated comments with a description, @param and @return tags):
/**
* [TODO:description]
*
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].a - [TODO:description]
* @param {str} [TODO:parentName].b - [TODO:description]
* @param {[TODO:type]} [[TODO:parentName].c] - [TODO:description]
* @param {int} [[TODO:parentName].d] - [TODO:description]
* @return {[TODO:type]} [TODO:description]
*/
function foo({ a, b: str, c = 3, d: int = 5 }) {}

/**
* [TODO:description]
*
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @return {[TODO:type]} [TODO:description]
*/
function foo({ a, b: str, c = 3, d: int = 5 }) {}
6 changes: 5 additions & 1 deletion test/filetypes/typescript/class-methods.vader
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Given typescript (functions with destructured parameters):
Do (trigger doge):
:4\<CR>
\<C-d>
:15\<CR>
:17\<CR>
\<C-d>

Expect typescript (generated comment with a description, @param and @return tags):
Expand All @@ -27,6 +27,8 @@ Expect typescript (generated comment with a description, @param and @return tags
* [TODO:description]
*
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].idUser - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].userModel - [TODO:description]
* @param {boolean} [p2] - [TODO:description]
* @param {[TODO:type]} [p3] - [TODO:description]
* @return {[TODO:type]} [TODO:description]
Expand All @@ -40,6 +42,8 @@ Expect typescript (generated comment with a description, @param and @return tags
*
* @async
* @param {[TODO:type]} [TODO:name] - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].idUser - [TODO:description]
* @param {[TODO:type]} [TODO:parentName].userModel - [TODO:description]
* @param {boolean} [p2] - [TODO:description]
* @param {[TODO:type]} [p3] - [TODO:description]
* @throws {[TODO:name]} - [TODO:description]
Expand Down

0 comments on commit 72f2432

Please sign in to comment.