Files
test-repo/tests/config/format-test/visualize-range.js
T
88ee8a5b3e Update babel to v8.0.0-rc.1 (#18822)
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>
2026-02-01 08:57:18 +08:00

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;