property_keys.py¶
clew/property_keys.py
Every JS/TS property key a symbol defines or assigns, with its holder (F1-mm; 2.0.0).
ensure_property_keys_table
None clew.property_keys.ensure_property_keys_table(sqlite3.Connection conn)
Create the property_keys table.
Source · 21 lines
def ensure_property_keys_table(conn: sqlite3.Connection) -> None:
"""Created unconditionally, so a tree with no JS/TS yields an EMPTY table, not an absent one.
@brief Create the table.
@return None.
"""
conn.executescript(
"""
CREATE TABLE IF NOT EXISTS property_keys (
path_rowid INTEGER NOT NULL REFERENCES path(rowid),
key TEXT NOT NULL,
line INTEGER NOT NULL,
holder TEXT NOT NULL,
kind TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_property_keys_key ON property_keys(key);
CREATE INDEX IF NOT EXISTS idx_property_keys_path ON property_keys(path_rowid, line);
"""
)
Activity · 1 steps
Requirements
harvest
Any clew.property_keys._PropertyKeyHarvester.harvest(self, Any tree, bytes src_bytes)
Harvest one file's key sites.
Source · 23 lines
def harvest(self, tree: Any, src_bytes: bytes) -> Any:
"""A WALK THAT CARRIES THE HOLDER: each node is visited with the nearest enclosing holder's
name, so a key nested three literals deep inside `DEFAULT_CONFIG` is still held by it.
@brief Walk the tree.
@return Rows.
"""
from .jsast import is_js_tree
if not is_js_tree(tree):
return []
found: list[list[Any]] = []
stack: list[tuple[Any, str]] = [(tree.root_node, "")]
while stack:
node, holder = stack.pop()
here = _holder_name(node) or holder
site = _site(node)
if site is not None and holder:
found.append([site[0], node.start_point[0] + 1, holder, site[1]])
stack.extend((child, here) for child in node.children)
return sorted(found, key=lambda r: (r[1], r[0]))
Activity · 13 steps
Requirements
import_property_keys
int clew.property_keys.import_property_keys(Path db_path, Path repo_root, IndexCache|None cache=None)
Record every indexed JS/TS file's property-key sites.
Called by _build_stages, run_real_stages
Calls ensure_property_keys_table, property_key_harvester, run_harvest
Source · 20 lines
def import_property_keys(db_path: Path, repo_root: Path, cache: IndexCache | None = None) -> int:
"""@brief Fill property_keys. @return Row count."""
conn = sqlite3.connect(str(db_path))
try:
ensure_property_keys_table(conn)
conn.execute("DELETE FROM property_keys")
rows = [
(src, key, line, holder, kind)
for src, payload in run_harvest(conn, repo_root, property_key_harvester(), cache)
for key, line, holder, kind in payload or ()
]
conn.executemany(
"INSERT INTO property_keys (path_rowid, key, line, holder, kind) VALUES (?, ?, ?, ?, ?)",
rows,
)
conn.commit()
finally:
conn.close()
logger.info("property keys: %d site(s)", len(rows))
return len(rows)1 more lines, not shown
Activity · 9 steps
Requirements
property_key_harvester
Harvester clew.property_keys.property_key_harvester()
The property-key stage's harvester.
Source · 5 lines
def property_key_harvester() -> Harvester:
"""ONE CONSTRUCTION SITE, shared with the plan's parse pass. @brief Build it. @return It."""
return _PropertyKeyHarvester()