-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Create a method to know if a type is a Built-in type #56066
Comments
Summary: The user requests a method to determine if a Dart type is a built-in type, like |
Why? Dart doesn't have primitive types like Java or JavaScript, built-in types are just classes that happen to be in the platform libraries. Some of them are Other platform library defined types can be subtyped, so telling you whether a type is a built-in type doesn't tell you anything about the type of an actual value of that type. If it's for |
As of why, so many times i needed that feature, i guess the intention of dart is to facilitate things for developers . If its on a normal dart project (not the generator), i can do this manually bool isBuiltIn(type){
if(type is String){
}
if(type is int){
}
//etc...
} but i feel like that should already be a part of dart The bigger problem for me is on the generator project class Foo{
String desc = "";
User user = User();
SomeEnum thing = SomeEnum.thing;
} i can extract the whole line of a field like this String desc = "" or User user = User() |
As for the analyzer side of the question, given a resolved instance of
Knowing whether the type is from an SDK library doesn't really answer the question of whether the type is defined in the package being analyzed (unless you can assume that the package being analyzed does not depend on any other packages). For that you really need to ask the type for its |
@bwilkerson class MyCustomGenerator extends GeneratorForAnnotation<SerialAnnotation> {
@override
generateForAnnotatedElement( Element element, ConstantReader annotation, BuildStep buildStep) async {
var list = fields(element);
//...
}
}
List<FieldDeclaration> fields(Element element) {
AnalysisSession? session = element.session;
var library = element.library;
if (library != null) {
var parsedLibraryByElement = session?.getParsedLibraryByElement(library);
if (parsedLibraryByElement != null) {
parsedLibraryByElement as ParsedLibraryResult;
var elementDeclaration = parsedLibraryByElement.getElementDeclaration(
element);
if (elementDeclaration != null) {
var node = elementDeclaration.node;
var childEntities = node.childEntities;
List<FieldDeclaration> fields = [];
for (var obj in childEntities) {
if (obj is FieldDeclaration) {
fields.add(obj);
}
}
return fields;
}
}
}
final libraryCompilationUnit = (element.library!.session
.getParsedLibraryByElement(element.library!) as ParsedLibraryResult)
.units[0]
.unit;
Iterable<SyntacticEntity> childEntities =
libraryCompilationUnit.declarations.first.childEntities;
List<FieldDeclaration> fields = [];
for (var obj in childEntities) {
if (obj is FieldDeclaration) {
fields.add(obj);
}
}
return fields;
}
i got this method from https://stackoverflow.com/questions/57030992/how-do-i-get-the-astnode-from-some-element-in-dart-analyzer-source-gen |
Yes. You need to use See https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/doc/tutorial/ast.md#the-states-of-an-ast for a description of the difference between a parsed and a resolved AST. |
Thanks! its working now with the getResolvedLibraryByElement. |
You could (perhaps, if I understood the explanation) do extension on DartType {
bool isPlatformType => this.element.library?.isInSdk ?? false;
} (Completely untested.) |
i tried the type.element.library?.isInSdk, but for enums i created it returns false,. Also i guess it should have a way on a normal project too.where i could get a object.runtimeType and maybe have isInSdk or isDartCore. |
Absolutely won't happen, you'll have to use |
Given that the analyzer is able to answer the question, and that the sdk libraries won't be enhanced, I think we can close this issue. |
I would like to have a method that can tell if a type is a Built-in type like the ones here https://dart.dev/language/built-in-types.
Also on the analyzer package it would be good to have the same feature on the FieldDeclaration class, i am trying to create a code geneator to create a fromJson method, but when i get to the FieldDeclaration i can't get a way to know that.
The text was updated successfully, but these errors were encountered: