@@ -71,37 +71,49 @@ export class Linter {
71
71
sourcePaths : string [ ] ,
72
72
additionalShellCheckArguments : string [ ] = [ ] ,
73
73
) : Promise < LintingResult > {
74
+ const documentText = document . getText ( )
75
+
76
+ const shellDialect = guessShellDialect ( {
77
+ documentText,
78
+ uri : document . uri ,
79
+ } )
80
+
81
+ if ( shellDialect && ! SUPPORTED_BASH_DIALECTS . includes ( shellDialect ) ) {
82
+ // We found a dialect that isn't supported by ShellCheck.
83
+ return { diagnostics : [ ] , codeActions : [ ] }
84
+ }
85
+
86
+ // NOTE: that ShellCheck actually does shebang parsing, but we manually
87
+ // do it here in order to fallback to bash for files without a shebang.
88
+ // This enables parsing files with a bash syntax, but could yield false positives.
89
+ const shellName =
90
+ shellDialect && SUPPORTED_BASH_DIALECTS . includes ( shellDialect )
91
+ ? shellDialect
92
+ : 'bash'
93
+
74
94
const result = await this . runShellCheck (
75
- document ,
95
+ documentText ,
96
+ shellName ,
76
97
[ ...sourcePaths , dirname ( fileURLToPath ( document . uri ) ) ] ,
77
98
additionalShellCheckArguments ,
78
99
)
100
+
79
101
if ( ! this . _canLint ) {
80
102
return { diagnostics : [ ] , codeActions : [ ] }
81
103
}
82
104
83
105
// Clean up the debounced function
84
106
delete this . uriToDebouncedExecuteLint [ document . uri ]
85
107
86
- return mapShellCheckResult ( { document, result } )
108
+ return mapShellCheckResult ( { uri : document . uri , result } )
87
109
}
88
110
89
111
private async runShellCheck (
90
- document : TextDocument ,
112
+ documentText : string ,
113
+ shellName : string ,
91
114
sourcePaths : string [ ] ,
92
115
additionalArgs : string [ ] = [ ] ,
93
116
) : Promise < ShellCheckResult > {
94
- const documentText = document . getText ( )
95
-
96
- const { shellDialect } = analyzeShebang ( documentText )
97
- // NOTE: that ShellCheck actually does shebang parsing, but we manually
98
- // do it here in order to fallback to bash. This enables parsing files
99
- // with a bash syntax.
100
- const shellName =
101
- shellDialect && SUPPORTED_BASH_DIALECTS . includes ( shellDialect )
102
- ? shellDialect
103
- : 'bash'
104
-
105
117
const sourcePathsArgs = sourcePaths
106
118
. map ( ( folder ) => folder . trim ( ) )
107
119
. filter ( ( folderName ) => folderName )
@@ -169,10 +181,10 @@ export class Linter {
169
181
}
170
182
171
183
function mapShellCheckResult ( {
172
- document ,
184
+ uri ,
173
185
result,
174
186
} : {
175
- document : TextDocument
187
+ uri : string
176
188
result : ShellCheckResult
177
189
} ) : {
178
190
diagnostics : LSP . Diagnostic [ ]
@@ -208,8 +220,8 @@ function mapShellCheckResult({
208
220
209
221
const codeAction = CodeActionProvider . getCodeAction ( {
210
222
comment,
211
- document,
212
223
diagnostics : [ diagnostic ] ,
224
+ uri,
213
225
} )
214
226
215
227
if ( codeAction ) {
@@ -230,12 +242,12 @@ function mapShellCheckResult({
230
242
class CodeActionProvider {
231
243
public static getCodeAction ( {
232
244
comment,
233
- document,
234
245
diagnostics,
246
+ uri,
235
247
} : {
236
248
comment : ShellCheckComment
237
- document : TextDocument
238
249
diagnostics : LSP . Diagnostic [ ]
250
+ uri : string
239
251
} ) : LSP . CodeAction | null {
240
252
const { code, fix } = comment
241
253
if ( ! fix || fix . replacements . length === 0 ) {
@@ -257,7 +269,7 @@ class CodeActionProvider {
257
269
diagnostics,
258
270
edit : {
259
271
changes : {
260
- [ document . uri ] : edits ,
272
+ [ uri ] : edits ,
261
273
} ,
262
274
} ,
263
275
kind : LSP . CodeActionKind . QuickFix ,
@@ -283,3 +295,7 @@ class CodeActionProvider {
283
295
}
284
296
}
285
297
}
298
+
299
+ function guessShellDialect ( { documentText, uri } : { documentText : string ; uri : string } ) {
300
+ return uri . endsWith ( '.zsh' ) ? 'zsh' : analyzeShebang ( documentText ) . shellDialect
301
+ }
0 commit comments