tjs-lang 0.7.6 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CLAUDE.md +101 -26
  2. package/bin/docs.js +4 -1
  3. package/demo/docs.json +46 -12
  4. package/demo/src/examples.test.ts +1 -0
  5. package/demo/src/imports.test.ts +16 -4
  6. package/demo/src/imports.ts +60 -15
  7. package/demo/src/playground-shared.ts +9 -8
  8. package/demo/src/tfs-worker.js +205 -147
  9. package/demo/src/tjs-playground.ts +34 -10
  10. package/demo/src/ts-playground.ts +24 -8
  11. package/dist/index.js +140 -119
  12. package/dist/index.js.map +4 -4
  13. package/dist/src/lang/bool-coercion.d.ts +50 -0
  14. package/dist/src/lang/docs.d.ts +31 -6
  15. package/dist/src/lang/linter.d.ts +8 -0
  16. package/dist/src/lang/parser-transforms.d.ts +18 -0
  17. package/dist/src/lang/parser-types.d.ts +2 -0
  18. package/dist/src/lang/parser.d.ts +9 -0
  19. package/dist/src/lang/runtime.d.ts +34 -0
  20. package/dist/src/lang/types.d.ts +9 -1
  21. package/dist/src/rbac/index.d.ts +1 -1
  22. package/dist/src/vm/runtime.d.ts +1 -1
  23. package/dist/tjs-eval.js +44 -39
  24. package/dist/tjs-eval.js.map +4 -4
  25. package/dist/tjs-from-ts.js +20 -20
  26. package/dist/tjs-from-ts.js.map +3 -3
  27. package/dist/tjs-lang.js +86 -80
  28. package/dist/tjs-lang.js.map +4 -4
  29. package/dist/tjs-vm.js +50 -45
  30. package/dist/tjs-vm.js.map +4 -4
  31. package/llms.txt +79 -0
  32. package/package.json +3 -2
  33. package/src/cli/commands/convert.test.ts +16 -21
  34. package/src/lang/bool-coercion.test.ts +203 -0
  35. package/src/lang/bool-coercion.ts +314 -0
  36. package/src/lang/codegen.test.ts +177 -0
  37. package/src/lang/docs.test.ts +328 -1
  38. package/src/lang/docs.ts +424 -24
  39. package/src/lang/emitters/ast.ts +11 -12
  40. package/src/lang/emitters/dts.test.ts +41 -0
  41. package/src/lang/emitters/dts.ts +9 -0
  42. package/src/lang/emitters/js-tests.ts +16 -4
  43. package/src/lang/emitters/js.ts +208 -2
  44. package/src/lang/features.test.ts +22 -0
  45. package/src/lang/inference.ts +54 -0
  46. package/src/lang/linter.test.ts +104 -1
  47. package/src/lang/linter.ts +124 -1
  48. package/src/lang/parser-params.ts +31 -0
  49. package/src/lang/parser-transforms.ts +539 -6
  50. package/src/lang/parser-types.ts +2 -0
  51. package/src/lang/parser.test.ts +73 -1
  52. package/src/lang/parser.ts +85 -1
  53. package/src/lang/runtime.ts +98 -0
  54. package/src/lang/tests.ts +21 -8
  55. package/src/lang/types.ts +6 -0
  56. package/src/rbac/index.ts +2 -2
  57. package/src/rbac/rules.tjs.d.ts +9 -0
  58. package/src/vm/atoms/batteries.ts +2 -2
  59. package/src/vm/runtime.ts +10 -3
  60. package/dist/src/rbac/rules.d.ts +0 -184
  61. package/src/rbac/rules.js +0 -338
package/CLAUDE.md CHANGED
@@ -12,6 +12,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
12
12
  - **AJS** — Agent language that compiles to JSON AST for safe, sandboxed execution with fuel limits and injected capabilities. Code travels to data.
13
13
  - **Toolchain** — Compresses transpilation, linting, testing, and documentation generation into one pass. Includes inline WASM with SIMD, polymorphic dispatch, local class extensions, and a browser-based playground.
14
14
 
15
+ > **TJS syntax is NOT TypeScript.** Full reference: [`CLAUDE-TJS-SYNTAX.md`](CLAUDE-TJS-SYNTAX.md). The single most common LLM mistake is treating `function foo(x: 'default')` as a TypeScript string-literal type. It is _not_ — the colon value is an **example**, and `'default'` widens to `string`. See the syntax doc before writing or modifying TJS source.
16
+
15
17
  ## Common Commands
16
18
 
17
19
  ```bash
@@ -52,13 +54,7 @@ bun run docs # Generate documentation
52
54
  # Build standalone CLI binaries
53
55
  bun run build:cli # Compiles tjs + tjsx to dist/
54
56
 
55
- # Compatibility testing (transpile popular TS libraries with fromTS)
56
- bun scripts/compat-zod.ts # Zod validation library
57
- bun scripts/compat-effect.ts # Effect (HKTs, intersections)
58
- bun scripts/compat-radash.ts # Radash utilities
59
- bun scripts/compat-superstruct.ts # Superstruct validation
60
- bun scripts/compat-ts-pattern.ts # ts-pattern matching
61
- bun scripts/compat-kysely.ts # Kysely SQL builder
57
+ # Compatibility testing — see scripts/compat-*.ts (zod, effect, radash, superstruct, ts-pattern, kysely)
62
58
 
63
59
  # Deployment (Firebase)
64
60
  bun run deploy # Build demo + deploy functions + hosting
@@ -72,15 +68,15 @@ bun run functions:serve # Local functions emulator
72
68
  ### Two-Layer Design
73
69
 
74
70
  1. **Builder Layer** (`src/builder.ts`): Fluent API that constructs AST nodes. Contains no execution logic.
75
- 2. **Runtime Layer** (`src/vm/runtime.ts`): Executes AST nodes. Contains all atom implementations (~2900 lines, security-critical).
71
+ 2. **Runtime Layer** (`src/vm/runtime.ts`): Executes AST nodes. Contains all atom implementations (~3024 lines, security-critical).
76
72
 
77
73
  ### Key Source Files
78
74
 
79
75
  - `src/index.ts` - Main entry, re-exports everything
80
- - `src/vm/runtime.ts` - All atom implementations, expression evaluation, fuel charging (~3000 lines, security-critical)
81
- - `src/vm/vm.ts` - AgentVM class (~226 lines)
76
+ - `src/vm/runtime.ts` - All atom implementations, expression evaluation, fuel charging (~3024 lines, security-critical)
77
+ - `src/vm/vm.ts` - AgentVM class (~247 lines)
82
78
  - `src/vm/atoms/batteries.ts` - Battery atoms (vector search, LLM, store operations)
83
- - `src/builder.ts` - TypedBuilder fluent API (~19KB)
79
+ - `src/builder.ts` - TypedBuilder fluent API (~757 lines / ~19KB)
84
80
  - `src/lang/parser.ts` - TJS parser with colon shorthand, unsafe markers, return type extraction
85
81
  - `src/lang/parser-transforms.ts` - Type, Generic, and FunctionPredicate block/function form transforms
86
82
  - `src/lang/emitters/ast.ts` - Emits Agent99 AST from parsed source
@@ -97,7 +93,7 @@ bun run functions:serve # Local functions emulator
97
93
  - `src/batteries/` - LM Studio integration (lazy init, model audit, vector search)
98
94
  - `src/store/` - Store implementations for persistence
99
95
  - `src/rbac/` - Role-based access control
100
- - `src/use-cases/` - Integration tests and real-world examples (28 test files)
96
+ - `src/use-cases/` - Integration tests and real-world examples (30 test files)
101
97
  - `src/cli/tjs.ts` - CLI tool for check/run/types/emit/convert/test commands
102
98
  - `src/cli/tjsx.ts` - JSX/component runner
103
99
  - `src/cli/playground.ts` - Local playground server
@@ -138,6 +134,9 @@ import { Agent, AgentVM, ajs, tjs } from 'tjs-lang' // Main entry
138
134
  import { Eval, SafeFunction } from 'tjs-lang/eval' // Safe eval utilities
139
135
  import { tjs, transpile } from 'tjs-lang/lang' // Language tools only
140
136
  import { fromTS } from 'tjs-lang/lang/from-ts' // TypeScript transpilation
137
+ import { AgentVM } from 'tjs-lang/vm' // VM only (smaller bundle)
138
+ import { batteryAtoms } from 'tjs-lang/batteries' // LM Studio batteries
139
+ // Editor integrations: 'tjs-lang/editors/monaco', '/codemirror', '/ace'
141
140
  ```
142
141
 
143
142
  ### Transpiler Chain (TS → TJS → JS)
@@ -246,7 +245,7 @@ AJS expressions behave differently from JavaScript in several important ways:
246
245
  - Unit tests alongside source files (`*.test.ts`)
247
246
  - Integration tests in `src/use-cases/` (RAG, orchestration, malicious actors)
248
247
  - Security tests in `src/use-cases/malicious-actor.test.ts`
249
- - Language tests split across 14 files in `src/lang/` (lang.test.ts, features.test.ts, codegen.test.ts, parser.test.ts, from-ts.test.ts, wasm.test.ts, etc.)
248
+ - Language tests split across 17 files in `src/lang/` (lang.test.ts, features.test.ts, codegen.test.ts, parser.test.ts, from-ts.test.ts, wasm.test.ts, etc.)
250
249
 
251
250
  Coverage targets: 98% lines on `src/vm/runtime.ts` (security-critical), 80%+ overall.
252
251
 
@@ -399,6 +398,66 @@ The playground is hosted on Firebase (`tjs-platform.web.app`). Demo build output
399
398
 
400
399
  The `docs/` directory contains real documentation (markdown), not build artifacts. See `docs/README.md` for the documentation index.
401
400
 
401
+ ### Playground Examples
402
+
403
+ The playground (https://tjs-platform.web.app) shows interactive TJS and AJS examples in a navigable sidebar. Examples live as markdown files with embedded code blocks, NOT as raw `.tjs` files.
404
+
405
+ **Where they live:**
406
+
407
+ - TJS playground examples: `guides/examples/tjs/<slug>.md`
408
+ - AJS playground examples: `guides/examples/ajs/<slug>.md` (assumed parallel structure)
409
+
410
+ **File format:**
411
+
412
+ <!-- prettier-ignore -->
413
+ ```markdown
414
+ <!--{"section":"tjs","type":"example","group":"basics","order":16}-->
415
+
416
+ # Example Title
417
+
418
+ Short intro paragraph (plain markdown).
419
+
420
+ ​```tjs
421
+ /*#
422
+ ## Optional H2 — markdown rendered above the code in the playground
423
+ Explain the concept here. Use markdown freely.
424
+ */
425
+
426
+ // Then the actual TJS code
427
+ function demo() { ... }
428
+
429
+ test 'a description' {
430
+ expect(...).toBe(...)
431
+ }
432
+ ​```
433
+ ```
434
+
435
+ Frontmatter fields: `section` (`tjs`/`ajs`), `type: "example"`, `group` (`basics`/`advanced`/etc.), `order` (numeric, controls sidebar position). The H1 becomes the example title in the nav.
436
+
437
+ **Registration:**
438
+
439
+ Examples are auto-discovered by `bin/docs.js` (run via `bun run docs`), which walks the markdown tree, parses frontmatter, extracts the `tjs`/`ajs` code block, and writes the result to `demo/docs.json`. The demo loads `docs.json` at runtime — no other registration step.
440
+
441
+ **After adding/editing an example:** run `bun run docs` and commit the regenerated `demo/docs.json` alongside the `.md` file. (The docs builder also runs as part of `bun run build:demo` and `bun run deploy`.)
442
+
443
+ **Testing playground examples:**
444
+
445
+ The CLI (`bun src/cli/tjs.ts run`) does NOT inject the test-block `expect` harness — that's a playground-only thing. So running an extracted code block via the CLI prints "expect is not defined" for any `test { expect(...) }` blocks even though they pass in the playground. To verify an example:
446
+
447
+ 1. **Console-log behavior** (works via CLI): extract the `tjs` code block and run it.
448
+
449
+ ````bash
450
+ awk '/^```tjs$/{flag=1; next} /^```$/{flag=0} flag' \
451
+ guides/examples/tjs/<slug>.md > /tmp/example.tjs
452
+ bun src/cli/tjs.ts run /tmp/example.tjs
453
+ ````
454
+
455
+ Verify the printed output matches the expected behavior shown in the example's comments.
456
+
457
+ 2. **Test blocks**: spin up the dev server (`bun run start`) and load the example in the playground UI to confirm tests pass under the real `expect` harness.
458
+
459
+ 3. **Frontmatter / registration**: after `bun run docs`, grep `demo/docs.json` for the slug to confirm it was picked up with the right `section`/`group`/`order`.
460
+
402
461
  ### Additional Directories
403
462
 
404
463
  - `tjs-src/` — TJS runtime written in TJS itself (self-hosting)
@@ -408,31 +467,47 @@ The `docs/` directory contains real documentation (markdown), not build artifact
408
467
 
409
468
  ### Additional Documentation
410
469
 
470
+ - `llms.txt` — agent-facing navigation index (ships in npm bundle); points to docs and source entry points
471
+ - `guides/footguns.md` — JS footguns TJS fixes (boxed-primitive truthiness, `==` coercion, `typeof null`, uninitialized `let`, etc.). Demo: `examples/js-footguns-fixed.tjs`.
472
+ - `guides/playground-imports.md` — how the playground/dev-server resolves bare imports: TFS service worker, default JSDelivr `/+esm` routing, esm.sh allowlist for peer-dep packages (React), CDN hints (`jsdelivr/`, `esmsh/`, `unpkg/`, `github/`), and full-URL passthrough.
473
+ - `README.md` — Project intro, install, quick start
411
474
  - `DOCS-TJS.md` — TJS language guide
412
475
  - `DOCS-AJS.md` — AJS runtime guide
413
476
  - `TJS-FOR-JS.md` — TJS guide for JavaScript developers (syntax differences, gotchas)
414
477
  - `TJS-FOR-TS.md` — TJS guide for TypeScript developers (migration, interop)
415
478
  - `CONTEXT.md` — Architecture deep dive
416
- - `AGENTS.md` — Agent workflow instructions (issue tracking with `bd`, mandatory push-before-done)
479
+ - `AGENTS.md` — Agent workflow instructions (session-completion checklist, push-before-done rule)
480
+ - `TODO.md` — Open work, organized by area; move items to the **Completed** section when done
417
481
  - `PLAN.md` — Roadmap
482
+ - `MANIFESTO-BUILDER.md` / `MANIFESTO-ENTERPRISE.md` — Positioning docs (audience-targeted pitches)
483
+ - `benchmarks.md` — Top-level benchmark results (separate from `guides/benchmarks.md`)
418
484
 
419
- ### Issue Tracking with `bd`
485
+ ### Keeping This File and `llms.txt` Current
420
486
 
421
- The project uses `bd` (beads) for issue tracking. Key commands:
487
+ Update both files when you change something an agent needs to discover:
422
488
 
423
- ```bash
424
- bd ready # Find available work
425
- bd show <id> # View issue details
426
- bd update <id> --status in_progress # Claim work
427
- bd close <id> # Complete work
428
- bd sync # Sync with git
429
- ```
489
+ - **New top-level markdown doc** → add to "Additional Documentation" here AND to the appropriate section of `llms.txt`.
490
+ - **New package entry point** (subpath export in `package.json`) → add to "Package Entry Points" here AND to "Package entry points" in `llms.txt`.
491
+ - **New CLI command or `bun run` script** → add to "Common Commands".
492
+ - **Renamed or moved key source file** → update "Key Source Files" here AND "Source map" in `llms.txt`.
493
+ - **New language mode / safety directive** → add to the TJS Syntax Reference section.
494
+ - **New playground example** → add to `guides/examples/{tjs,ajs}/<slug>.md`, then `bun run docs` to regenerate `demo/docs.json`. See "Playground Examples" above.
495
+
496
+ Skip stale-prone precision (exact line counts, file sizes) for new entries — they drift silently. The existing `~3024` etc. are kept current opportunistically, not on every commit.
497
+
498
+ ### Tracking Work
499
+
500
+ Work is tracked in plain markdown — no external issue tracker. Open items live in `TODO.md` (organized by area). When you start a task, find or add the relevant entry; when you finish, check the box and (for substantial work) move it to the Completed section with a short note.
501
+
502
+ ### Landing the Plane (Session Completion Checklist)
430
503
 
431
- **Critical rule**: Work is NOT complete until `git push` succeeds. Never stop before pushing work stranded locally is work lost. If push fails, resolve and retry.
504
+ See `AGENTS.md` for the canonical session-completion checklist. Hard rule: work is not complete until `git push` succeeds never stop before pushing, never `--no-verify` to bypass hooks.
432
505
 
433
- ### Known Gotcha: `tjs()` Returns an Object, Not a String
506
+ ### Common Gotchas
434
507
 
435
- `tjs(source)` returns `{ code, types, metadata, testResults, ... }`. Use `.code` to get the transpiled JavaScript string. This is a common mistake.
508
+ - **`tjs(source)` returns an object, not a string.** It returns `{ code, types, metadata, testResults, ... }` use `.code` for the transpiled JS string.
509
+ - **Prettier mangles bare-expression code blocks in markdown.** Code blocks tagged ` ```js` get reformatted; bare expressions like `'5' == 5` and `[1] == 1` on consecutive lines collapse into one expression with ASI guards. Use `<!-- prettier-ignore -->` directly above the code fence to preserve hand-formatted JS examples (or tag the block as `text`/`tjs`/`ts` instead — Prettier ignores those).
510
+ - **`tjs-lang` package alias only works inside the project** (set in `bunfig.toml`). Test scripts written in `/tmp` won't resolve `import { tjs } from 'tjs-lang/lang'` to the local source — they'll resolve to whatever's in `node_modules`. For ad-hoc experiments outside the repo, use absolute paths: `import { tjs } from '/Users/.../tjs-lang/src/lang/index'`.
436
511
 
437
512
  ### Running Emitted TJS Code
438
513
 
package/bin/docs.js CHANGED
@@ -54,7 +54,10 @@ function extractDescription(content) {
54
54
  }
55
55
 
56
56
  function metadata(content, filePath) {
57
- let source = content.match(/<\!\-\-(\{.*\})\-\->|\/\*(\{.*\})\*\//)
57
+ // Only the FIRST non-blank line may be frontmatter. Matching anywhere in
58
+ // the file produces false positives when docs document the format itself
59
+ // (e.g. CLAUDE.md showing an example of <!--{...}--> ).
60
+ let source = content.match(/^\s*(?:<\!\-\-(\{.*\})\-\->|\/\*(\{.*\})\*\/)/)
58
61
  let data = {}
59
62
  if (source) {
60
63
  try {