Use unifying type visitor instead of custom logic

This commit is contained in:
Simon Binder 2023-05-10 22:28:51 +02:00
parent 18868221d9
commit 20b75bd7e3
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 23 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_visitor.dart';
import 'package:build/build.dart';
import 'package:json_annotation/json_annotation.dart';
@ -115,7 +116,7 @@ class AnnotatedDartCodeBuilder {
}
void addDartType(DartType type) {
_AddFromDartType(this).writeType(type);
type.accept(_AddFromDartType(this));
}
void addAstNode(AstNode node, {Set<AstNode> exclude = const {}}) {
@ -244,9 +245,7 @@ class DartTopLevelSymbol {
///
/// This representation allwos emitting the Dart type with relevant imports
/// managed dynamically.
// Note: We don't extend TypeVisitor because we're not supposed to and the
// inteface doesn't appear to be stable.
class _AddFromDartType {
class _AddFromDartType extends UnifyingTypeVisitor<void> {
final AnnotatedDartCodeBuilder _builder;
_AddFromDartType(this._builder);
@ -264,29 +263,15 @@ class _AddFromDartType {
}
}
void writeType(DartType type) {
if (type is DynamicType) {
visitDynamicType(type);
} else if (type is FunctionType) {
visitFunctionType(type);
} else if (type is InterfaceType) {
visitInterfaceType(type);
} else if (type is InvalidType) {
visitInvalidType(type);
} else if (type is NeverType) {
visitNeverType(type);
} else if (type is RecordType) {
visitRecordType(type);
} else if (type is TypeParameterType) {
visitTypeParameterType(type);
} else if (type is VoidType) {
visitVoidType(type);
} else {
_builder.addText(
'/* unknown type ${type.getDisplayString(withNullability: true)} */');
}
@override
void visitDartType(DartType type) {
_builder
..addText('dynamic')
..addText(
'/* unhandled ${type.getDisplayString(withNullability: true)} */');
}
@override
void visitRecordType(RecordType type) {
_builder.addText('(');
var first = true;
@ -297,7 +282,7 @@ class _AddFromDartType {
}
first = false;
writeType(field.type);
field.type.accept(this);
}
if (type.namedFields.isNotEmpty) {
@ -309,7 +294,7 @@ class _AddFromDartType {
}
first = false;
writeType(field.type);
field.type.accept(this);
_builder
..addText(' ')
..addText(field.name);
@ -321,12 +306,14 @@ class _AddFromDartType {
_writeSuffix(type.nullabilitySuffix);
}
@override
void visitDynamicType(DynamicType type) {
_builder.addText('dynamic');
}
@override
void visitFunctionType(FunctionType type) {
writeType(type.returnType);
type.returnType.accept(this);
_builder.addText(' Function');
final formals = type.typeFormals;
@ -342,7 +329,7 @@ class _AddFromDartType {
final bound = arg.bound;
if (bound != null) {
_builder.addText(' extends ');
writeType(bound);
bound.accept(this);
}
i++;
}
@ -374,7 +361,7 @@ class _AddFromDartType {
_builder.addText(', ');
}
writeType(parameter.type);
parameter.type.accept(this);
if (parameter.isNamed) {
_builder.addText(' ${parameter.name}');
}
@ -389,6 +376,7 @@ class _AddFromDartType {
_writeSuffix(type.nullabilitySuffix);
}
@override
void visitInterfaceType(InterfaceType type) {
final alias = type.alias;
if (alias != null) {
@ -405,7 +393,7 @@ class _AddFromDartType {
_builder.addText(', ');
}
writeType(arg);
arg.accept(this);
i++;
}
_builder.addText('>');
@ -414,22 +402,26 @@ class _AddFromDartType {
_writeSuffix(type.nullabilitySuffix);
}
@override
void visitInvalidType(InvalidType type) {
_builder
..addText('dynamic')
..addText('/* = invalid*/');
}
@override
void visitNeverType(NeverType type) {
_builder.addText('Never');
_writeSuffix(type.nullabilitySuffix);
}
@override
void visitTypeParameterType(TypeParameterType type) {
_builder.addText(type.element.name);
_writeSuffix(type.nullabilitySuffix);
}
@override
void visitVoidType(VoidType type) {
_builder.addText('void');
_writeSuffix(type.nullabilitySuffix);