# ranlexer

> Tiny JavaScript parser and generator

Latest version **0.0.9** (published 2023-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install ranlexer
pnpm add ranlexer
yarn add ranlexer
bun add ranlexer
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2023-06-06 |
| First published | 2023-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 198.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | chaxus |
| Maintainers | chaxus |
| Keywords | frontend, build-tool, transpiler, es6, transpile, compiler |

## Links

- npm: https://www.npmjs.com/package/ranlexer
- Repository: https://github.com/chaxus/ranlexer
- Homepage: https://github.com/chaxus/ranlexer#readme
- Issues: https://github.com/chaxus/ranlexer/issues
- npm.io page: https://npm.io/package/ranlexer

## Dependencies (1)

- [magic-string](https://npm.io/package/magic-string.md) ^0.30.0

## 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.9 (latest) — 2023-06-06
- 0.0.8 — 2023-05-06
- 0.0.7 — 2023-05-06
- 0.0.6 — 2023-05-06
- 0.0.5 — 2023-05-06
- 0.0.4 — 2023-05-06
- 0.0.3 — 2023-04-29
- 0.0.2 — 2023-04-28
- 0.0.1 — 2023-04-26

## README

# ranlexer

Tiny JavaScript parser and generator

---

<a href="https://github.com/chaxus/ranlexer/actions"><img src="https://img.shields.io/github/actions/workflow/status/chaxus/ranlexer/main.yml" alt="Build Status"></a>
<a href="https://npmjs.com/package/ranlexer"><img src="https://img.shields.io/npm/v/ranlexer.svg" alt="npm-v"></a>
<a href="https://npmjs.com/package/ranlexer"><img src="https://img.shields.io/npm/dt/ranlexer.svg" alt="npm-d"></a>
<a href="https://bundlephobia.com/result?p=ranlexer"><img src="https://img.badgesize.io/https:/unpkg.com/ranlexer/dist/index.umd.js?label=brotli&compression=brotli" alt="brotli"></a>
<a href="#alternative-installation-methods"><img src="https://img.shields.io/badge/module%20formats-umd%2C%20esm-green.svg" alt="module formats: umd, esm"></a>

## Install

Using npm:

```console
npm install ranlexer --save
```

## Usage

ranlexer can export the following methods

- **parse**: Parse the code into an ast
- **walk**: Walk through the structure of the ast to perform custom operations
- **generate**: Parse the ast into code
- **build**: A lightweight build tool that supports treeshaking

### parse

Parse the code into an ast:

```ts
import { parse } from 'ranlexer'

const code = 'let a = 1;'
const ast = parse(code)
```

### walk

Walk through the structure of the ast to perform custom operations

There are two options：

- **ast**: ast structure node
- **opts**: One object with two methods in it

```ts
// Export the type of the ast node
import type { Types } from 'ranlexer'

const opts = {
  enter: (node: Types.Node) => {
    // Enter the processing of the node
  },
  leave: (node: Types.Node) => {
    // Leave the node processing
  }
walk(ast, opts)

```

### generate

Parse the ast into code:

```ts
import { generate } from 'ranlexer'

const ast = {
  type: NodeType.Program,
  start: 0,
  end: 9,
  body: [
    {
      type: NodeType.VariableDeclaration,
      start: 0,
      end: 9,
      declarations: [
        {
          type: NodeType.VariableDeclarator,
          id: {
            type: NodeType.Identifier,
            name: 'a',
            start: 4,
            end: 5,
          },
          start: 4,
          end: 9,
          init: {
            type: NodeType.Literal,
            value: '1',
            raw: '1',
            start: 8,
            end: 9,
          },
        },
      ],
      kind: 'let',
    },
  ],
}
const code = generate(ast) // 'let a = 1;'
```

### build

A lightweight build tool that supports treeshaking

```ts
import { build } from 'ranlexer'

const bundle = await build(option)
```

Generate a bundle by passing in options，All options are well, optional:

- **input**: Build entry, if not set, the default value is `./index.js`
- **output**: Path to the build output file. If not set, the default value is `./dist/index.js`
- **external**: String array，the modules in the array are not built

The bundle has two methods:

- **generate**: generate is to output the built code directly,

```ts
import { build } from 'ranlexer'

const bundle = await build(option)

const code = bundle.generate()
```

- **write**: write is to output the file to a directory.

```ts
import { build } from 'ranlexer'

const bundle = await build(option)

bundle.write()
```

### Meta

[LICENSE (MIT)](/LICENSE)

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