generateForElement method

  1. @override
String generateForElement(
  1. Element element,
  2. BuildStep buildStep
)
override

Implementation

@override
String generateForElement(Element element, BuildStep buildStep) {
  if (element is! ClassElement) {
    final name = element.name;
    throw InvalidGenerationSourceError('Generator cannot target `$name`.',
        todo: 'Remove the $AnalyticsEventStubs annotation from `$name`.',
        element: element);
  }
  final classElement = element;
  final result = StringBuffer();
  final eventNameCase = _getCaseFor(element, 'eventNameCase');
  final parameterNameCase = _getCaseFor(element, 'parameterNameCase');

  result.writeln('// got to generate for ${element.name}');

  final methods = classElement.methods
      .where((method) => method.isAbstract)
      .map((method) => Method.returnsVoid((mb) => mb
            ..name = method.name
            ..annotations.add(_override)
            ..requiredParameters.addAll(method.parameters
                .where((p) => p.isRequiredPositional)
                .map((parameter) => _toParameter(
                      classElement,
                      parameter,
                      nameCase: parameterNameCase,
                    )))
            ..optionalParameters = ListBuilder(
              method.parameters
                  .where((p) => !p.isRequiredPositional)
                  .map<Parameter>((parameter) => _toParameter(
                        classElement,
                        parameter,
                        nameCase: parameterNameCase,
                      )),
            )
            ..body = refer(_trackEventMethodName)
//              .property('track')
                .call([
              literalString(eventNameCase.convert(_eventName(method.name))),
              _convertParametersToDictionary(
                method.parameters,
                nameCase: parameterNameCase,
              )
            ]).code
//            ..body = Code(
//                '''\n$_trackerFieldName.track('${method.name}', ${_convertParametersToDictionary(method.parameters)});\n'''),
          ));

  final c = Class((cb) {
    cb
      ..name = '_\$${element.name}'
      ..constructors.add(
        Constructor((conb) => conb
          ..optionalParameters.add(Parameter((pb) => pb
            ..name = _trackerFieldName
            ..type = _trackAnalyticsFunc.asNullable(true)))
          ..body = refer(_trackerFieldName)
              .notEqualTo(literalNull)
              .conditional(
                  _registerTrackerFunc.call([refer(_trackerFieldName)]),
                  literalNull)
              .statement),
      )
//        ..fields.add(Field((fb) => fb
//          ..name = _trackerFieldName
//          ..type = _trackAnalyticsFunc))
      ..extend = refer(element.name)
      ..mixins.add(refer('AnalyticsEventStubsImpl'))
      ..methods.addAll(methods);
  });

  final emitter = DartEmitter(
    allocator: Allocator.simplePrefixing(),
    useNullSafetySyntax: useNullSafetySyntax,
  );
  return '// useNullSafetySyntax: $useNullSafetySyntax\n${DartFormatter().format('${c.accept(emitter)}')}';
//    return result.toString();
}