{"record":{"id":"34f234df54064d00","repo":"facebook/flow","slug":"could-not-find-target-in-array-of-parent-type","errorCode":null,"errorMessage":"Could not find target in array of `${parent.type}.${key}`.","messagePattern":"Could not find target in array of `(.+?)\\.(.+?)`\\.","errorType":"validation","errorClass":"InvalidStatementError","httpStatus":null,"severity":"error","filePath":"packages/flow-transform/src/transform/mutations/utils/getStatementParent.js","lineNumber":60,"sourceCode":"    for (const key of invalidKeys) {\n      // $FlowExpectedError[prop-missing]\n      const value = parentWithType[key];\n\n      if (\n        // $FlowFixMe[invalid-compare]\n        value === target ||\n        (Array.isArray(value) && value.includes(target))\n      ) {\n        throw new InvalidStatementError(\n          `Attempted to insert a statement into \\`${parentWithType.type}.${key}\\`.`,\n        );\n      }\n    }\n  }\n  function getAssertedIndex(key: string, arr: ReadonlyArray<unknown>): number {\n    const idx = arr.indexOf(target);\n    if (idx === -1) {\n      throw new InvalidStatementError(\n        `Could not find target in array of \\`${parent.type}.${key}\\`.`,\n      );\n    }\n    return idx;\n  }\n\n  const parent = target.parent;\n  const result: StatementParent = (() => {\n    switch (parent.type) {\n      case 'IfStatement': {\n        assertValidStatementLocation(parent, 'test');\n        const key = parent.consequent === target ? 'consequent' : 'alternate';\n        return {type: 'single', parent, key};\n      }\n\n      case 'LabeledStatement': {\n        assertValidStatementLocation(parent, 'label');\n        return {type: 'single', parent, key: 'body'};","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/facebook/flow/blob/f88ac94bcf6992f5d5a158854d94613ebb92c6e6/packages/flow-transform/src/transform/mutations/utils/getStatementParent.js#L42-L78","documentation":"InvalidStatementError thrown by getStatementParent's `getAssertedIndex` helper. For array-based statement parents (Program.body, BlockStatement.body, SwitchCase.consequent) the anchor statement must be present by identity (`indexOf`) in that array. If the anchor was already removed/replaced by an earlier mutation in the run, or is a stale/detached node object, indexOf returns -1 and this error fires.","triggerScenarios":"insertStatementBefore/After or statement removal anchored on a statement that a previous mutation in the same transform run already deleted or replaced; using a node captured from an earlier traversal.","commonSituations":"Codemods that both remove statements and insert around them; chained visitors mutating the same BlockStatement body; holding node references across transform passes.","solutions":["One mutation per anchor statement per run; insert relative to a statement you have not mutated.","Re-query the current AST (re-traverse) between dependent mutation batches.","Assert `anchor.parent.body.includes(anchor)` before issuing the mutation."],"exampleFix":"// before\nmutate.removeNode(stmt);\nmutate.insertStatementAfter(stmt, replacement); // stmt no longer in body - throws\n\n// after\nmutate.replaceStatementWithMany(stmt, [replacement]);","handlingStrategy":"validation","validationCode":"function anchorStillInBody(anchor): boolean {\n  const p = anchor.parent;\n  const arr = p.type === 'SwitchCase' ? p.consequent : p.body;\n  return Array.isArray(arr) && arr.includes(anchor);\n}\n\nif (anchorStillInBody(anchor)) {\n  mutate.insertStatementBefore(anchor, stmt);\n}","typeGuard":null,"tryCatchPattern":"try {\n  mutate.insertStatementBefore(anchor, stmt);\n} catch (e) {\n  if (e.name === 'InvalidStatementError' && /Could not find target/.test(e.message)) {\n    // anchor was already removed/replaced - re-traverse and pick a live anchor\n  } else throw e;\n}","preventionTips":["Anchor inserts on statements you have not mutated in this run.","Re-traverse between dependent mutation batches.","Prefer replaceStatementWithMany for remove-plus-insert sequences."],"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"}