WidgetClassGlobalDependencies.analyze constructor

WidgetClassGlobalDependencies.analyze(
  1. FFWidgetClass widgetClass
)

Analyze a widget class node to determine its actual global dependencies.

This method extracts granular dependency keys where possible:

  • App state variable keys
  • API endpoint keys
  • Action block keys
  • Custom function keys
  • Custom widget keys
  • Custom action keys
  • Data struct keys
  • Enum keys
  • App constant keys

Implementation

factory WidgetClassGlobalDependencies.analyze(FFWidgetClass widgetClass) {
  final deps = WidgetClassGlobalDependencies._();
  final node = widgetClass.node;

  _extractApiCallKeys(node, deps);

  // Extract dependencies from variables
  for (final variable in allProtosOfType<FFVariable>(node)) {
    switch (variable.source) {
      case FFVariableSource.LOCAL_STATE:
        final localState = variable.baseVariable.localState;
        if (localState.stateVariableType == FFStateVariableType.APP_STATE &&
            localState.fieldIdentifier.key.isNotEmpty) {
          deps.appStateVariableKeys.add(localState.fieldIdentifier.key);
        }

      case FFVariableSource.DATA_STRUCTS:
        _extractDataStructKeyFromVariable(variable, deps);

      case FFVariableSource.ENUMS:
        _extractEnumKeyFromVariable(variable, deps);

      case FFVariableSource.CONSTANTS:
        if (variable.hasBaseVariable() &&
            variable.baseVariable.hasConstants() &&
            variable.baseVariable.constants.hasIdentifier() &&
            variable.baseVariable.constants.identifier.key.isNotEmpty) {
          deps.appConstantKeys.add(
            variable.baseVariable.constants.identifier.key,
          );
        }

      default:
        break;
    }

    _extractDataStructFieldAccesses(variable, deps);
  }

  // Extract custom function keys
  for (final fc in allProtosOfType<FFFunctionCall>(node)) {
    if (fc.hasCustomFunction() && fc.customFunction.key.isNotEmpty) {
      deps.customFunctionKeys.add(fc.customFunction.key);
    }
  }

  // Extract custom widget keys and library component references
  for (final n in allProtosOfType<FFNode>(node)) {
    if (n.hasCustomWidgetIdentifier() &&
        n.customWidgetIdentifier.key.isNotEmpty) {
      deps.customWidgetKeys.add(n.customWidgetIdentifier.key);
    }
    // Track library component references
    if (n.isComponent &&
        n.hasComponentClassKeyRef() &&
        n.componentClassKeyRef.dependencyProjectId.isNotEmpty) {
      deps.libraryComponentProjectIds.add(
        n.componentClassKeyRef.dependencyProjectId,
      );
    }
  }

  // Extract action-related dependencies
  for (final action in allProtosOfType<FFAction>(node)) {
    _extractActionDependencies(action, deps);
  }

  // Check for theme usage
  _extractThemeUsage(node, deps);

  // Extract asset dependencies
  _extractAssetDependencies(node, deps);

  // Check widget class parameters for data type references
  _extractParamTypeDependencies(widgetClass, deps);

  return deps;
}