Skip to content

Commit e9b0394

Browse files
committed
Introduce @codama/fragments with JavaScript and Rust subpaths
This PR introduces `@codama/fragments`, a public package for composing generated source code. A `Fragment` is a snippet of code paired with the imports it depends on; fragments compose through tagged templates that propagate both content and imports, so generators can build code top-down without threading imports manually. The package consolidates what the existing renderers (`@codama/renderers-js`, `@codama/renderers-rust`, `@codama/renderers-rust-cpi`, `@codama/renderers-demo`) have each been duplicating in their own `src/utils/`. Three subpaths are exposed: the root entrypoint ships only the language-agnostic primitives (`BaseFragment`, `createFragmentTemplate`, `mapFragmentContent`, `setFragmentContent`); `@codama/fragments/javascript` and `@codama/fragments/rust` each layer on a concrete `Fragment` type, an `ImportMap` shape suited to the language, and the helpers needed to build, merge, resolve, and stringify imports. Both flavors are immutable, frozen, and free-function-based. As part of this change, `@codama/renderers-core` gets a patch bump: its `src/fragment.ts` becomes a named re-export from `@codama/fragments`, so existing importers see the same names at the same path with no behavior change. Sister renderers keep their own `importMap.ts` and `fragment.ts` for now and can migrate to `@codama/fragments` in subsequent PRs.
1 parent a753939 commit e9b0394

69 files changed

Lines changed: 3789 additions & 246 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/public-falcons-joke.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@codama/fragments': minor
3+
---
4+
5+
Introduce `@codama/fragments`, a public package that bundles Codama's composable code-generation primitives. The root entrypoint exposes the language-agnostic core (`BaseFragment`, `createFragmentTemplate`, `mapFragmentContent`, `setFragmentContent`) plus a small framework of shared helpers: casing utilities (`camelCase`, `pascalCase`, `snakeCase`, `kebabCase`, `titleCase`, `capitalize`), filesystem and path helpers (`writeFile`, `readFile`, `joinPath`, `pathDirectory`, `relativePath`, …), and the `RenderMap` data structure with its pure data operations (`createRenderMap`, `addToRenderMap`, `mergeRenderMaps`, `mapRenderMapContent`, `writeRenderMap`, …). The casing helpers return plain `string`; the branded `CamelCaseString` / `PascalCaseString` / … types stay in `@codama/node-types` for spec-validation purposes.
6+
7+
JavaScript- and Rust-flavored `Fragment`, `ImportMap`, and `fragment` tagged-template helpers live under the `@codama/fragments/javascript` and `@codama/fragments/rust` subpaths. Both subpaths also expose `getDocblockFragment`, which now accepts `undefined` (in addition to `readonly string[]`) so generators can thread a node's optional `docs` attribute straight through without a ternary guard; the helper still returns `undefined` for empty or absent input, composing naturally with the `fragment` tagged template's optional-interpolation behaviour.
8+
9+
The package ships at `0.1.0` to signal pre-stability while the renderer stack settles around it.

.changeset/whole-bobcats-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-core': patch
3+
---
4+
5+
Re-export the language-agnostic fragment primitives, path helpers, filesystem helpers, and `RenderMap` data operations from `@codama/fragments`. The implementations have moved to that package so they can be shared with code generators and other consumers outside the renderers stack; existing importers of `@codama/renderers-core` continue to see every public name at the same import path with no behaviour change. `writeRenderMapVisitor` stays in `renderers-core` since it depends on the visitor + node infrastructure that `@codama/fragments` deliberately does not pull in.

packages/fragments/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

packages/fragments/.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist/
2+
test/e2e/
3+
test-ledger/
4+
target/
5+
CHANGELOG.md

0 commit comments

Comments
 (0)