Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/38777-hook-body-bound-handle-lookback.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 7 additions & 2 deletions packages/cli/src/utils/extract-hook-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) =>
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/test/extract-hook-body-local-alias-38690.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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') {
Expand Down
Loading