isDefined method

bool isDefined(
  1. String name
)

Whether name is defined anywhere in this environment's chain, WITHOUT evaluating it (SCG6).

lookup answers the same question but returns the VALUE, and for a registered global getter that means calling the host's getter. A pre-execution check that asks about every candidate name must not run host code, so it asks here. Every scope lookup consults is consulted: prefixed imports, values (a getter counts, uncalled), bridged classes and bridged enums, and an ambiguous name counts as defined — the ambiguity is reported where it is used.

Implementation

bool isDefined(String name) {
  // `prefix.Name` (PR #8): a relationship clause written with an import
  // prefix is asked about by its qualified name. It is defined when the
  // nearest scope importing [prefix] defines `Name`; an unknown prefix is
  // not defined, never a fallback to the bare name.
  final dot = name.indexOf('.');
  if (dot > 0) {
    final prefix = name.substring(0, dot);
    for (Environment? env = this; env != null; env = env._enclosing) {
      final imported = env._prefixedImports[prefix];
      if (imported != null) {
        return imported.isDefined(name.substring(dot + 1));
      }
    }
    return false;
  }
  for (Environment? env = this; env != null; env = env._enclosing) {
    if (env._prefixedImports.containsKey(name) ||
        env._values.containsKey(name) ||
        env._bridgedClasses.containsKey(name) ||
        env._bridgedEnums.containsKey(name) ||
        (env._ambiguousBridgeNamesRaw?.containsKey(name) ?? false)) {
      return true;
    }
  }
  return false;
}