Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
30 lines
857 B
JavaScript
30 lines
857 B
JavaScript
import indexToPosition from "index-to-position";
|
|
import { codeFrameColumns } from "../../../vendors/babel-code-frame-for-test.js";
|
|
const codeFrameColumnsOptions = {
|
|
linesAbove: Number.POSITIVE_INFINITY,
|
|
linesBelow: Number.POSITIVE_INFINITY,
|
|
};
|
|
|
|
const locationForRange = (text, range) => {
|
|
const [start, end] = [...range]
|
|
.sort((indexA, indexB) => indexA - indexB)
|
|
.map((index) => indexToPosition(text, index, { oneBased: true }));
|
|
|
|
if (start.line !== end.line) {
|
|
end.column -= 1;
|
|
}
|
|
|
|
return { start, end };
|
|
};
|
|
|
|
const visualizeRange = (text, { rangeStart = 0, rangeEnd = text.length }) =>
|
|
codeFrameColumns(
|
|
text,
|
|
locationForRange(text, [rangeStart, rangeEnd]),
|
|
rangeStart > rangeEnd
|
|
? { ...codeFrameColumnsOptions, message: "[Reversed range]" }
|
|
: codeFrameColumnsOptions,
|
|
);
|
|
|
|
export default visualizeRange;
|