refactor(auto-reply): share directive arg parsing

This commit is contained in:
Peter Steinberger
2026-02-15 05:05:42 +00:00
parent 5c746d7751
commit 935ca39945
3 changed files with 50 additions and 50 deletions

View File

@@ -0,0 +1,40 @@
export function skipDirectiveArgPrefix(raw: string): number {
let i = 0;
const len = raw.length;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
if (raw[i] === ":") {
i += 1;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
}
return i;
}
export function takeDirectiveToken(
raw: string,
startIndex: number,
): { token: string | null; nextIndex: number } {
let i = startIndex;
const len = raw.length;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
if (i >= len) {
return { token: null, nextIndex: i };
}
const start = i;
while (i < len && !/\s/.test(raw[i])) {
i += 1;
}
if (start === i) {
return { token: null, nextIndex: i };
}
const token = raw.slice(start, i);
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
return { token, nextIndex: i };
}

View File

@@ -1,4 +1,5 @@
import type { ExecAsk, ExecHost, ExecSecurity } from "../../../infra/exec-approvals.js";
import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js";
type ExecDirectiveParse = {
cleaned: string;
@@ -48,17 +49,8 @@ function parseExecDirectiveArgs(raw: string): Omit<
> & {
consumed: number;
} {
let i = 0;
const len = raw.length;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
if (raw[i] === ":") {
i += 1;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
}
let i = skipDirectiveArgPrefix(raw);
let consumed = i;
let execHost: ExecHost | undefined;
let execSecurity: ExecSecurity | undefined;
@@ -75,21 +67,9 @@ function parseExecDirectiveArgs(raw: string): Omit<
let invalidNode = false;
const takeToken = (): string | null => {
if (i >= len) {
return null;
}
const start = i;
while (i < len && !/\s/.test(raw[i])) {
i += 1;
}
if (start === i) {
return null;
}
const token = raw.slice(start, i);
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
return token;
const res = takeDirectiveToken(raw, i);
i = res.nextIndex;
return res.token;
};
const splitToken = (token: string): { key: string; value: string } | null => {

View File

@@ -1,5 +1,6 @@
import type { QueueDropPolicy, QueueMode } from "./types.js";
import { parseDurationMs } from "../../../cli/parse-duration.js";
import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js";
import { normalizeQueueDropPolicy, normalizeQueueMode } from "./normalize.js";
function parseQueueDebounce(raw?: string): number | undefined {
@@ -45,17 +46,8 @@ function parseQueueDirectiveArgs(raw: string): {
rawDrop?: string;
hasOptions: boolean;
} {
let i = 0;
const len = raw.length;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
if (raw[i] === ":") {
i += 1;
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
}
let i = skipDirectiveArgPrefix(raw);
let consumed = i;
let queueMode: QueueMode | undefined;
let queueReset = false;
@@ -68,21 +60,9 @@ function parseQueueDirectiveArgs(raw: string): {
let rawDrop: string | undefined;
let hasOptions = false;
const takeToken = (): string | null => {
if (i >= len) {
return null;
}
const start = i;
while (i < len && !/\s/.test(raw[i])) {
i += 1;
}
if (start === i) {
return null;
}
const token = raw.slice(start, i);
while (i < len && /\s/.test(raw[i])) {
i += 1;
}
return token;
const res = takeDirectiveToken(raw, i);
i = res.nextIndex;
return res.token;
};
while (i < len) {
const token = takeToken();