-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-internal API for accessing ElementAnnotation
source ranges
#32454
Comments
Thanks! This is for dart-lang/source_gen#316: // TODO: Remove internal API once ElementAnnotation has source information.
// https://github.com/dart-lang/sdk/issues/32454
static SourceSpan _getSourceSpanFrom(ElementAnnotation annotation) {
final internals = annotation as ElementAnnotationImpl;
final astNode = internals.annotationAst;
final contents = annotation.source.contents.data;
final start = astNode.offset;
final end = start + astNode.length;
return new SourceSpan(
new SourceLocation(start, sourceUrl: annotation.source.uri),
new SourceLocation(end, sourceUrl: annotation.source.uri),
contents.substring(start, end),
);
} |
@MichaelRFairhurst Is this still relevant? |
Looks like yes, |
This might also be giving us trouble in the migration to analysis driver, some details of the workaround in Any chance we can get a nicer and backwards/forwards compatible API for this? |
We discussed this and found a different way to achieve the same result: by having the angular compiler retrieve the So we no longer need a special API for this. |
Chatted internally with @natebosch - despite this being marked fixed I actually don't see how I can access the import 'package:analyzer/dart/element/element.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/element.dart';
class BuildError {
/// Creates a build error using the provided source annotation as [context].
factory BuildError.forAnnotation(
ElementAnnotation context,
String message,
) {
// TOOD(b/170758395): Replace w/ patches from upstream (see pkg/source_gen).
// https://github.com/dart-lang/sdk/issues/32454
final annotation = context as ElementAnnotationImpl;
final astNode = annotation.annotationAst;
final file = SourceFile.fromString(
annotation.source.contents.data,
url: annotation.source.uri,
);
return BuildError.forSourceSpan(
file.span(astNode.offset, astNode.offset + astNode.length),
message,
);
}
} |
ElementAnnotation
source ranges
If someone uses an annotation wrong,
then the ElementAnnotation for this cannot compute constant value, and has no element.
To aid creators of tools such as angular, the element model should contain a source range for the element annotation so that Angular can attempt to help the user in this case, rather than just having some 'null's in their program that could mean anything at all.
+@matanlurey says he could use this change to remove some instances of computeNode().
The text was updated successfully, but these errors were encountered: