feat: add genExportDefault for ESM export default support
Closes #77
Problem
knitwork can generate import, export, and dynamic import() statements, but there is no helper for generating export default <expression>.
Changes
Adds genExportDefault(expression) which returns an export default <expression>; statement.
The expression is used as-is (raw, not escaped or quoted), consistent with knitwork's existing raw-expression helpers such as genObjectFromRaw. Serialization of runtime values is left to the caller, e.g. genObjectFromValues(...) or JSON.stringify(...). This keeps the API unambiguous and composable rather than guessing whether a string is a value or an expression.
genExportDefault("foo");
// ~> export default foo;
genExportDefault("() => {}");
// ~> export default () => {};
genExportDefault(genObjectFromValues({ foo: "bar" }));
// ~> export default { foo: "bar" };
src/esm.ts: implement and documentgenExportDefaulttest/esm.test.ts: add cases for identifiers, objects, arrow functions, strings, and numbersREADME.md: regenerated via automd from the new JSDoc
Validation
pnpm vitest runβ 64 tests passing (5 new)pnpm lintβ eslint + prettier cleanpnpm buildβ unbuild succeeds,genExportDefaultexported in both ESM and CJS
Summary by CodeRabbit
-
New Features
- Added a new helper to generate ESM
export defaultstatements from a provided expression. - The helper supports a range of valid JavaScript expressions, including objects, functions, strings, and numbers.
- Added a new helper to generate ESM
-
Documentation
- Expanded the README with clearer usage guidance and examples showing the generated output format.
- Added a note explaining that expressions are inserted directly, along with a recommendation to use serialization helpers when needed.
-
Tests
- Added coverage for multiple expression types to verify the generated output matches expectations.
No actionable comments were generated in the recent review. π
βΉοΈ Recent review info
βοΈ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 50db1ab0-11f5-4385-8e2a-14c2a76ddc7b
π₯ Commits
Reviewing files that changed from the base of the PR and between a95f4d421ffa602723cb5ac2606bd854e1dfc49a and b45dcb42f9018b32e0148c57e5b70123ea4d272e.
π Files selected for processing (3)
README.mdsrc/esm.tstest/esm.test.ts
π Walkthrough
Walkthrough
Adds genExportDefault(expression) to generate export default ...; strings, documents its direct expression interpolation, and adds tests covering several expression forms.
Changes
Export default helper
| Layer / File(s) | Summary |
|---|---|
Helper and docs src/esm.ts, README.md |
genExportDefault(expression) is added to src/esm.ts, and README.md documents the direct interpolation behavior with example outputs. |
Helper tests test/esm.test.ts |
genExportDefault is imported and tested against identifier, object literal, arrow function, string literal, and numeric literal inputs. |
Estimated code review effort
π― 2 (Simple) | β±οΈ ~10 minutes
Poem
Iβm a bunny with a fluffy grin,
I hop to the code and let exports in.
defaultnow dances, neat and bright,
With tests that hop along just right.
π°β¨
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title clearly matches the main change: adding genExportDefault for ESM default exports. |
| Linked Issues check | β Passed | The PR implements support for generating export default <exports>; via a new helper and tests it, matching issue #77. |
| Out of Scope Changes check | β Passed | The changes are limited to the new helper, its tests, and related README docs, with no obvious unrelated additions. |
| Docstring Coverage | β Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.