function updateErrorSuppression()

in packages/flow-dev-tools/src/update-suppressions/update-suppressionsRunner.js [202:252]


function updateErrorSuppression(
  contents: Buffer,
  startOffset: number,
  endOffset: number,
  commentAST: Object | void,
  ast: Object,
  knownRoots: Set<RootName>,
  unusedRoots: Set<RootName>,
): Buffer {
  let commentStartOffset;
  let commentEndOffset;
  if (commentAST) {
    [commentStartOffset, commentEndOffset] = commentAST.range;
  } else {
    commentStartOffset = startOffset;
    commentEndOffset = endOffset;
  }
  let innerOffset = commentStartOffset + 2; // `/*` and `//` are both 2 chars
  let text = contents.slice(innerOffset, endOffset).toString('utf8');

  const existingRoots = getSites(text); // may include unknown roots
  const roots = new Set([...existingRoots, ...knownRoots]);
  unusedRoots.forEach(root => roots.delete(root));

  if (roots.size === 0) {
    // no roots need the suppression, so remove it
    return removeUnusedErrorSuppressionFromText(
      contents,
      startOffset,
      endOffset,
      commentAST,
      ast,
    );
  } else {
    if (commentAST && isLintSuppression(commentAST)) {
      // lints can't be suppressed per site, so we have to convert it to a
      // $FlowFixMe(site=<roots>)
      const sites = [...roots].sort().join(',');
      text = ` $FlowFixMe(site=${sites}) --${text}`;
    } else {
      // only sites in `roots` need the suppression, so add/update site=
      text = replaceSites(text, [...roots]);
    }

    return Buffer.concat([
      contents.slice(0, innerOffset),
      Buffer.from(text),
      contents.slice(endOffset),
    ]);
  }
}