@@ -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 */
268270function 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