Skip to content

Commit 9237aaa

Browse files
committed
Fix flow's type does not work right
1 parent e95bdfb commit 9237aaa

File tree

3 files changed

+95
-42
lines changed

3 files changed

+95
-42
lines changed

autoload/jsdoc.vim

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let s:regexs = {
111111
\ 'arrow_single': '^.\{-}\s*\([a-zA-Z_$][a-zA-Z0-9_$]*\)\s*[:=]\s*\([^=]*\).*$',
112112
\ 'return_type': ')\(:\|:\s\|\s*:\s*\)\([a-zA-Z]\+\).*$',
113113
\ 'interface': '^.\{-}\s*interface\s*\([a-zA-Z_$][a-zA-Z0-9_$]*\).*$',
114+
\ 'flow_type': '^.\{-}\s*type\s*\([a-zA-Z_$][a-zA-Z0-9_$]*\).*$',
114115
\ 'access': '^\s*\(public\|protected\|private\)',
115116
\ 'implements': '^.\{-}\s*implements\s*\(\([^{]*\)\).*$',
116117
\ 'extends': '^.\{-}\s*extends\s*\([^\s*]\)',
@@ -181,7 +182,12 @@ endfunction
181182
function! s:hookArgs(lines, space, arg, hook, argType, argDescription) abort
182183
" Hook function signature's args for insert as default value.
183184
if g:jsdoc_custom_args_hook == {}
184-
call add(a:lines, a:space . ' * @' . g:jsdoc_tags['param'] . ' ' . a:arg)
185+
if a:argType == ''
186+
let param = printf(' * @%s %s', g:jsdoc_tags['param'], a:arg)
187+
else
188+
let param = printf(' * @%s {%s} %s', g:jsdoc_tags['param'], a:argType, a:arg)
189+
endif
190+
call add(a:lines, a:space . param)
185191
else
186192

187193
let l:matchedArg = ''
@@ -245,44 +251,49 @@ function! s:determine_style(line)
245251
let l:is_named = 0
246252
let l:is_static = 0
247253
let l:is_interface = 0
254+
let l:is_type = 0
248255
let l:regex = 0
249256

250257
if a:line =~ s:regexs['function_declaration']
251-
let l:is_function = 1
252-
let l:is_named = 1
253-
let l:regex = s:regexs['function_declaration']
258+
let l:is_function = 1
259+
let l:is_named = 1
260+
let l:regex = s:regexs['function_declaration']
254261
elseif a:line =~ s:regexs['function_expression']
255-
let l:is_function = 1
256-
let l:is_named = 1
257-
let l:regex = s:regexs['function_expression']
262+
let l:is_function = 1
263+
let l:is_named = 1
264+
let l:regex = s:regexs['function_expression']
258265
elseif a:line =~ s:regexs['anonymous_function']
259-
let l:is_function = 1
260-
let l:regex = s:regexs['anonymous_function']
266+
let l:is_function = 1
267+
let l:regex = s:regexs['anonymous_function']
261268
elseif g:jsdoc_enable_es6 == 1 && a:line =~ s:regexs['static']
262-
let l:is_function = 1
263-
let l:is_named = 1
264-
let l:is_static = 1
265-
let l:regex = s:regexs['static']
269+
let l:is_function = 1
270+
let l:is_named = 1
271+
let l:is_static = 1
272+
let l:regex = s:regexs['static']
266273
elseif (g:jsdoc_allow_shorthand == 1 || g:jsdoc_enable_es6 == 1) && a:line =~ s:regexs['shorthand']
267-
let l:is_function = 1
268-
let l:is_named = 1
269-
let l:regex = s:regexs['shorthand']
274+
let l:is_function = 1
275+
let l:is_named = 1
276+
let l:regex = s:regexs['shorthand']
270277
elseif g:jsdoc_enable_es6 == 1 && a:line =~ s:regexs['arrow']
271-
let l:is_function = 1
272-
let l:is_named = 1
273-
let l:regex = s:regexs['arrow']
278+
let l:is_function = 1
279+
let l:is_named = 1
280+
let l:regex = s:regexs['arrow']
281+
elseif a:line =~ s:regexs['flow_type']
282+
let l:is_type = 1
283+
let l:is_named = 1
284+
let l:regex = s:regexs['flow_type']
274285
elseif g:jsdoc_enable_es6 == 1 && a:line =~ s:regexs['arrow_single']
275-
let l:is_function = 1
276-
let l:is_named = 1
277-
let l:regex = s:regexs['arrow_single']
286+
let l:is_function = 1
287+
let l:is_named = 1
288+
let l:regex = s:regexs['arrow_single']
278289
elseif g:jsdoc_enable_es6 == 1 && a:line =~ s:regexs['class_extend']
279-
let l:is_class = 1
280-
let l:is_named = 1
281-
let l:regex = s:regexs['class_extend']
290+
let l:is_class = 1
291+
let l:is_named = 1
292+
let l:regex = s:regexs['class_extend']
282293
elseif g:jsdoc_enable_es6 == 1 && a:line =~ s:regexs['class']
283-
let l:is_class = 1
284-
let l:is_named = 1
285-
let l:regex = s:regexs['class']
294+
let l:is_class = 1
295+
let l:is_named = 1
296+
let l:regex = s:regexs['class']
286297
elseif a:line =~ s:regexs['interface']
287298
let l:is_interface = 1
288299
let l:is_named = 1
@@ -295,6 +306,7 @@ function! s:determine_style(line)
295306
\ 'is_named': l:is_named,
296307
\ 'is_static': l:is_static,
297308
\ 'is_interface': l:is_interface,
309+
\ 'is_type': l:is_type,
298310
\ 'regex': l:regex,
299311
\ }
300312
endfunction
@@ -360,30 +372,24 @@ function! jsdoc#insert() abort
360372
let l:is_named = l:style['is_named']
361373
let l:is_static = l:style['is_static']
362374
let l:is_interface = l:style['is_interface']
375+
let l:is_type = l:style['is_type']
363376
let l:regex = l:style['regex']
364377

365378
let l:lines = []
366379
let l:desc = g:jsdoc_input_description == 1 ? input('Description: ') : ''
367380
call add(l:lines, l:space . '/**')
368381
call add(l:lines, l:space . ' * ' . l:desc)
369382
" Class and interface generate only typed name.
370-
if !l:is_class && !l:is_interface
383+
if !l:is_class && !l:is_interface && !l:is_type
371384
call add(l:lines, l:space . ' *')
372385
endif
373386

374387
let l:funcName = ''
375388
let l:parent_class = ''
376389
let l:implements = ''
377390
let l:return_type = ''
378-
if l:is_function || l:is_class || l:is_interface
391+
if l:is_function || l:is_class || l:is_interface || l:is_type
379392
" Parse function definition
380-
" @FIXME: Does not work if function is split over several lines...
381-
" e.g.
382-
" ```
383-
" function name(
384-
" arg1
385-
" arg2
386-
" ) { }
387393
let l:argString = ''
388394
if l:is_named
389395
let l:funcName = substitute(l:line, l:regex, '\1', 'g')
@@ -482,6 +488,7 @@ function! jsdoc#insert() abort
482488

483489
call add(l:lines, l:space . ' * @extends ' . '{' . l:parent_class . '}')
484490
endif
491+
elseif l:is_type
485492
elseif g:jsdoc_return == 1
486493
if g:jsdoc_allow_input_prompt == 1
487494
let s:candidate_type = l:return_type

tests/minimal_vimrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
" Minimal vimrc to run tests
22
" --------------------------
3-
43
set nocompatible
5-
set nu
64
filetype off
75
" Clear all rtp
86
set rtp=$VIMRUNTIME
97

10-
118
" Add vader.vim to rtp
129
set rtp+=./vader.vim
1310

tests/ts.vader

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Expect javascript:
1111
/**
1212
* foo
1313
*
14-
* @param foo='foo'
15-
* @param bar=1
14+
* @param {string} foo='foo'
15+
* @param {number} bar=1
1616
* @returns {string}
1717
*/
1818
function foo(foo: string = 'foo', bar: number = 1): string {
@@ -70,3 +70,52 @@ Expect javascript:
7070
class Bar extends Foo implements IFoo {
7171
}
7272

73+
Given typescript (scope arguments):
74+
class Bar extends Foo implements IFoo {
75+
constructor(private arg1: string, public arg2: string) {
76+
super()
77+
}
78+
}
79+
80+
Execute:
81+
call search('constructor')
82+
JsDoc
83+
84+
Expect javascript:
85+
class Bar extends Foo implements IFoo {
86+
/**
87+
* constructor
88+
*
89+
* @param {string} arg1
90+
* @param {string} arg2
91+
* @returns {undefined}
92+
*/
93+
constructor(private arg1: string, public arg2: string) {
94+
super()
95+
}
96+
}
97+
98+
Given typescript (private method):
99+
class Bar extends Foo implements IFoo {
100+
private foo(arg1: string, arg2: number = 0): void {
101+
}
102+
}
103+
104+
Execute:
105+
let jsdoc_access_descriptions=1
106+
call search('foo')
107+
JsDoc
108+
109+
Expect javascript:
110+
class Bar extends Foo implements IFoo {
111+
/**
112+
* foo
113+
*
114+
* @access private
115+
* @param {string} arg1
116+
* @param {number} arg2=0
117+
* @returns {void}
118+
*/
119+
private foo(arg1: string, arg2: number = 0): void {
120+
}
121+
}

0 commit comments

Comments
 (0)