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

Function _codeFrameColumns

packages/babel-code-frame/src/common.ts:126–208  ·  view source on GitHub ↗
(
  rawLines: string,
  loc: NodeLocation,
  opts: Options = {},
  colorOpts?: {
    defs: Pick<Defs, "gutter" | "marker" | "message" | "reset">;
    highlight: (code: string) => string;
  },
)

Source from the content-addressed store, hash-verified

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

Callers 2

codeFrameColumnsFunction · 0.90
codeFrameColumnsFunction · 0.90

Calls 5

getMarkerLinesFunction · 0.85
mapMethod · 0.80
replaceMethod · 0.80
messageMethod · 0.80
highlightFunction · 0.70

Tested by

no test coverage detected