diff --git a/.changeset/38777-hook-body-bound-handle-lookback.md b/.changeset/38777-hook-body-bound-handle-lookback.md new file mode 100644 index 00000000000..d2490b40337 --- /dev/null +++ b/.changeset/38777-hook-body-bound-handle-lookback.md @@ -0,0 +1,13 @@ +--- +'@objectstack/cli': patch +--- + +fix(cli): the `./hook-body` capability extractor reads a handle bound across a line break or a comment, as the publish rule does + +Clause-②: no +Metadata-protocol: none + +Who sees it: code that calls the published `@objectstack/cli/hook-body` entry directly on a handler whose source keeps its line breaks and comments. `os build` is unaffected: it loads the config through a bundler that reprints each handler on one line without comments. + +- **A bound handle is read across a line break or a comment.** In `const repo = ctx.api` followed by `.object('x');` on the next line (or after a `//` or `/* */` comment), and then `repo.update(…)`, the extractor now infers `api.write` (and `api.read` for a read leaf), as the publish rule already did. Before, it inferred nothing for that spelling, so the lowered body drew a capability finding from the publish rule that the same body written on one line did not. +- Nothing an author writes is accepted or refused differently: the publish rule and the metadata schema are unchanged. diff --git a/packages/cli/src/utils/extract-hook-body.ts b/packages/cli/src/utils/extract-hook-body.ts index 45a90c21083..957939c6473 100644 --- a/packages/cli/src/utils/extract-hook-body.ts +++ b/packages/cli/src/utils/extract-hook-body.ts @@ -676,7 +676,9 @@ function closingParen(source: string, open: number): number { * * - an argument holding a call or other parentheses: `ctx.api.object(name()).update(…)`; * - `?.` before the leaf: `ctx.api.object('x')?.update(…)`; - * - a bound handle: `const repo = ctx.api.object('x'); … repo.update(…)`. + * - a bound handle: `const repo = ctx.api.object('x'); … repo.update(…)`, + * [#38777] its receiver chain split by a line break or a line comment as + * the rule reads it (`const repo = ctx.api // c` then `.object('x');`). * * Asked twice, once per token: {@link callsObjectWriteThroughHandle} for the * write leaves and {@link callsObjectReadThroughHandle} (#38653) for the read @@ -696,7 +698,10 @@ function callsObjectLeafThroughHandle(source: string, leaves: string): boolean { if (close < 0) continue; const after = source.slice(close + 1); if (chainedLeaf.test(after)) return true; - const bound = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:await\s+)?[\w$.? \t]*$/.exec(source.slice(0, m.index ?? 0)); + // The receiver chain between `=` and `.object` takes the rule's own step: + // whitespace on either side of a dot, a line break included, and (in the + // comment-blanked copy this is also asked over) a comment. + const bound = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:await\s+)?[\w$.?\s]*$/.exec(source.slice(0, m.index ?? 0)); if (bound && /^\s*(?:;|\n|$)/.test(after)) handles.push(bound[1]); } return handles.some((name) => diff --git a/packages/cli/test/extract-hook-body-local-alias-38690.test.ts b/packages/cli/test/extract-hook-body-local-alias-38690.test.ts index 93ca43395c5..e434c56ecf7 100644 --- a/packages/cli/test/extract-hook-body-local-alias-38690.test.ts +++ b/packages/cli/test/extract-hook-body-local-alias-38690.test.ts @@ -113,7 +113,9 @@ const EVERY_TOKEN = { * publish rule reads a call (`memberLeafCalls`, `objectLeafCalls`, * `calledWithArgument`): plain, `?.` before the leaf, the optional call * `LEAF?.(…)` where the rule reads one, whitespace around the dot and before - * the paren, and a comment between the steps (the rule blanks comments). + * the paren, and a comment between the steps (the rule blanks comments); an + * `.object(…)` leaf also through a handle bound once to it, its receiver chain + * split by a line break or a comment. */ function gatedCalls(gate: SandboxCapabilityGate, receiver: string): string[] { if (gate.shape === 'object-leaf') { @@ -122,6 +124,12 @@ function gatedCalls(gate: SandboxCapabilityGate, receiver: string): string[] { `await ${receiver}?.object('x')?.${leaf}({});`, `await ${receiver} . object('x') . ${leaf} ({});`, `await ${receiver}.object('x')/* c */.${leaf}({});`, + // [#38777] A handle bound once to `.object(…)`, its receiver chain split + // the ways the rule's step reads: a line break, a line comment, a block comment. + `const h = ${receiver}.object('x'); await h.${leaf}({});`, + `const h = ${receiver}\n .object('x');\n await h.${leaf}({});`, + `const h = ${receiver} // c\n .object('x');\n await h.${leaf}({});`, + `const h = ${receiver}/* c */.object('x'); await h.${leaf}({});`, ]); } if (gate.shape === 'member-leaf') {