File tree Expand file tree Collapse file tree 5 files changed +31
-4
lines changed Expand file tree Collapse file tree 5 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 1+ ## 0.8.1
2+
3+ * ` InvalidGenerationSourceError ` added an optional ` element `
4+ parameter to support more helpful error messages.
5+
16## 0.8.0
27
38* ** BREAKING** removed the deprecated ` requireLibraryDirective ` parameter in
Original file line number Diff line number Diff line change 44
55import 'dart:async' ;
66
7+ import 'package:analyzer/dart/element/element.dart' ;
78import 'package:build/build.dart' ;
89
910import 'library.dart' ;
11+ import 'span_for_element.dart' ;
1012
1113/// A tool to generate Dart code based on a Dart library source.
1214///
@@ -36,9 +38,25 @@ class InvalidGenerationSourceError extends Error {
3638 /// May be an empty string if unknown.
3739 final String todo;
3840
39- InvalidGenerationSourceError (this .message, {String todo})
41+ /// The code element associated with this error.
42+ ///
43+ /// May be `null` if the error had no associated element.
44+ final Element element;
45+
46+ InvalidGenerationSourceError (this .message, {String todo, this .element})
4047 : this .todo = todo ?? '' ;
4148
4249 @override
43- String toString () => message;
50+ String toString () {
51+ var buffer = new StringBuffer (message);
52+
53+ if (element != null ) {
54+ var span = spanForElement (element);
55+ buffer.writeln ();
56+ buffer.writeln (span.start.toolString);
57+ buffer.write (span.highlight ());
58+ }
59+
60+ return buffer.toString ();
61+ }
4462}
Original file line number Diff line number Diff line change 11name : source_gen
2- version : 0.8.0
2+ version : 0.8.1-dev
33author :
Dart Team <[email protected] > 44description : Automated source code generation for Dart.
55homepage : https://github.com/dart-lang/source_gen
Original file line number Diff line number Diff line change @@ -343,6 +343,9 @@ part of test_lib;
343343// **************************************************************************
344344
345345// Error: Don't use classes with the word 'Error' in the name
346+ // package:pkg/test_lib.dart:4:7
347+ // class MyGoodError { }
348+ // ^^^^^^^^^^^
346349// TODO: Rename MyGoodError to something else.
347350''' ;
348351
Original file line number Diff line number Diff line change @@ -29,7 +29,8 @@ class CommentGenerator extends Generator {
2929 if (classElement.displayName.contains ('GoodError' )) {
3030 throw new InvalidGenerationSourceError (
3131 "Don't use classes with the word 'Error' in the name" ,
32- todo: 'Rename ${classElement .displayName } to something else.' );
32+ todo: 'Rename ${classElement .displayName } to something else.' ,
33+ element: classElement);
3334 }
3435 output.writeln ('// Code for "$classElement "' );
3536 }
You can’t perform that action at this time.
0 commit comments