Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8.1

* `InvalidGenerationSourceError` added an optional `element`
parameter to support more helpful error messages.

## 0.8.0

* **BREAKING** removed the deprecated `requireLibraryDirective` parameter in
Expand Down
22 changes: 20 additions & 2 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -36,9 +38,25 @@ 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 element;

InvalidGenerationSourceError(this.message, {String todo, this.element})
: this.todo = todo ?? '';

@override
String toString() => message;
String toString() {
var buffer = new StringBuffer(message);

if (element != null) {
var span = spanForElement(element);
buffer.writeln();
buffer.writeln(span.start.toolString);
buffer.write(span.highlight());
}

return buffer.toString();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 0.8.0
version: 0.8.1-dev
author: Dart Team <misc@dartlang.org>
description: Automated source code generation for Dart.
homepage: https://github.com/dart-lang/source_gen
Expand Down
3 changes: 3 additions & 0 deletions test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
''';

Expand Down
3 changes: 2 additions & 1 deletion test/src/comment_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
element: classElement);
}
output.writeln('// Code for "$classElement"');
}
Expand Down