MCPcopy Create free account
hub / github.com/babel/babel / codeFrameColumns

Function codeFrameColumns

packages/babel-code-frame/src/index.ts:126–201  ·  view source on GitHub ↗
(
  rawLines: string,
  loc: NodeLocation,
  opts: Options = {},
)

Source from the content-addressed store, hash-verified

124}
125
126export 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}` : "",

Callers 9

runFunction · 0.90
replaceWithSourceStringFunction · 0.90
toContextualSyntaxErrorFunction · 0.90
parseWithCodeFrameFunction · 0.90
parserFunction · 0.90
buildCodeFrameErrorMethod · 0.90
index.jsFile · 0.85
color-detection.jsFile · 0.85
index.tsFile · 0.85

Calls 7

isColorSupportedFunction · 0.90
getDefsFunction · 0.90
highlightFunction · 0.90
getMarkerLinesFunction · 0.85
mapMethod · 0.80
replaceMethod · 0.80
messageMethod · 0.80

Tested by

no test coverage detected