litscript icon indicating copy to clipboard operation
litscript copied to clipboard

Fix: Add dependency-based region resolution and JSDoc improvements

Open Tjorbenn opened this issue 3 months ago • 0 comments

Hey @johtela,

I made this PR to tackle some annoying bugs and limitations with regions and JSDoc comments that have been bugging me since I was working a little more with LitScript. I hope you like my fixes and I hope I could help you a bit :)

The Problems:

  • @ Symbols Broke Everything TypeScript's parser has this quirk where it treats every @ in a JSDoc comment as a tag, even when you're just writing an email address or a markdown reference. This meant documentation would crash or get truncated whenever you wrote something like @username.

  • Regions Had to Be Defined First You couldn't reference a region before defining it or reliably reference something from another file. This was frustrating because sometimes you want to show the high-level structure first and fill in details later. This made a literate programming approach less feasible.

  • JSDoc Comments Showed Up Twice JSDoc would appear both as prose in the documentation and in the code blocks. Not a huge deal, but definitely cluttered things up.

  • No Nested Region References If region A referenced region B, and B referenced C, it wouldn't work. You could only go one level deep, which really limited how you could organize documentation.

What Changed:

  • Fixed the @ Symbol Issue Instead of using TypeScript's .comment property (which truncates at @), I'm now using .getText() to grab the full source and parsing it manually. Also added a showJSDocTags config option (defaults to false) so you can show those @param and @returns tags only if you want.

  • Multi-Phase Processing for Forward References Completely rewrote how files get processed. Now it happens in 5 phases:

    1. Scan all TypeScript files to find region definitions
    2. Parse TypeScript files and create placeholders where regions are referenced
    3. Parse markdown files (same placeholder approach)
    4. Build a dependency graph and figure out the right order to expand everything
    5. Output the final files with all references resolved

    The key insight was to defer the actual expansion until we know about all regions, then resolve them in the right order.

  • Dependency Graph & Topological Sorting Added a RegionResolver class that builds a dependency graph and does a topological sort to figure out the right order to expand regions. It also catches circular dependencies and gives you a helpful error message showing exactly where the cycle is.

  • Unlimited Region Nesting You can now nest regions theoretically as deep as you want. The resolver expands them bottom-up.

  • JSDoc Cleanup Two-pass approach: first collect where all the JSDoc comments are, then skip them when parsing trivia. This way they only show up as documentation prose, not in code blocks.

Tjorbenn avatar Nov 14 '25 19:11 Tjorbenn