Prism · Transform

$walk

Visits every node of a tree, at any depth, and replaces a node with the first rule whose when holds for it. Children are rewritten before their parent unless order is pre. A walk visits objects and lists as well as leaves, so a rule for strings checks the type first.

The source
{
  document: {
    component: 'Stack',
    children: [
      { component: 'Text', children: '{{$.q}}' },
      { component: 'Field', props: { value: '$.q' } },
    ],
  },
}
The config
{
  $walk: {
    over: { $ref: '$.document' },
    as: 'n',
    rules: [
      {
        when: { $eq: [{ $var: 'n' }, { $const: '$.q' }] },
        then: { $const: '$.search' },
      },
      {
        when: { $eq: [{ $var: 'n' }, { $const: '{{$.q}}' }] },
        then: { $const: '{{$.search}}' },
      },
    ],
  },
}
What comes out
{
  component: 'Stack',
  children: [
    { component: 'Text', children: '{{$.search}}' },
    { component: 'Field', props: { value: '$.search' } },
  ],
}
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.