Skip to content

Commit

Permalink
feat: use wasm for plugin sh, drop umd (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Apr 2, 2022
1 parent 1d427a0 commit fd19f3a
Show file tree
Hide file tree
Showing 19 changed files with 751 additions and 129 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-toys-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-sh": minor
---

feat: use wasm for plugin sh
7 changes: 7 additions & 0 deletions .changeset/spicy-swans-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"prettier-plugin-pkg": minor
"prettier-plugin-sh": minor
"prettier-plugin-sql": minor
---

feat: drop umd format support
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/test/fixtures
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"build": "run-p build:*",
"build:docs": "w -e docs -p --publicPath /",
"build:r": "r -f cjs,umd -g \"{'mvdan-sh':'sh','prettier/parser-babel':'prettierPlugins.babel','prettier-plugin-pkg':'prettierPlugins.pkg','prettier-plugin-sh':'prettierPlugins.sh','prettier-plugin-sql':'prettierPlugins.sql','node-sql-parser':'NodeSQLParser','sql-formatter':'sqlFormatter'}\" -x mvdan-sh,node-sql-parser,sql-formatter -p",
"build:r": "r -f cjs",
"build:ts": "tsc -b",
"clean": "rimraf 'packages/*/{lib,*.tsbuildinfo}'",
"docs": "w -e docs --publicPath /",
Expand All @@ -36,18 +36,17 @@
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.181",
"@types/mvdan-sh": "^0.5.1",
"@types/prettier": "^2.4.4",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@types/web": "^0.0.60",
"@types/web": "^0.0.61",
"github-markdown-css": "^5.1.0",
"https-proxy-agent": "^5.0.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "^6.2.2",
"react-router-dom": "^6.3.0",
"remark-gfm": "^3.0.1",
"sirv-cli": "^2.0.2",
"ts-jest": "^27.1.4",
Expand Down Expand Up @@ -82,10 +81,12 @@
]
},
"typeCoverage": {
"atLeast": 98.21,
"atLeast": 100,
"cache": true,
"detail": true,
"ignoreAsAssertion": true,
"ignoreNested": true,
"ignoreNonNullAssertion": true,
"ignoreFiles": [
"**/*.d.ts"
],
Expand Down
8 changes: 5 additions & 3 deletions packages/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"homepage": "https://github.com/rx-ts/prettier/blob/master/packages/pkg",
"author": "JounQin <admin@1stg.me>",
"license": "MPL-2.0",
"engines": {
"node": ">=12.20"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
},
"jsdelivr": "./lib/umd.min.js",
"unpkg": "./lib/umd.min.js",
"types": "./lib/index.d.ts",
"files": [
"lib"
Expand Down
7 changes: 3 additions & 4 deletions packages/pkg/test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import pkg2 from './fixtures/fixture2.json'
const pkgs = [pkg1, pkg2]

const createFixture = (index: 0 | 1 = 0) => {
const pkg = pkgs[index]
const pkg: Record<string, unknown> = pkgs[index]
return shuffle(Object.keys(pkg)).reduce(
(acc, key) =>
Object.assign(acc, {
[key]: pkg[key as keyof typeof pkg],
[key]: pkg[key],
}),
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
{} as typeof pkg,
{},
)
}

Expand Down
11 changes: 7 additions & 4 deletions packages/sh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"homepage": "https://github.com/rx-ts/prettier/blob/master/packages/sh",
"author": "JounQin <admin@1stg.me>",
"license": "MIT",
"engines": {
"node": ">=12.20"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
"require": "./lib/index.cjs",
"types": "./lib/index.d.ts"
},
"jsdelivr": "./lib/umd.min.js",
"unpkg": "./lib/umd.min.js",
"types": "./lib/index.d.ts",
"files": [
"lib"
Expand All @@ -34,7 +36,8 @@
"prettier": "^2.0.0"
},
"dependencies": {
"mvdan-sh": "^0.5.0"
"sh-syntax": "^0.0.2",
"synckit": "^0.6.1"
},
"publishConfig": {
"access": "public"
Expand Down
133 changes: 58 additions & 75 deletions packages/sh/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,90 @@
import sh, { LangVariant, Node, Pos } from 'mvdan-sh'
import path from 'path'
import { fileURLToPath } from 'url'

import {
IntSupportOption,
ParserOptions,
PathSupportOption,
Plugin,
RequiredOptions,
} from 'prettier'
import { ParseError, ShOptions, print } from 'sh-syntax'
import { createSyncFn } from 'synckit'

import { languages } from './languages.js'

const { syntax } = sh

export interface ShOptions extends RequiredOptions {
// parser
keepComments: boolean
stopAt: string
variant: LangVariant
export type ShParserOptions = ParserOptions & ShOptions

// printer
indent: number
binaryNextLine: boolean
switchCaseIndent: boolean
spaceRedirects: boolean
keepPadding: boolean
minify: boolean
functionNextLine: boolean
}
const _dirname =
typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: __dirname

export type ShParserOptions = ParserOptions<Node> & ShOptions
const workerPath = path.resolve(_dirname, 'worker.js')

export interface ShParseError {
Filename: string
Pos: Pos
Text: string
Incomplete: boolean
Error(): void
}
const printSync = createSyncFn<typeof print>(workerPath)

const ShPlugin: Plugin<Node> = {
const ShPlugin: Plugin = {
languages,
parsers: {
sh: {
parse: (
text,
_parsers,
{ filepath, keepComments = true, stopAt, variant }: Partial<ShOptions>,
) => {
const parserOptions = [syntax.KeepComments(keepComments)]

if (stopAt != null) {
parserOptions.push(syntax.StopAt(stopAt))
}

if (variant != null) {
parserOptions.push(syntax.Variant(variant))
}

try {
return syntax.NewParser(...parserOptions).Parse(text, filepath)
} catch (e) {
const err = e as ShParseError
throw Object.assign(new SyntaxError(err.Text), {
loc: {
start: {
column: err.Pos.Col(),
line: err.Pos.Line(),
},
},
})
}
},
parse: (_text, _parsers, options: Partial<ShOptions>) => options,
astFormat: 'sh',
locStart: node => node.Pos().Offset(),
locEnd: node => node.End().Offset(),
locStart: () => 0,
locEnd: () => 0,
},
},
printers: {
sh: {
print: (
path,
_path,
{
originalText,
filepath,
useTabs,
tabWidth,
indent = useTabs ? 0 : tabWidth,
binaryNextLine = true,
switchCaseIndent = true,
spaceRedirects = true,
keepComments,
stopAt,
variant,
indent,
binaryNextLine,
switchCaseIndent,
spaceRedirects,
keepPadding,
minify,
functionNextLine,
}: ShParserOptions,
) =>
syntax
.NewPrinter(
syntax.Indent(indent),
syntax.BinaryNextLine(binaryNextLine),
syntax.SwitchCaseIndent(switchCaseIndent),
syntax.SpaceRedirects(spaceRedirects),
syntax.KeepPadding(keepPadding),
syntax.Minify(minify),
syntax.FunctionNextLine(functionNextLine),
)
.Print(path.getValue()),
) => {
try {
return printSync(originalText, {
filepath,
useTabs,
tabWidth,
keepComments,
stopAt,
variant,
indent,
binaryNextLine,
switchCaseIndent,
spaceRedirects,
keepPadding,
minify,
functionNextLine,
})
} catch (err: unknown) {
const error = err as ParseError | string
if (typeof error === 'string') {
throw new SyntaxError(error)
}
throw Object.assign(error, {
loc: {
start: {
column: error.pos.col,
line: error.pos.line,
},
},
})
}
},
},
},
options: {
Expand Down
4 changes: 4 additions & 0 deletions packages/sh/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { print } from 'sh-syntax'
import { runAsWorker } from 'synckit'

runAsWorker(print)
Loading

0 comments on commit fd19f3a

Please sign in to comment.