-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export const codeFrameColumns = (raw, loc) => { | ||
const lines = raw.split(/\r\n|[\n\r\u2028\u2029]/); | ||
|
||
const startLoc = Object.assign({ column: 0, line: -1 }, loc.start); | ||
const endLoc = Object.assign({}, startLoc, loc.end); | ||
|
||
const startLine = startLoc.line; | ||
const startColumn = startLoc.column; | ||
const endLine = endLoc.line; | ||
const endColumn = endLoc.column; | ||
|
||
const start = startLine === -1 ? 0 : Math.max(startLine - 3, 0); | ||
const end = endLine === -1 ? lines.length : Math.min(lines.length, endLine + 3); | ||
|
||
const lineDiff = endLine - startLine; | ||
const markerLines = new Map(); | ||
|
||
if (lineDiff) { | ||
for (let i = 0; i <= lineDiff; i++) { | ||
const lineNumber = i + startLine; | ||
|
||
if (!startColumn) { | ||
markerLines.set(lineNumber, true); | ||
} else if (i === 0) { | ||
const sourceLength = lines[lineNumber - 1].length; | ||
markerLines.set(lineNumber, [startColumn, sourceLength - startColumn]); | ||
} else if (i === lineDiff) { | ||
markerLines.set(lineNumber, [0, endColumn]); | ||
} else { | ||
const sourceLength = lines[lineNumber - i].length; | ||
markerLines.set(lineNumber, [0, sourceLength]); | ||
} | ||
} | ||
} else { | ||
if (startColumn === endColumn) { | ||
if (startColumn) { | ||
markerLines.set(startLine, [startColumn, 0]); | ||
} else { | ||
markerLines.set(startLine, true); | ||
} | ||
} else { | ||
markerLines.set(startLine, [startColumn, endColumn - startColumn]); | ||
} | ||
} | ||
|
||
const numberMaxWidth = String(end).length; | ||
|
||
return lines | ||
.slice(start, end) | ||
.map((line, index) => { | ||
const number = start + 1 + index; | ||
const paddedNumber = ` ${number}`.slice(-numberMaxWidth); | ||
const gutter = ` ${paddedNumber} | `; | ||
const hasMarker = markerLines.get(number); | ||
|
||
if (hasMarker) { | ||
let markerLine = ''; | ||
|
||
if (Array.isArray(hasMarker)) { | ||
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, ' '); | ||
const numberOfMarkers = hasMarker[1] || 1; | ||
|
||
markerLine = `\n ${gutter.replace(/\d/g, ' ')}${markerSpacing}${'^'.repeat(numberOfMarkers)}`; | ||
} | ||
|
||
return `>${gutter}${line}${markerLine}`; | ||
} | ||
|
||
return ` ${gutter}${line}`; | ||
}) | ||
.join('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters