1- // The fork-hook seam model. A `fork-hook:` marker in an upstream-owned source
2- // file is the whole declaration of a seam: there is no hand-kept manifest, and
3- // nothing outside the marked source decides whether a hook exists
4- // (RSI-Software/t3code-hyprws#1155). `deriveForkHooks` reads the markers at the
5- // fork tip and derives every entry the sync walk re-applies.
6- //
7- // Three marker forms:
8- //
9- // - a trailing `// fork-hook: <domain>/<name>` on a one-line hook (an import,
10- // a call, a `const`, a property/spread);
11- // - `{/* fork-hook: <domain>/<name> */}` … `{/* fork-hook-end *` + `/}`
12- // (the end-marker JSX comment) around a multi-line JSX construct;
13- // - a trailing `/* fork-hook: <domain>/<name> */` for one-line hooks in
14- // languages where `//` is not a comment (CSS).
15- //
16- // A hook is exactly one construct and never removes or modifies an upstream
17- // line; `forkHookConstructViolation` is that doctrine as code. An unmatched
18- // JSX pair or an unknown domain fails the derivation with `path:line`, and a
19- // marker mid-comment marks nothing.
1+ // The fork-hook seam model: a `fork-hook:` marker in an upstream-owned file is the whole seam
2+ // declaration; no manifest. Forms: a trailing `// fork-hook: <domain>/<name>` (CSS: `/* ... */`),
3+ // or `{/* fork-hook: <domain>/<name> */}` … `{/* fork-hook-end *` + `/}` around multi-line JSX.
4+ // A hook is one construct that never changes an upstream line; a marker mid-comment marks nothing.
205
216// @effect -diagnostics nodeBuiltinImport:off
227import * as NodeChildProcess from "node:child_process" ;
@@ -26,12 +11,9 @@ import * as NodePath from "node:path";
2611import { FORK_DOMAINS } from "./fork-trailers.ts" ;
2712
2813/**
29- * Where the hook sits in its file, derived from the marked span's context at
30- * the fork tip: the shortest run of immediately preceding lines that carry no
31- * marker of their own and occur exactly once in the file with every marked span
32- * removed. Re-apply puts the hook back directly after that run, so the anchor
33- * needs no typed kinds — an import block, a collection member and a JSX child
34- * are all "after these lines".
14+ * Where the hook sits, derived at the fork tip: the shortest run of immediately preceding unmarked
15+ * lines that occurs exactly once in the file with every marked span removed. Re-apply puts the hook
16+ * back right after that run, so the anchor needs no typed kinds.
3517 */
3618export interface ForkHookAnchor {
3719 readonly context : ReadonlyArray < string > ;
@@ -107,6 +89,25 @@ const INITIAL_SCAN: LiteralScan = {
10789 comment : false ,
10890} ;
10991
92+ // A `/` at line start or after one of these characters or keywords on its line opens a regex
93+ // literal; anywhere else it divides.
94+ const REGEX_PRECEDER = "(,=:[!&|?{};+-*%<>~^" ;
95+ const REGEX_KEYWORD =
96+ / \b (?: r e t u r n | t y p e o f | c a s e | d o | e l s e | i n | o f | v o i d | y i e l d | a w a i t | d e l e t e | t h r o w | n e w | i n s t a n c e o f ) \s * $ / ;
97+
98+ /** The index of the `/` closing a regex literal opened at `start`, or -1 when the line never closes it. */
99+ const regexLiteralEnd = ( line : string , start : number ) : number => {
100+ let inClass = false ;
101+ for ( let index = start + 1 ; index < line . length ; index += 1 ) {
102+ const char = line [ index ] ;
103+ if ( char === "\\" ) index += 1 ;
104+ else if ( char === "[" ) inClass = true ;
105+ else if ( char === "]" ) inClass = false ;
106+ else if ( char === "/" && ! inClass ) return index ;
107+ }
108+ return - 1 ;
109+ } ;
110+
110111const scanLiteralLine = ( line : string , start : LiteralScan ) : LiteralScan => {
111112 let { depth, quote, lastCode, comment } = start ;
112113 const frames = [ ...start . frames ] ;
@@ -138,6 +139,19 @@ const scanLiteralLine = (line: string, start: LiteralScan): LiteralScan => {
138139 index += 1 ;
139140 continue ;
140141 }
142+ // A regex literal's quotes and brackets are pattern text, not literals or nesting.
143+ if (
144+ char === "/" &&
145+ ( REGEX_PRECEDER . includes ( line . slice ( 0 , index ) . trimEnd ( ) . at ( - 1 ) ?? "(" ) ||
146+ REGEX_KEYWORD . test ( line . slice ( 0 , index ) ) )
147+ ) {
148+ const end = regexLiteralEnd ( line , index ) ;
149+ if ( end !== - 1 ) {
150+ index = end ;
151+ lastCode = "/" ;
152+ continue ;
153+ }
154+ }
141155 if ( char === '"' || char === "'" || char === "`" ) {
142156 quote = char ;
143157 continue ;
@@ -174,17 +188,10 @@ const scanLiteralLine = (line: string, start: LiteralScan): LiteralScan => {
174188const COMMENT_ONLY = / ^ (?: \/ \/ | \/ \* | \* (?: \/ | $ ) ) / ;
175189
176190/**
177- * The first line (1-based) of the statement a trailing marker on `markerLine` closes, found by
178- * walking backwards: while `()[]{}` brackets or string/template literals are still open, or the
179- * previous line neither ends a statement (`;`, `{`, `,` as the last code character outside any
180- * literal, or a balanced `}` closing a block) nor is blank or
181- * comment-only, the statement continues upward. `null` when the start cannot be proven — the
182- * marker sits inside a string or template text, or a stray close leaves the file unbalanced.
183- * Pure and line-based; there is deliberately no parser dependency.
184- *
185- * "Open" is judged against the depth the statement itself ends at, not against the file's top
186- * level: a hook inside a function body sits at depth 1 for its whole life, and comparing with 0
187- * would walk the span out to the enclosing declaration and swallow upstream lines with it.
191+ * The 1-based first line of the statement a trailing marker on `markerLine` closes, walking back
192+ * while brackets or literals stay open or the previous line ends no statement; `null` if unproven.
193+ * "Open" is judged against the statement's own end depth, not depth 0, or a hook inside a function
194+ * body would swallow the enclosing declaration. Line-based; there is deliberately no parser.
188195 */
189196export const statementStartLine = (
190197 lines : ReadonlyArray < string > ,
@@ -305,11 +312,9 @@ export const isWellFormedForkHookKey = (key: string): boolean => {
305312 ) ;
306313} ;
307314
308- // The marker-construct doctrine (RSI-Software/t3code-hyprws#1155 folded the
309- // authoring guard's classifier in here, next to the parser it judges). A marked
310- // line hook must be exactly one of these shapes; the property/spread shape
311- // additionally names a fork identifier, the others are structural. `@import` is
312- // the CSS spelling of the import construct: a fork sheet is pulled in that way.
315+ // The marker-construct doctrine: a marked line hook must be exactly one of these shapes. The
316+ // property/spread shape also names a fork identifier; the others are structural. `@import` is the
317+ // CSS spelling of the import construct.
313318const HOOK_IMPORT = / ^ \s * (?: @ ? i m p o r t \b | e x p o r t \s + \{ [ ^ } ] * \} \s * f r o m \b | e x p o r t \s * \* ) / ;
314319const HOOK_SINGLE_CALL =
315320 / ^ (? ! \s * (?: i f | f o r | w h i l e | s w i t c h | c a t c h | r e t u r n ) \s * \( ) \s * [ A - Z a - z _ $ ] [ \w $ . ] * \s * \( / ;
0 commit comments