# inline-codegen

> Insert code inline next to the statement that generates it

Latest version **0.0.8** (published 2018-07-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install inline-codegen
pnpm add inline-codegen
yarn add inline-codegen
bun add inline-codegen
```

Provides the command `inline-codegen`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2018-07-19 |
| First published | 2018-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | benjaminjackman |
| Maintainers | benjaminjackman |
| Keywords | ash9g, typescript |

## Links

- npm: https://www.npmjs.com/package/inline-codegen
- npm.io page: https://npm.io/package/inline-codegen

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2018-07-19
- 0.0.7 — 2018-07-19
- 0.0.6 — 2018-07-19
- 0.0.5 — 2018-07-19
- 0.0.4 — 2018-07-19
- 0.0.3 — 2018-07-19
- 0.0.2 — 2018-07-19
- 0.0.1 — 2018-07-19

## README

# inline-codegen
(perhaps a better name would be living-snippets?)

Generates code and places the result directly inline in the source code file.


1. Scans a typescript file for lines starting with INSERT_SNIPPET_ABOVE(fn, vars)
2. Executes the source code file and sets an environmental variable that causes INSERT_SNIPPET_ABOVE to run which in turn executes fn(vars) and saves the result
3. interpolates those results directly above the INSERT_SNIPPET_ABOVE statements as necessary

under normal conditions INSERT_SNIPPET_ABOVE just noops


## Why?
I am prototyping an idea I have. I have to generate a lot of boilerplate code to
get typescript to not explode due to https://github.com/Microsoft/TypeScript/issues/25023

I was able to encode a solution as a snippet but it's about a dozen+ plus lines
of code per use. So it starts to get pretty hard to maintain all that boilerplate.

So instead I wrote this module to see if:

per `h-mst`:
```typescript
export const MSTModelSnippet = (name: string) => {
  return `
type ${name}ModelTypeOf = typeof ${name}Model
type ${name}TypeTypeOf = typeof ${name}Model.Type
export interface ${name}Type extends ${name}TypeTypeOf {}
type ${name}SnapshotTypeTypeOf = typeof ${name}Model.SnapshotType
export interface ${name}SnapshotType extends ${name}SnapshotTypeTypeOf {}
type ${name}CreationTypeTypeOf = typeof ${name}Model.CreationType
export interface ${name}CreationType extends ${name}CreationTypeTypeOf {}
export interface ${name} extends ${name}ModelTypeOf {
  Type: ${name}Type
  CreationType: ${name}CreationType
  SnapshotType: ${name}SnapshotType
}
export const ${name}: ${name} = ${name}Model
`
}
```

source code where the expansion occurs:


```typescript
import {types} from "mobx-state-tree"
const M1Model = types.model("M1", {})
INSERT_SNIPPET_ABOVE(import("h-mst").MSTModelSnippet, M1Model.name)
```

which transforms into 

```typescript
const M1Model = types.model("M1", {})
//#region ISA_START Automatically Generated Code (Do not edit)
type M1ModelTypeOf = typeof M1Model
type M1TypeTypeOf = typeof M1Model.Type
export interface M1Type extends M1TypeTypeOf {}
type M1SnapshotTypeTypeOf = typeof M1Model.SnapshotType
export interface M1SnapshotType extends M1SnapshotTypeTypeOf {}
type M1CreationTypeTypeOf = typeof M1Model.CreationType
export interface M1CreationType extends M1CreationTypeTypeOf {}
export interface M1 extends M1ModelTypeOf {
  Type: M1Type
  CreationType: M1CreationType
  SnapshotType: M1SnapshotType
}
export const M1: M1 = M1Model
//#endregion ISA_END Automatically Generated Code (Do not edit)
INSERT_SNIPPET_ABOVE(MSTModelSnippet, M1Model.name)
```

Would work... and so far it looks promising (if obviously a bit less than ideal)

(Also notice the use of a #region this allows vcode to collapse and hide the 
codegen fairly seamlessly)

I believe this approach may have applications in other areas and be a good
general compromise to waiting for macros to be added to typescript.

I have several additional ideas I would like to try out to further enhance
the power of this tool.

## Current shortcomings
Imagine the simplest possible implementation and that's what's done here, no
fancy parsing (probably not a good idea anyhow in the long run since the
snippets may need to be able to run for the rest of the file to compile).

Currently the way it works is to actually execute the file with the codegen
in it, then capture the result and then re-insert it. This allows for the trick
of pulling the `M1Model.name`. However if there are multiple codegens that depend
on each other then it can cause a catch-22.

---
_Source: https://npm.io/package/inline-codegen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
