| 37 | } |
| 38 | |
| 39 | export function parseWithComments(docblock: string): { |
| 40 | comments: string; |
| 41 | pragmas: Pragmas; |
| 42 | } { |
| 43 | const line = detectNewline(docblock) ?? EOL; |
| 44 | |
| 45 | docblock = docblock |
| 46 | .replace(commentStartRe, '') |
| 47 | .replace(commentEndRe, '') |
| 48 | .replaceAll(stringStartRe, '$1'); |
| 49 | |
| 50 | // Normalize multi-line directives |
| 51 | let prev = ''; |
| 52 | while (prev !== docblock) { |
| 53 | prev = docblock; |
| 54 | docblock = docblock.replaceAll(multilineRe, `${line}$1 $2${line}`); |
| 55 | } |
| 56 | docblock = docblock.replace(ltrimNewlineRe, '').trimEnd(); |
| 57 | |
| 58 | const result = Object.create(null) as Pragmas; |
| 59 | const comments = docblock |
| 60 | .replaceAll(propertyRe, '') |
| 61 | .replace(ltrimNewlineRe, '') |
| 62 | .trimEnd(); |
| 63 | |
| 64 | let match; |
| 65 | while ((match = propertyRe.exec(docblock))) { |
| 66 | // strip linecomments from pragmas |
| 67 | const nextPragma = match[2].replaceAll(lineCommentRe, ''); |
| 68 | if ( |
| 69 | typeof result[match[1]] === 'string' || |
| 70 | Array.isArray(result[match[1]]) |
| 71 | ) { |
| 72 | const resultElement = result[match[1]]; |
| 73 | result[match[1]] = [ |
| 74 | ...STRING_ARRAY, |
| 75 | ...(Array.isArray(resultElement) ? resultElement : [resultElement]), |
| 76 | nextPragma, |
| 77 | ]; |
| 78 | } else { |
| 79 | result[match[1]] = nextPragma; |
| 80 | } |
| 81 | } |
| 82 | return {comments, pragmas: result}; |
| 83 | } |
| 84 | |
| 85 | export function print({ |
| 86 | comments = '', |