prettier-plugin-sh
Advanced tools
Comparing version 0.12.6 to 0.12.7
@@ -1,16 +0,16 @@ | ||
import { LangVariant, Node, Pos } from 'mvdan-sh'; | ||
import { ParserOptions, Plugin, RequiredOptions } from 'prettier'; | ||
export interface ShOptions extends RequiredOptions { | ||
keepComments: boolean; | ||
stopAt: string; | ||
variant: LangVariant; | ||
indent: number; | ||
binaryNextLine: boolean; | ||
switchCaseIndent: boolean; | ||
spaceRedirects: boolean; | ||
keepPadding: boolean; | ||
minify: boolean; | ||
functionNextLine: boolean; | ||
import { type Node, type Pos } from 'mvdan-sh'; | ||
import type { ParserOptions, Plugin } from 'prettier'; | ||
import type { File, Node as ShSyntaxNode, ShOptions } from 'sh-syntax'; | ||
export interface Processor { | ||
(text: string, options?: ShOptions): File; | ||
(text: string, options?: ShOptions & { | ||
print: true; | ||
}): string; | ||
(ast: File, options?: ShOptions & { | ||
originalText: string; | ||
}): string; | ||
} | ||
export declare type ShParserOptions = ParserOptions<Node> & ShOptions; | ||
export interface ShParserOptions extends ParserOptions<Node | ShSyntaxNode>, Required<ShOptions> { | ||
experimentalWasm: boolean; | ||
} | ||
export interface IShParseError extends Error { | ||
@@ -23,3 +23,3 @@ Filename: string; | ||
} | ||
declare const ShPlugin: Plugin<Node>; | ||
declare const ShPlugin: Plugin<Node | ShSyntaxNode>; | ||
export default ShPlugin; |
@@ -0,4 +1,12 @@ | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import sh from 'mvdan-sh'; | ||
import { createSyncFn } from 'synckit'; | ||
import { languages } from './languages.js'; | ||
/* c8 ignore next 4 */ | ||
const _dirname = typeof __dirname === 'undefined' | ||
? path.dirname(fileURLToPath(import.meta.url)) | ||
: __dirname; | ||
const { syntax } = sh; | ||
const processor = createSyncFn(path.resolve(_dirname, 'worker.js')); | ||
class ShParseError extends SyntaxError { | ||
@@ -16,2 +24,15 @@ constructor(err) { | ||
} | ||
class ShSyntaxParseError extends SyntaxError { | ||
constructor(err) { | ||
const error = err; | ||
super(/* c8 ignore next */ ('Text' in error && error.Text) || error.message); | ||
this.cause = err; | ||
// `error instanceof ParseError` won't not work because the error is thrown wrapped by `synckit` | ||
if ('Pos' in error && error.Pos != null && typeof error.Pos === 'object') { | ||
this.loc = { start: { column: error.Pos.Col, line: error.Pos.Line } }; | ||
} | ||
} | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const isFunction = (val) => typeof val === 'function'; | ||
const ShPlugin = { | ||
@@ -21,4 +42,18 @@ languages, | ||
sh: { | ||
parse: (text, _parsers, { filepath, keepComments = true, stopAt, variant }) => { | ||
parse: (text, _parsers, { filepath, keepComments = true, stopAt, variant, experimentalWasm, }) => { | ||
if (experimentalWasm) { | ||
try { | ||
return processor(text, { | ||
filepath, | ||
keepComments, | ||
stopAt, | ||
variant, | ||
}); | ||
} | ||
catch (err) { | ||
throw new ShSyntaxParseError(err); | ||
} | ||
} | ||
const parserOptions = [syntax.KeepComments(keepComments)]; | ||
/* c8 ignore next 8 */ | ||
if (stopAt != null) { | ||
@@ -38,4 +73,8 @@ parserOptions.push(syntax.StopAt(stopAt)); | ||
astFormat: 'sh', | ||
locStart: node => node.Pos().Offset(), | ||
locEnd: node => node.End().Offset(), | ||
locStart: node => | ||
/* c8 ignore next */ | ||
isFunction(node.Pos) ? node.Pos().Offset() : node.Pos.Offset, | ||
locEnd: node => | ||
/* c8 ignore next */ | ||
isFunction(node.End) ? node.End().Offset() : node.End.Offset, | ||
}, | ||
@@ -45,5 +84,24 @@ }, | ||
sh: { | ||
print: (path, { useTabs, tabWidth, indent = useTabs ? 0 : tabWidth, binaryNextLine = true, switchCaseIndent = true, spaceRedirects = true, keepPadding, minify, functionNextLine, }) => 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()), | ||
print: (path, { originalText, filepath, useTabs, tabWidth, | ||
/* c8 ignore next */ | ||
indent = useTabs ? 0 : tabWidth, binaryNextLine = true, switchCaseIndent = true, spaceRedirects = true, keepPadding, minify, functionNextLine, experimentalWasm, }) => { | ||
if (experimentalWasm) { | ||
return processor(path.getNode(), { | ||
originalText, | ||
filepath, | ||
useTabs, | ||
tabWidth, | ||
indent, | ||
binaryNextLine, | ||
switchCaseIndent, | ||
spaceRedirects, | ||
keepPadding, | ||
minify, | ||
functionNextLine, | ||
}); | ||
} | ||
return 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()); | ||
}, | ||
}, | ||
@@ -88,2 +146,6 @@ }, | ||
}, | ||
{ | ||
value: 3, | ||
description: 'Bats', | ||
}, | ||
], | ||
@@ -143,2 +205,9 @@ description: 'Variant changes the shell language variant that the parser will accept.', | ||
}, | ||
experimentalWasm: { | ||
since: '0.13.0', | ||
category: 'config', | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether prefer to use experimental `sh-syntax` instead of `mvdan-sh`, it could still be buggy', | ||
}, | ||
}, | ||
@@ -145,0 +214,0 @@ }; |
{ | ||
"name": "prettier-plugin-sh", | ||
"version": "0.12.6", | ||
"version": "0.12.7", | ||
"type": "module", | ||
@@ -71,3 +71,5 @@ "description": "An opinionated `shellscript` formatter plugin for Prettier, also support simple format of `Dockerfile`, `properties`, `gitignore`, `dotenv`, `hosts`, `jvmoptions`...", | ||
"dependencies": { | ||
"mvdan-sh": "^0.10.1" | ||
"mvdan-sh": "^0.10.1", | ||
"sh-syntax": "^0.3.6", | ||
"synckit": "^0.8.1" | ||
}, | ||
@@ -77,2 +79,2 @@ "publishConfig": { | ||
} | ||
} | ||
} |
@@ -63,2 +63,5 @@ <p align="center"> | ||
functionNextLine: boolean | ||
// use `sh-syntax` which is still buggy instead | ||
experimentalWasm: boolean | ||
} | ||
@@ -65,0 +68,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52178
13
1275
96
4
+ Addedsh-syntax@^0.3.6
+ Addedsynckit@^0.8.1
+ Added@pkgr/core@0.1.1(transitive)
+ Addedsh-syntax@0.3.7(transitive)
+ Addedsynckit@0.8.8(transitive)
+ Addedtslib@2.8.1(transitive)