Skip to content

Commit 0c9504c

Browse files
committed
Add support for --language-dialect shfmt option
Language dialect is something that can be set via `.editorconfig`, so we'll need to respect that when running `shfmt`.
1 parent fe93389 commit 0c9504c

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

server/src/__tests__/config.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('ConfigSchema', () => {
1818
"caseIndent": false,
1919
"funcNextLine": false,
2020
"keepPadding": false,
21+
"languageDialect": "auto",
2122
"path": "shfmt",
2223
"simplifyCode": false,
2324
"spaceRedirects": false,
@@ -39,6 +40,7 @@ describe('ConfigSchema', () => {
3940
caseIndent: true,
4041
funcNextLine: true,
4142
keepPadding: true,
43+
languageDialect: 'posix',
4244
path: 'myshfmt',
4345
simplifyCode: true,
4446
spaceRedirects: true,
@@ -64,6 +66,7 @@ describe('ConfigSchema', () => {
6466
"caseIndent": true,
6567
"funcNextLine": true,
6668
"keepPadding": true,
69+
"languageDialect": "posix",
6770
"path": "myshfmt",
6871
"simplifyCode": true,
6972
"spaceRedirects": true,
@@ -99,6 +102,7 @@ describe('getConfigFromEnvironmentVariables', () => {
99102
"caseIndent": false,
100103
"funcNextLine": false,
101104
"keepPadding": false,
105+
"languageDialect": "auto",
102106
"path": "shfmt",
103107
"simplifyCode": false,
104108
"spaceRedirects": false,
@@ -128,6 +132,7 @@ describe('getConfigFromEnvironmentVariables', () => {
128132
"caseIndent": false,
129133
"funcNextLine": false,
130134
"keepPadding": false,
135+
"languageDialect": "auto",
131136
"path": "",
132137
"simplifyCode": false,
133138
"spaceRedirects": false,
@@ -166,6 +171,7 @@ describe('getConfigFromEnvironmentVariables', () => {
166171
"caseIndent": true,
167172
"funcNextLine": false,
168173
"keepPadding": false,
174+
"languageDialect": "auto",
169175
"path": "/path/to/shfmt",
170176
"simplifyCode": false,
171177
"spaceRedirects": false,

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+
// Language dialect to use when parsing (bash/posix/mksh/bats).
62+
languageDialect: z.string().trim().default('auto'),
63+
6164
// Simplify code before formatting.
6265
simplifyCode: z.boolean().default(false),
6366

@@ -84,6 +87,7 @@ export function getConfigFromEnvironmentVariables(): {
8487
shellcheckPath: process.env.SHELLCHECK_PATH,
8588
shfmt: {
8689
path: process.env.SHFMT_PATH,
90+
languageDialect: process.env.SHFMT_LANGUAGE_DIALECT,
8791
binaryNextLine: toBoolean(process.env.SHFMT_BINARY_NEXT_LINE),
8892
caseIndent: toBoolean(process.env.SHFMT_CASE_INDENT),
8993
funcNextLine: toBoolean(process.env.SHFMT_FUNC_NEXT_LINE),

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ describe('formatter', () => {
6262
)
6363
})
6464

65+
it('should throw when parsing using the wrong language dialect', async () => {
66+
expect(async () => {
67+
await getFormattingResult({
68+
document: FIXTURE_DOCUMENT.SHFMT,
69+
shfmtConfig: { languageDialect: 'posix' },
70+
})
71+
}).rejects.toThrow(
72+
/Shfmt: exited with status 1: .*\/testing\/fixtures\/shfmt.sh:25:14: the "function" builtin exists in bash; tried parsing as posix/,
73+
)
74+
})
75+
6576
it('should format when shfmt is present', async () => {
6677
const [result] = await getFormattingResult({ document: FIXTURE_DOCUMENT.SHFMT })
6778
expect(result).toMatchInlineSnapshot(`

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?.keepPadding) args.push('-kp') // --keep-padding
7272
if (shfmtConfig?.simplifyCode) args.push('-s') // --simplify
7373
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects
74+
if (shfmtConfig?.languageDialect) args.push(`-ln=${shfmtConfig.languageDialect}`) // --language-dialect
7475

7576
// If we can determine a local filename, pass that to shfmt to aid language dialect detection
7677
const filePathMatch = document.uri.match(/^file:\/\/(.*)$/)

vscode-client/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
"default": "shfmt",
8484
"description": "Controls the executable used for Shfmt formatting. An empty string will disable formatting."
8585
},
86+
"bashIde.shfmt.languageDialect": {
87+
"type": "string",
88+
"default": "auto",
89+
"description": "Language dialect to use when parsing (bash/posix/mksh/bats)."
90+
},
8691
"bashIde.shfmt.binaryNextLine": {
8792
"type": "boolean",
8893
"default": false,

0 commit comments

Comments
 (0)