Skip to content

Commit ad408bc

Browse files
committed
feat(prettier-plugin-sh): ✨ support zsh dialect via sh-syntax@^0.6.0
Closes #511
1 parent 4323d8c commit ad408bc

7 files changed

Lines changed: 59 additions & 10 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+
feat: ✨ support `zsh` dialect by upgrading `sh-syntax` to `^0.6.0`

‎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.2",
50-
"sh-syntax": "^0.5.8"
50+
"sh-syntax": "^0.6.0"
5151
},
5252
"devDependencies": {
5353
"@types/common-tags": "^1.8.4",

‎packages/sh/src/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ export const options: Plugin['options'] = {
313313
'Its string representation is "bats".',
314314
].join('\n'),
315315
},
316+
{
317+
value: LangVariant.LangZsh,
318+
description: [
319+
'LangZsh corresponds to the Z shell language, as described at https://www.zsh.org.',
320+
'Note that it shares most features with Bash, adding zsh-specific extensions such as parameter modifier chains (e.g. "${0:A:h}").',
321+
'',
322+
'Its string representation is "zsh".',
323+
].join('\n'),
324+
},
316325
{
317326
value: LangVariant.LangAuto,
318327
description: [

‎packages/sh/test/__snapshots__/fixtures.spec.ts.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ CMD mkdir --parents /var/log/nginx && nginx -g "daemon off;"
912912
"
913913
`;
914914
915-
exports[`parser and printer > should format 445.sh fixture correctly 1`] = `"reached EOF without closing quote '"`;
915+
exports[`parser and printer > should format 445.sh fixture correctly 1`] = `"reached EOF without closing quote \`'\`"`;
916916
917917
exports[`parser and printer > should format Dockerfile fixture correctly 1`] = `
918918
"FROM node:lts-alpine as builder

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ test('fatal parse error with meaningful message', async () => {
1313
)
1414
await rejects.toThrow(ParseError)
1515
await rejects.toThrow(
16-
'a command can only contain words and redirects; encountered )',
16+
'a command can only contain words and redirects; encountered `)`',
1717
)
1818
})

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { format } from 'prettier'
2+
import { LangVariant } from 'sh-syntax'
3+
import { describe, expect, it } from 'vitest'
4+
5+
import * as sh from 'prettier-plugin-sh'
6+
7+
/*
8+
* Regression test for zsh support, tracking:
9+
* - sh-syntax issue: https://github.com/un-ts/sh-syntax/issues/135
10+
* - sh-syntax fix PR: https://github.com/un-ts/sh-syntax/pull/136
11+
*
12+
* `${0:A:h:h}` is valid zsh (modifier chain: :A absolute path, :h head/dirname)
13+
* but is rejected by the bash grammar with "ternary operator missing ? before :".
14+
* sh-syntax >= 0.6.0 rebuilds the WASM against mvdan/sh v3.13 and exposes a
15+
* dedicated `LangZsh` variant that parses it correctly.
16+
*
17+
* Expected results by installed sh-syntax version:
18+
* - 0.5.8 (before): RED - `LangZsh` is undefined and format() throws.
19+
* - 0.6.0 (after): GREEN - the zsh variant parses and prints the input.
20+
*/
21+
describe('zsh support (sh-syntax >= 0.6.0)', () => {
22+
const ZSH_SOURCE = 'DIR=${0:A:h:h}\n'
23+
24+
it('exposes the LangZsh variant constant', () => {
25+
expect(LangVariant.LangZsh).toBeDefined()
26+
})
27+
28+
it('formats a zsh modifier chain when using the zsh variant', async () => {
29+
const output = await format(ZSH_SOURCE, {
30+
filepath: 'script.zsh',
31+
plugins: [sh],
32+
variant: LangVariant.LangZsh,
33+
})
34+
35+
expect(output).toContain('${0:A:h:h}')
36+
})
37+
})

‎yarn.lock‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12730,7 +12730,7 @@ __metadata:
1273012730
"@reteps/dockerfmt": "npm:^0.5.2"
1273112731
"@types/common-tags": "npm:^1.8.4"
1273212732
common-tags: "npm:^1.8.2"
12733-
sh-syntax: "npm:^0.5.8"
12733+
sh-syntax: "npm:^0.6.0"
1273412734
peerDependencies:
1273512735
prettier: ^3.6.0
1273612736
languageName: unknown
@@ -14569,12 +14569,10 @@ __metadata:
1456914569
languageName: node
1457014570
linkType: hard
1457114571

14572-
"sh-syntax@npm:^0.5.8":
14573-
version: 0.5.8
14574-
resolution: "sh-syntax@npm:0.5.8"
14575-
dependencies:
14576-
tslib: "npm:^2.8.1"
14577-
checksum: 10c0/2d2609fc8760ef97175c852be26ee3eeb196078c5aec282c8b96a59ee362be4f470d3e0df4e372da6c7a7b44ccc42910cbdcb0915271b3cb6a6212d00dde116f
14572+
"sh-syntax@npm:^0.6.0":
14573+
version: 0.6.0
14574+
resolution: "sh-syntax@npm:0.6.0"
14575+
checksum: 10c0/495c660a23c67725cdee077b423ca974521ae580302ee62eda9309c84f6cf31722560cdf55bbea5ca98ba7dd7ac352d59f3eed61612d42b1f8a41cfc7673ba9f
1457814576
languageName: node
1457914577
linkType: hard
1458014578

0 commit comments

Comments
 (0)