|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +npm run build # compile CJS + ESM into lib/ (via tsup) |
| 9 | +npm run watch # build in watch mode during development |
| 10 | +npm run typecheck # tsc --noEmit |
| 11 | +npm run lint # eslint --fix |
| 12 | +npm run check # lint + typecheck |
| 13 | +npm test # jest --ci |
| 14 | +npm run testWatch # jest --watchAll |
| 15 | +``` |
| 16 | + |
| 17 | +Run a single test file: `npx jest tests/query.test.ts` |
| 18 | + |
| 19 | +Consumers import from `lib/`, not `src/` — always run `npm run build` before testing integration or publishing. |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +**Purpose:** `query(code, query)` and `multiQuery(code, namedQueries)` parse JavaScript source and run a compact XPath-inspired query language over the resulting AST. |
| 24 | + |
| 25 | +**Execution flow:** |
| 26 | +1. `parseSource` (in `src/index.ts`) parses JS via `meriyah` — tries `module: true`, falls back to `module: false, webcompat: true`. |
| 27 | +2. `createTraverser()` walks the AST once, building `NodePath` wrappers (via `createNodePath`, backed by a WeakMap) and registering scopes/bindings. |
| 28 | +3. The query string is tokenized and parsed by `src/parseQuery.ts` into a tree of `QNode`s. |
| 29 | +4. `createQuerier()` evaluates QNodes against NodePaths, memoizing subquery results by `(QNode, NodePath)` pair. |
| 30 | + |
| 31 | +**Key source files:** |
| 32 | +- [src/index.ts](src/index.ts) — traversal, binding registration (`registerBindings`, `getBinding`), querier, built-in `functions` map (`join`, `concat`, `first`, `nthchild`), public API |
| 33 | +- [src/parseQuery.ts](src/parseQuery.ts) — tokenizer and parser for the query DSL; defines `AvailableFunction` type |
| 34 | +- [src/nodeutils.ts](src/nodeutils.ts) — `VISITOR_KEYS`, type predicates (`isIdentifier`, `isScopable`, etc.), `NodePath` helpers |
| 35 | +- [src/utils.ts](src/utils.ts) — small generic utilities |
| 36 | + |
| 37 | +**Build output:** `tsup` produces both CJS (`lib/index.js`) and ESM (`lib/index.mjs`) plus `.d.ts`/`.d.mts` files. The `exports` map in `package.json` routes `require`/`import` to the right build. |
| 38 | + |
| 39 | +## Key conventions |
| 40 | + |
| 41 | +**Adding a query language function** (e.g. `/fn:uppercase(sel)`): |
| 42 | +1. Add implementation to the `functions` map in `src/index.ts`. |
| 43 | +2. The `AvailableFunction` type in `parseQuery.ts` is derived from `typeof functions` — no manual update needed unless you change the type structure. |
| 44 | +3. Add tests in `tests/`. |
| 45 | + |
| 46 | +**Bindings and scopes:** Scope IDs are stored on AST nodes as `extra.scopeId`. The `nodePathMap` WeakMap ties AST nodes to their `NodePath`. Do not mutate node identity or add per-node state — this silently breaks memoization and binding lookups. |
| 47 | + |
| 48 | +**Performance (large/minified files):** |
| 49 | +- Use `multiQuery` instead of repeated `query` calls — the traverser is designed for a single pass over multiple queries. |
| 50 | +- Prefer child selectors (`/Type`) over descendant (`//Type`) when the depth is known. |
| 51 | +- Avoid `../` (parent) filters in hot paths — they cause extra traversal and defeat memoization. |
0 commit comments