Skip to content

Commit 52e3cec

Browse files
committed
refactor(metadata-protocol): host-split reads the per-member series' generic and plain stubs as stubs, and refuses to move a stub again
Claude-Session: https://claude.ai/code/session_01ADHkmR4MjTzeUv5x6tNkbk Co-authored-by: Claude <noreply@anthropic.com>
1 parent c88467d commit 52e3cec

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

‎packages/metadata-protocol/scripts/host-split.mjs‎

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ export function planMove(source, member, moduleFile, className = CLASS_NAME) {
304304
if (!cls) return { refusals: [{ line: 0, reason: `no class ${className}` }] };
305305
const method = methodOf(cls, member);
306306
if (!method) return { refusals: [{ line: 0, reason: `no instance method ${member} with a body` }] };
307+
const already = stubKindOf(method);
308+
if (already) return { refusals: [{ line: 0, reason: `already a stub ('${already}'): its body moved before` }] };
307309
const table = memberTable(cls);
308310
const { sites, refusals, used } = rewriteSites(sf, method, table, className);
309311
const sig = signatureParts(sf, method);
@@ -526,6 +528,41 @@ export function checkMove({ baseSource, headSource, moduleSource, moduleFile, me
526528

527529
// ── Survey ──────────────────────────────────────────────────────────────────
528530

531+
/**
532+
* The function a per-member stub calls: `fn` in `fn.call(host, …)`, and in the
533+
* generic spelling `(fn<R>).call(host, …)` (an instantiation expression in
534+
* parentheses), which the per-member series wrote for a generic method.
535+
*/
536+
function calleeOf(node) {
537+
let n = node;
538+
while (ts.isParenthesizedExpression(n) || ts.isExpressionWithTypeArguments(n)) n = n.expression;
539+
return n;
540+
}
541+
542+
/**
543+
* Is this method already a stub — its body moved before? One of three shapes,
544+
* its last statement returning (awaited or not):
545+
* 'call' — `fn.call(host, …)` or `(fn<R>).call(host, …)` (the per-member series);
546+
* 'host' — `fn(protocolHost(this), …)` (this tool);
547+
* 'plain' — `fn(<its own parameters, in order>)`, `fn` named as the method:
548+
* a body that read no member moved as a plain function.
549+
*/
550+
export function stubKindOf(m) {
551+
const last = m.body?.statements[m.body.statements.length - 1];
552+
let call = last && ts.isReturnStatement(last) ? last.expression : undefined;
553+
if (call && ts.isAwaitExpression(call)) call = call.expression;
554+
if (!call || !ts.isCallExpression(call)) return undefined;
555+
if (ts.isPropertyAccessExpression(call.expression) && call.expression.name.text === 'call'
556+
&& ts.isIdentifier(calleeOf(call.expression.expression)) && !!call.arguments[0] && ts.isIdentifier(call.arguments[0])) return 'call';
557+
if (call.arguments[0] && ts.isCallExpression(call.arguments[0]) && ts.isIdentifier(call.arguments[0].expression)
558+
&& call.arguments[0].expression.text === HOST_OF) return 'host';
559+
const name = nameOf(m);
560+
const params = m.parameters.map((p) => (ts.isIdentifier(p.name) ? p.name.text : undefined));
561+
if (ts.isIdentifier(call.expression) && call.expression.text === name && call.arguments.length === params.length
562+
&& call.arguments.every((x, i) => ts.isIdentifier(x) && x.text === params[i])) return 'plain';
563+
return undefined;
564+
}
565+
529566
/** Every instance method with a body: its line count, sites and refusals under the set. */
530567
export function survey(source, className = CLASS_NAME) {
531568
const sf = parse('protocol.ts', source);
@@ -536,14 +573,7 @@ export function survey(source, className = CLASS_NAME) {
536573
if (!ts.isMethodDeclaration(m) || !m.body || hasModifier(m, ts.SyntaxKind.StaticKeyword)) continue;
537574
const name = nameOf(m);
538575
const { sites, refusals } = rewriteSites(sf, m, table, className);
539-
const last = m.body.statements[m.body.statements.length - 1];
540-
let call = last && ts.isReturnStatement(last) ? last.expression : undefined;
541-
if (call && ts.isAwaitExpression(call)) call = call.expression;
542-
const stub = !!call && ts.isCallExpression(call) && ts.isPropertyAccessExpression(call.expression)
543-
&& call.expression.name.text === 'call' && ts.isIdentifier(call.expression.expression)
544-
&& !!call.arguments[0] && ts.isIdentifier(call.arguments[0]) ? 'call'
545-
: !!call && ts.isCallExpression(call) && call.arguments[0] && ts.isCallExpression(call.arguments[0])
546-
&& ts.isIdentifier(call.arguments[0].expression) && call.arguments[0].expression.text === HOST_OF ? 'host' : undefined;
576+
const stub = stubKindOf(m);
547577
const lines = sf.getLineAndCharacterOfPosition(m.getEnd()).line - sf.getLineAndCharacterOfPosition(m.getStart(sf)).line + 1;
548578
rows.push({ name, lines, sites: sites.length, refusals, stub });
549579
}
@@ -593,7 +623,7 @@ function main(argv) {
593623
const all = survey(readFileSync(PROTOCOL, 'utf8'));
594624
const { clashes } = all;
595625
const stubs = all.rows.filter((r) => r.stub);
596-
console.log(`already stubs: ${stubs.length} (${stubs.filter((r) => r.stub === 'call').length} by \`fn.call(host, …)\` from the per-member series, ${stubs.filter((r) => r.stub === 'host').length} by \`fn(${HOST_OF}(this), …)\`)`);
626+
console.log(`already stubs: ${stubs.length} (${stubs.filter((r) => r.stub === 'call').length} by \`fn.call(host, …)\` from the per-member series, ${stubs.filter((r) => r.stub === 'host').length} by \`fn(${HOST_OF}(this), …)\`, ${stubs.filter((r) => r.stub === 'plain').length} by a plain \`fn(<params>)\`)`);
597627
const rows = all.rows.filter((r) => !r.stub);
598628
const movable = rows.filter((r) => !r.refusals.length);
599629
const reasons = new Map();

‎packages/metadata-protocol/scripts/host-split.test.mjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ line two \${k}\`;
6060
destructured({ a }: { a: number }) { return a + this.count; }
6161
hostAsPropertyName() { return { host: this.count }.host; }
6262
stubbed(k: string) { const self = { count: this.count }; return stubbedBody.call(self, k); }
63+
private plainStub(k: string, n: number) { return plainStub(k, n); }
64+
stubbedGeneric<R>(k: R) { const host = { count: this.count }; return (stubbedGenericBody<R>).call(host, k); }
6365
}
6466
`;
6567

@@ -150,6 +152,11 @@ describe('host-split: the move', () => {
150152
expect(plan.fnText).toBeUndefined();
151153
});
152154

155+
it.each(['stubbed', 'stubbedGeneric', 'plainStub'])('refuses to move %s again: it is already a stub', (member) => {
156+
const plan = planMove(BASE, member, 'protocol-target.ts');
157+
expect(plan.refusals.map((r) => r.reason).join(' | ')).toContain('already a stub');
158+
});
159+
153160
it('does not refuse `host` as a property name', () => {
154161
expect(planMove(BASE, 'hostAsPropertyName', 'protocol-target.ts').refusals).toEqual([]);
155162
});
@@ -159,6 +166,11 @@ describe('host-split: the move', () => {
159166
const by = Object.fromEntries(rows.map((r) => [r.name, r]));
160167
expect(clashes).toEqual([]);
161168
expect(by.stubbed.stub).toBe('call');
169+
// The per-member series' generic spelling `(fn<R>).call(host, …)` is a stub too,
170+
// not a body that binds `host`.
171+
expect(by.stubbedGeneric.stub).toBe('call');
172+
// A body that read no member and moved as a plain function: `return fn(<params>)`.
173+
expect(by.plainStub.stub).toBe('plain');
162174
expect(by.target.refusals).toEqual([]);
163175
expect(by.bareThis.refusals).toHaveLength(1);
164176
const { head } = move();

0 commit comments

Comments
 (0)