When using GritQL, rewrites are typically automatically applied on any AST nodes that are matched in the original text.
However, there are cases where you may want to preserve the original code while inserting a modified version of it. This can be done by using the text()
function to capture a detached copy of the original code.
For example, consider the following code:
PATTERN
`const $identifier = $obj` as $target where { // Capture an immutable copy of $target $original = text($target), // Apply rewrites as normal $identifier => `b`, $obj <: contains `foo` => `bar` } // replace $target with $original, and insert $target after it => `$original $target`
INPUTOUTPUT
const a = { something: foo }
const a = { something: foo } const b = { something: bar }