defineBridgeAlias method

bool defineBridgeAlias(
  1. String aliasName,
  2. String targetName, {
  3. bool warnIfMissing = true,
})

GEN-078: Registers a class alias that maps aliasName to an existing bridged class registered under targetName.

This allows deprecated type alias names (e.g., MaterialStateProperty) to resolve to their canonical bridged class (e.g., WidgetStateProperty).

Returns whether the target was found. With warnIfMissing false a missing target is not logged: the module loader then retries the alias once the library's re-exports are merged (its target may come from a re-exported library, material.dart's MaterialStateProperty → widgets.dart's WidgetStateProperty).

Implementation

bool defineBridgeAlias(
  String aliasName,
  String targetName, {
  bool warnIfMissing = true,
}) {
  // Walk the scope chain to find the target bridged class
  BridgedClass? target;
  Environment? current = this;
  while (current != null) {
    target = current._bridgedClasses[targetName];
    if (target != null) break;
    current = current._enclosing;
  }
  if (target == null) {
    // SCI1: an alias may name an ENUM — `typedef MaterialState =
    // WidgetState;` — and the generator emits it beside the class aliases.
    // Looking among classes only defined nothing, so the alias was undefined
    // in every run.
    BridgedEnum? targetEnum;
    for (Environment? env = this; env != null; env = env._enclosing) {
      targetEnum = env._bridgedEnums[targetName];
      if (targetEnum != null) break;
    }
    if (targetEnum != null) {
      _bridgedEnumsOrNew[aliasName] = targetEnum;
      (_enumAliasTargetsRaw ??= {})[aliasName] = targetName;
      if (Logger.isDebug) {
        Logger.debug(
          "[Environment] Defined bridge alias: $aliasName -> enum $targetName",
        );
      }
      return true;
    }
    if (!warnIfMissing) return false;
    Logger.warn(
      "[Environment] Cannot register alias '$aliasName' -> '$targetName': "
      "target class or enum not found",
    );
    return false;
  }
  _bridgedClassesOrNew[aliasName] = target;
  if (Logger.isDebug) {
    Logger.debug(
      "[Environment] Defined bridge alias: $aliasName -> $targetName",
    );
  }
  return true;
}