feat: add functional CodeBlock system for code generation#67
Open
feat: add functional CodeBlock system for code generation#67
Conversation
Introduce a React-like approach where handlers return code blocks instead of imperatively mutating a CodeBuilder. - Add CodeBlock class with automatic indentation handling - Add `code` tagged template for composing blocks - Add helper functions: ifBlock, ifElseBlock, forBlock, compose - Create type-functional.ts as proof of concept This enables cleaner, more testable code generation where each handler is a pure function returning its generated code.
Remove helper functions (ifBlock, forBlock, etc.) - the `code` template literal is sufficient for all patterns. Just write the code you want to generate. Also update type-functional.ts to use pure `code` templates with inline if statements instead of helpers.
Contributor
Benchmark ResultsOnly tests where both validators pass are compared (apples-to-apples) tjs vs ajv
Overall: 🟢 tjs is 2.94× faster on 6602 shared tests tjs vs is-my-json-valid
Overall: 🟢 tjs is 2.38× faster on 3702 shared tests tjs vs djv
Overall: 🟢 tjs is 10.07× faster on 3717 shared tests tjs vs jsen
Overall: 🟢 tjs is 2.40× faster on 3744 shared tests tjs vs schemasafe
Overall: 🟢 tjs is 1.45× faster on 6344 shared tests |
Instead of manually writing if/error blocks:
code`if (!(${check})) { ${genError(...)} }`
Use the fail.unless primitive:
fail.unless(check, ctx, 'keyword', 'message', params)
Takes the PASSING condition and negates internally,
avoiding operator precedence bugs with manual negation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce a React-like approach where handlers return code blocks
instead of imperatively mutating a CodeBuilder.
codetagged template for composing blocksThis enables cleaner, more testable code generation where each
handler is a pure function returning its generated code.