From f1ea98876bc2a38801152e9e13027eefab97c974 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Mon, 9 Apr 2018 08:22:38 -0700 Subject: [PATCH 1/3] added optional `associatedElement` param to InvalidGenerationSourceError --- CHANGELOG.md | 5 +++++ lib/src/generator.dart | 23 +++++++++++++++++++++-- pubspec.yaml | 2 +- test/builder_test.dart | 3 +++ test/src/comment_generator.dart | 3 ++- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db5fb0ce..a9f39abe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.8.1 + +* `InvalidGenerationSourceError` added an optional `associatedElement` + parameter to support more helpful error messages. + ## 0.8.0 * **BREAKING** removed the deprecated `requireLibraryDirective` parameter in diff --git a/lib/src/generator.dart b/lib/src/generator.dart index 8317e446..ae98b623 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -4,9 +4,11 @@ import 'dart:async'; +import 'package:analyzer/dart/element/element.dart'; import 'package:build/build.dart'; import 'library.dart'; +import 'span_for_element.dart'; /// A tool to generate Dart code based on a Dart library source. /// @@ -36,9 +38,26 @@ class InvalidGenerationSourceError extends Error { /// May be an empty string if unknown. final String todo; - InvalidGenerationSourceError(this.message, {String todo}) + /// The code element associated with this error. + /// + /// May be `null` if the error had no associated element. + final Element associatedElement; + + InvalidGenerationSourceError(this.message, + {String todo, this.associatedElement}) : this.todo = todo ?? ''; @override - String toString() => message; + String toString() { + var buffer = new StringBuffer(message); + + if (associatedElement != null) { + var span = spanForElement(associatedElement); + buffer.writeln(); + buffer.writeln(span.start.toolString); + buffer.write(span.highlight()); + } + + return buffer.toString(); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 55de30ef..007b7309 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 0.8.0 +version: 0.8.1-dev author: Dart Team description: Automated source code generation for Dart. homepage: https://github.com/dart-lang/source_gen diff --git a/test/builder_test.dart b/test/builder_test.dart index 8e739dd8..ba3e77d2 100644 --- a/test/builder_test.dart +++ b/test/builder_test.dart @@ -343,6 +343,9 @@ part of test_lib; // ************************************************************************** // Error: Don't use classes with the word 'Error' in the name +// package:pkg/test_lib.dart:4:7 +// class MyGoodError { } +// ^^^^^^^^^^^ // TODO: Rename MyGoodError to something else. '''; diff --git a/test/src/comment_generator.dart b/test/src/comment_generator.dart index f4b8999f..f5e40697 100644 --- a/test/src/comment_generator.dart +++ b/test/src/comment_generator.dart @@ -29,7 +29,8 @@ class CommentGenerator extends Generator { if (classElement.displayName.contains('GoodError')) { throw new InvalidGenerationSourceError( "Don't use classes with the word 'Error' in the name", - todo: 'Rename ${classElement.displayName} to something else.'); + todo: 'Rename ${classElement.displayName} to something else.', + associatedElement: classElement); } output.writeln('// Code for "$classElement"'); } From 82293df6e2ccb96d82ca1c0a7d7a631f4edcb9a0 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Mon, 9 Apr 2018 14:04:12 -0700 Subject: [PATCH 2/3] rename to just 'element' --- CHANGELOG.md | 2 +- lib/src/generator.dart | 8 ++++---- test/src/comment_generator.dart | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f39abe..e5d96864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.8.1 -* `InvalidGenerationSourceError` added an optional `associatedElement` +* `InvalidGenerationSourceError` added an optional `element` parameter to support more helpful error messages. ## 0.8.0 diff --git a/lib/src/generator.dart b/lib/src/generator.dart index ae98b623..9ed7bf33 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -41,18 +41,18 @@ class InvalidGenerationSourceError extends Error { /// The code element associated with this error. /// /// May be `null` if the error had no associated element. - final Element associatedElement; + final Element element; InvalidGenerationSourceError(this.message, - {String todo, this.associatedElement}) + {String todo, this.element}) : this.todo = todo ?? ''; @override String toString() { var buffer = new StringBuffer(message); - if (associatedElement != null) { - var span = spanForElement(associatedElement); + if (element != null) { + var span = spanForElement(element); buffer.writeln(); buffer.writeln(span.start.toolString); buffer.write(span.highlight()); diff --git a/test/src/comment_generator.dart b/test/src/comment_generator.dart index f5e40697..2d8ef976 100644 --- a/test/src/comment_generator.dart +++ b/test/src/comment_generator.dart @@ -30,7 +30,7 @@ class CommentGenerator extends Generator { throw new InvalidGenerationSourceError( "Don't use classes with the word 'Error' in the name", todo: 'Rename ${classElement.displayName} to something else.', - associatedElement: classElement); + element: classElement); } output.writeln('// Code for "$classElement"'); } From b8f53605b3e05292fc09d7286aae02ea2d94cee7 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Mon, 9 Apr 2018 14:08:43 -0700 Subject: [PATCH 3/3] format oops --- lib/src/generator.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/generator.dart b/lib/src/generator.dart index 9ed7bf33..fc89c94b 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -43,8 +43,7 @@ class InvalidGenerationSourceError extends Error { /// May be `null` if the error had no associated element. final Element element; - InvalidGenerationSourceError(this.message, - {String todo, this.element}) + InvalidGenerationSourceError(this.message, {String todo, this.element}) : this.todo = todo ?? ''; @override