From 57712e9021a52ee5d8ca29e07cb9daed7a4e4873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20=E9=87=91=E5=8F=AF=E6=98=8E?= Date: Fri, 22 May 2020 23:34:46 +0800 Subject: [PATCH] feat(javascript): Add Promise as a return type when a function is async --- autoload/doge.vim | 2 +- autoload/doge/pattern.vim | 2 +- autoload/doge/preprocessors/javascript.vim | 8 +++++++- autoload/doge/preprocessors/php.vim | 2 +- autoload/doge/preprocessors/typescript.vim | 2 +- test/filetypes/javascript/functions-inside-objects.vader | 2 +- test/filetypes/javascript/functions.vader | 4 ++-- 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/autoload/doge.vim b/autoload/doge.vim index 4092c05e..06fb0344 100644 --- a/autoload/doge.vim +++ b/autoload/doge.vim @@ -33,7 +33,7 @@ function! doge#generate(arg) abort if a:arg <= len(b:doge_supported_doc_standards) let b:doge_doc_standard = b:doge_supported_doc_standards[a:arg - 1] endif - elseif type(a:arg) ==# type('') && a:arg !=# '' + elseif type(a:arg) ==# type('') && !empty(a:arg) if index(b:doge_supported_doc_standards, a:arg) >= 0 let b:doge_doc_standard = a:arg endif diff --git a/autoload/doge/pattern.vim b/autoload/doge/pattern.vim index 45aede21..6eac82b5 100644 --- a/autoload/doge/pattern.vim +++ b/autoload/doge/pattern.vim @@ -181,7 +181,7 @@ endfunction " Generates a template for a custom doc standard, and places it in " after/ftplugin/{&ft}.vim function! doge#pattern#custom(name) abort - if &filetype ==# '' + if empty(&filetype) echo '[DoGe] A filetype is required in order to create a custom pattern.' return 0 endif diff --git a/autoload/doge/preprocessors/javascript.vim b/autoload/doge/preprocessors/javascript.vim index 8212aa0d..d675febb 100644 --- a/autoload/doge/preprocessors/javascript.vim +++ b/autoload/doge/preprocessors/javascript.vim @@ -7,8 +7,14 @@ function! doge#preprocessors#javascript#tokens(tokens) abort if has_key(a:tokens, 'returnType') if a:tokens['returnType'] ==# 'void' let a:tokens['returnType'] = '' - elseif a:tokens['returnType'] ==# '' + elseif empty(a:tokens['returnType']) let a:tokens['returnType'] = '!type' + + " When we're dealing with an async function the return type is Promise. + " Only wrap the return type in a Promise when the type is not specified. + if has_key(a:tokens, 'async') && !empty(a:tokens['async']) + let a:tokens['returnType'] = 'Promise<' . a:tokens['returnType'] . '>' + endif endif endif endfunction diff --git a/autoload/doge/preprocessors/php.vim b/autoload/doge/preprocessors/php.vim index 0a63e2a5..b6bf6c11 100644 --- a/autoload/doge/preprocessors/php.vim +++ b/autoload/doge/preprocessors/php.vim @@ -59,7 +59,7 @@ function! s:get_property_type_via_constructor(propertyName) abort " Search for the constructor function in that class. if match(l:class_content, '__construct(') != -1 - let l:constructor_func_match = filter(matchlist(l:class_content, '\m\(__construct(.\{-})\s*{.\{-}}\)'), "v:val !=# ''") + let l:constructor_func_match = filter(matchlist(l:class_content, '\m\(__construct(.\{-})\s*{.\{-}}\)'), "!empty(v:val)") let l:constructor_func_contents = l:constructor_func_match[1] if l:constructor_func_contents != v:false " Constructor exists, grab the type hint and if it exists then set it. diff --git a/autoload/doge/preprocessors/typescript.vim b/autoload/doge/preprocessors/typescript.vim index df8fad82..8b0deedc 100644 --- a/autoload/doge/preprocessors/typescript.vim +++ b/autoload/doge/preprocessors/typescript.vim @@ -5,7 +5,7 @@ function! doge#preprocessors#typescript#tokens(tokens) abort if has_key(a:tokens, 'returnType') if a:tokens['returnType'] ==# 'void' let a:tokens['returnType'] = '' - elseif a:tokens['returnType'] ==# '' + elseif empty(a:tokens['returnType']) let a:tokens['returnType'] = '!type' endif endif diff --git a/test/filetypes/javascript/functions-inside-objects.vader b/test/filetypes/javascript/functions-inside-objects.vader index 1fe709a8..d573d3a3 100644 --- a/test/filetypes/javascript/functions-inside-objects.vader +++ b/test/filetypes/javascript/functions-inside-objects.vader @@ -70,7 +70,7 @@ Expect javascript (generated comment with @async, @function, @param and @return * @param {array} [p2] - [TODO:description] * @param {[TODO:type]} p3 - [TODO:description] * @param {[TODO:type]} p4 - [TODO:description] - * @return {[TODO:type]} [TODO:description] + * @return {Promise<[TODO:type]>} [TODO:description] */ 'my-func': async function test($p1: string = 'value', p2: array = [], p3, p4 /* inline comment */) { // diff --git a/test/filetypes/javascript/functions.vader b/test/filetypes/javascript/functions.vader index e67f7c05..87bdc927 100644 --- a/test/filetypes/javascript/functions.vader +++ b/test/filetypes/javascript/functions.vader @@ -176,7 +176,7 @@ Expect javascript (generated comment with @async, @param and @return tags): * @param {[TODO:type]} [p2] - [TODO:description] * @param {[TODO:type]} p3 - [TODO:description] * @param {[TODO:type]} p4 - [TODO:description] - * @return {[TODO:type]} [TODO:description] + * @return {Promise<[TODO:type]>} [TODO:description] */ async function myFunc($p1 = 'value', p2 = [], p3, p4) {} @@ -221,7 +221,7 @@ Expect javascript (generated comment with @async, @param and @return tags): * @param {[TODO:type]} [p2] - [TODO:description] * @param {[TODO:type]} p3 - [TODO:description] * @param {[TODO:type]} p4 - [TODO:description] - * @return {[TODO:type]} [TODO:description] + * @return {Promise<[TODO:type]>} [TODO:description] */ async function* myFunc($p1 = 'value', p2 = [], p3, p4) {}