transformVariables method

void transformVariables (
  1. Iterable<PropertyAccessorElement> accessors
)

Implementation

void transformVariables(Iterable<PropertyAccessorElement> accessors) {
  for (final accessor in accessors) {
    final varDeclList =
        accessor.variable.computeNode().parent as VariableDeclarationList;
    var jsName = getNameAnnotation(
        accessor.variable.computeNode() as AnnotatedNode, _jsNameClass);
    jsName = jsName != null
        ? jsName
        : getNameAnnotation(
            varDeclList.parent as AnnotatedNode, _jsNameClass);
    jsName = jsName != null
        ? jsName
        : accessor.isPrivate
            ? accessor.displayName.substring(1)
            : accessor.displayName;
    final name = accessor.displayName;

    final target = accessor.isStatic
        ? getPath(computeJsName(clazz, _jsNameClass))
        : 'asJsObject(this)';

    final varType =
        varDeclList.type != null ? varDeclList.type.toString() : '';
    var code = accessor.isStatic ? 'static ' : '';
    if (accessor.isGetter) {
      final getterBody =
          createGetterBody(accessor.returnType, jsName, target);
      code += '$varType get $name => $getterBody';
    } else if (accessor.isSetter) {
      final param = accessor.parameters.first;
      final setterBody = createSetterBody(param, target, jsName: jsName);
      code += ' set $name($varType ${param.displayName}){ $setterBody }';
    }
    transformer.insertAt(varDeclList.end + 1, code);
  }

  // remove variable declarations
  final variables = accessors.map((e) => e.variable.computeNode()).toSet();
  final varDeclLists = variables.map((e) => e.parent).toSet();
  for (final varDeclList in varDeclLists) {
    transformer.removeNode(varDeclList.parent);
  }
}