# code-red

> code-red

Latest version **1.0.4** (published 2023-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install code-red
pnpm add code-red
yarn add code-red
bun add code-red
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2023-08-11 |
| First published | 2019-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 5 |
| Unpacked size | 53.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 334 |
| Maintainers | rich_harris, conduitry |

## Links

- npm: https://www.npmjs.com/package/code-red
- Repository: https://github.com/Rich-Harris/code-red
- Homepage: https://github.com/Rich-Harris/code-red#readme
- Issues: https://github.com/Rich-Harris/code-red/issues
- npm.io page: https://npm.io/package/code-red

## Dependencies (5)

- [acorn](https://npm.io/package/acorn.md) ^8.10.0
- [periscopic](https://npm.io/package/periscopic.md) ^3.1.0
- [@types/estree](https://npm.io/package/@types/estree.md) ^1.0.1
- [estree-walker](https://npm.io/package/estree-walker.md) ^3.0.3
- [@jridgewell/sourcemap-codec](https://npm.io/package/@jridgewell/sourcemap-codec.md) ^1.4.15

## Recent versions

- 1.0.4 (latest) — 2023-08-11
- 1.0.3 — 2023-06-19
- 1.0.2 — 2023-06-13
- 1.0.1 — 2023-06-10
- 1.0.0 — 2023-03-03
- 0.2.7 — 2023-03-03
- 0.2.6 — 2023-03-03
- 0.2.5 — 2022-01-31
- 0.2.4 — 2022-01-06
- 0.2.3 — 2021-11-01
- 0.2.2 — 2021-07-21
- 0.2.1 — 2021-06-22
- 0.2.0 — 2021-04-29
- 0.1.7 — 2021-04-28
- 0.1.6 — 2021-04-28
- … 38 more at https://npm.io/package/code-red/versions

## README

# code-red

Experimental toolkit for writing x-to-JavaScript compilers. It is used in [Svelte](https://svelte.dev).


## API

The `code-red` package exposes three core functions — `b`, `x` and `print`.

`b` and `x` take a template literal and return an [ESTree](https://github.com/estree/estree) program body, or a single node:

```js
import { b, x } from 'code-red';

const expression = x`i + j`;

assert.equal(expression.type, 'AssignmentExpression');
assert.equal(expression.operator, '+');
assert.equal(expression.left.name, 'i');
assert.equal(expression.right.name, 'j');

const body = b`
	const i = 1;
	const j = 2;
	const k = i + j;
`;

assert.equal(body.length, 3);
assert.equal(body[0].type, 'VariableDeclaration');
```

Expressions in template literals correspond to replacement nodes — so you could express the above like so:

```js
const i = x`i`;
const j = x`j`;
const expression = x`${i} + ${j}`;

const body = b`
	const ${i} = 1;
	const ${j} = 2;
	const k = ${expression};
`;
```

The `print` function takes a node and turns it into a `{code, map}` object:

```js
const add = x`
	function add(${i}, ${j}) {
		return ${expression};
	}
`;

print(add).code;
/*
function add(i, j) {
	return i + j;
}
*/

i.name = 'foo';
j.name = 'bar';

print(add).code;
/*
function add(foo, bar) {
	return foo + bar;
}
*/
```

## Prefixes

### `@`-prefixed names (replaceable globals)

So that you can use globals in your code. In Svelte, we use this to insert utility functions.

```js
// input
import { x } from 'code-red';
x`@foo(bar)`

// output
FOO(bar)
```

### `#`-prefixed names (automatically deconflicted names)

So that you can insert variables in your code without worrying if they clash with existing variable names.


`bar` used in user code and in inserted code gets a `$1` suffix:

```js
// input
import { x } from 'code-red';
x`
function foo(#bar) {
	return #bar * bar;
}`;

// output
function foo(bar$1) {
	return bar$1 * bar;
}
```

Without conflicts, no `$1` suffix:

```js
// input
import { b } from 'code-red';
b`const foo = #bar => #bar * 2`;

// output
const foo = bar => bar * 2;
```

## Optimiser

TODO add an optimiser that e.g. collapses consecutive identical if blocks


## Compiler

TODO add a `code-red/compiler` module that replaces template literals with the nodes they evaluate to, so that there's nothing to parse at runtime.


## Sourcemaps

TODO support source mappings for inserted nodes with location information.


## License

[MIT](LICENSE)

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