maybeApplyMacro method Null safety

  1. @override
Future<List<String>?> maybeApplyMacro(
  1. TypeChecker checker,
  2. Macro macro,
  3. Element element,
  4. Resolver resolver,
  5. StringBuffer buffer,
  6. StringBuffer libraryBuffer,
  7. String originalSource
)

Implementation

@override
Future<List<String>?> maybeApplyMacro(
    TypeChecker checker,
    Macro macro,
    analyzer.Element element,
    Resolver resolver,
    StringBuffer buffer,
    StringBuffer libraryBuffer,
    String originalSource) async {
  if (!checker.hasAnnotationOf(element)) return null;
  _checkValidMacroApplication(element, macro);
  macro = _instantiateFromMeta(macro, checker.firstAnnotationOf(element)!);
  if (element is analyzer.FieldElement && macro is FieldDefinitionMacro) {
    var fieldBuffer = StringBuffer();
    var parentClass = element.enclosingElement as analyzer.ClassElement;
    var definition =
        AnalyzerFieldDefinition(element, parentClass: parentClass);
    var parent = AnalyzerClassDefinition(parentClass,
        originalReference: parentClass.thisType);
    macro.visitFieldDefinition(
        definition,
        _MacroFieldDefinitionBuilder(
            buffer, definition, parent, element.library));
    if (fieldBuffer.isNotEmpty) {
      var node = (await resolver.astNodeFor(element, resolve: true))!
          .parent!
          .parent as analyzer.FieldDeclaration;
      for (var meta in node.metadata) {
        buffer.writeln(meta.toSource());
      }
      buffer.writeln(fieldBuffer);
      return [element.name];
    }
  } else if (element is analyzer.MethodElement &&
      macro is MethodDefinitionMacro) {
    var methodBuffer = StringBuffer();
    FunctionDefinitionBuilder builder;
    MethodDefinition definition;
    var node = (await resolver.astNodeFor(element, resolve: true))
        as analyzer.Declaration;
    definition = AnalyzerMethodDefinition(element,
        parentClass: element.enclosingElement as analyzer.ClassElement);
    builder = _MacroFunctionDefinitionBuilder(
        methodBuffer, definition, node, originalSource, element.library);
    macro.visitMethodDefinition(definition, builder);
    if (methodBuffer.isNotEmpty) {
      for (var meta in node.metadata) {
        buffer.writeln(meta.toSource());
      }
      buffer.writeln(methodBuffer);
      return [element.name];
    }
  } else if (element is analyzer.ConstructorElement &&
      macro is ConstructorDefinitionMacro) {
    var methodBuffer = StringBuffer();
    ConstructorDefinitionBuilder builder;
    ConstructorDefinition definition;
    var parentClass = element.enclosingElement;
    var parent = AnalyzerClassDefinition(parentClass,
        originalReference: parentClass.thisType);
    var node = (await resolver.astNodeFor(element, resolve: true))
        as analyzer.ConstructorDeclaration;
    definition = AnalyzerConstructorDefinition(element,
        parentClass: element.enclosingElement);
    builder = _MacroConstructorDefinitionBuilder(methodBuffer, definition,
        parent, node, originalSource, element.library);

    macro.visitConstructorDefinition(definition, builder);
    if (methodBuffer.isNotEmpty) {
      for (var meta in node.metadata) {
        buffer.writeln(meta.toSource());
      }
      buffer.writeln(methodBuffer);
      return [element.name];
    }
  } else if (element is analyzer.FunctionElement &&
      macro is FunctionDefinitionMacro) {
    var fnBuffer = StringBuffer();
    var definition = AnalyzerFunctionDefinition(element);
    var node = (await resolver.astNodeFor(element, resolve: true))
        as analyzer.Declaration;
    var builder = _MacroFunctionDefinitionBuilder(
        fnBuffer, definition, node, originalSource, element.library);
    macro.visitFunctionDefinition(definition, builder);
    if (fnBuffer.isNotEmpty) {
      for (var meta in node.metadata) {
        buffer.writeln(meta.toSource());
      }
      buffer.writeln(fnBuffer);
      return [element.name];
    }
  }
}