Skip to content

Commit 1acfa82

Browse files
committed
fix(audits): track helper argument positions, destructured rests, helper results, and shadowed names in check:mapper-operation; drop _operation strips
1 parent 9ccc360 commit 1acfa82

8 files changed

Lines changed: 128 additions & 49 deletions

File tree

‎apps/sim/blocks/blocks/box.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export const BoxBlock = {
534534
if (normalizedFile) {
535535
params.file = normalizedFile
536536
}
537-
const { credential, operation: _operation, ...rest } = params
537+
const { credential, ...rest } = params
538538
const operation = mapperOperation(params, context)
539539

540540
const baseParams: Record<string, unknown> = {

‎apps/sim/blocks/blocks/hubspot.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,6 @@ Return ONLY the JSON array of property names - no explanations, no markdown, no
16351635
params: (params, context) => {
16361636
const {
16371637
oauthCredential,
1638-
operation: _operation,
16391638
propertiesToSet,
16401639
properties,
16411640
searchProperties,

‎apps/sim/blocks/blocks/intercom.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ type IntercomParamsMapper = NonNullable<NonNullable<BlockConfig['tools']['config
4343
/** Maps Intercom block fields to tool params; shared by every Intercom block version. */
4444
const mapIntercomParams: IntercomParamsMapper = (params, context) => {
4545
const {
46-
operation: _operation,
4746
message_type_msg,
4847
company_name,
4948
contact_company_id,

‎apps/sim/blocks/blocks/notion.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ export const NotionBlock: BlockConfig = {
629629
params: (params, context) => {
630630
const {
631631
oauthCredential,
632-
operation: _operation,
633632
properties,
634633
filter,
635634
sorts,

‎apps/sim/blocks/blocks/servicenow.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,6 @@ Output: {"state": "2", "assigned_to": "john.doe", "work_notes": "Assigned and st
16301630
tool: (params) => params.operation,
16311631
params: (params, context?: BlockToolParamsContext) => {
16321632
const {
1633-
operation: _operation,
16341633
fields,
16351634
readFields,
16361635
returnFields,

‎apps/sim/blocks/blocks/workday.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,7 @@ Output: {"Marital_Status_Reference":{"ID":{"attributes":{"wd:type":"Marital_Stat
410410
config: {
411411
tool: (params) => `workday_${params.operation}`,
412412
params: (params, context) => {
413-
const {
414-
operation: _operation,
415-
orgType,
416-
fields,
417-
jobProfileId,
418-
locationId,
419-
supervisoryOrgId,
420-
...rest
421-
} = params
413+
const { orgType, fields, jobProfileId, locationId, supervisoryOrgId, ...rest } = params
422414
/** Agent tool calls carry the stored operation only on `context`, not in `params`. */
423415
const operation = mapperOperation(params, context)
424416

‎scripts/check-mapper-operation.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,47 @@ describe('countMapperOperationReads', () => {
4545
expect(count(block('mapX', `const mapX = (params) => (params.operation ? {} : {})`))).toBe(1)
4646
})
4747

48+
it('tracks the argument position a helper receives params at, a destructured rest, and helper results', () => {
49+
expect(
50+
count(
51+
block(
52+
'(params) => build({}, params)',
53+
`function build(options, input) { return input.operation === 'a' ? options : {} }`
54+
)
55+
)
56+
).toBe(1)
57+
expect(
58+
count(
59+
block(
60+
'(params) => build(params, {})',
61+
`function build(input, options) { return options.operation === 'a' ? input : {} }`
62+
)
63+
)
64+
).toBe(0)
65+
expect(count(block(`({ credential, ...rest }) => (rest.operation === 'a' ? {} : {})`))).toBe(1)
66+
expect(
67+
count(
68+
block(
69+
`(params) => { const normalized = normalize(params); return normalized.operation === 'a' ? {} : {} }`,
70+
'function normalize(input) { return { ...input } }'
71+
)
72+
)
73+
).toBe(1)
74+
})
75+
76+
it('does not count a nested callback parameter that reuses an alias name', () => {
77+
expect(
78+
count(block(`(params) => ({ items: params.items.map((params) => params.operation) })`))
79+
).toBe(0)
80+
expect(
81+
count(
82+
block(
83+
`(params) => { const { operation, ...rest } = params; return { list: rest.items.map((operation) => operation) } }`
84+
)
85+
)
86+
).toBe(0)
87+
})
88+
4889
it('does not flag strip-only destructuring, the helper, or the tool selector', () => {
4990
expect(count(block(`(params) => { const { operation, ...rest } = params; return rest }`))).toBe(
5091
0

‎scripts/check-mapper-operation.ts‎

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,27 @@ function findLocalFn(source: ts.SourceFile, name: string): Fn | undefined {
9797
return found
9898
}
9999

100-
/** Identifier uses of `name` inside `scope`, excluding declarations and property-name positions. */
100+
/** Names a binding pattern or identifier declares. */
101+
function boundNames(name: ts.BindingName, into: Set<string> = new Set()): Set<string> {
102+
if (ts.isIdentifier(name)) into.add(name.text)
103+
else
104+
for (const element of name.elements)
105+
if (!ts.isOmittedExpression(element)) boundNames(element.name, into)
106+
return into
107+
}
108+
109+
/** Names a nested function's own parameters declare, which shadow the outer ones in its body. */
110+
function shadowedBy(node: ts.Node): Set<string> {
111+
const names = new Set<string>()
112+
if (isFn(node)) for (const parameter of node.parameters) boundNames(parameter.name, names)
113+
return names
114+
}
115+
116+
/** Identifier uses of `name` inside `scope`, excluding declarations, property names, and shadowed scopes. */
101117
function referencesOf(scope: ts.Node, name: string, declaration: ts.Node): number {
102118
let count = 0
103119
const visit = (node: ts.Node) => {
120+
if (node !== scope && shadowedBy(node).has(name)) return
104121
if (ts.isIdentifier(node) && node.text === name && node !== declaration) {
105122
const parent = node.parent
106123
const isPropertyName =
@@ -143,62 +160,95 @@ function restAlias(pattern: ts.ObjectBindingPattern): string | undefined {
143160
return rest && ts.isIdentifier(rest.name) ? rest.name.text : undefined
144161
}
145162

146-
function operationReads(source: ts.SourceFile, fn: Fn, seen: Set<Fn>): number {
147-
if (seen.has(fn)) return 0
148-
seen.add(fn)
149-
const first = fn.parameters[0]
150-
if (!first) return 0
151-
if (ts.isObjectBindingPattern(first.name)) return usedOperationBindings(first.name)
152-
if (!ts.isIdentifier(first.name) || !fn.body) return 0
163+
/**
164+
* Counts operation reads from parameter `index` of `fn`: through aliases of it, and through
165+
* same-file helpers it is handed to (at whichever argument position), whose returned value is
166+
* treated as an alias too. Names a nested function rebinds are not aliases inside it.
167+
*/
168+
function operationReads(source: ts.SourceFile, fn: Fn, index: number, seen: Set<string>): number {
169+
const key = `${fn.pos}:${index}`
170+
if (seen.has(key)) return 0
171+
seen.add(key)
172+
const parameter = fn.parameters[index]
173+
if (!parameter || !fn.body) return 0
153174

154-
const aliases = new Set([first.name.text])
155175
let reads = 0
156-
const visit = (node: ts.Node) => {
176+
const initial = new Set<string>()
177+
if (ts.isIdentifier(parameter.name)) initial.add(parameter.name.text)
178+
else if (ts.isObjectBindingPattern(parameter.name)) {
179+
reads += usedOperationBindings(parameter.name)
180+
const rest = restAlias(parameter.name)
181+
if (rest) initial.add(rest)
182+
}
183+
if (initial.size === 0) return reads
184+
185+
const isAlias = (expression: ts.Expression, aliases: Set<string>) => {
186+
const value = unwrap(expression)
187+
return ts.isIdentifier(value) && aliases.has(value.text)
188+
}
189+
190+
/** Same-file helper calls that receive an alias; reads inside them are counted once per position. */
191+
const helperCall = (node: ts.Expression, aliases: Set<string>): boolean => {
192+
const call = unwrap(node)
193+
if (!ts.isCallExpression(call) || !ts.isIdentifier(call.expression)) return false
194+
const helper = findLocalFn(source, call.expression.text)
195+
if (!helper) return false
196+
let passed = false
197+
call.arguments.forEach((argument, position) => {
198+
if (!isAlias(argument, aliases)) return
199+
passed = true
200+
reads += operationReads(source, helper, position, seen)
201+
})
202+
return passed
203+
}
204+
205+
const visit = (node: ts.Node, inherited: Set<string>) => {
206+
let aliases = inherited
207+
if (node !== fn) {
208+
const shadowed = shadowedBy(node)
209+
if (shadowed.size) aliases = new Set([...inherited].filter((name) => !shadowed.has(name)))
210+
}
157211
if (ts.isVariableDeclaration(node) && node.initializer) {
158212
const init = unwrap(node.initializer)
159-
const fromAlias = ts.isIdentifier(init) && aliases.has(init.text)
213+
const fromAlias = isAlias(init, aliases)
160214
const spreadsAlias =
161215
ts.isObjectLiteralExpression(init) &&
162-
init.properties.some((property) => {
163-
if (!ts.isSpreadAssignment(property)) return false
164-
const spread = unwrap(property.expression)
165-
return ts.isIdentifier(spread) && aliases.has(spread.text)
166-
})
167-
if (fromAlias && ts.isObjectBindingPattern(node.name)) {
216+
init.properties.some(
217+
(property) => ts.isSpreadAssignment(property) && isAlias(property.expression, aliases)
218+
)
219+
const fromHelper = helperCall(init, aliases)
220+
if ((fromAlias || fromHelper) && ts.isObjectBindingPattern(node.name)) {
168221
reads += usedOperationBindings(node.name)
169222
const rest = restAlias(node.name)
170-
if (rest) aliases.add(rest)
171-
} else if ((fromAlias || spreadsAlias) && ts.isIdentifier(node.name)) {
172-
aliases.add(node.name.text)
223+
if (rest) inherited.add(rest)
224+
} else if ((fromAlias || spreadsAlias || fromHelper) && ts.isIdentifier(node.name)) {
225+
inherited.add(node.name.text)
226+
}
227+
if (fromHelper) {
228+
visit(node.name, aliases)
229+
return
173230
}
174231
}
175232
if (ts.isPropertyAccessExpression(node) && node.name.text === 'operation') {
176-
const target = unwrap(node.expression)
177-
if (ts.isIdentifier(target) && aliases.has(target.text)) reads++
233+
if (isAlias(node.expression, aliases)) reads++
178234
}
179235
if (ts.isElementAccessExpression(node)) {
180-
const target = unwrap(node.expression)
181236
const argument = node.argumentExpression
182237
if (
183-
ts.isIdentifier(target) &&
184-
aliases.has(target.text) &&
238+
isAlias(node.expression, aliases) &&
185239
ts.isStringLiteralLike(argument) &&
186240
argument.text === 'operation'
187241
) {
188242
reads++
189243
}
190244
}
191-
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
192-
const passesParams = node.arguments.some((argument) => {
193-
const value = unwrap(argument)
194-
return ts.isIdentifier(value) && aliases.has(value.text)
195-
})
196-
const helper = passesParams ? findLocalFn(source, node.expression.text) : undefined
197-
if (helper) reads += operationReads(source, helper, seen)
245+
if (ts.isCallExpression(node) && helperCall(node, aliases)) {
246+
for (const argument of node.arguments) visit(argument, aliases)
247+
return
198248
}
199-
ts.forEachChild(node, visit)
249+
ts.forEachChild(node, (child) => visit(child, aliases))
200250
}
201-
visit(fn.body)
251+
visit(fn.body, initial)
202252
return reads
203253
}
204254

@@ -228,7 +278,7 @@ export function countMapperOperationReads(fileName: string, text: string): numbe
228278
} else if (ts.isShorthandPropertyAssignment(property)) {
229279
mapper = findLocalFn(source, property.name.text)
230280
}
231-
if (mapper) reads += operationReads(source, mapper, new Set())
281+
if (mapper) reads += operationReads(source, mapper, 0, new Set())
232282
}
233283
}
234284
ts.forEachChild(node, visit)

0 commit comments

Comments
 (0)