@@ -30,41 +30,53 @@ export type DefaultFunctionDoc = NormalizedFunctionParameters & ParameterContain
3030 * an object.
3131 */
3232export function normalizeFunctionParameters ( doc : DefaultFunctionDoc ) {
33- if ( doc . parameters ) {
34- doc . parameters . forEach ( parameter => {
35- let [ parameterName , parameterType ] = parameter . split ( ':' ) ;
33+ if ( ! doc . parameters ?. length ) {
34+ return ;
35+ }
3636
37- // If the parameter is optional, the name here will contain a '?'. We store whether the
38- // parameter is optional and remove the '?' for comparison.
39- let isOptional = false ;
40- if ( parameterName . includes ( '?' ) ) {
41- isOptional = true ;
42- parameterName = parameterName . replace ( '?' , '' ) ;
43- }
37+ doc . parameters . forEach ( parameter => {
38+ const colonIndex = parameter . indexOf ( ':' ) ;
39+ let parameterName : string ;
40+ let parameterType : string ;
4441
45- doc . params = doc . params || [ ] ;
42+ if ( colonIndex === - 1 ) {
43+ parameterName = parameter ;
44+ parameterType = '' ;
45+ } else {
46+ parameterName = parameter . slice ( 0 , colonIndex ) ;
47+ parameterType = parameter . slice ( colonIndex + 1 ) . trim ( ) ;
48+ }
4649
47- if ( ! parameterType ) {
48- console . warn (
49- `Missing parameter type information ( ${ parameterName } ) in ` +
50- ` ${ doc . fileInfo . relativePath } : ${ doc . startingLine } ` ,
51- ) ;
52- return ;
53- }
50+ // If the parameter is optional, the name here will contain a '?'. We store whether the
51+ // parameter is optional and remove the '?' for comparison.
52+ let isOptional = false ;
53+ if ( parameterName . includes ( '?' ) ) {
54+ isOptional = true ;
55+ parameterName = parameterName . replace ( '?' , '' ) ;
56+ }
5457
55- const existingParameterInfo = doc . params . find ( p => p . name == parameterName ) ;
58+ doc . params = doc . params || [ ] ;
5659
57- if ( ! existingParameterInfo ) {
58- doc . params . push ( {
59- name : parameterName ,
60- type : parameterType . trim ( ) ,
61- isOptional : isOptional ,
62- description : '' ,
63- } ) ;
64- } else {
65- existingParameterInfo . type = parameterType . trim ( ) ;
66- existingParameterInfo . isOptional = isOptional ;
67- }
68- } ) ;
69- }
60+ if ( ! parameterType ) {
61+ console . warn (
62+ `Missing parameter type information (${ parameterName } ) in ` +
63+ `${ doc . fileInfo . relativePath } :${ doc . startingLine } ` ,
64+ ) ;
65+ return ;
66+ }
67+
68+ const existingParameterInfo = doc . params . find ( p => p . name == parameterName ) ;
69+
70+ if ( ! existingParameterInfo ) {
71+ doc . params . push ( {
72+ name : parameterName ,
73+ type : parameterType ,
74+ isOptional : isOptional ,
75+ description : '' ,
76+ } ) ;
77+ } else {
78+ existingParameterInfo . type = parameterType ;
79+ existingParameterInfo . isOptional = isOptional ;
80+ }
81+ } ) ;
7082}
0 commit comments