Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ jobs:
# bad Windows -- https://github.com/codacy/codacy-coverage-reporter-action/issues/91
if: ${{ matrix.os != 'windows-latest' }}
uses: codacy/codacy-coverage-reporter-action@89d6c85cfafaec52c72b6c5e8b2878d33104c699 # v1
continue-on-error: true
with:
api-token: ${{ secrets.CODACY_API_TOKEN }}
1 change: 0 additions & 1 deletion .lintstagedrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .nano-staged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@1stg/nano-staged/tsc'
935 changes: 0 additions & 935 deletions .yarn/releases/yarn-4.8.1.cjs

This file was deleted.

948 changes: 948 additions & 0 deletions .yarn/releases/yarn-4.9.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ plugins:
path: .yarn/plugins/plugin-prepare-lifecycle.cjs
spec: 'https://github.com/un-es/yarn-plugin-prepare-lifecycle/releases/download/v0.0.1/index.js'

yarnPath: .yarn/releases/yarn-4.8.1.cjs
yarnPath: .yarn/releases/yarn-4.9.1.cjs
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,17 @@ See [benchmark](./benchmark/benchmark.txt) for more details.

You can try it with running `node benchmark` by yourself. [Here](./benchmark/index.js) is the benchmark source code.

[![Sponsors](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)
## Sponsors and Backers

## Sponsors
[![Sponsors and Backers](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)

### Sponsors

| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| [![1stG Open Collective sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |

## Backers
### Backers

| 1stG | RxTS | UnTS |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -149,5 +151,5 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m
[MIT][] © [JounQin][]@[1stG.me][]

[1stG.me]: https://www.1stg.me
[JounQin]: https://GitHub.com/JounQin
[JounQin]: https://github.com/JounQin
[MIT]: http://opensource.org/licenses/MIT
18 changes: 3 additions & 15 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

/* eslint-disable @babel/new-cap */

import fs from 'node:fs'
import { fileURLToPath } from 'node:url'

Expand All @@ -11,16 +9,10 @@ import { createSyncFn } from 'synckit'

import { print } from '../lib/index.js'

/**
* @typedef {import('../lib').ShOptions} ShOptions
* @typedef {import('mvdan-sh').LangVariant} LangVariant
*/
/** @import { ShOptions } from 'sh-syntax' */

const keepComments = true

/**
* @type {LangVariant}
*/
const variant = 0 // LangVariant.LangBash

const indent = 2
Expand All @@ -37,9 +29,7 @@ const filePath = fileURLToPath(

const text = fs.readFileSync(filePath, 'utf8')

/**
* @type {ShOptions}
*/
/** @type {ShOptions} */
const shOptions = {
keepComments,
variant,
Expand All @@ -53,9 +43,7 @@ const shOptions = {
functionNextLine,
}

/**
* @type {(text: string, options?: ShOptions) => string})}
*/
/** @type {(text: string, options?: ShOptions) => string} */
const printSync = createSyncFn(new URL('worker.mjs', import.meta.url))

summary(() => {
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
"funding": "https://opencollective.com/sh-syntax",
"license": "MIT",
"packageManager": "yarn@4.8.1",
"packageManager": "yarn@4.9.1",
"engines": {
"node": ">=16.0.0"
},
Expand Down Expand Up @@ -46,7 +46,7 @@
"!**/*.tsbuildinfo"
],
"scripts": {
"benchmark": "NO_COLOR=1 node benchmark > benchmark/benchmark.txt",
"benchmark": "sudo env NO_COLOR=1 node benchmark > benchmark/benchmark.txt",
"build": "run-p 'build:*'",
"build:r": "r -f cjs -x '**/vendors/wasm_exec.cjs'",
"build:tsc": "tsc -p src --declaration false && tsc -p src --removeComments false --emitDeclarationOnly",
Expand All @@ -65,37 +65,37 @@
"tslib": "^2.8.1"
},
"devDependencies": {
"@1stg/common-config": "^12.0.0",
"@1stg/common-config": "^13.0.1",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.28.1",
"@changesets/cli": "^2.29.2",
"@commitlint/cli": "^19.8.0",
"@mitata/counters": "^0.0.8",
"@pkgr/rollup": "^6.0.2",
"@pkgr/rollup": "^6.0.3",
"@stylistic/eslint-plugin-js": "^4.2.0",
"@types/golang-wasm-exec": "^1.15.2",
"@types/mvdan-sh": "^0.10.9",
"@types/node": "^22.14.0",
"@types/web": "^0.0.216",
"@types/node": "^22.14.1",
"@types/web": "^0.0.222",
"@vitest/coverage-istanbul": "^3.1.1",
"@vitest/eslint-plugin": "^1.1.43",
"clean-pkg-json": "^1.2.1",
"eslint": "^9.24.0",
"lint-staged": "^15.5.0",
"eslint": "^9.25.0",
"mitata": "^1.0.34",
"mvdan-sh": "^0.10.1",
"nano-staged": "^0.8.0",
"npm-run-all2": "^7.0.2",
"prettier": "^3.5.3",
"sh-syntax": "link:.",
"simple-git-hooks": "^2.12.1",
"size-limit": "^11.2.0",
"size-limit-preset-node-lib": "^0.3.0",
"synckit": "^0.11.2",
"synckit": "^0.11.4",
"type-coverage": "^2.29.7",
"typescript": "^5.8.3",
"vitest": "^3.1.1",
"yarn-berry-deduplicate": "^6.1.1"
"yarn-berry-deduplicate": "^6.1.3"
},
"resolutions": {
"es5-ext": "npm:@unes/es5-ext@latest",
"prettier": "^3.5.3",
"sh-syntax": "link:."
},
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import '../vendors/wasm_exec.cjs'
import { getProcessor } from './processor.js'
import type { File, ShOptions, ShPrintOptions } from './types.js'

const _dirname =
typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: __dirname
/* istanbul ignore next -- @preserve */
const _dirname = import.meta.url
? path.dirname(fileURLToPath(import.meta.url))
: __dirname

export const processor = getProcessor(() =>
fs.readFile(path.resolve(_dirname, '../main.wasm')),
Expand Down
70 changes: 46 additions & 24 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export class ParseError extends Error implements IParseError {
}

export type GetWebAssemblySource = () =>
| Buffer
| BufferSource
| Promise<BufferSource | Response>
| Promise<Buffer | BufferSource | Response>
| Response

export type GetWebAssemblyInstance = (
Expand All @@ -39,8 +40,8 @@ let decoder: TextDecoder | undefined
export const getProcessor = (
getWasm: GetWebAssemblyInstance | GetWebAssemblySource,
) => {
let wasmBufferSource: BufferSource | undefined
let wasmBufferSourcePromise: Promise<BufferSource> | undefined
let wasmBufferSource: Buffer | BufferSource | undefined
let wasmBufferSourcePromise: Promise<Buffer | BufferSource> | undefined

encoder ??= new TextEncoder()
decoder ??= new TextDecoder()
Expand All @@ -60,30 +61,44 @@ export const getProcessor = (
/**
* Processes a shell script input using a WebAssembly module.
*
* This asynchronous function accepts shell script input either as a string or as an AST File, along with a set of options
* that control formatting, error recovery, and output. It ensures that the WebAssembly module is loaded and instantiated,
* allocates memory for the file path and text content, and then calls the module's processing function with the provided options.
* Depending on the `print` flag, it returns either the processed text or a File representing the parsed AST.
* This asynchronous function accepts shell script input either as a string or
* as an AST File, along with a set of options that control formatting, error
* recovery, and output. It ensures that the WebAssembly module is loaded and
* instantiated, allocates memory for the file path and text content, and then
* calls the module's processing function with the provided options. Depending
* on the `print` flag, it returns either the processed text or a File
* representing the parsed AST.
*
* @param textOrAst - The shell script input as a string or as an AST File. When providing a non-string input and `print` is false,
* the `originalText` option must be supplied.
* @param textOrAst - The shell script input as a string or as an AST File.
* When providing a non-string input and `print` is false, the
* `originalText` option must be supplied.
* @param options - An object containing processing options:
* - filepath: The file path associated with the input, used primarily for error reporting.
* - print: If true, the function returns the processed text; otherwise, it returns the processed AST as a File.
* - originalText: The original text of the shell script, required when `textOrAst` is not a string.
* - keepComments: Determines whether comments should be preserved in the output.
* - variant: Specifies the shell scripting variant (e.g., {@link LangVariant.LangBash}).
* - stopAt: A token indicating where to halt further processing.
* - recoverErrors: Sets the level of error recovery during processing (default is 0).
* - useTabs, tabWidth, indent: Options to control indentation formatting.
* - binaryNextLine, switchCaseIndent, spaceRedirects, keepPadding, minify, singleLine, functionNextLine:
* Additional flags that influence formatting details and output structure.
*
* @returns A promise that resolves to either the processed text (if `print` is true) or a File (if `print` is false).
* - `filepath`: The file path associated with the input, used primarily for
* error reporting.
* - `print`: If true, the function returns the processed text; otherwise, it
* returns the processed AST as a File.
* - `originalText`: The original text of the shell script, required when
* `textOrAst` is not a string.
* - `keepComments`: Determines whether comments should be preserved in the
* output.
* - `variant`: Specifies the shell scripting variant (e.g.,
* {@link LangVariant.LangBash}).
* - `stopAt`: A token indicating where to halt further processing.
* - `recoverErrors`: Sets the level of error recovery during processing
* (default is 0).
* - `useTabs`, `tabWidth`, `indent`: Options to control indentation formatting.
* - `binaryNextLine`, `switchCaseIndent`, `spaceRedirects`, `keepPadding`,
* `minify`, `singleLine`, `functionNextLine`: Additional flags that
* influence formatting details and output structure.
*
* @returns A promise that resolves to either the processed text (if `print`
* is true) or a File (if `print` is false).
* @throws {TypeError} If the original text is required but not provided.
* @throws {ParseError} If the processed output is not valid JSON or indicates a parsing error.
* @throws {SyntaxError} If a syntax error is detected without an associated parse error object.
* @throws {ParseError} If the processed output is not valid JSON or indicates
* a parsing error.
* @throws {SyntaxError} If a syntax error is detected without an associated
* parse error object.
*/
async function processor(
textOrAst: File | string,
Expand Down Expand Up @@ -114,7 +129,9 @@ export const getProcessor = (
wasmBufferSourcePromise = Promise.resolve(
(getWasm as GetWebAssemblySource)(),
).then(source =>
'arrayBuffer' in source ? source.arrayBuffer() : source,
/* istanbul ignore next -- @preserve */ 'arrayBuffer' in source
? source.arrayBuffer()
: source,
)
}

Expand All @@ -136,7 +153,10 @@ export const getProcessor = (

const wasm =
getWasm.length === 0
? await WebAssembly.instantiate(wasmBufferSource!, go.importObject)
? await WebAssembly.instantiate(
wasmBufferSource as BufferSource,
go.importObject,
)
: {
instance: await (getWasm as GetWebAssemblyInstance)(
go.importObject,
Expand Down Expand Up @@ -234,6 +254,7 @@ export const getProcessor = (
const string = decoder!.decode(result.subarray(0, end))

// naive check whether the string is a json
/* istanbul ignore if -- @preserve */
if (!string.startsWith('{"') || !string.endsWith('}')) {
throw new ParseError({
Filename: filepath,
Expand All @@ -255,6 +276,7 @@ export const getProcessor = (
}

if (parseError || message) {
/* istanbul ignore next -- @preserve */
throw parseError == null
? new SyntaxError(message)
: new ParseError(parseError)
Expand Down
Loading
Loading