44
55import 'dart:async' ;
66
7+ import 'package:analyzer/dart/ast/ast.dart' ;
78import 'package:analyzer/dart/element/element.dart' ;
89import 'package:build/build.dart' ;
910
@@ -41,18 +42,30 @@ class InvalidGenerationSourceError extends Error {
4142
4243 /// The code element associated with this error.
4344 ///
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] .
4547 final Element ? element;
4648
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+ });
4861
4962 @override
5063 String toString () {
5164 final buffer = StringBuffer (message);
5265
53- if (element != null ) {
66+ if (element case final element ? ) {
5467 try {
55- final span = spanForElement (element! );
68+ final span = spanForElement (element);
5669 buffer
5770 ..writeln ()
5871 ..writeln (span.start.toolString)
@@ -66,6 +79,20 @@ class InvalidGenerationSourceError extends Error {
6679 }
6780 }
6881
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+
6996 return buffer.toString ();
7097 }
7198}
0 commit comments