|
44 | 44 | while (pcl.length && !pcl[0].trim()) { |
45 | 45 | pcl.shift() |
46 | 46 | } |
47 | | - let lastempty = pcl.findLastIndex((l) => /^\s*$/.test(l)) |
48 | | - if (lastempty !== -1) { |
49 | | - pcl = pcl.slice(lastempty + 1) |
| 47 | + |
| 48 | + // Check if there's a blank line followed by comments |
| 49 | + // If so, those comments should be linecomments for the previous field |
| 50 | + let firstCommentAfterBlank = -1 |
| 51 | + let hasBlankLine = false |
| 52 | + for (let i = 0; i < pcl.length; i++) { |
| 53 | + if (/^\s*$/.test(pcl[i])) { |
| 54 | + hasBlankLine = true |
| 55 | + } else if (hasBlankLine && /^\s*#/.test(pcl[i])) { |
| 56 | + firstCommentAfterBlank = i |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (firstCommentAfterBlank !== -1 && !linecomment) { |
| 62 | + // Move comments after blank line to linecomment |
| 63 | + const commentLines = pcl.slice(firstCommentAfterBlank) |
| 64 | + linecomment = commentLines.join('\n') |
| 65 | + pcl = pcl.slice(0, firstCommentAfterBlank) |
| 66 | + // Remove any trailing empty lines from pcl |
| 67 | + while (pcl.length && !pcl[pcl.length - 1].trim()) { |
| 68 | + pcl.pop() |
| 69 | + } |
| 70 | + } else { |
| 71 | + // Original behavior: only keep comments after the last empty line |
| 72 | + let lastempty = pcl.findLastIndex((l) => /^\s*$/.test(l)) |
| 73 | + if (lastempty !== -1) { |
| 74 | + pcl = pcl.slice(lastempty + 1) |
| 75 | + } |
50 | 76 | } |
51 | 77 | // trim leading space and # on each line |
52 | 78 | pcl = pcl.map((l) => l.replace(/^[ \t]*#[ \t]?/gm, '')) |
53 | | - linecomment = linecomment ? flattenArray(linecomment).join('').replace(/^[ \t]*#[ \t]?/, '') : null |
| 79 | + if (linecomment && typeof linecomment !== 'string') { |
| 80 | + linecomment = flattenArray(linecomment).join('') |
| 81 | + } |
| 82 | + linecomment = linecomment ? linecomment.replace(/^[ \t]*#[ \t]?/gm, '') : null |
54 | 83 | const comments = (pcl.length || linecomment) ? {} : null |
55 | 84 | if (pcl.length) { |
56 | 85 | comments.precomments = pcl.join('\n') |
|
0 commit comments