StringSink visitLibrary()

in lib/src/emitter.dart [393:434]


  StringSink visitLibrary(Library spec, [StringSink? output]) {
    output ??= StringBuffer();
    // Process the body first in order to prime the allocators.
    final body = StringBuffer();
    for (final spec in spec.body) {
      spec.accept(this, body);
      if (spec is Method && _isLambdaMethod(spec)) {
        body.write(';');
      }
    }

    if (spec.name != null) {
      for (var a in spec.annotations) {
        visitAnnotation(a, output);
      }
      output.write('library ${spec.name!};');
    } else if (spec.annotations.isNotEmpty) {
      throw StateError('a library name is required for annotations');
    }

    final directives = <Directive>[...allocator.imports, ...spec.directives];

    if (orderDirectives) {
      directives.sort();
    }

    Directive? previous;
    for (final directive in directives) {
      if (_newLineBetween(orderDirectives, previous, directive)) {
        // Note: dartfmt handles creating new lines between directives.
        // 2 lines are written here. The first one comes after the previous
        // directive `;`, the second is the empty line.
        output
          ..writeln()
          ..writeln();
      }
      directive.accept(this, output);
      previous = directive;
    }
    output.write(body);
    return output;
  }