getValue method

dynamic getValue(
  1. Name node
)

Implementation

dynamic getValue(Name node) {
  for (final _LexicalContext lexical in _lexicalContexts.reversed) {
    if (lexical.has(node.value)) {
      return lexical.get(node.value, node.line ?? 1);
    }
  }

  // 1) Dynamic object scopes from `with` shadow lexical scopes.
  for (final Context ctx in _dynamicContexts.reversed) {
    if (ctx.hasContext(node.value)) {
      return ctx.getContextById(node.value);
    }
  }

  // 2) Try the node's own scope next (captures nested function declarations).
  if (node.scope != null) {
    final Context scopedCtx = getContextForScope(node.scope!);
    if (scopedCtx.hasContext(node.value)) {
      return scopedCtx.getContextById(node.value);
    }
  }

  // 3) Scan all known contexts (newest first) as a safety net.
  for (final Scope s in contexts.keys.toList().reversed) {
    if (contexts[s]!.hasContext(node.value)) {
      return contexts[s]!.getContextById(node.value);
    }
  }

  // 4) Fallback to the resolved scope via environment tracking.
  Scope scope = findScope(node);
  Context ctx = getContextForScope(scope);
  return ctx.getContextById(node.value);
}