Skip to content

Commit 69cde75

Browse files
committed
fix(lint): keep literal table input errors visible beside references
Try string and one-item-list placeholders per reference instead of dropping any field that holds one, so in-list references are still checked and an invalid literal condition still reports. Only a field that is a single whole reference is deferred to runtime.
1 parent a560d00 commit 69cde75

2 files changed

Lines changed: 103 additions & 50 deletions

File tree

‎apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts‎

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,30 +276,53 @@ describe('standalone table diagnostics against the actual Table block', () => {
276276
expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other'])
277277
})
278278

279-
it.each([
280-
['an in-list', '{"field":"name","op":"in","value":<start.names>}'],
281-
['a whole-field', '<start.filter>'],
282-
])(
283-
'notes %s filter reference as runtime-resolved and still checks literal row data',
284-
async (_shape, filter) => {
285-
setGraph(
286-
block('write', {
287-
operation: 'update_rows_by_filter',
288-
bulkFilterMode: 'json',
289-
filter,
290-
data: '{"name":"Ada","missing":1}',
291-
})
292-
)
293-
const result = await lint()
294-
expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([])
295-
expect(result.notes).toContain(
296-
'Table inputs in block "write" depend on references resolved at runtime and were not checked: filterInput.'
297-
)
298-
expect(
299-
result.unresolvedReferences.filter((reference) => reference.kind === 'resource')
300-
).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })])
301-
}
302-
)
279+
it('checks predicate fields around an in-list reference', async () => {
280+
setGraph(
281+
block('query', {
282+
filter:
283+
'{"all":[{"field":"other","op":"in","value":<start.names>},{"field":"name","op":"eq","value":<start.name>}]}',
284+
})
285+
)
286+
const result = await lint()
287+
expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([])
288+
expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other'])
289+
})
290+
291+
it('notes a whole-field filter reference as runtime-resolved and still checks row data', async () => {
292+
setGraph(
293+
block('write', {
294+
operation: 'update_rows_by_filter',
295+
bulkFilterMode: 'json',
296+
filter: '<start.filter>',
297+
data: '{"name":"Ada","missing":1}',
298+
})
299+
)
300+
const result = await lint()
301+
expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([])
302+
expect(result.notes).toContain(
303+
'Table inputs in block "write" depend on references resolved at runtime and were not checked: filterInput.'
304+
)
305+
expect(
306+
result.unresolvedReferences.filter((reference) => reference.kind === 'resource')
307+
).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })])
308+
})
309+
310+
it('still reports an invalid literal condition beside a reference', async () => {
311+
setGraph(
312+
block('query', {
313+
filter:
314+
'{"all":[{"field":"name","op":"eq","value":<start.n>},{"field":"name","op":"in","value":"not a list"}]}',
315+
})
316+
)
317+
const result = await lint()
318+
expect(result.notes).toEqual(
319+
expect.arrayContaining([
320+
expect.stringContaining(
321+
'Table input checks in block "query" could not complete before runtime:'
322+
),
323+
])
324+
)
325+
})
303326
})
304327

305328
it.each([

‎apps/sim/lib/workflows/application/read-workflow-lint.ts‎

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,42 +258,72 @@ function hasRuntimeReference(value: string): boolean {
258258
/**
259259
* Outside Function code a reference resolves to JSON text (objects, arrays, numbers, booleans), so
260260
* `{"name": <start.name>}` or a whole-field `<start.row>` is valid JSON at runtime but not before.
261-
* Quoting each reference written outside a JSON string lets the Table block's own transform read
262-
* the literal parts, and the quoted references then reach the existing runtime-resolution notes.
263-
* When the transform's shape validation rejects a quoted reference (a filter `in` list, a
264-
* whole-field filter), the fields that only parse once quoted are left out and returned as
265-
* `runtimeFields`; malformed literal JSON still throws. Only this lint-local copy is rewritten;
266-
* references to string outputs left unquoted are reported by `collectUnquotedJsonStringReferences`.
261+
* Each reference written outside a JSON string is replaced by a placeholder so the Table block's
262+
* own transform reads the literal parts: a quoted string, or a one-item list for slots such as a
263+
* filter `in`. Every combination is tried while there are few references (otherwise all-string
264+
* and all-list). A field that is a single whole reference has no literal part, so if no
265+
* combination fits it is left out and returned in `runtimeFields`. Any other failure, such as
266+
* malformed JSON or an invalid literal condition, is rethrown. Only this lint-local copy is
267+
* rewritten; references to string outputs left unquoted are reported by
268+
* `collectUnquotedJsonStringReferences`.
267269
*/
268270
function normalizeTableInputs(
269271
transform: (params: Record<string, unknown>) => Record<string, unknown>,
270272
params: Record<string, unknown>
271273
): { normalized: Record<string, unknown>; runtimeFields: string[] } {
272-
const quoted: Record<string, unknown> = {}
273-
const runtimeFields: string[] = []
274+
let hasReference = false
275+
const wholeReferenceFields: string[] = []
274276
for (const [key, value] of Object.entries(params)) {
275-
quoted[key] = typeof value === 'string' ? quoteBareReferences(value) : value
276-
if (quoted[key] !== value && parsesAsJson(quoted[key])) runtimeFields.push(key)
277+
if (typeof value !== 'string') continue
278+
const trimmed = value.trim()
279+
const tokens = findWorkflowReferenceTokens(trimmed)
280+
if (tokens.length === 0) continue
281+
hasReference = true
282+
if (tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === trimmed.length) {
283+
wholeReferenceFields.push(key)
284+
}
277285
}
278-
try {
279-
return { normalized: transform(quoted), runtimeFields: [] }
280-
} catch (error) {
281-
if (runtimeFields.length === 0) throw error
282-
for (const key of runtimeFields) delete quoted[key]
283-
return { normalized: transform(quoted), runtimeFields }
286+
if (!hasReference) return { normalized: transform(params), runtimeFields: [] }
287+
const tiers = wholeReferenceFields.length > 0 ? [[], wholeReferenceFields] : [[]]
288+
let firstError: unknown
289+
for (const runtimeFields of tiers) {
290+
const withPlaceholders = (asList: (ordinal: number) => boolean) => {
291+
let ordinal = 0
292+
const candidate: Record<string, unknown> = {}
293+
for (const [key, value] of Object.entries(params)) {
294+
if (runtimeFields.includes(key)) continue
295+
candidate[key] =
296+
typeof value === 'string'
297+
? replaceBareReferences(value, (reference) =>
298+
asList(ordinal++) ? `[${JSON.stringify(reference)}]` : JSON.stringify(reference)
299+
)
300+
: value
301+
}
302+
return { candidate, count: ordinal }
303+
}
304+
const { count } = withPlaceholders(() => false)
305+
const assignments: Array<(ordinal: number) => boolean> =
306+
count <= MAX_PLACEHOLDER_SEARCH_REFERENCES
307+
? Array.from(
308+
{ length: 2 ** count },
309+
(_, mask) => (ordinal) => ((mask >> ordinal) & 1) === 1
310+
)
311+
: [() => false, () => true]
312+
for (const asList of assignments) {
313+
try {
314+
return { normalized: transform(withPlaceholders(asList).candidate), runtimeFields }
315+
} catch (error) {
316+
firstError ??= error
317+
}
318+
}
284319
}
320+
throw firstError
285321
}
286322

287-
function parsesAsJson(value: unknown): boolean {
288-
try {
289-
JSON.parse(String(value))
290-
return true
291-
} catch {
292-
return false
293-
}
294-
}
323+
/** Bounds the placeholder search to 2^6 pure transform calls per Table block. */
324+
const MAX_PLACEHOLDER_SEARCH_REFERENCES = 6
295325

296-
function quoteBareReferences(json: string): string {
326+
function replaceBareReferences(json: string, placeholder: (reference: string) => string): string {
297327
const tokens = findWorkflowReferenceTokens(json)
298328
if (tokens.length === 0) return json
299329
let result = ''
@@ -306,7 +336,7 @@ function quoteBareReferences(json: string): string {
306336
else if (char === '"') inString = !inString
307337
}
308338
result += json.slice(cursor, token.start)
309-
result += inString ? token.value : JSON.stringify(token.value)
339+
result += inString ? token.value : placeholder(token.value)
310340
cursor = token.end
311341
}
312342
return result + json.slice(cursor)

0 commit comments

Comments
 (0)