Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 54x 54x 54x 54x 920x 920x 920x 920x 920x 908x 908x 908x 210832x 210832x 210832x 210832x 210832x 210832x 396951366x 210832x 396740534x 1694244x 396740534x 396740534x 12742673x 12742673x 383997861x 396740534x 105536x 105536x 105536x 288x 105248x 105248x 105248x 105248x 16x 16x 105248x 16x 105232x 105232x 105232x 5355x 99877x 99877x 99877x 99877x 99877x 99877x 105232x 105232x 105232x 896x 896x 896x 105232x 18x 105214x 48x 48x 48x 48x 48x 48x 48x 48x 210800x 210800x 455x 210800x 320x 210480x 804x 804x 4280x 4280x 4280x 4280x 48536x 48536x 4280x 804x 796x 796x 796x 8x 8x 4x 4x 4x 4x 4x 4x 28x 28x 20x | import { readFileSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import {
allGeneratedPositionsFor,
LEAST_UPPER_BOUND,
originalPositionFor,
type TraceMap,
type Needle,
type SourceMapSegment,
type DecodedSourceMap,
type SourceMap,
sourceContentFor,
} from "@jridgewell/trace-mapping";
import { type Node } from "estree";
import { getIgnoredLines } from "./ignore-hints";
const WORD_PATTERN = /(\w+|\s|[^\w\s])/g;
const INLINE_MAP_PATTERN = /#\s*sourceMappingURL=(.*)\s*$/m;
const BASE_64_PREFIX = "data:application/json;base64,";
/** How often should offset calculations be cached */
const CACHE_THRESHOLD = 250;
type Filename = string;
export class Locator {
#cache = new Map<number, Needle>();
#codeParts: string[];
#map: TraceMap;
#directory: string;
#ignoredLines = new Map<Filename, ReturnType<typeof getIgnoredLines>>();
constructor(code: string, map: TraceMap, directory: string) {
this.#codeParts = code.split("");
this.#map = map;
this.#directory = directory;
}
reset() {
this.#cache.clear();
this.#ignoredLines.clear();
this.#codeParts = [];
}
offsetToNeedle(offset: number): Needle {
const closestThreshold =
Math.floor(offset / CACHE_THRESHOLD) * CACHE_THRESHOLD;
const cacheHit = this.#cache.get(closestThreshold);
let current = cacheHit ? closestThreshold : 0;
let line = cacheHit?.line ?? 1;
let column = cacheHit?.column ?? 0;
for (let i = current; i <= this.#codeParts.length; i++) {
if (current === offset) {
return { line, column };
}
if (current % CACHE_THRESHOLD === 0) {
this.#cache.set(current, { line, column });
}
const char = this.#codeParts[i];
if (char === "\n") {
line++;
column = 0;
} else {
column++;
}
current++;
}
return { line, column };
}
getLoc(node: Pick<Node, "start" | "end">) {
const startNeedle = this.offsetToNeedle(node.start);
const start = getPosition(startNeedle, this.#map);
if (start === null) {
// Does not exist in source maps, e.g. generated code
return null;
}
// End-mapping tracing logic from istanbul-lib-source-maps
const endNeedle = this.offsetToNeedle(node.end);
endNeedle.column -= 1;
let end = getPosition(endNeedle, this.#map);
// e.g. tsc that doesnt include } in source maps
if (end === null) {
endNeedle.column++;
end = getPosition(endNeedle, this.#map);
}
if (end === null) {
// Does not exist in source maps, e.g. generated code
return null;
}
const loc = { start, end };
const afterEndMappings = allGeneratedPositionsFor(this.#map, {
source: loc.end.filename,
line: loc.end.line,
column: loc.end.column + 1,
bias: LEAST_UPPER_BOUND,
});
if (afterEndMappings.length === 0) {
loc.end.column = Infinity;
} else {
for (const mapping of afterEndMappings) {
Iif (mapping.line === null) continue;
const original = originalPositionFor(this.#map, mapping);
Eif (original.line === loc.end.line) {
loc.end = { ...original, filename: original.source! };
break;
}
}
}
const filename = loc.start.filename;
let ignoredLines = this.#ignoredLines.get(filename);
if (!ignoredLines) {
const sources = sourceContentFor(this.#map, filename);
ignoredLines = getIgnoredLines(sources ?? tryReadFileSync(filename));
this.#ignoredLines.set(filename, ignoredLines);
}
// Anything that starts between the line ignore hints is ignored
if (ignoredLines.has(loc.start.line)) {
return null;
}
return loc;
}
getSourceLines(loc: { start: Needle; end: Needle }, filename: string) {
const index = this.#map.resolvedSources.findIndex(
(source) =>
source === filename || resolve(this.#directory, source) === filename,
);
const sourcesContent = this.#map.sourcesContent?.[index];
Iif (sourcesContent == null) {
return null;
}
const lines = sourcesContent
.split("\n")
.slice(loc.start.line - 1, loc.end.line);
lines[0] = lines[0].slice(loc.start.column);
// eslint-disable-next-line e18e/prefer-array-at -- https://github.com/e18e/eslint-plugin/issues/27
lines[lines.length - 1] = lines[lines.length - 1].slice(0, loc.end.column);
return lines.join("\n");
}
}
function getPosition(needle: Needle, map: TraceMap) {
let position = originalPositionFor(map, needle);
if (position.source == null) {
position = originalPositionFor(map, {
column: needle.column,
line: needle.line,
bias: LEAST_UPPER_BOUND,
});
}
if (position.source == null) {
return null;
}
return {
line: position.line,
column: position.column,
filename: position.source,
};
}
export function createEmptySourceMap(
filename: string,
code: string,
): DecodedSourceMap {
const mappings: SourceMapSegment[][] = [];
// Identical mappings as "magic-string"'s { hires: "boundary" }
for (const [line, content] of code.split("\n").entries()) {
const parts = content.match(WORD_PATTERN) || [];
const segments: SourceMapSegment[] = [];
let column = 0;
for (const part of parts) {
segments.push([column, 0, line, column]);
column += part.length;
}
mappings.push(segments);
}
return {
version: 3,
mappings,
file: filename,
sources: [filename],
sourcesContent: [code],
names: [],
};
}
export async function getInlineSourceMap(filename: string, code: string) {
const matches = code.match(INLINE_MAP_PATTERN);
const match = matches?.[1];
if (!match) return null;
try {
if (match.includes(BASE_64_PREFIX)) {
const encoded = match.split(BASE_64_PREFIX).at(-1) || "";
const decoded = atob(encoded);
return JSON.parse(decoded) as SourceMap;
}
const directory = dirname(filename);
const content = await readFile(resolve(directory, match), "utf-8");
return JSON.parse(content) as SourceMap;
} catch {
return null;
}
}
function tryReadFileSync(filename: string) {
try {
return readFileSync(filename, "utf8");
} catch {
return undefined;
}
}
|