visitFieldDeclaration method

  1. @override
Object? visitFieldDeclaration(
  1. FieldDeclaration node
)
override

Implementation

@override
Object? visitFieldDeclaration(dart_ast.FieldDeclaration node) {
  if (_currentModel != null) {
    final type = node.fields.type;
    if (!node.isStatic && type is dart_ast.NamedType) {
      final initializerVisitor = InitializerVisitor();
      node.visitChildren(initializerVisitor);
      if (initializerVisitor.initializer != null) {
        printf(
            'visitFieldDeclaration error! Initialization isn\'t supported for fields in UniAPI data classes ("$node"), just use nullable types with no initializer (example "int? x;").');
      } else {
        final typeArguments = type.typeArguments;
        // 类型 type.name.name
        // isNullable type.question != null
        // 属性名称 node.fields.variables[0].name.lexeme
        final fieldName = node.fields.variables[0].name.name;
        final typeString = type.name.name;
        final isNullable = type.question != null;
        final codeComments =
            _codeCommentsParser(node.documentationComment?.tokens);

        final genericsMaybeNull = <bool>[];
        typeGenericsArgumentsMaybeNull(typeArguments, genericsMaybeNull);
        final astType = stringToAstType(typeString,
            generics: typeAnnotationsToTypeArguments(typeArguments),
            maybeNull: isNullable,
            genericsMaybeNull: genericsMaybeNull);

        if (astType != null) {
          final variable =
              Variable(astType, fieldName, codeComments: codeComments);
          _currentModel?.fields.add(variable);
        }
      }
    }
  }
  node.visitChildren(this);
  return null;
}