1010import detectNewline from 'detect-newline' ;
1111import { EOL } from 'os' ;
1212
13+ type Pragmas = { [ key : string ] : string | string [ ] , __proto__ : null } ;
14+
1315const commentEndRe = / \* \/ $ / ;
1416const commentStartRe = / ^ \/ \* \* / ;
1517const docblockRe = / ^ \s * ( \/ \* \* ? ( .| \r ? \n ) * ?\* \/ ) / ;
@@ -31,15 +33,13 @@ export function strip(contents: string) {
3133 return match && match [ 0 ] ? contents . substring ( match [ 0 ] . length ) : contents ;
3234}
3335
34- export function parse (
35- docblock : string ,
36- ) : { [ key : string ] : string , __proto__ : null } {
36+ export function parse ( docblock : string ) : Pragmas {
3737 return parseWithComments ( docblock ) . pragmas ;
3838}
3939
4040export function parseWithComments (
4141 docblock : string ,
42- ) : { comments: string , pragmas : { [ key : string ] : string , __proto__ : null } } {
42+ ) : { comments: string , pragmas : Pragmas } {
4343 const line = detectNewline ( docblock ) || EOL ;
4444
4545 docblock = docblock
@@ -64,7 +64,15 @@ export function parseWithComments(
6464 let match ;
6565 while ( ( match = propertyRe . exec ( docblock ) ) ) {
6666 // strip linecomments from pragmas
67- result [ match [ 1 ] ] = match [ 2 ] . replace ( lineCommentRe , '' ) ;
67+ const nextPragma = match [ 2 ] . replace ( lineCommentRe , '' ) ;
68+ if (
69+ typeof result [ match [ 1 ] ] === 'string' ||
70+ Array . isArray ( result [ match [ 1 ] ] )
71+ ) {
72+ result [ match [ 1 ] ] = [ ] . concat ( result [ match [ 1 ] ] , nextPragma ) ;
73+ } else {
74+ result [ match [ 1 ] ] = nextPragma ;
75+ }
6876 }
6977 return { comments, pragmas : result } ;
7078}
@@ -74,7 +82,7 @@ export function print({
7482 pragmas = { } ,
7583} : {
7684 comments ?: string ,
77- pragmas ?: { [ key : string ] : string , __proto__ : null } ,
85+ pragmas ?: Pragmas ,
7886 __proto__ : null ,
7987} ) : string {
8088 const line = detectNewline ( comments ) || EOL ;
@@ -85,15 +93,18 @@ export function print({
8593 const keys = Object . keys ( pragmas ) ;
8694
8795 const printedObject = keys
88- . map ( key => start + ' ' + printKeyValue ( key , pragmas [ key ] ) + line )
96+ . map ( key => printKeyValues ( key , pragmas [ key ] ) )
97+ . reduce ( ( arr , next ) => arr . concat ( next ) , [ ] )
98+ . map ( keyValue => start + ' ' + keyValue + line )
8999 . join ( '' ) ;
90100
91101 if ( ! comments ) {
92102 if ( keys . length === 0 ) {
93103 return '' ;
94104 }
95- if ( keys . length === 1 ) {
96- return `${ head } ${ printKeyValue ( keys [ 0 ] , pragmas [ keys [ 0 ] ] ) } ${ tail } ` ;
105+ if ( keys . length === 1 && ! Array . isArray ( pragmas [ keys [ 0 ] ] ) ) {
106+ const value = pragmas [ keys [ 0 ] ] ;
107+ return `${ head } ${ printKeyValues ( keys [ 0 ] , value ) [ 0 ] } ${ tail } ` ;
97108 }
98109 }
99110
@@ -113,6 +124,6 @@ export function print({
113124 ) ;
114125}
115126
116- function printKeyValue ( key , value ) {
117- return `@${ key } ${ value } ` . trim ( ) ;
127+ function printKeyValues ( key , valueOrArray ) {
128+ return [ ] . concat ( valueOrArray ) . map ( value => `@${ key } ${ value } ` . trim ( ) ) ;
118129}
0 commit comments