generateForAnnotatedElement method

  1. @override
dynamic generateForAnnotatedElement(
  1. Element element,
  2. ConstantReader annotation,
  3. BuildStep buildStep
)

Generates the golden tests

Implementation

@override
generateForAnnotatedElement(
  Element element,
  ConstantReader annotation,
  BuildStep buildStep,
) {
  final String className =
      annotation.peek('type')!.typeValue.element!.displayName;
  final String? builder =
      annotation.peek('builder')?.objectValue.toFunctionValue()?.displayName;
  final String? goldenName = annotation.peek('name')?.stringValue;
  final String? lightTheme = annotation
      .peek('lightTheme')
      ?.objectValue
      .toFunctionValue()
      ?.displayName;
  final String? darkTheme = annotation
      .peek('darkTheme')
      ?.objectValue
      .toFunctionValue()
      ?.displayName;
  final List<String> dimensions = annotation
          .peek('dimensions')
          ?.listValue
          .map((e) => e.toStringValue()!)
          .toList() ??
      _defaultDimensions;
  if (dimensions.isEmpty) {
    dimensions.addAll(_defaultDimensions);
  }

  final String referenceFile = buildStep.inputId.uri.pathSegments.last;

  final emitter = DartEmitter(
    orderDirectives: true,
  );
  final library = Library(
    (l) => l
      ..directives.addAll([
        Directive.partOf(referenceFile),
      ])
      ..body.add(
        mainMethod(
          className,
          builder,
          goldenName ?? 'goldens/${className}_golden.png',
          lightTheme,
          darkTheme,
          dimensions,
        ),
      ),
  );
  return DartFormatter().format('${library.accept(emitter)}');
}