mirror of
https://github.com/morluto/rea
synced 2026-10-27 06:33:32 -04:00
refactor(javascript): centralize source coordinate operations
This commit is contained in:
@@ -1,4 +1,28 @@
|
||||
import { z } from "zod";
|
||||
import type {
|
||||
JavaScriptSourcePoint,
|
||||
JavaScriptSourceRange,
|
||||
} from "./javascriptStaticAnalysisTypes.js";
|
||||
|
||||
/** Compare parser coordinates by line, then UTF-16 column. */
|
||||
export const compareJavaScriptSourcePoints = (
|
||||
left: JavaScriptSourcePoint,
|
||||
right: JavaScriptSourcePoint,
|
||||
): number => left.line - right.line || left.column - right.column;
|
||||
|
||||
/** Include equal endpoints without implying runtime reachability. */
|
||||
export const javascriptSourceRangeContains = (
|
||||
outer: JavaScriptSourceRange,
|
||||
inner: JavaScriptSourceRange,
|
||||
): boolean =>
|
||||
compareJavaScriptSourcePoints(outer.start, inner.start) <= 0 &&
|
||||
compareJavaScriptSourcePoints(outer.end, inner.end) >= 0;
|
||||
|
||||
/** Exact artifact-local coordinate key; callers supply the artifact identity. */
|
||||
export const javascriptSourceRangeKey = (
|
||||
range: JavaScriptSourceRange,
|
||||
): string =>
|
||||
`${range.start.line}:${range.start.column}-${range.end.line}:${range.end.column}`;
|
||||
|
||||
const sourcePointSchema = z.strictObject({
|
||||
line: z.number().int().min(1),
|
||||
@@ -9,11 +33,7 @@ const sourcePointSchema = z.strictObject({
|
||||
export const javascriptSourceRangeSchema = z
|
||||
.strictObject({ start: sourcePointSchema, end: sourcePointSchema })
|
||||
.superRefine((range, context) => {
|
||||
if (
|
||||
range.end.line < range.start.line ||
|
||||
(range.end.line === range.start.line &&
|
||||
range.end.column < range.start.column)
|
||||
)
|
||||
if (compareJavaScriptSourcePoints(range.end, range.start) < 0)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["end"],
|
||||
|
||||
Reference in New Issue
Block a user