toDartProperty method

DartProperty? toDartProperty(
  1. List<DartField?> fields
)

Implementation

DartProperty? toDartProperty(List<DartField?> fields) {
  DartProperty? base;
  bool _hasValue = false;

  // e.g.
  // [<FieldFormalParameterImpl: this.listField>, <SimpleToken: =>, <ListLiteralImpl: const []>]
  for (final node in this.childEntities) {
    // e.g.
    // constructor({Key key})
    if (node is SimpleFormalParameterImpl) {
      base = _processProperty(node);
      for (final child in node.childEntities) {
        if (child is StringTokenImpl || child is DeclaredSimpleIdentifier) {
          base = base!.copyWith(name: child.toString());
        }
        if (child is NamedTypeImpl) {
          base = base!.copyWith(type: child.toString());
        }
      }
    }

    // e.g.
    // constructor({this.field})
    // this   : KeywordToken
    // .      : SimpleToken
    // field  : SimpleIdentifierImpl
    if (node is FieldFormalParameterImpl) {
      base = _processProperty(node);

      // get the name
      for (final child in node.childEntities) {
        // e.g.
        // in `this.field` -> "field" is StringTokenImpl
        if (child is StringTokenImpl || child is SimpleIdentifierImpl) {
          base = base?.copyWith(name: child.toString());
        }
      }

      // get the type - the fields are in the parent class
      for (final field in fields) {
        if (field?.name == base?.name) {
          base = base?.copyWith(type: field?.type);
        }
      }
    }

    // value initializaation token
    if (node is SimpleToken && node.toString() == '=') {
      _hasValue = true;
      continue;
    }

    if (_hasValue && node is LiteralImpl) {
      base = base?.copyWith(value: node.toDartCore());
    }
  }
  return base;
}