4
4
5
5
import 'dart:async' ;
6
6
7
+ import 'package:analyzer/dart/ast/ast.dart' ;
7
8
import 'package:analyzer/dart/element/element.dart' ;
8
9
import 'package:build/build.dart' ;
9
10
@@ -41,18 +42,30 @@ class InvalidGenerationSourceError extends Error {
41
42
42
43
/// The code element associated with this error.
43
44
///
44
- /// May be `null` if the error had no associated element.
45
+ /// May be `null` if the error had no associated element, or if the location
46
+ /// was passed with [node] .
45
47
final Element ? element;
46
48
47
- InvalidGenerationSourceError (this .message, {this .todo = '' , this .element});
49
+ /// The AST Node associated with this error.
50
+ ///
51
+ /// May be `null` if the error has no associated node in the input source
52
+ /// code, or if the location was passed with [element] .
53
+ final AstNode ? node;
54
+
55
+ InvalidGenerationSourceError (
56
+ this .message, {
57
+ this .todo = '' ,
58
+ this .element,
59
+ this .node,
60
+ });
48
61
49
62
@override
50
63
String toString () {
51
64
final buffer = StringBuffer (message);
52
65
53
- if (element != null ) {
66
+ if (element case final element ? ) {
54
67
try {
55
- final span = spanForElement (element! );
68
+ final span = spanForElement (element);
56
69
buffer
57
70
..writeln ()
58
71
..writeln (span.start.toolString)
@@ -66,6 +79,20 @@ class InvalidGenerationSourceError extends Error {
66
79
}
67
80
}
68
81
82
+ if (node case final node? ) {
83
+ try {
84
+ final span = spanForNode (node);
85
+ buffer
86
+ ..writeln ()
87
+ ..writeln (span.start.toolString)
88
+ ..write (span.highlight ());
89
+ } catch (_) {
90
+ buffer
91
+ ..writeln ()
92
+ ..writeln ('Cause: $node ' );
93
+ }
94
+ }
95
+
69
96
return buffer.toString ();
70
97
}
71
98
}
0 commit comments