visitConstructor method Null safety

  1. @override
StringSink visitConstructor(
  1. Constructor spec,
  2. String clazz,
  3. [StringSink? output]
)

Implementation

@override
StringSink visitConstructor(Constructor spec, String clazz,
    [StringSink? output]) {
  output ??= StringBuffer();
  spec.docs.forEach(output.writeln);
  spec.annotations.forEach((a) => visitAnnotation(a, output));
  if (spec.external) {
    output.write('external ');
  }
  if (spec.constant) {
    output.write('const ');
  }
  if (spec.factory) {
    output.write('factory ');
  }
  output.write(clazz);
  if (spec.name != null) {
    output
      ..write('.')
      ..write(spec.name);
  }
  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.initializers.isNotEmpty) {
    output.write(' : ');
    var count = 0;
    for (final initializer in spec.initializers) {
      count++;
      initializer.accept(this, output);
      if (count != spec.initializers.length) {
        output.write(', ');
      }
    }
  }
  if (spec.redirect != null) {
    output.write(' = ');
    spec.redirect!.type.accept(this, output);
    output.write(';');
  } else if (spec.body != null) {
    if (_isLambdaConstructor(spec)) {
      output.write(' => ');
      spec.body!.accept(this, output);
      output.write(';');
    } else {
      output.write(' { ');
      spec.body!.accept(this, output);
      output.write(' }');
    }
  } else {
    output.write(';');
  }
  output.writeln();
  return output;
}