format method

String format(
  1. String source, {
  2. Object? uri,
})

Formats the given source string containing an entire Dart compilation unit.

If uri is given, it is a String or Uri used to identify the file being formatted in error messages.

Implementation

String format(String source, {Object? uri}) {
  if (uri == null) {
    // Do nothing.
  } else if (uri is Uri) {
    uri = uri.toString();
  } else if (uri is String) {
    // Do nothing.
  } else {
    throw ArgumentError('uri must be `null`, a Uri, or a String.');
  }

  return formatSource(
          SourceCode(source, uri: uri as String?, isCompilationUnit: true))
      .text;
}