getSemanticWrappingInfo function

SemanticWrappingInfo getSemanticWrappingInfo(
  1. InstanceCreationExpression node
)

Check if a widget is wrapped in Semantics or ExcludeSemantics

Implementation

SemanticWrappingInfo getSemanticWrappingInfo(InstanceCreationExpression node) {
  // Walk up the AST to find parent widget
  AstNode? current = node.parent;

  while (current != null) {
    if (current is NamedExpression && current.name.label.name == 'child') {
      // Check if the parent is a Semantics or ExcludeSemantics widget
      final grandParent = current.parent?.parent;
      if (grandParent is InstanceCreationExpression) {
        final parentType = grandParent.staticType?.element?.name;

        // Check for ExcludeSemantics
        if (parentType == 'ExcludeSemantics') {
          // Check if excluding is true (default is true)
          bool isExcluding = true;
          for (final arg in grandParent.argumentList.arguments) {
            if (arg is NamedExpression && arg.name.label.name == 'excluding') {
              if (arg.expression is BooleanLiteral) {
                isExcluding = (arg.expression as BooleanLiteral).value;
              }
            }
          }
          if (isExcluding) {
            return SemanticWrappingInfo(hasSemantics: false, isExcluded: true);
          }
        }

        // Check for Semantics with label
        if (parentType == 'Semantics') {
          // Check if Semantics has a label
          for (final arg in grandParent.argumentList.arguments) {
            if (arg is NamedExpression &&
                arg.name.label.name == 'label' &&
                hasNonEmptyStringValue(arg.expression)) {
              return SemanticWrappingInfo(
                hasSemantics: true,
                isExcluded: false,
              );
            }
          }
        }
      }
      // Continue searching up the tree instead of breaking
    }
    current = current.parent;
  }

  return SemanticWrappingInfo(hasSemantics: false, isExcluded: false);
}