Coverage report for lib/src/exceptions.dart

Line coverage: 16 / 23 (69.6%)

All files > lib/src/exceptions.dart

1
// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4
5
library dart_style.src.formatter_exception;
6
7
import 'package:analyzer/error/error.dart';
8
import 'package:source_span/source_span.dart';
9
10
/// Thrown when one or more errors occurs while parsing the code to be
11
/// formatted.
12
class FormatterException implements Exception {
13
  /// The [AnalysisError]s that occurred.
14
  final List<AnalysisError> errors;
15
16
  /// Creates a new FormatterException with an optional error [message].
171
  const FormatterException(this.errors);
18
19
  /// Creates a human-friendly representation of the analysis errors.
201
  String message({bool color}) {
211
    var buffer = new StringBuffer();
221
    buffer.writeln("Could not format because the source could not be parsed:");
23
24
    // In case we get a huge series of cascaded errors, just show the first few.
251
    var shownErrors = errors;
263
    if (errors.length > 10) shownErrors = errors.take(10).toList();
27
282
    for (var error in shownErrors) {
293
      var source = error.source.contents.data;
30
31
      // If the parse error is for something missing from the end of the file,
32
      // the error position will go past the end of the source. In that case,
33
      // just pad the source with spaces so we can report it nicely.
345
      if (error.offset + error.length > source.length) {
350
        source += " " * (error.offset + error.length - source.length);
36
      }
37
383
      var file = new SourceFile.fromString(source, url: error.source.fullName);
395
      var span = file.span(error.offset, error.offset + error.length);
402
      if (buffer.isNotEmpty) buffer.writeln();
413
      buffer.write(span.message(error.message, color: color));
42
    }
43
444
    if (shownErrors.length != errors.length) {
450
      buffer.writeln();
460
      buffer.write("(${errors.length - shownErrors.length} more errors...)");
47
    }
48
491
    return buffer.toString();
50
  }
51
520
  String toString() => message();
53
}
54
55
/// Exception thrown when the internal sanity check that only whitespace
56
/// changes are made fails.
57
class UnexpectedOutputException implements Exception {
58
  /// The source being formatted.
59
  final String _input;
60
61
  /// The resulting output.
62
  final String _output;
63
641
  UnexpectedOutputException(this._input, this._output);
65
660
  String toString() {
67
    return """The formatter produced unexpected output. Input was:
680
$_input
69
Which formatted to:
700
$_output""";
71
  }
72
}