MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _ruby_extra_walk

Function _ruby_extra_walk

graphify/extractors/engine.py:8982–9084  ·  view source on GitHub ↗

Ruby: a constant assignment whose RHS is ``Struct.new(...)``, ``Class.new(Super)`` or ``Data.define(...)`` defines a class named after the constant (#1640). Synthesize the class node, attach block-defined methods via ``method`` (by recursing the block with the new node as parent), and em

(node, source: bytes, file_nid: str, stem: str, str_path: str,
                     nodes: list, edges: list, seen_ids: set, function_bodies: list,
                     parent_class_nid: str | None, add_node, add_edge, walk,
                     callable_def_nids: set, callable_class_nids: set,
                     ruby_namespace: list, ruby_lexical_scopes: list)

Source from the content-addressed store, hash-verified

8980_RUBY_CLASS_FACTORIES = frozenset({("Struct", "new"), ("Class", "new"), ("Data", "define")})
8981
8982def _ruby_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,
8983 nodes: list, edges: list, seen_ids: set, function_bodies: list,
8984 parent_class_nid: str | None, add_node, add_edge, walk,
8985 callable_def_nids: set, callable_class_nids: set,
8986 ruby_namespace: list, ruby_lexical_scopes: list) -> bool:
8987 """Ruby: a constant assignment whose RHS is ``Struct.new(...)``,
8988 ``Class.new(Super)`` or ``Data.define(...)`` defines a class named after the
8989 constant (#1640). Synthesize the class node, attach block-defined methods via
8990 ``method`` (by recursing the block with the new node as parent), and emit an
8991 ``inherits`` edge for ``Class.new(Super)``. Returns True if handled.
8992 """
8993 if node.type != "assignment":
8994 return False
8995 left = node.child_by_field_name("left")
8996 right = node.child_by_field_name("right")
8997 if left is None or right is None or left.type != "constant" or right.type != "call":
8998 return False
8999 recv = right.child_by_field_name("receiver")
9000 meth = right.child_by_field_name("method")
9001 if recv is None or meth is None or recv.type != "constant":
9002 return False
9003 if (_read_text(recv, source), _read_text(meth, source)) not in _RUBY_CLASS_FACTORIES:
9004 return False
9005
9006 const_name = _read_text(left, source)
9007 if not const_name:
9008 return False
9009 # Qualify the factory-defined const against the enclosing scope, mirroring
9010 # the generic class branch (#2302): `module Billing; Invoice = Struct.new`
9011 # labels `Billing::Invoice`.
9012 const_segments = const_name.split("::")
9013 const_name = "::".join(ruby_namespace + const_segments)
9014 line = node.start_point[0] + 1
9015 class_nid = _make_id(stem, const_name)
9016 # Class factories can install methods through block/runtime behavior that
9017 # the narrow inherited-call proof does not model.
9018 add_node(
9019 class_nid,
9020 const_name,
9021 line,
9022 metadata={"ruby_lookup_unsafe": True},
9023 )
9024 callable_def_nids.add(class_nid) # a class is callable (its constructor)
9025 callable_class_nids.add(class_nid) # ...but only via its constructor (#2137)
9026 # Mirror the generic class branch: containment always hangs off the file node.
9027 add_edge(file_nid, class_nid, "contains", line)
9028
9029 # `Class.new(Super)` — the first positional constant argument is the superclass.
9030 if _read_text(recv, source) == "Class":
9031 args = next((c for c in right.children if c.type == "argument_list"), None)
9032 if args is not None:
9033 for arg in args.children:
9034 if arg.type in ("constant", "scope_resolution"):
9035 base = _ruby_const_last_name(arg, source)
9036 raw_base = _ruby_const_full_name(arg, source)
9037 if base:
9038 base_nid = _make_id(stem, base)
9039 if base_nid not in seen_ids:

Callers 1

walkFunction · 0.85

Calls 8

_read_textFunction · 0.90
_make_idFunction · 0.90
_ruby_const_last_nameFunction · 0.85
_ruby_const_full_nameFunction · 0.85
add_nodeFunction · 0.70
add_edgeFunction · 0.70
walkFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected