|
| 1 | +# Foresta.js |
| 2 | + |
| 3 | +Foresta.js is a JavaScript library that provides a selector engine for JavaScript Abstract Syntax Trees (ASTs). It allows you to write CSS-like queries against ASTs generated by Esprima to extract specific expressions from JavaScript code. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +- Bootstrap, build, and test the repository: |
| 10 | + - `npm ci` -- installs dependencies from lockfile (takes ~2-3 seconds, NEVER CANCEL) |
| 11 | + - `npm run build` -- builds all distribution formats (takes <1 second, NEVER CANCEL) |
| 12 | + - `npm test` -- runs Jest test suite (takes <1 second, NEVER CANCEL) |
| 13 | + - `npm run test:coverage` -- runs tests with coverage report (takes <2 seconds, NEVER CANCEL) |
| 14 | + |
| 15 | +- Alternative installation method: |
| 16 | + - `npm install` -- installs dependencies and automatically runs prepare script (takes ~45-50 seconds, NEVER CANCEL) |
| 17 | + |
| 18 | +- Run examples to validate functionality: |
| 19 | + - `node examples/basic.js` -- demonstrates all major features with sample code |
| 20 | + |
| 21 | +- Development workflow: |
| 22 | + - `npm run test:watch` -- runs Jest in watch mode for development (interactive, press 'a' to run all tests, 'q' to quit) |
| 23 | + - `npm run clean` -- removes dist/ directory |
| 24 | + - `npm run prepare` -- runs clean and build (automatically triggered by npm install) |
| 25 | + |
| 26 | +## Validation |
| 27 | + |
| 28 | +- ALWAYS run through at least one complete end-to-end scenario after making changes. |
| 29 | +- Test the library functionality by running `node examples/basic.js` to verify all selector types work correctly. |
| 30 | +- Run `npm test` to ensure all 17 test cases pass. |
| 31 | +- ALWAYS run `npx eslint src/ test/ examples/ --env es6 --env node` before committing (no default eslint config exists). |
| 32 | +- The CI (.github/workflows/ci.yml) runs tests on Node.js 14.x, 16.x, 18.x, and 20.x. |
| 33 | + |
| 34 | +## Common Tasks |
| 35 | + |
| 36 | +The following are outputs from frequently run commands. Reference them instead of viewing, searching, or running bash commands to save time. |
| 37 | + |
| 38 | +### Repo root |
| 39 | +``` |
| 40 | +ls -la |
| 41 | +.github/ |
| 42 | +.gitignore |
| 43 | +README.md |
| 44 | +dist/ # Build output (created by npm run build) |
| 45 | +examples/ |
| 46 | +jest.config.json |
| 47 | +logo.png |
| 48 | +node_modules/ # Dependencies (created by npm install/ci) |
| 49 | +package-lock.json |
| 50 | +package.json |
| 51 | +rollup.config.js |
| 52 | +src/ |
| 53 | +test/ |
| 54 | +``` |
| 55 | + |
| 56 | +### Package.json scripts |
| 57 | +```json |
| 58 | +{ |
| 59 | + "scripts": { |
| 60 | + "build": "rollup -c", |
| 61 | + "test": "jest", |
| 62 | + "test:watch": "jest --watch", |
| 63 | + "test:coverage": "jest --coverage", |
| 64 | + "clean": "rm -rf dist/", |
| 65 | + "prepare": "npm run clean && npm run build" |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Key files structure |
| 71 | +- `src/foresta.js` -- Main library source code (275 lines) |
| 72 | +- `test/foresta.test.js` -- Jest test suite (17 tests covering all functionality) |
| 73 | +- `examples/basic.js` -- Comprehensive usage examples |
| 74 | +- `dist/foresta.js` -- CommonJS build output |
| 75 | +- `dist/foresta.esm.js` -- ES Module build output |
| 76 | +- `dist/foresta.umd.js` -- UMD build output for browsers |
| 77 | + |
| 78 | +### Core functionality |
| 79 | +The library provides these selector types: |
| 80 | +- `#IdentifierName` -- Select by identifier name (e.g., `#config`) |
| 81 | +- `ExpressionType` -- Select by AST node type (e.g., `Literal`, `FunctionExpression`) |
| 82 | +- `*` -- Wildcard, matches any expression type |
| 83 | +- Property modifiers -- `selector:property:modifier` (e.g., `#update:parent:value`) |
| 84 | +- Path queries -- Multi-level selectors (e.g., `Program VariableDeclaration VariableDeclarator`) |
| 85 | + |
| 86 | +### Dependencies |
| 87 | +- Runtime: `esprima` (v4.0.1) for JavaScript parsing |
| 88 | +- Dev: `jest`, `rollup`, `eslint`, `typescript`, `@types/esprima` |
| 89 | + |
| 90 | +### Build process |
| 91 | +- Uses Rollup to create 3 distribution formats from single source file |
| 92 | +- Build warning about module type is expected (can be ignored) |
| 93 | +- Outputs to `dist/` directory with automatic cleaning |
| 94 | + |
| 95 | +### Test coverage |
| 96 | +- 17 test cases covering all selector types and edge cases |
| 97 | +- ~92% statement coverage, ~74% branch coverage |
| 98 | +- Tests validate: basic functionality, literals, identifiers, property modifiers, wildcards, complex queries, edge cases |
| 99 | + |
| 100 | +### Manual validation scenarios |
| 101 | +After making changes, validate by running the example and checking that it produces: |
| 102 | +1. 3 literal values (4, 2, "hello") |
| 103 | +2. 1 instance of "theValue" identifier |
| 104 | +3. 2 global variable declarations (theValue, config) |
| 105 | +4. 1 function expression via property modifier |
| 106 | +5. 1 binary expression (4 + 2) |
| 107 | + |
| 108 | +### CI validation |
| 109 | +The GitHub Actions workflow runs: |
| 110 | +```bash |
| 111 | +npm ci |
| 112 | +npm test |
| 113 | +npm run test:coverage |
| 114 | +``` |
| 115 | + |
| 116 | +Always ensure these commands pass before pushing changes. |
0 commit comments