forInLoop function

Expression forInLoop({
  1. required Expression declaration,
  2. required Expression iterable,
  3. Code? body,
  4. bool blockBody = true,
})

Implementation

Expression forInLoop({
  required Expression declaration,
  required Expression iterable,
  Code? body,
  bool blockBody = true,
}) {
  if ((blockBody, body) case (false, null)) {
    throw ArgumentError('body is required when blockBody is false');
  }

  return CodeExpression(
    Block.of([
      const Code('for ('),
      declaration.code,
      const Code(' in '),
      iterable.code,
      const Code(') '),
      if (blockBody) const Code('{'),
      if (body case final body?) body,
      if (blockBody) const Code('}'),
    ]),
  );
}