visitMethod method Null safety

  1. @override
StringSink visitMethod(
  1. Method spec,
  2. [StringSink? output]
)

Implementation

@override
StringSink visitMethod(Method spec, [StringSink? output]) {
  output ??= StringBuffer();
  spec.docs.forEach(output.writeln);
  spec.annotations.forEach((a) => visitAnnotation(a, output));
  if (spec.external) {
    output.write('external ');
  }
  if (spec.static) {
    output.write('static ');
  }
  if (spec.returns != null) {
    spec.returns!.accept(this, output);
    output.write(' ');
  }
  if (spec.type == MethodType.getter) {
    output
      ..write('get ')
      ..write(spec.name);
  } else {
    if (spec.type == MethodType.setter) {
      output.write('set ');
    }
    if (spec.name != null) {
      output.write(spec.name);
    }
    visitTypeParameters(spec.types.map((r) => r.type), output);
    output.write('(');
    if (spec.requiredParameters.isNotEmpty) {
      var count = 0;
      for (final p in spec.requiredParameters) {
        count++;
        _visitParameter(p, output);
        if (spec.requiredParameters.length != count ||
            spec.optionalParameters.isNotEmpty) {
          output.write(', ');
        }
      }
    }
    if (spec.optionalParameters.isNotEmpty) {
      final named = spec.optionalParameters.any((p) => p.named);
      if (named) {
        output.write('{');
      } else {
        output.write('[');
      }
      var count = 0;
      for (final p in spec.optionalParameters) {
        count++;
        _visitParameter(p, output, optional: true, named: named);
        if (spec.optionalParameters.length != count) {
          output.write(', ');
        }
      }
      if (named) {
        output.write('}');
      } else {
        output.write(']');
      }
    }
    output.write(')');
  }
  if (spec.body != null) {
    if (spec.modifier != null) {
      switch (spec.modifier!) {
        case MethodModifier.async:
          output.write(' async ');
          break;
        case MethodModifier.asyncStar:
          output.write(' async* ');
          break;
        case MethodModifier.syncStar:
          output.write(' sync* ');
          break;
      }
    }
    if (_isLambdaMethod(spec)) {
      output.write(' => ');
    } else {
      output.write(' { ');
    }
    spec.body!.accept(this, output);
    if (!_isLambdaMethod(spec)) {
      output.write(' } ');
    }
  } else {
    output.write(';');
  }
  return output;
}