Skip to content

Commit 2d81228

Browse files
committed
Add support for --simplify shfmt option
This wasn't included initially as it does more than just format code. However, its absence has been noted in a comment on the last PR: #1136 (comment) and it is a valid `shfmt` option, so support has been added. There's a similar option (`-mn` or `--minify`), but this will not be implemented as its output is not intended for human consumption and it will essentially ignore all other options presented anyway.
1 parent 351b194 commit 2d81228

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

server/src/__tests__/config.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('ConfigSchema', () => {
1919
"funcNextLine": false,
2020
"keepPadding": false,
2121
"path": "shfmt",
22+
"simplifyCode": false,
2223
"spaceRedirects": false,
2324
},
2425
}
@@ -39,6 +40,7 @@ describe('ConfigSchema', () => {
3940
funcNextLine: true,
4041
keepPadding: true,
4142
path: 'myshfmt',
43+
simplifyCode: true,
4244
spaceRedirects: true,
4345
},
4446
}),
@@ -63,6 +65,7 @@ describe('ConfigSchema', () => {
6365
"funcNextLine": true,
6466
"keepPadding": true,
6567
"path": "myshfmt",
68+
"simplifyCode": true,
6669
"spaceRedirects": true,
6770
},
6871
}
@@ -97,6 +100,7 @@ describe('getConfigFromEnvironmentVariables', () => {
97100
"funcNextLine": false,
98101
"keepPadding": false,
99102
"path": "shfmt",
103+
"simplifyCode": false,
100104
"spaceRedirects": false,
101105
},
102106
}
@@ -125,6 +129,7 @@ describe('getConfigFromEnvironmentVariables', () => {
125129
"funcNextLine": false,
126130
"keepPadding": false,
127131
"path": "",
132+
"simplifyCode": false,
128133
"spaceRedirects": false,
129134
},
130135
}
@@ -162,6 +167,7 @@ describe('getConfigFromEnvironmentVariables', () => {
162167
"funcNextLine": false,
163168
"keepPadding": false,
164169
"path": "/path/to/shfmt",
170+
"simplifyCode": false,
165171
"spaceRedirects": false,
166172
},
167173
}

server/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export const ConfigSchema = z.object({
5858
// (Deprecated) Keep column alignment padding.
5959
keepPadding: z.boolean().default(false),
6060

61+
// Simplify code before formatting.
62+
simplifyCode: z.boolean().default(false),
63+
6164
// Follow redirection operators with a space.
6265
spaceRedirects: z.boolean().default(false),
6366
})
@@ -85,6 +88,7 @@ export function getConfigFromEnvironmentVariables(): {
8588
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
8689
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),
8790
keepPadding: toBoolean(process.env.SHFMT_KEEP_PADDING),
91+
simplifyCode: toBoolean(process.env.SHFMT_SIMPLIFY_CODE),
8892
spaceRedirects: toBoolean(process.env.SHFMT_SPACE_REDIRECTS),
8993
},
9094
}

server/src/shfmt/__tests__/index.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ describe('formatter', () => {
8787
echo four five six
8888
echo seven eight nine
8989
90+
[[ "$simplify" == "simplify" ]]
91+
9092
echo space redirects >/dev/null
9193
9294
function next() {
@@ -136,6 +138,8 @@ describe('formatter', () => {
136138
echo four five six
137139
echo seven eight nine
138140
141+
[[ "$simplify" == "simplify" ]]
142+
139143
echo space redirects >/dev/null
140144
141145
function next() {
@@ -185,6 +189,8 @@ describe('formatter', () => {
185189
echo four five six
186190
echo seven eight nine
187191
192+
[[ "$simplify" == "simplify" ]]
193+
188194
echo space redirects >/dev/null
189195
190196
function next() {
@@ -235,6 +241,8 @@ describe('formatter', () => {
235241
echo four five six
236242
echo seven eight nine
237243
244+
[[ "$simplify" == "simplify" ]]
245+
238246
echo space redirects >/dev/null
239247
240248
function next() {
@@ -285,6 +293,8 @@ describe('formatter', () => {
285293
echo four five six
286294
echo seven eight nine
287295
296+
[[ "$simplify" == "simplify" ]]
297+
288298
echo space redirects >/dev/null
289299
290300
function next() {
@@ -335,6 +345,8 @@ describe('formatter', () => {
335345
echo four five six
336346
echo seven eight nine
337347
348+
[[ "$simplify" == "simplify" ]]
349+
338350
echo space redirects >/dev/null
339351
340352
function next()
@@ -386,6 +398,60 @@ describe('formatter', () => {
386398
echo four five six
387399
echo seven eight nine
388400
401+
[[ "$simplify" == "simplify" ]]
402+
403+
echo space redirects >/dev/null
404+
405+
function next() {
406+
echo line
407+
}
408+
",
409+
"range": {
410+
"end": {
411+
"character": 2147483647,
412+
"line": 2147483647,
413+
},
414+
"start": {
415+
"character": 0,
416+
"line": 0,
417+
},
418+
},
419+
},
420+
]
421+
`)
422+
})
423+
424+
it('should format after simplifying the code when simplifyCode is true', async () => {
425+
const [result] = await getFormattingResult({
426+
document: FIXTURE_DOCUMENT.SHFMT,
427+
formatOptions: { tabSize: 2, insertSpaces: true },
428+
shfmtConfig: { simplifyCode: true },
429+
})
430+
expect(result).toMatchInlineSnapshot(`
431+
[
432+
{
433+
"newText": "#!/bin/bash
434+
set -ueo pipefail
435+
436+
if [ -z "$arg" ]; then
437+
echo indent
438+
fi
439+
440+
echo binary &&
441+
echo next line
442+
443+
case "$arg" in
444+
a)
445+
echo case indent
446+
;;
447+
esac
448+
449+
echo one two three
450+
echo four five six
451+
echo seven eight nine
452+
453+
[[ $simplify == "simplify" ]]
454+
389455
echo space redirects >/dev/null
390456
391457
function next() {
@@ -436,6 +502,8 @@ describe('formatter', () => {
436502
echo four five six
437503
echo seven eight nine
438504
505+
[[ "$simplify" == "simplify" ]]
506+
439507
echo space redirects > /dev/null
440508
441509
function next() {
@@ -466,6 +534,7 @@ describe('formatter', () => {
466534
caseIndent: true,
467535
funcNextLine: true,
468536
keepPadding: true,
537+
simplifyCode: true,
469538
spaceRedirects: true,
470539
},
471540
})
@@ -492,6 +561,8 @@ describe('formatter', () => {
492561
echo four five six
493562
echo seven eight nine
494563
564+
[[ $simplify == "simplify" ]]
565+
495566
echo space redirects > /dev/null
496567
497568
function next()

server/src/shfmt/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class Formatter {
7171
if (shfmtConfig?.caseIndent) args.push('-ci') // --case-indent
7272
if (shfmtConfig?.funcNextLine) args.push('-fn') // --func-next-line
7373
if (shfmtConfig?.keepPadding) args.push('-kp') // --keep-padding
74+
if (shfmtConfig?.simplifyCode) args.push('-s') // --simplify
7475
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects
7576

7677
logger.debug(`Shfmt: running "${this.executablePath} ${args.join(' ')}"`)

testing/fixtures/shfmt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ echo one two three
1818
echo four five six
1919
echo seven eight nine
2020

21+
[[ "$simplify" == "simplify" ]]
22+
2123
echo space redirects> /dev/null
2224

2325
function next(){

vscode-client/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
"description": "(Deprecated) Keep column alignment padding.",
105105
"markdownDescription": "**([Deprecated](https://github.com/mvdan/sh/issues/658))** Keep column alignment padding."
106106
},
107+
"bashIde.shfmt.simplifyCode": {
108+
"type": "boolean",
109+
"default": false,
110+
"description": "Simplify code before formatting."
111+
},
107112
"bashIde.shfmt.spaceRedirects": {
108113
"type": "boolean",
109114
"default": false,

0 commit comments

Comments
 (0)