Skip to content

Commit 02f37a2

Browse files
authored
feat(sh): add simplify option (#520)
1 parent 0314b38 commit 02f37a2

6 files changed

Lines changed: 52 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prettier-plugin-sh": minor
3+
---
4+
5+
Add a `simplify` option to remove redundant shell syntax.

‎packages/sh/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ interface ShOptions {
157157
recoverErrors: number
158158

159159
// printer
160+
/** Simplify modifies the syntax tree to remove redundant shell syntax. */
161+
simplify: boolean
160162
/**
161163
* Indent sets the number of spaces used for indentation. If set to 0, tabs
162164
* will be used instead.

‎packages/sh/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"dependencies": {
4949
"@reteps/dockerfmt": "^0.5.4",
50-
"sh-syntax": "^0.6.0"
50+
"sh-syntax": "^0.7.0"
5151
},
5252
"devDependencies": {
5353
"@types/common-tags": "^1.8.4",

‎packages/sh/src/index.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export type { ShPrintOptions }
2626
export interface ShPrinterOptions extends ShPrintOptions {
2727
filepath: string
2828
tabWidth: number
29+
/** Simplify modifies the syntax tree to remove redundant shell syntax. */
30+
simplify?: boolean
2931
}
3032

3133
export class ShSyntaxParseError<
@@ -123,6 +125,7 @@ const dockerPrinter: Printer<string> = {
123125
// printer options
124126
useTabs,
125127
tabWidth,
128+
simplify,
126129
indent = useTabs ? 0 : (tabWidth ?? 2),
127130
binaryNextLine = true,
128131
switchCaseIndent = true,
@@ -158,6 +161,7 @@ const dockerPrinter: Printer<string> = {
158161
recoverErrors,
159162
useTabs,
160163
tabWidth,
164+
simplify,
161165
indent,
162166
binaryNextLine,
163167
switchCaseIndent,
@@ -218,6 +222,7 @@ const shPrinter: Printer<Node | string> = {
218222
// printer options
219223
useTabs,
220224
tabWidth,
225+
simplify,
221226
indent = useTabs ? 0 : tabWidth,
222227
binaryNextLine = true,
223228
switchCaseIndent = true,
@@ -238,6 +243,7 @@ const shPrinter: Printer<Node | string> = {
238243
recoverErrors,
239244
useTabs,
240245
tabWidth,
246+
simplify,
241247
indent,
242248
binaryNextLine,
243249
switchCaseIndent,
@@ -358,6 +364,13 @@ export const options: Plugin['options'] = {
358364
'Second, the subshell needs to be closed, so [Subshell.Rparen] is recovered.',
359365
].join('\n'),
360366
},
367+
simplify: {
368+
category: 'Format',
369+
type: 'boolean',
370+
default: false,
371+
description:
372+
'Simplify modifies the syntax tree to remove redundant shell syntax.',
373+
},
361374
indent: {
362375
// since: '0.1.0',
363376
category: 'Format',

‎packages/sh/test/options.spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { format } from 'prettier'
2+
3+
import * as sh from 'prettier-plugin-sh'
4+
5+
describe('printer options', () => {
6+
const redundantSyntax = 'echo $(( (1 + 2) ))\n'
7+
8+
it('preserves redundant shell syntax by default', async () => {
9+
await expect(
10+
format(redundantSyntax, {
11+
filepath: 'test.sh',
12+
plugins: [sh],
13+
}),
14+
).resolves.toBe('echo $(((1 + 2)))\n')
15+
})
16+
17+
it('simplifies redundant shell syntax when enabled', async () => {
18+
await expect(
19+
format(redundantSyntax, {
20+
filepath: 'test.sh',
21+
plugins: [sh],
22+
simplify: true,
23+
}),
24+
).resolves.toBe('echo $((1 + 2))\n')
25+
})
26+
})

‎yarn.lock‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13013,7 +13013,7 @@ __metadata:
1301313013
"@reteps/dockerfmt": "npm:^0.5.4"
1301413014
"@types/common-tags": "npm:^1.8.4"
1301513015
common-tags: "npm:^1.8.2"
13016-
sh-syntax: "npm:^0.6.0"
13016+
sh-syntax: "npm:^0.7.0"
1301713017
peerDependencies:
1301813018
prettier: ^3.6.0
1301913019
languageName: unknown
@@ -14854,10 +14854,10 @@ __metadata:
1485414854
languageName: node
1485514855
linkType: hard
1485614856

14857-
"sh-syntax@npm:^0.6.0":
14858-
version: 0.6.0
14859-
resolution: "sh-syntax@npm:0.6.0"
14860-
checksum: 10c0/495c660a23c67725cdee077b423ca974521ae580302ee62eda9309c84f6cf31722560cdf55bbea5ca98ba7dd7ac352d59f3eed61612d42b1f8a41cfc7673ba9f
14857+
"sh-syntax@npm:^0.7.0":
14858+
version: 0.7.0
14859+
resolution: "sh-syntax@npm:0.7.0"
14860+
checksum: 10c0/04eb1589e59d0d0313d1ca09718e0ae675f4ac74eb915377ff1c44dbdc7814d3917c5d981bff4e26fd0b313c358fd8de38e8838a8bd96f4608284de99c412319
1486114861
languageName: node
1486214862
linkType: hard
1486314863

0 commit comments

Comments
 (0)