mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
trace_web_source_location reported a well-formed generated_position past the end of the selected script as invalid_format on generated_position, with no bounds, so callers were told their input was malformed and not which coordinate to change. Report out_of_range on the line or column that is past the end, with its valid bounds, as export_web_scripts and inspect_web_network_capture now do for their selections. The position scan now returns the bound it ran past, so the offset and the reported limit come from one pass and cannot disagree on line terminators. Fixes #7132
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
/** JavaScript positions use one-based lines and zero-based UTF-16 columns. */
|
|
export interface WebSourcePosition {
|
|
readonly line: number;
|
|
readonly column: number;
|
|
}
|
|
|
|
/** A resolved text offset, or the coordinate past the end of the text with its inclusive maximum. */
|
|
export type WebSourcePositionResolution =
|
|
| { readonly offset: number }
|
|
| { readonly field: "line" | "column"; readonly maximum: number };
|
|
|
|
/** Resolve a selected position against actual text without splitting the whole source. */
|
|
export const resolveWebSourcePosition = (
|
|
text: string,
|
|
position: WebSourcePosition,
|
|
): WebSourcePositionResolution | undefined => {
|
|
if (
|
|
!Number.isSafeInteger(position.line) ||
|
|
position.line < 1 ||
|
|
!Number.isSafeInteger(position.column) ||
|
|
position.column < 0
|
|
)
|
|
return undefined;
|
|
let line = 1;
|
|
let start = 0;
|
|
for (const boundary of text.matchAll(/\r\n?|\n|\u2028|\u2029/gu)) {
|
|
if (line === position.line) return column(start, boundary.index, position);
|
|
line += 1;
|
|
start = boundary.index + boundary[0].length;
|
|
}
|
|
return line === position.line
|
|
? column(start, text.length, position)
|
|
: { field: "line", maximum: line };
|
|
};
|
|
|
|
const column = (
|
|
start: number,
|
|
end: number,
|
|
position: WebSourcePosition,
|
|
): WebSourcePositionResolution =>
|
|
position.column <= end - start
|
|
? { offset: start + position.column }
|
|
: { field: "column", maximum: end - start };
|
|
|
|
/** Verify a selected position against actual text without splitting the whole source. */
|
|
export const webSourceOffset = (
|
|
text: string,
|
|
position: WebSourcePosition,
|
|
): number | undefined => {
|
|
const resolved = resolveWebSourcePosition(text, position);
|
|
return resolved !== undefined && "offset" in resolved
|
|
? resolved.offset
|
|
: undefined;
|
|
};
|