# magic-string-ast

> magic-string with AST shortcut.

Latest version **2.1.1** (published 2026-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install magic-string-ast
pnpm add magic-string-ast
yarn add magic-string-ast
bun add magic-string-ast
```

## Health

**Score 65/100 (B)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-07-16 |
| First published | 2023-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | ^22.18.0 \|\| >=24.0.0 |
| Dependencies | 1 |
| Unpacked size | 9.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | Kevin Deng <sxzz@sxzz.moe> |
| Maintainers | sxzz |

## Links

- npm: https://www.npmjs.com/package/magic-string-ast
- Repository: https://github.com/sxzz/magic-string-ast
- Homepage: https://github.com/sxzz/magic-string-ast#readme
- Issues: https://github.com/sxzz/magic-string-ast/issues
- Funding: https://github.com/sponsors/sxzz
- npm.io page: https://npm.io/package/magic-string-ast

## Dependencies (1)

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

## Recent versions

- 2.1.1 (latest) — 2026-07-16
- 2.1.0 — 2026-07-16
- 2.0.0 — 2026-05-12
- 1.0.3 — 2025-10-06
- 1.0.2 — 2025-08-11
- 1.0.1 — 2025-08-11
- 1.0.0 — 2025-06-26
- 0.9.1 — 2025-04-03
- 0.9.0 — 2025-03-29
- 0.8.0 — 2025-03-05
- 0.7.1 — 2025-03-05
- 0.7.0 — 2024-12-19
- 0.6.3 — 2024-11-20
- 0.6.2 — 2024-06-24
- 0.6.1 — 2024-05-16
- … 10 more at https://npm.io/package/magic-string-ast/versions

## README

# magic-string-ast

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![JSR][jsr-badge-src]][jsr-badge-href]
[![Unit Test][unit-test-src]][unit-test-href]

[magic-string](https://github.com/rich-harris/magic-string) with AST shortcut.

## Install

```bash
# npm
npm i magic-string-ast

# jsr
npx jsr add -D @sxzz/magic-string-ast
```

## Usage

```ts
import { MagicStringAST } from 'magic-string-ast'

const offset = 0
const node = {
  // AST node with `start` and `end` properties
  start: 6,
  end: 7,
  // ...
}

const s = new MagicStringAST('const a = 1')
s.sliceNode(node, { offset }) // 'a'
s.removeNode(node)
s.moveNode(node, 0)
s.overwriteNode(node, 'foo')

// support source-map, inspired by muggle-string.
s.replaceRange(5, 5, '(', expression, ')') // appendLeft
s.replaceRange(5, 8, '(', expression, ')') // overwrite
s.replaceRange(5, 8) // remove
```

For more APIs, see [docs](https://jsr.io/@sxzz/magic-string-ast/doc) and [magic-string](https://github.com/rich-harris/magic-string#usage).

## Sponsors

<p align="center">
  <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
    <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
  </a>
</p>

## License

[MIT](./LICENSE) License © 2023-PRESENT [Kevin Deng](https://github.com/sxzz)

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/magic-string-ast.svg
[npm-version-href]: https://npmjs.com/package/magic-string-ast
[npm-downloads-src]: https://img.shields.io/npm/dm/magic-string-ast
[npm-downloads-href]: https://www.npmcharts.com/compare/magic-string-ast?interval=30
[jsr-badge-src]: https://jsr.io/badges/@sxzz/magic-string-ast
[jsr-badge-href]: https://jsr.io/@sxzz/magic-string-ast
[unit-test-src]: https://github.com/sxzz/magic-string-ast/actions/workflows/unit-test.yml/badge.svg
[unit-test-href]: https://github.com/sxzz/magic-string-ast/actions/workflows/unit-test.yml

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