{"record":{"id":"6b17a6c136834b56","repo":"facebook/flow","slug":"expected-to-find-the-target-target-type-on-th","errorCode":null,"errorMessage":"Expected to find the target \"${target.type}\" on the \"${result.parent.type}.${result.key}\", but found a different node. This likely means that you attempted to mutate around the target after it was deleted/replaced.","messagePattern":"Expected to find the target \"(.+?)\" on the \"(.+?)\\.(.+?)\", but found a different node\\. This likely means that you attempted to mutate around the target after it was deleted/replaced\\.","errorType":"exception","errorClass":"InvalidStatementError","httpStatus":null,"severity":"error","filePath":"packages/flow-transform/src/transform/mutations/utils/getStatementParent.js","lineNumber":142,"sourceCode":"          key: 'body',\n          targetIndex: getAssertedIndex('body', parent.body),\n        };\n      }\n    }\n\n    throw new InvalidStatementError(\n      `Expected to find a valid statement parent, but found a parent of type \"${parent.type}\".`,\n    );\n  })();\n\n  if (\n    // array insertions are already validated by the getAssertedIndex function\n    result.targetIndex == null &&\n    // $FlowExpectedError[prop-missing]\n    // $FlowFixMe[invalid-compare]\n    result.parent[result.key] !== target\n  ) {\n    throw new InvalidStatementError(\n      `Expected to find the target \"${target.type}\" on the \"${result.parent.type}.${result.key}\", but found a different node. ` +\n        'This likely means that you attempted to mutate around the target after it was deleted/replaced.',\n    );\n  }\n\n  return result;\n}\n","sourceCodeStart":124,"sourceCodeEnd":150,"githubUrl":"https://github.com/facebook/flow/blob/f88ac94bcf6992f5d5a158854d94613ebb92c6e6/packages/flow-transform/src/transform/mutations/utils/getStatementParent.js#L124-L150","documentation":"InvalidStatementError thrown by getStatementParent's final identity check: for single-key parents (e.g. `IfStatement.consequent`, `LabeledStatement.body`, loop bodies) it verifies `parent[key] === target`. If a previous mutation in the same run already replaced that slot, the slot now holds a different node and the error explicitly hints that you mutated around a target after it was deleted/replaced.","triggerScenarios":"replaceNode(ifStmt.consequent, x) followed by insertStatementBefore(originalConsequent, ...) in the same run; any second statement mutation anchored on a single-child slot that an earlier replace already swapped.","commonSituations":"Chained codemod visitors both targeting the same if-branch or loop body; inserting around a statement that a previous visitor replaced with a block.","solutions":["Anchor follow-up mutations on the replacement node you installed, not the original.","Issue at most one statement mutation per single-child slot per run.","When both a replace and insert are needed, use one replaceStatementWithMany with the full final statement list."],"exampleFix":"// before\nmutate.replaceNode(ifStmt.consequent, newBlock);\nmutate.insertStatementBefore(oldConsequent, extra); // slot now holds newBlock - throws\n\n// after\nmutate.replaceStatementWithMany(ifStmt.consequent, [extra, ...oldConsequentBody]);","handlingStrategy":"validation","validationCode":"function slotStillHoldsTarget(anchor, key): boolean {\n  return anchor.parent[key] === anchor;\n}\n\nif (slotStillHoldsTarget(anchor, 'consequent')) {\n  mutate.insertStatementBefore(anchor, stmt);\n}","typeGuard":null,"tryCatchPattern":"try {\n  mutate.insertStatementBefore(anchor, stmt);\n} catch (e) {\n  if (e.name === 'InvalidStatementError' && /found a different node/.test(e.message)) {\n    // slot was replaced earlier in this run - anchor on the replacement instead\n  } else throw e;\n}","preventionTips":["After replacing a single-child slot, use the replacement node for follow-up anchors.","One statement mutation per slot per run.","Compose final state with replaceStatementWithMany instead of chained edits."],"tags":["flow","flow-transform","codemod","statement-anchor","stale-reference","ast-mutation"],"backgroundTag":"invalid-ast-mutation","analyzedSha":"f88ac94bcf6992f5d5a158854d94613ebb92c6e6","analyzedAt":"2026-08-20T10:41:37.992Z","contentChangedAt":"2026-08-20T10:41:37.992Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}