async function addCommentsToCode()

in packages/flow-dev-tools/src/comment/add-commentsRunner.js [156:189]


async function addCommentsToCode(
  comment: ?string,
  error_code: ?string,
  code: string,
  locs: Array<Suppression>,
  flowBinPath: string,
): Promise<
  [string, number],
> /* [resulting code, number of comments inserted] */ {
  locs.sort((l1, l2) => l2.loc.start.line - l1.loc.start.line);

  const ast = await getAst(code, flowBinPath);

  let commentCount = 0;
  for (let {loc, isError, lints, error_codes} of locs) {
    if (error_code != null) {
      error_codes = error_codes.filter(c => c === error_code);
    }

    const path = getPathToLoc(loc, ast);

    if (path != null) {
      let c = comment || '';
      const comments = [...new Set(error_codes)].map(
        error_code => `$FlowFixMe[${error_code}]${c ? ` ${c}` : ''}`,
      );
      for (c of comments) {
        code = addCommentToCode(c, code, loc, path);
      }
      commentCount += error_codes.length;
    }
  }
  return [code, commentCount];
}