Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sh-syntax

un-ts825.5kMIT0.5.8TypeScript support: included

A WASM shell parser and formatter with bash support, based on mvdan/sh

readme

sh-syntax

GitHub Actions Workflow Status Codecov type-coverage CodeRabbit Pull Request Reviews npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier changesets

A WASM shell parser and formatter with bash support, based on mvdan/sh

TOC

Usage

Install

# yarn
yarn add sh-syntax

# npm
npm i sh-syntax

API

node

import { parse, print } from 'sh-syntax'

const text = "echo 'Hello World!'"
const ast = await parse(text)
const newText = await print(ast, {
  // `originalText` is required for now, hope we will find better solution later
  originalText: text,
})

browser

import { getProcessor } from 'sh-syntax'

// choose your own way to load the WASM file

// with `fetch`
const processor = getProcessor(() =>
  fetch('http://cdn.jsdelivr.net/npm/sh-syntax@latest/main.wasm'),
)

// with `wasm?init` via `vite`
import initWasm from 'sh-syntax/wasm?init'
const processor = getProcessor(initWasm)

const parse = (text, options) => processor(text, options)

const print = (textOrAst, options) => {
  if (typeof textOrAst === 'string') {
    return processor(textOrAst, {
      ...options,
      print: true,
    })
  }
  return processor(textOrAst, options)
}

// just like node again
const text = "echo 'Hello World!'"
const ast = await parse(text)
const newText = await print(ast, { originalText: text })

Benchmark

clk: ~2.81 GHz
cpu: Apple M1 Max
runtime: node 18.20.8 (arm64-darwin)

benchmark                   avg (min … max) p75 / p99    (min … top 1%)
------------------------------------------- -------------------------------
sh-syntax                     18.33 ms/iter  18.56 ms   ▃        ▃█
                      (17.48 ms … 20.83 ms)  19.09 ms ▂▂█    ▂   ██▂  ▂ ▂
                    ( 28.52 kb …   9.82 mb) 890.46 kb ███▆▆▁▁█▁▁▆███▁▆█▁█▆▆
                  5.07 ipc (  1.22% stalls)  99.36% L1 data cache
         56.96M cycles 288.57M instructions  39.27% retired LD/ST (113.32M)

sh-syntax (synckit)           18.38 ms/iter  18.82 ms ▂▂▂           ▂▂ █▂
                      (17.63 ms … 19.06 ms)  19.04 ms ███▅          ██ ██▅▅
                    (291.31 kb … 312.45 kb) 292.09 kb ████▇▇▇▇▁▁▁▁▇▁██▇████
                  1.11 ipc ( 11.51% stalls)  40.34% L1 data cache
        434.86k cycles 484.78k instructions  19.58% retired LD/ST ( 94.92k)

mvdan-sh                      79.47 ms/iter  78.95 ms  █
                     (71.22 ms … 126.09 ms)  80.84 ms ▅█ ▅▅▅    ▅    ▅▅▅  ▅
                    (  8.18 mb …   9.70 mb)   8.83 mb ██▁███▁▁▁▁█▁▁▁▁███▁▁█
                  5.18 ipc (  1.05% stalls)  99.04% L1 data cache
        249.41M cycles   1.29G instructions  34.41% retired LD/ST (444.42M)

summary
  sh-syntax
   1x faster than sh-syntax (synckit)
   4.34x faster than mvdan-sh

See benchmark for more details.

You can try it with running node benchmark by yourself. Here is the benchmark source code.

Sponsors and Backers

Sponsors and Backers

Sponsors

1stG RxTS UnTS
1stG Open Collective sponsors RxTS Open Collective sponsors UnTS Open Collective sponsors

Backers

1stG RxTS UnTS
1stG Open Collective backers RxTS Open Collective backers UnTS Open Collective backers

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

MIT © JounQin@1stG.me

changelog

sh-syntax

0.5.8

Patch Changes

0.5.7

Patch Changes

0.5.6

Patch Changes

0.5.5

Patch Changes

0.5.4

Patch Changes

0.5.3

Patch Changes

0.5.2

Patch Changes

  • #107 3152cd3 Thanks @JounQin! - feat: add new LangVariant option LangAuto although it's unsupported

0.5.1

Patch Changes

0.5.0

Minor Changes

  • #97 28d68ba Thanks @JounQin! - chore: housekeeping, bump mvdan/sh and easyjson deps, upgrade go to v1.23

Patch Changes

0.4.2

Patch Changes

0.4.1

Patch Changes

  • 5760ee9 Thanks @JounQin! - fix: better LangVariant typing for compatibility with mvdan-sh

0.4.0

Minor Changes

  • e13b13a Thanks @JounQin! - fix: add a trailing 0 for bytes, this should fix several known issues

    feat: drop node 14 support

0.3.7

Patch Changes

0.3.6

Patch Changes

0.3.5

Patch Changes

0.3.4

Patch Changes

0.3.3

Patch Changes

0.3.2

Patch Changes

  • 6193518 Thanks @JounQin! - chore(deps): upgrade sh v3.5.1 and tinygo v0.23.0

0.3.1

Patch Changes

0.3.0

Minor Changes

0.2.1

Patch Changes

0.2.0

Minor Changes

0.1.4

Patch Changes

  • #15 174f66c Thanks @JounQin! - fix: use global Go to store text due to go.argv limitation

0.1.3

Patch Changes

0.1.2

Patch Changes

0.1.1

Patch Changes

0.1.0

Minor Changes

0.0.2

Patch Changes

0.0.1

Patch Changes