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.
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
Requirements
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.
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