(
source: string,
start: number | { line: number; column: number; lo: number } = 0,
end?: number
)
| 108 | } |
| 109 | |
| 110 | export function generateCodeFrame( |
| 111 | source: string, |
| 112 | start: number | { line: number; column: number; lo: number } = 0, |
| 113 | end?: number |
| 114 | ): string { |
| 115 | start = posToNumber(source, start); |
| 116 | end = end || start; |
| 117 | const lines = source.split(splitRE); |
| 118 | let count = 0; |
| 119 | const res: string[] = []; |
| 120 | for (let i = 0; i < lines.length; i++) { |
| 121 | count += lines[i].length + 1; |
| 122 | if (count >= start) { |
| 123 | for (let j = i - range; j <= i + range || end > count; j++) { |
| 124 | if (j < 0 || j >= lines.length) { |
| 125 | continue; |
| 126 | } |
| 127 | const line = j + 1; |
| 128 | res.push(`${line}${' '.repeat(Math.max(3 - String(line).length, 0))}| ${lines[j]}`); |
| 129 | const lineLength = lines[j].length; |
| 130 | if (j === i) { |
| 131 | // push underline |
| 132 | const pad = Math.max(start - (count - lineLength) + 1, 0); |
| 133 | const length = Math.max(1, end > count ? lineLength - pad : end - start); |
| 134 | res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length)); |
| 135 | } else if (j > i) { |
| 136 | if (end > count) { |
| 137 | const length = Math.max(Math.min(end - count, lineLength), 1); |
| 138 | res.push(` | ` + '^'.repeat(length)); |
| 139 | } |
| 140 | count += lineLength + 1; |
| 141 | } |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | return res.join('\n'); |
| 147 | } |
| 148 | |
| 149 | export function isWin(os: string): boolean { |
| 150 | return os === 'win32'; |
no test coverage detected