chore(ratchets): charge calls through an alias, not the alias binding

Decider ruling B on review finding C6: binding a plain alias of a gated call
counted as a use, so an alias refactor failed every collector-based ratchet.
Conditional/fallback aliases and exported bindings stay charged.
This commit is contained in:
Brennan Benson
2026-11-08 11:26:48 -08:00
parent 2047da846d
commit 4c62b2a92c
2 changed files with 41 additions and 8 deletions
@@ -77,12 +77,12 @@ describe('main sync-spawn ratchet', () => {
],
[
"const { execSync } = require('child_process')\nconst run = execSync\nrun('a')",
2,
1,
['execSync']
],
[
"function f() { const run = cp.execSync; run('a') }\nconst cp = require('child_process')",
2,
1,
['execSync']
],
[
@@ -91,10 +91,18 @@ describe('main sync-spawn ratchet', () => {
['execFileSync']
],
[
"import { runProcessSync } from '@orca/process-host'\nconst run = runProcessSync<string>",
"import { runProcessSync } from '@orca/process-host'\nconst run = runProcessSync<string>\nrun(a)",
1,
['runProcessSync']
]
],
// Binding an alias is setup; each call through it is a use, so an alias refactor keeps the count.
[
"import { execSync } from 'child_process'\nconst run = execSync\nrun('a')\nrun('b')",
2,
['execSync']
],
["import { execSync } from 'child_process'\nexport const run = execSync", 1, ['execSync']],
["import { execSync } from 'child_process'\nconst out = execSync('a')", 1, ['execSync']]
])('counts %s', (source, count, symbols) => {
expect(uses(source)).toEqual({ count, symbols })
})
@@ -38,6 +38,15 @@ function isValueReference(node) {
)
}
function isExportedDeclaration(declaration) {
const statement = declaration.parent?.parent
return (
!!statement &&
ts.isVariableStatement(statement) &&
!!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)
)
}
/**
* Each family lists module specifiers (packages, or repo paths without extension) and the gated
* exports to count, each with advice for the failure message.
@@ -72,7 +81,7 @@ export function createGatedCallCollector(families, ratchetName) {
)
}
// Count each gated value reference, alias use and re-export; imports alone do not count.
// Count each gated value reference, alias use and re-export; imports and plain alias bindings alone do not count.
// File-wide, not scope-aware: computed specifiers/members and non-const holder flows are unresolved; local wrappers are not traced.
function collectUses(file, contents) {
// Escaped spellings and relative imports inside gated modules still need the full analysis.
@@ -197,7 +206,7 @@ export function createGatedCallCollector(families, ratchetName) {
identifiers.push(node)
} else if (ts.isVariableDeclaration(node) && node.initializer) {
if (ts.isIdentifier(node.name)) {
aliases.push({ name: node.name.text, initializer: node.initializer })
aliases.push({ name: node.name.text, initializer: node.initializer, declaration: node })
} else if (ts.isObjectBindingPattern(node.name)) {
destructures.push(node)
}
@@ -236,14 +245,30 @@ export function createGatedCallCollector(families, ratchetName) {
}
}
}
// Binding a tracked alias is setup, not a use: its calls are charged instead, so an alias refactor
// keeps the count. Conditional/fallback aliases and exported bindings stay charged (conservative).
const aliasSetup = new Set()
for (const { name, initializer, declaration } of aliases) {
const target = unwrap(initializer)
if (
functions.has(name) &&
(ts.isIdentifier(target) ||
ts.isPropertyAccessExpression(target) ||
ts.isElementAccessExpression(target)) &&
gatedFunction(target) &&
!isExportedDeclaration(declaration)
) {
aliasSetup.add(target)
}
}
for (const node of members) {
const name = propertyName(node)
if (moduleSymbols(node.expression)?.has(name)) {
if (!aliasSetup.has(node) && moduleSymbols(node.expression)?.has(name)) {
recordUse(name)
}
}
for (const node of identifiers) {
const name = functions.get(node.text)
const name = aliasSetup.has(node) ? null : functions.get(node.text)
if (name) {
recordUse(name)
}