Skip to content

Commit 197e3ff

Browse files
fix(cli, lint): os build and the body-capability rule count declared-once locals the same way (#38873)
<!-- claude-projects-attribution: {"github_login":"huangyiirene"} --> _Requested by **Irene** · [project thread](https://claude.ai/code/project/chan_01TJXtQ1tVD26ecLDZxFS67x?thread=cmsg_01TJXtQ1tVD26ecLDZxFS67xCc7t6qXBp9PVyzTRkHFg2s)_ Fixes #38801 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:开发者在钩子或动作里直接写函数时,`os build` 推断权限的规则和发布检查判断"只声明一次的本地变量"的方式以前不一致,于是会出现构建通过、发布检查却拒绝同一份产物的情况(例如参数叫 `e`、函数里又有 `catch (e)`)。现在两边按同一套规则数,卡上的三个例子构建后能拿到 `log` 权限并正常运行。 - 为什么做:评审在 #38799 上发现这类分歧,属于"两边读同样的写法"这一类缺陷。 - 出错会怎样:检查新增的报错只会落在沙箱本来就会在调用处拒绝(403)的函数体上;检查不再报的写法,构建也不授予权限,手写且未授权时仍由沙箱在调用处拒绝。 - 要你做什么:无。 ## What changes A local counts as a context root (`const c = ctx`) or a member alias (`const l = c.log`, `const { log } = c`) only when it is declared once in the handler and never assigned again. The build (`bodyMemberAliases` in `packages/cli/src/utils/extract-hook-body.ts`) read that off its parse; the publish rule (`boundOnce` in `packages/lint/src/validate-action-body-capabilities.ts`) read it off the text and counted differently. Both now count the same way, as ruled with the seat on the card thread: | | Build (before) | Rule (before) | Both (after) | |:---|:---|:---|:---| | `const` / `let` / `var` declarator, a plain name | counted | counted when first in its list | counted, any position in the list | | object pattern element | counted | counted (non-nested `{…}` only) | counted, any depth | | array pattern element | counted | not counted | counted, any depth | | `catch (NAME)` binding | counted | not counted | **not counted** (a parameter of its own block, like a nested function's parameter, which neither side counts) | | `[c] = …`, `({ c } = …)`, `for (c of/in …)` | not a reassignment | not a reassignment | a reassignment, as `c = …` is | Item by item: - **Item 1 (S1).** The extractor no longer counts a `catch` binding; the rule now counts array-pattern elements and every declarator of a list (`scanDeclarations`). The card's three bodies and the `const log = ctx.log; … catch (log) {}` class now infer `log` and the rule reads them, so the lowered bodies run. - **Item 2 (N4).** The rule reads a root, a member alias and a destructure anywhere in a declarator list (`const c = ctx, l = c.log`, `const a = 1, { log } = ctx`), keyed on the declarator offsets the same scan records. - **Item 3 (N3).** Fixed on both sides: the extractor adds destructuring-assignment targets and declaration-free `for … of` / `for … in` heads to `reassigned` (`assignmentTargetNames`); the rule's `patternAssigned` reads the same shapes. - **Item 4 (N5).** The root terminator follows JavaScript's semicolon insertion: a line terminator is `\n`, `\r\n`, a lone `\r`, U+2028 or U+2029, and a next line opening with `;`, `}`, `{`, a string or number literal, `++`, `--`, `!` (not `!=`), `~` or an identifier (non-ASCII included, not `in` / `instanceof`) ends the initializer. The docblock no longer calls `++`, `--` and `!` continuing operators. - **Item 5 (N6).** A step is read as JavaScript reads it: `i++` at a line's end steps `i`, not the name opening the next line; a prefix step after `return` / `typeof` / … still counts. - **Same terminator for the receivers.** A member alias and a destructure now end where a root ends, so `const l = ctx.log || null` and `const { log } = ctx || {}` are no alias, as the build already read them (its alias requires the member itself). A TypeScript `as` cast after a member alias is still read (the existing pin `const api = ctx.api as HookApi`); a lowered body carries no cast. **Over-read, on record (seat ruling):** the count is blind to scope on both sides, so in `const e = ctx; try {} catch (e) { e.log.info('x') }` both sides read the inner `e` as the outer root: the build grants `log` the call never uses, and the rule agrees. An over-grant never causes a refusal. The parity test pins it as the `OVER-READ` row. ## Clause-② no, shown per rule-side change Each rule-side change, and whether it can add a finding to a body that runs today: - **Declarator lists (item 2), root terminator (item 4), step reading (item 5).** These read more. Each new read is a local that is `ctx` or `ctx.MEMBER` at run time, so a new finding lands only on a call the sandbox gates and the body does not grant. That body already fails with 403 at the call. One exception, and it already exists for a first declarator on `main`: the count is blind to scope, so a nested function whose parameter shadows a root (`const a = 1, c = ctx; function f(c) { c.log.info() }`) is read through the root. A list position now reaches that case too. - **Array patterns and declarator lists counted, `patternAssigned`, receiver terminator.** These read less and add no finding. - **Catch bindings.** No change on the rule side. The rule never counted them. So no body that runs today is newly refused, except in the scope-blind shadow case named above. That case already exists on `main`, and now also reaches list positions. A freshly built body is granted the same capability, so only a hand-written body with no grant can see that finding. ## Tests - `packages/cli/test/extract-hook-body-bound-once-38801.test.ts` (new, 38 rows): every body goes to both sides, on the hook spelling `async (c) => …` and the action spelling `async (input, c) => …`. Rows that infer `log` must draw the rule's finding when the grant is dropped, and none on the lowered body. Control rows must infer nothing and draw nothing. It covers item 1 (the card's three bodies, the `main` class and the over-read), item 2, items 4 and 5 (every new terminator and both step shapes), and as controls item 3, array-pattern and list re-declarations, and expression aliases. - `packages/lint/src/validate-action-body-capabilities.bound-once-38801.test.ts` (new, 61 rows): the rule alone, on both doors. It covers the catch, list, terminator and step rows, plus controls: re-declarations (nested array patterns, an array inside an object pattern, a list with no initializer, a for-of declaration), every reassignment shape (with `for await`, a keyword before `[`, and an element assignment `o[c] = 1` that is not a destructuring), and continuations (`!==`, a template, `in`, a member step). <details><summary>Ablations at head 2e04c47 (both new files plus the existing extractor and rule tests, which are 195 lint + 698 cli tests)</summary> Each ablation edited the tree, rebuilt `@objectstack/lint`, ran, and was restored with `git checkout HEAD --` (`git status --porcelain` empty after each): | Ablation | lint failed | cli failed | |:---|:---|:---| | A0: both source files at `main` 94d552c | 42 | 38 (every new parity row) | | A1: extractor counts catch bindings again | 0 | 5 (the item-1 rows) | | A2: extractor ignores destructuring / for-head reassignment | 0 | 10 (the item-3 controls) | | A3: rule counts declarations the old way | 12 | 8 | | A4: rule's old root terminator | 13 | 11 (the item-4 rows) | | A5: rule's old step reading | 4 | 3 (the item-5 rows) | | A6: rule ignores pattern reassignment | 12 | 10 | | A7: rule reads only the first declarator of a list | 7 | 4 (the item-2 rows) | | A8: rule's old receiver end | 2 | 2 (the expression-alias controls) | At A0, rows such as "a root declared again by an array pattern in a nested block" fail on the rule side: on `main` the build infers nothing and the rule refuses the lowered body. That is the card's class. </details> <details><summary>Local gates at head 2e04c47</summary> - `pnpm --filter @objectstack/lint typecheck` and `pnpm --filter @objectstack/cli typecheck`: exit 0, with deps built with declarations. - `@objectstack/lint` full suite: 258 files, 10280 passed. - `@objectstack/cli`: the 47 non-e2e test files that import the extractor, `lower-callables`, `@objectstack/lint` or the hook-body subpath: 1307 passed, 29 skipped. The two `published-subpath-*` pins first needed a cli build and pass after it. The e2e layer was NOT MEASURED; CI runs it. - eslint on the four changed source and test files: clean. - `check:doc-authoring`, `check:empty-changeset`, `check:changeset-fixed`, `check:adr-0087-registration`, `check-commit-card-trailers --range origin/main..HEAD`: pass. </details> ## Acceptance notes - The count stays blind to scope by design on both sides. A name declared again in a nested block (`const c = ctx; { const [c] = x } c.log.info()`) is no root on either side, so the build grants nothing and the sandbox refuses the call. That matches how a second plain `const c` in a nested block was already treated on both sides. - Not changed: a handle bound to `.object(…)` in a declarator list (`const a = 1, repo = ctx.api.object('x')`) is read by neither side, so both agree. The extractor's `api` patterns are receiver-loose anyway. - A regular-expression literal holding an unbalanced bracket inside an initializer can hide later declarators of the same list from the rule's scan. It is documented as the accepted remainder, as the rule already does for regex literals. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01J8VVAjXyCrN7tYqPLmvyW4 <!-- signed: repo:objectstack@huangyiirene · huangyiirene · session_01J8VVAjXyCrN7tYqPLmvyW4 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b06efa9 commit 197e3ff

5 files changed

Lines changed: 599 additions & 22 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@objectstack/cli': patch
3+
'@objectstack/lint': patch
4+
---
5+
6+
fix(cli, lint): `os build` and the body-capability rule count "declared once" the same way
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
An inline hook or action handler can reach a gated `ctx` member through a local: a context root (`const c = ctx`, or a renamed handler parameter) or a member alias (`const l = c.log`, `const { log } = c`). Both `os build`, which infers the body's capabilities, and the `hook-body-capability-undeclared` / `action-body-capability-undeclared` / `action-body-write-capability-undeclared` publish check read such a local only when it is declared once and never assigned again, but they counted differently. So the build could lower a handler with no grant that the check then refused, such as `async (e) => { const { log } = e; try { … } catch (e) { log.error('x') } }`, or the check could miss a call the build granted.
12+
13+
Both sides now count the same way:
14+
15+
- A declaration is every declarator of a `const`, `let` or `var` list: a name, or an object or array pattern at any depth, in any position of the list. The check now reads `const c = ctx, l = c.log` and `const a = 1, { log } = ctx`. It no longer reads a name that an array pattern declares a second time.
16+
- A `catch` binding is not a declaration, like a nested function's parameter. The build now infers `log` for the body above and for `const log = ctx.log; … catch (log) {} log.info('x')`, so the lowered body runs. A read of the catch binding inside its own block is taken as the outer local. That can grant a token the call never uses, which never causes a refusal.
17+
- A destructuring assignment (`[c] = …`, `({ c } = …)`) and a `for (c of …)` / `for (c in …)` head assign the name again, as `c = …` does.
18+
- A member alias or a destructure counts only when its value is the member or the context itself, as a root already did. `const l = ctx.log || null` is no alias on either side.
19+
20+
The check also reads a semicolon-free body the way JavaScript does. A root ends at a line break when the next line opens with `++`, `--`, `!`, `~`, a string or number literal, `{` or a non-ASCII identifier, and a lone `\r`, U+2028 or U+2029 is a line break. `i++` at the end of a line steps `i`, not the name that opens the next line.
21+
22+
What changes for authors: a new finding falls on a body whose gated call the sandbox already refuses (403 at the call), so no body that runs today is newly refused. The one exception is a case that already exists: the count does not follow scopes, so a nested function whose parameter has the same name as a root is read as that root. That case now also applies to a root declared later in a list. A freshly built body is granted the same capability, so only a hand-written body with no grant can see that finding. A finding the check drops is on a shape `os build` grants nothing for either (a name declared twice, assigned again, or bound to an expression on the member). A hand-written body of that shape with no grant is refused by the sandbox at the call, as before, and no longer also by the check.

‎packages/cli/src/utils/extract-hook-body.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,27 @@ function bindingNames(name: ts.BindingName): string[] {
12341234
return out;
12351235
}
12361236

1237+
/**
1238+
* [#38801] Every name an assignment target writes: a name itself, and each name
1239+
* inside a destructuring pattern (`[a, { b, c: d }, ...e] = …`), defaults and
1240+
* nesting included. A member expression writes no name.
1241+
*/
1242+
function assignmentTargetNames(target: ts.Node): string[] {
1243+
if (ts.isIdentifier(target)) return [target.text];
1244+
if (ts.isParenthesizedExpression(target) || ts.isSpreadElement(target) || ts.isSpreadAssignment(target)) {
1245+
return assignmentTargetNames(target.expression);
1246+
}
1247+
if (ts.isBinaryExpression(target) && target.operatorToken.kind === ts.SyntaxKind.EqualsToken) return assignmentTargetNames(target.left);
1248+
if (ts.isArrayLiteralExpression(target)) return target.elements.flatMap(assignmentTargetNames);
1249+
if (ts.isObjectLiteralExpression(target)) {
1250+
return target.properties.flatMap((p) =>
1251+
ts.isShorthandPropertyAssignment(p) ? [p.name.text]
1252+
: ts.isPropertyAssignment(p) || ts.isSpreadAssignment(p) ? assignmentTargetNames(ts.isPropertyAssignment(p) ? p.initializer : p.expression)
1253+
: []);
1254+
}
1255+
return [];
1256+
}
1257+
12371258
/**
12381259
* The names a handler's body declares where the rebind prologue would meet
12391260
* them: `let` / `const` / `class` and `function` declarations among the body's
@@ -1333,6 +1354,17 @@ const ALIASABLE_MEMBERS: ReadonlySet<string> = new Set(['log', 'crypto', 'title'
13331354
* Either counts only when the name is declared once in the handler and never
13341355
* assigned or stepped again, so it stands for the member wherever it is read.
13351356
* Read off the handler's parse, so a string or a comment declares nothing.
1357+
* [#38801] The publish rule counts the same way (`scanDeclarations`,
1358+
* `patternAssigned`): a declaration is any declarator of a `const` / `let` /
1359+
* `var` list, a name or an object or array pattern at any depth; a `catch`
1360+
* binding is none, being a parameter of its own block like a nested
1361+
* function's parameter. So `(e) => { const { log } = e; try { … } catch (e) {
1362+
* log.error('x') } }` aliases `ctx.log`. Its other side: a read of the catch
1363+
* binding inside its block, `const e = ctx; try {} catch (e) { e.log.info('x')
1364+
* }`, is read through the outer root and over-infers `log`, which the sandbox
1365+
* never checks. A destructuring assignment (`[c] = …`) and a `for … of` /
1366+
* `for … in` head that declares nothing assign each name again, as `c = …`
1367+
* does.
13361368
* `title` is collected on the hook face alone: the host builds `ctx.title`
13371369
* only for hook bodies.
13381370
*
@@ -1348,7 +1380,9 @@ function bodyMemberAliases(fn: ts.FunctionLikeDeclarationBase, face: LoweringFac
13481380
const rootCandidates: Array<{ local: string; from: string }> = [];
13491381
const candidates: Array<{ member: string; local: string; from: string }> = [];
13501382
const visit = (node: ts.Node): void => {
1351-
if (ts.isVariableDeclaration(node)) {
1383+
// [#38801] A catch binding is no declaration here: like a nested
1384+
// function's parameter, it is a parameter of its own block.
1385+
if (ts.isVariableDeclaration(node) && !ts.isCatchClause(node.parent)) {
13521386
for (const n of bindingNames(node.name)) declared.set(n, (declared.get(n) ?? 0) + 1);
13531387
const init = node.initializer;
13541388
if (init && ts.isIdentifier(node.name) && ts.isIdentifier(init)) {
@@ -1369,6 +1403,13 @@ function bodyMemberAliases(fn: ts.FunctionLikeDeclarationBase, face: LoweringFac
13691403
} else if ((ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) && ts.isIdentifier(node.operand)
13701404
&& (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) {
13711405
reassigned.add(node.operand.text);
1406+
} else if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken
1407+
&& (ts.isArrayLiteralExpression(node.left) || ts.isObjectLiteralExpression(node.left))) {
1408+
// [#38801] A destructuring assignment assigns each name in its pattern again.
1409+
for (const n of assignmentTargetNames(node.left)) reassigned.add(n);
1410+
} else if ((ts.isForOfStatement(node) || ts.isForInStatement(node)) && !ts.isVariableDeclarationList(node.initializer)) {
1411+
// [#38801] So does a `for … of` / `for … in` head that declares nothing.
1412+
for (const n of assignmentTargetNames(node.initializer)) reassigned.add(n);
13721413
}
13731414
ts.forEachChild(node, visit);
13741415
};
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* The build and the publish rule count "declared once and never assigned
5+
* again" the same way.
6+
*
7+
* A local is a context root (`const c = ctx`) or a member alias
8+
* (`const l = c.log`, `const { log } = c`) only when it is declared once in the
9+
* handler and never assigned again. The extractor read that off the parse and
10+
* counted a `catch` binding and an array-pattern element as declarations; the
11+
* rule read it off the text and counted neither, nor a declarator past the
12+
* first in a list. So `os build` lowered a body with no grant and the rule
13+
* refused the same body (`async (e) => { const { log } = e; try {} catch (e) {
14+
* log.error('x') } }`), or the build granted a token the rule never read.
15+
*
16+
* Both sides now count every `const` / `let` / `var` declarator, whatever its
17+
* shape and wherever it stands in its list, and no `catch` binding (a parameter
18+
* of its own block, like a nested function's parameter); both count a
19+
* destructuring assignment and a `for … of` / `for … in` head as assigning
20+
* again. Every body here is fed to both sides, with the hook spelling
21+
* `async (c) => …` and the action spelling `async (input, c) => …`: the rows
22+
* that infer a token must draw the rule's finding when the grant is dropped and
23+
* none on the body the build writes, and the rows that infer none must draw no
24+
* finding either way.
25+
*/
26+
27+
import { describe, expect, it } from 'vitest';
28+
import {
29+
ACTION_BODY_CAPABILITY_UNDECLARED,
30+
ACTION_BODY_WRITE_CAPABILITY_UNDECLARED,
31+
HOOK_BODY_CAPABILITY_UNDECLARED,
32+
runAuthoringRules,
33+
} from '@objectstack/lint';
34+
import { extractLoweredBody, type ExtractedBody } from '../src/utils/extract-hook-body.js';
35+
36+
type Face = 'hook' | 'action';
37+
const FACES: readonly Face[] = ['hook', 'action'];
38+
39+
/** A handler built from source text, so `String(fn)` is exactly that text. */
40+
const handler = (src: string): ((...a: unknown[]) => unknown) =>
41+
new Function(`return (${src});`)() as (...a: unknown[]) => unknown;
42+
43+
/** The handler with its context parameter named `param`: `(param)` on a hook, `(input, param)` on an action. */
44+
const lower = (param: string, body: string, face: Face): ExtractedBody =>
45+
extractLoweredBody(handler(`async (${face === 'hook' ? param : `input, ${param}`}) => { ${body} }`), `${face} 'h'`, face);
46+
47+
/** The capability findings the publish rule draws on the body record the build writes. */
48+
const judged = (face: Face, extracted: Pick<ExtractedBody, 'source' | 'capabilities'>): string[] => {
49+
const body = { language: 'js', source: extracted.source, capabilities: extracted.capabilities };
50+
const stack =
51+
face === 'hook'
52+
? { hooks: [{ name: 'h', object: 'x', events: ['beforeUpdate'], handler: 'h_fn', body }] }
53+
: { actions: [{ name: 'a', label: 'A', objectName: 'x', type: 'script', target: 'x_a', body }] };
54+
return runAuthoringRules('build', { normalized: stack, parsed: stack, loweredHookRefs: new Set(['h_fn']) })
55+
.filter((f) => [HOOK_BODY_CAPABILITY_UNDECLARED, ACTION_BODY_CAPABILITY_UNDECLARED, ACTION_BODY_WRITE_CAPABILITY_UNDECLARED].includes(f.rule))
56+
.map((f) => `${f.rule}: ${f.message}`);
57+
};
58+
59+
/** Both sides read the body: the build infers `log`, the rule finds it ungranted and passes the lowered body. */
60+
function bothRead(param: string, body: string): void {
61+
for (const face of FACES) {
62+
const lowered = lower(param, body, face);
63+
expect(lowered.capabilities, `${face}: ${body}`).toEqual(['log']);
64+
expect(judged(face, { ...lowered, capabilities: [] }).some((f) => f.includes("'log'")), `${face}: the rule reads ${body}`).toBe(true);
65+
expect(judged(face, lowered), `${face}: ${body}`).toEqual([]);
66+
}
67+
}
68+
69+
/** Neither side reads the body: the build infers nothing and the rule finds nothing, granted or not. */
70+
function neitherReads(param: string, body: string): void {
71+
for (const face of FACES) {
72+
const lowered = lower(param, body, face);
73+
expect(lowered.capabilities, `${face}: ${body}`).toEqual([]);
74+
expect(judged(face, lowered), `${face}: ${body}`).toEqual([]);
75+
}
76+
}
77+
78+
describe('a catch binding is no declaration on either side (item 1)', () => {
79+
const rows: Array<{ what: string; param: string; body: string }> = [
80+
{ what: 'a destructure off the context parameter, the parameter re-bound by catch', param: 'e', body: "const { log } = e; try { void 0; } catch (e) { log.error('x'); }" },
81+
{ what: 'a member alias off the context parameter, the parameter re-bound by catch', param: 'c', body: "const l = c.log; try { l.info('x'); } catch (c) {}" },
82+
{ what: 'a local bound to ctx, re-bound by catch, then called', param: 'ctx', body: "const e = ctx; try { void 0; } catch (e) {} e.log.info('x');" },
83+
{ what: 'a member alias re-bound by catch (the class on main before this change)', param: 'ctx', body: "const log = ctx.log; try { void 0; } catch (log) {} log.info('x');" },
84+
// The over-read, on record: inside its block `e` is the caught value, and
85+
// both sides read it as the outer root, so the build grants a `log` the
86+
// call never uses. An over-grant is never a refusal.
87+
{ what: 'OVER-READ: the catch binding read inside its own block', param: 'ctx', body: "const e = ctx; try { void 0; } catch (e) { e.log.info('x'); }" },
88+
];
89+
for (const { what, param, body } of rows) it(what, () => bothRead(param, body));
90+
});
91+
92+
describe('every declarator of a list is read on both sides (item 2)', () => {
93+
const rows: Array<{ what: string; param: string; body: string }> = [
94+
{ what: 'a root and its member alias in one list', param: 'ctx', body: "const c = ctx, l = c.log; l.info('x');" },
95+
{ what: 'a root second in its list', param: 'ctx', body: "const a = 1, c = ctx; c.log.info('x');" },
96+
{ what: 'a destructure second in its list', param: 'ctx', body: "const a = 1, { log } = ctx; log.info('x');" },
97+
{ what: 'a member alias off the context parameter, second in its list', param: 'c', body: "const a = 1, l = c.log; l.info('x');" },
98+
];
99+
for (const { what, param, body } of rows) it(what, () => bothRead(param, body));
100+
});
101+
102+
describe('a root is read wherever JavaScript ends its initializer (items 4 and 5)', () => {
103+
const rows: Array<{ what: string; body: string }> = [
104+
{ what: 'the next line opens with ++', body: "let i = 0; const c = ctx\n++i\nc.log.info('x')" },
105+
{ what: 'the next line opens with --', body: "let i = 0; const c = ctx\n--i\nc.log.info('x')" },
106+
{ what: 'the next line opens with !', body: "let i = 0; const c = ctx\n!i\nc.log.info('x')" },
107+
{ what: 'the next line opens with ~', body: "let i = 0; const c = ctx\n~i\nc.log.info('x')" },
108+
{ what: 'the next line opens with a string literal', body: "const c = ctx\n'use x'\nc.log.info('x')" },
109+
{ what: 'the next line opens with a number literal', body: "const c = ctx\n5\nc.log.info('x')" },
110+
{ what: 'the next line opens with a block', body: "const c = ctx\n{ c.log.info('x') }" },
111+
{ what: 'the next line opens with a non-ASCII identifier', body: "let é = 0; const c = ctx\né++\nc.log.info('x')" },
112+
{ what: 'a lone carriage return ends the line', body: "const c = ctx\rc.log.info('x')" },
113+
{ what: 'U+2028 ends the line', body: "const c = ctx
c.log.info('x')" },
114+
{ what: 'U+2029 ends the line', body: "const c = ctx
c.log.info('x')" },
115+
// Item 5: `i++` steps `i`, not the root opening the next line.
116+
{ what: 'a step at the end of the line before the root is read', body: "let i = 0\nconst c = ctx\ni++\nc.log.info('x')" },
117+
{ what: 'a root at the end of a line, a step opening the next', body: "let i = 0\nconst c = ctx\nc\n++i\nc.log.info('x')" },
118+
];
119+
for (const { what, body } of rows) it(what, () => bothRead('ctx', body));
120+
});
121+
122+
describe('CONTROL: what assigns a root again, or declares it twice, roots nothing on either side (items 1 and 3)', () => {
123+
const rows: Array<{ what: string; param: string; body: string }> = [
124+
{ what: 'a destructuring assignment', param: 'ctx', body: "let c = ctx; [c] = [1]; c.log.info('x');" },
125+
{ what: 'a nested destructuring assignment', param: 'ctx', body: "let c = ctx; [[c]] = [[1]]; c.log.info('x');" },
126+
{ what: 'an object destructuring assignment', param: 'ctx', body: "let c = ctx; ({ c } = { c: 1 }); c.log.info('x');" },
127+
{ what: 'a renamed object destructuring assignment', param: 'ctx', body: "let c = ctx; ({ k: c } = { k: 1 }); c.log.info('x');" },
128+
{ what: 'a rest element in a destructuring assignment', param: 'ctx', body: "let c = ctx; [...c] = []; c.log.info('x');" },
129+
{ what: 'a for-of head', param: 'ctx', body: "let c = ctx; for (c of [1]) { void 0; } c.log.info('x');" },
130+
{ what: 'a for-in head', param: 'ctx', body: "let c = ctx; for (c in {}) { void 0; } c.log.info('x');" },
131+
{ what: 'a for-of head destructuring', param: 'ctx', body: "let c = ctx; for ([c] of [[1]]) { void 0; } c.log.info('x');" },
132+
{ what: 'a member alias assigned again by destructuring', param: 'ctx', body: "let l = ctx.log; [l] = [null]; l.info('x');" },
133+
{ what: 'the context parameter assigned again by destructuring', param: 'c', body: "[c] = [1]; const l = c.log; l.info('x');" },
134+
// Scope-blind, on both sides: an array pattern in a nested block is a second declaration.
135+
{ what: 'a root declared again by an array pattern in a nested block', param: 'ctx', body: "const c = ctx; { const [c] = [1]; void c; } c.log.info('x');" },
136+
{ what: 'a root declared again by a nested array pattern', param: 'ctx', body: "const c = ctx; { const [a, [c]] = [1, [2]]; void a; void c; } c.log.info('x');" },
137+
{ what: 'a root declared again later in a declarator list', param: 'ctx', body: "const c = ctx; { let a, c; void a; void c; } c.log.info('x');" },
138+
{ what: 'a member alias declared again by an array pattern', param: 'ctx', body: "const l = ctx.log; { const [l] = [1]; void l; } l.info('x');" },
139+
// A member alias is the member itself, as a root is `ctx` itself.
140+
{ what: 'a member alias bound to an expression on the member', param: 'ctx', body: "const l = ctx.log || null; l.info('x');" },
141+
{ what: 'a destructure off an expression on ctx', param: 'ctx', body: "const { log } = ctx || {}; log.info('x');" },
142+
];
143+
for (const { what, param, body } of rows) it(what, () => neitherReads(param, body));
144+
});

0 commit comments

Comments
 (0)