Skip to content

Commit

Permalink
feat(javascript): Add pattern for functions inside objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Jul 23, 2019
1 parent e69ea80 commit 718cada
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
40 changes: 40 additions & 0 deletions ftplugin/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,46 @@ let b:doge_patterns = []
" <param-access> <param-name>: <param-type> = <param-default-value>
let s:parameters_match_pattern = '\m\%(\%(public\|private\|protected\)\?\s*\)\?\([[:alnum:]_$]\+\)\%(\s*:\s*\([[:alnum:][:space:]._|]\+\%(\[[[:alnum:][:space:]_[\],]*\]\)\?\)\)\?\%(\s*=\s*\([^,]\+\)\+\)\?'

" ==============================================================================
" Matches fat-arrow / functions inside objects.
" ==============================================================================
"
" Matches the following scenarios:
"
" myKey: function myRealFunction(p1, p2) {}
"
" myKey: async function myRealFunction(p1, p2) {}
"
" myKey: (p1, p2) => {}
"
" myKey: async (p1, p2) => {}
call add(b:doge_patterns, {
\ 'match': '\m^[[:punct:]]\?\([[:alnum:]_-]\+\)[[:punct:]]\?\s*:\s*\(async\)\?\s*\%(function\)\?\s*\%([[:alnum:]_]\+\)\?(\(.\{-}\))\%(\s*:\s*(\?\([[:alnum:][:space:]_[\].,|<>]\+\))\?\)\?\%(\s*=>\s*\)\?\s*[({]',
\ 'match_group_names': ['funcName', 'async', 'parameters', 'returnType'],
\ 'parameters': {
\ 'match': s:parameters_match_pattern,
\ 'match_group_names': ['name', 'type'],
\ 'format': {
\ 'jsdoc': '@param {{type|!type}} {name} !description',
\ },
\ },
\ 'comment': {
\ 'insert': 'above',
\ 'template': {
\ 'jsdoc': [
\ '/**',
\ ' * !description',
\ ' *',
\ '#(async| * @{async})',
\ ' * @function {funcName|}',
\ '#(parameters| * {parameters})',
\ '#(returnType| * @return {{returnType}} !description)',
\ ' */',
\ ],
\ },
\ },
\})

" ==============================================================================
" Matches class declarations.
" ==============================================================================
Expand Down
40 changes: 40 additions & 0 deletions ftplugin/typescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ let b:doge_patterns = []
" <param-access> <param-name>: <param-type> = <param-default-value>
let s:parameters_match_pattern = '\m\%(\%(public\|private\|protected\)\?\s*\)\?\([[:alnum:]_$]\+\)\%(\s*:\s*\([[:alnum:][:space:]._|]\+\%(\[[[:alnum:][:space:]_[\],]*\]\)\?\)\)\?\%(\s*=\s*\([^,]\+\)\+\)\?'

" ==============================================================================
" Matches fat-arrow / functions inside objects.
" ==============================================================================
"
" Matches the following scenarios:
"
" myKey: function myRealFunction(p1, p2) {}
"
" myKey: async function myRealFunction(p1, p2) {}
"
" myKey: (p1, p2) => {}
"
" myKey: async (p1, p2) => {}
call add(b:doge_patterns, {
\ 'match': '\m^\([[:alnum:]_]\+\)\s*:\s*\(async\)\?\%(function\)\?\s*\%([[:alnum:]_]\+\)\?(\(.\{-}\))\%(\s*:\s*(\?\([[:alnum:][:space:]_[\].,|<>]\+\))\?\)\?\%(\s*=>\s*\)\?\s*[({]',
\ 'match_group_names': ['funcName', 'async', 'parameters', 'returnType'],
\ 'parameters': {
\ 'match': s:parameters_match_pattern,
\ 'match_group_names': ['name', 'type'],
\ 'format': {
\ 'jsdoc': '@param {{type|!type}} {name} !description',
\ },
\ },
\ 'comment': {
\ 'insert': 'above',
\ 'template': {
\ 'jsdoc': [
\ '/**',
\ ' * !description',
\ ' *',
\ '#(async| * @{async})',
\ ' * @function {funcName|}',
\ '#(parameters| * {parameters})',
\ '#(returnType| * @return {{returnType}} !description)',
\ ' */',
\ ],
\ },
\ },
\})

" ==============================================================================
" Matches class declarations.
" ==============================================================================
Expand Down
2 changes: 1 addition & 1 deletion test/filetypes/javascript/es6.vader
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Given javascript (fat-arrow function without parameters):
Do (trigger doge):
\<C-d>

Expect javascript (generated comment with @description @function tags):
Expect javascript (generated comment with the 'TODO' text and @function tags):
/**
* [TODO:description]
*
Expand Down
89 changes: 89 additions & 0 deletions test/filetypes/javascript/functions-inside-objects.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# ==============================================================================
# Anonymous functions inside objects without parameters.
# ==============================================================================
Given javascript (anonymous function inside object without parameters):
var myObj = {
myFunc: function(/* inline comment */) {
//
},
myArrowFunc: (/* inline comment */) => {
//
},
};

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

Expect javascript (generated comment with nothing but the text 'TODO' and @function tag):
var myObj = {
/**
* [TODO:description]
*
* @function myFunc
*/
myFunc: function(/* inline comment */) {
//
},
/**
* [TODO:description]
*
* @function myArrowFunc
*/
myArrowFunc: (/* inline comment */) => {
//
},
};


# ==============================================================================
# Async named functions with parameters without type hints.
# ==============================================================================

Given javascript (functions with punctation with parameters without type hints):
var myObj = {
'my-func': async function test($p1: string = 'value', p2: array = [], p3, p4 /* inline comment */) {
//
},
'my-arrow-func': async ($p1: string = 'value', p2: array = [], p3, p4 /* inline comment */) => {
//
},
};

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

Expect javascript (generated comment with @async, @function and @param tags):
var myObj = {
/**
* [TODO:description]
*
* @async
* @function my-func
* @param {string} $p1 [TODO:description]
* @param {array} p2 [TODO:description]
* @param {[TODO:type]} p3 [TODO:description]
* @param {[TODO:type]} p4 [TODO:description]
*/
'my-func': async function test($p1: string = 'value', p2: array = [], p3, p4 /* inline comment */) {
//
},
/**
* [TODO:description]
*
* @async
* @function my-arrow-func
* @param {string} $p1 [TODO:description]
* @param {array} p2 [TODO:description]
* @param {[TODO:type]} p3 [TODO:description]
* @param {[TODO:type]} p4 [TODO:description]
*/
'my-arrow-func': async ($p1: string = 'value', p2: array = [], p3, p4 /* inline comment */) => {
//
},
};
4 changes: 2 additions & 2 deletions test/filetypes/javascript/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Given javascript (function without parameters):
Do (trigger doge):
\<C-d>

Expect javascript (generated comment with @description tag):
Expect javascript (generated comment with nothing but the text 'TODO'):
/**
* [TODO:description]
*
Expand Down Expand Up @@ -227,7 +227,7 @@ Expect javascript (generated comment with @description and @param tags):
function* myFunc($p1 = 'value', p2 = [], p3, p4) {}

# ==============================================================================
# Async generator functions with parameters without typehints.
# Async generator functions with parameters without type-hints.
# ==============================================================================
Given javascript (async generator function with parameters without type hints):
async function* myFunc($p1 = 'value', p2 = [], p3, p4) {}
Expand Down

0 comments on commit 718cada

Please sign in to comment.