(
rawLines: string,
loc: NodeLocation,
opts: Options = {},
)
| 124 | } |
| 125 | |
| 126 | export function codeFrameColumns( |
| 127 | rawLines: string, |
| 128 | loc: NodeLocation, |
| 129 | opts: Options = {}, |
| 130 | ): string { |
| 131 | const shouldHighlight = |
| 132 | opts.forceColor || (isColorSupported() && opts.highlightCode); |
| 133 | const startLineBaseZero = (opts.startLine || 1) - 1; |
| 134 | const defs = getDefs(shouldHighlight); |
| 135 | |
| 136 | const lines = rawLines.split(NEWLINE); |
| 137 | const { start, end, markerLines } = getMarkerLines( |
| 138 | loc, |
| 139 | lines, |
| 140 | opts, |
| 141 | startLineBaseZero, |
| 142 | ); |
| 143 | const hasColumns = loc.start && typeof loc.start.column === "number"; |
| 144 | |
| 145 | const numberMaxWidth = String(end + startLineBaseZero).length; |
| 146 | |
| 147 | const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines; |
| 148 | |
| 149 | let frame = highlightedLines |
| 150 | .split(NEWLINE, end) |
| 151 | .slice(start, end) |
| 152 | .map((line, index) => { |
| 153 | const number = start + 1 + index; |
| 154 | const paddedNumber = ` ${number + startLineBaseZero}`.slice( |
| 155 | -numberMaxWidth, |
| 156 | ); |
| 157 | const gutter = ` ${paddedNumber} |`; |
| 158 | const hasMarker = markerLines[number]; |
| 159 | const lastMarkerLine = !markerLines[number + 1]; |
| 160 | if (hasMarker) { |
| 161 | let markerLine = ""; |
| 162 | if (Array.isArray(hasMarker)) { |
| 163 | const markerSpacing = line |
| 164 | .slice(0, Math.max(hasMarker[0] - 1, 0)) |
| 165 | .replace(/[^\t]/g, " "); |
| 166 | const numberOfMarkers = hasMarker[1] || 1; |
| 167 | |
| 168 | markerLine = [ |
| 169 | "\n ", |
| 170 | defs.gutter(gutter.replace(/\d/g, " ")), |
| 171 | " ", |
| 172 | markerSpacing, |
| 173 | defs.marker("^").repeat(numberOfMarkers), |
| 174 | ].join(""); |
| 175 | |
| 176 | if (lastMarkerLine && opts.message) { |
| 177 | markerLine += " " + defs.message(opts.message); |
| 178 | } |
| 179 | } |
| 180 | return [ |
| 181 | defs.marker(">"), |
| 182 | defs.gutter(gutter), |
| 183 | line.length > 0 ? ` ${line}` : "", |
no test coverage detected