# @smikhalevski/codegen

> Language-agnostic codegen utils.

Latest version **2.0.1** (published 2021-09-21) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @smikhalevski/codegen
pnpm add @smikhalevski/codegen
yarn add @smikhalevski/codegen
bun add @smikhalevski/codegen
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2021-09-21 |
| First published | 2021-07-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Savva Mikhalevski |
| Maintainers | smikhalevski |

## Links

- npm: https://www.npmjs.com/package/@smikhalevski/codegen
- Repository: https://github.com/smikhalevski/codegen
- Homepage: https://github.com/smikhalevski/codegen#readme
- Issues: https://github.com/smikhalevski/codegen/issues
- npm.io page: https://npm.io/package/@smikhalevski/codegen

## Recent versions

- 2.0.1 (latest) — 2021-09-21
- 2.0.0 — 2021-08-11
- 1.0.0 — 2021-07-13

## README

# codegen [![build](https://github.com/smikhalevski/codegen/actions/workflows/master.yml/badge.svg?branch=master&event=push)](https://github.com/smikhalevski/codegen/actions/workflows/master.yml)

Language-agnostic codegen utils.

```shell
npm install --save-prod @smikhalevski/codegen
```

# Usage

⚠️ [API documentation is available here.](https://smikhalevski.github.io/codegen/)

```ts
import {template as _, compileJsSource} from '@smikhalevski/codegen';

const kebabVar = _.var();
const indexVar = _.var();
const valueVar = _.var();

const functionBlock = _.block`function makeKebab(${valueVar}){${[
  _`let ${kebabVar};`,
  _.assignment(kebabVar, _`""`),

  _`for(let ${indexVar}=0;${indexVar}<${valueVar}.length;${indexVar}++){`,
  _`${kebabVar}+="-"+${valueVar}.charAt(${indexVar});`,
  _`}`,

  _`return ${kebabVar}`,
]}}`;

compileJsSource(functionBlock);
// → 'function makeKebab(a){let b;b="";for(let c=0;c<a.length;c++){b+="-"+a.charAt(c);}return b}'
```

Codegen can optimize code by inlining assignments and eliminating redundant variable declarations.

```ts
import {template as _, compileJsSource, inlineVarAssignments} from '@smikhalevski/codegen';

const aVar = _.var();
const bVar = _.var();
const cVar = _.var();

const block = _(
    _.assignment(aVar, 3),
    _.assignment(bVar, 2),
    _.assignment(cVar, _`${aVar}+${bVar}`),

    _`console.log(${cVar})`,
);

// This operation mutates block
inlineVarAssignments(block);

compileJsSource(block);
// → 'console.log(3+2)'
```

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