Skip to content

Commit f2adf1e

Browse files
authored
added optional element param to InvalidGenerationSourceError (#324)
1 parent c138133 commit f2adf1e

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
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

lib/src/generator.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import 'dart:async';
66

7+
import 'package:analyzer/dart/element/element.dart';
78
import 'package:build/build.dart';
89

910
import '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
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.8.0
2+
version: 0.8.1-dev
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen

test/builder_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

test/src/comment_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)