jsast.py

clew/jsast.py

The JavaScript / TypeScript dialect: definitions for the parse-built front end, and call sites.

harvest_calls

list[list[Any]] clew.jsast.harvest_calls(Any tree, bytes src_bytes, str plain, str member, str binding, str jsx="")

Version 3: member sites carry whether the receiver is an imported module.

clew/jsast.py:804

Show on the map →

Called by none

Calls _argument_bindings, _call_site, _class_ranges, _imported_names, _jsx_component, _shadowed, _tail

Source · 35 lines
def harvest_calls(
    tree: Any, src_bytes: bytes, plain: str, member: str, binding: str, jsx: str = ""
) -> list[list[Any]]:
    """@brief Harvest JS/TS call sites. @version 4"""
    from .guard.ts_parser import _imported_names

    ranges = _class_ranges(tree.root_node)
    imported = _imported_names(tree.root_node)
    sites: list[list[Any]] = []
    scopes: dict[int, dict[str, bool]] = {}
    stack = [tree.root_node]
    while stack:
        node = stack.pop()
        stack.extend(node.children)
        line = node.start_point[0] + 1
        if node.type == "call_expression":
            site = _call_site(node, ranges, plain, member, imported)
            if site is not None and not (site[1] == plain and _shadowed(node, site[0], scopes)):
                sites.append([*site[:1], line, *site[1:]])
            sites += [
                b
                for b in _argument_bindings(node, line, binding)
                if not _shadowed(node, b[0], scopes)
            ]
        elif node.type == "new_expression":
            cls = _tail(node.child_by_field_name("constructor"))
            if cls:
                sites.append(["constructor", line, member, f"{cls}.constructor", ""])
        elif jsx and node.type in _JSX_ELEMENTS:
            component = _jsx_component(node)
            if component:
                sites.append([component, line, jsx, "", ""])
    return sites

Activity · 24 steps
lines 808–808from .guard.ts_parser import _imported_nameslines 810–810ranges = _class_ranges(tree.root_node)lines 811–811imported = _imported_names(tree.root_node)lines 812–812sites: list[list[Any]] = []lines 813–813scopes: dict[int, dict[str, bool]] = {}lines 814–814stack = [tree.root_node]lines 815–815◇ while stacklines 816–816node = stack.pop()lines 817–817stack.extend(node.children)lines 818–818line = node.start_point[0] + 1lines 819–819◇ if node.type == "call_expression"lines 820–820site = _call_site(node, ranges, plain, member, imported)lines 821–821◇ if site is not None and not (site[1] == plain and _shadowed(node, site[0], scopes))lines 822–822sites.append([*site[:1], line, *site[1:]])lines 823–827sites += [ b for b in _argument_bindings(node, line, binding) if not _shadowed(node, b[0], scopes) ]lines 828–828◇ elif node.type == "new_expression"lines 829–829cls = _tail(node.child_by_field_name("constructor"))lines 830–830◇ if clslines 831–831sites.append(["constructor", line, member, f"{cls}.constructor", ""])lines 832–832◇ elif jsx and node.type in _JSX_ELEMENTSlines 833–833component = _jsx_component(node)lines 834–834◇ if componentlines 835–835sites.append([component, line, jsx, "", ""])lines 836–836return sites

Requirements

REQ-DDB-PIPE-012

harvest_definitions

dict[str, list[dict[str, Any]]] clew.jsast.harvest_definitions(Any tree, bytes src_bytes)

Version 3: the payload carries the file's line count ( lines ) for the __module__ row.

clew/jsast.py:358

Show on the map →

Called by none

Calls _module_values, _walk

Source · 20 lines
def harvest_definitions(tree: Any, src_bytes: bytes) -> dict[str, list[dict[str, Any]]]:
    """Positions are 1-based lines and 0-based columns, as doxygen writes them. A doc
    is the raw JSDoc block text; `clew/synth.py` renders it, so a rendering fix does not
    have to re-parse every file.

    @brief Harvest one file's JS/TS definitions.
    @return The definitions payload.
    """
    from .guard.ts_languages import LANGUAGE_SPECS

    spec = LANGUAGE_SPECS["typescript"]
    out: dict[str, list[dict[str, Any]]] = {"functions": [], "classes": [], "variables": []}
    _walk(tree.root_node, spec, None, out)
    bound = {f["name"] for f in out["functions"] if not f["cls"]}
    out["variables"].extend(_module_values(tree.root_node, bound))
    ## THE FILE'S LENGTH, so `synth` can write the per-file `__module__` caller (see MODULE_CALLER).
    out["lines"] = src_bytes.count(b"\n") + 1
    return out

Activity · 8 steps
lines 366–366from .guard.ts_languages import LANGUAGE_SPECSlines 368–368spec = LANGUAGE_SPECS["typescript"]lines 369–369out: dict[str, list[dict[str, Any]]] = {"functions": [], "classes": [], "variables": []}lines 370–370_walk(tree.root_node, spec, None, out)lines 371–371bound = {f["name"] for f in out["functions"] if not f["cls"]}lines 372–372out["variables"].extend(_module_values(tree.root_node, bound))lines 374–374out["lines"] = src_bytes.count(b"\n") + 1lines 375–375return out

Requirements

REQ-DDB-PIPE-012