branch: elpa/flycheck
commit 672ed3d35c7d0a386aa755bae15bb143f356923a
Author: Jackson Ray Hamilton <[email protected]>
Commit: GitHub <[email protected]>

    Add new option flycheck-shellcheck-infer-shell (#2132)
---
 CHANGES.rst                                    |  6 ++++++
 doc/languages.rst                              |  4 ++++
 flycheck.el                                    | 14 +++++++++++++-
 test/flycheck-test.el                          | 19 +++++++++++++++----
 test/resources/language/sh/shellcheck-infer.sh |  6 ++++++
 5 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index bd9ad20a3e1..391b8c3a64b 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,6 +1,12 @@
 ``master`` (unreleased)
 ======================
 
+------------
+New Features
+------------
+
+- [#2132]: Add the ``flycheck-shellcheck-infer-shell`` option to the 
``sh-shellcheck`` checker.
+
 -----------
 Bugs fixed
 -----------
diff --git a/doc/languages.rst b/doc/languages.rst
index 22366219d58..8946f8476eb 100644
--- a/doc/languages.rst
+++ b/doc/languages.rst
@@ -1548,6 +1548,10 @@ to view the docstring of the syntax checker.  Likewise, 
you may use
 
          Allow shellcheck to read sourced files.
 
+      .. defcustom:: flycheck-shellcheck-infer-shell
+
+         Whether to let ShellCheck infer the shell from the script.
+
 .. supported-language:: Slim
 
    .. syntax-checker:: slim
diff --git a/flycheck.el b/flycheck.el
index 6fb9fd2c3b8..6b21bf3886d 100644
--- a/flycheck.el
+++ b/flycheck.el
@@ -12511,13 +12511,25 @@ or added as a shellcheck directive before the source 
command:
   :safe #'booleanp
   :package-version '(flycheck . "31"))
 
+(flycheck-def-option-var flycheck-shellcheck-infer-shell nil sh-shellcheck
+  "Whether to let ShellCheck infer the shell from the script.
+
+When non-nil, the --shell flag is not passed to ShellCheck,
+allowing it to infer the shell from the shebang line or
+shellcheck directives in the script."
+  :type 'boolean
+  :safe #'booleanp
+  :package-version '(flycheck . "36"))
+
 (flycheck-define-checker sh-shellcheck
   "A shell script syntax and style checker using Shellcheck.
 
 See URL `https://github.com/koalaman/shellcheck/'."
   :command ("shellcheck"
             "--format" "checkstyle"
-            "--shell" (eval (symbol-name sh-shell))
+            (eval
+             (unless flycheck-shellcheck-infer-shell
+               (list "--shell" (symbol-name sh-shell))))
             (option-flag "--external-sources"
                          flycheck-shellcheck-follow-sources)
             (option "--exclude" flycheck-shellcheck-excluded-warnings list
diff --git a/test/flycheck-test.el b/test/flycheck-test.el
index 62618ab2625..aaee28b1bab 100644
--- a/test/flycheck-test.el
+++ b/test/flycheck-test.el
@@ -5022,16 +5022,27 @@ The manifest path is relative to
   (let ((inhibit-message t))
     (flycheck-ert-should-syntax-check
      "language/sh/shellcheck.sh" 'sh-mode
+     ;; Matches output from shellcheck 0.11.0:
      '(2 5 warning "Tilde does not expand in quotes. Use $HOME."
          :checker sh-shellcheck :id "SC2088")
      '(3 7 error "Double quote array expansions to avoid re-splitting 
elements."
          :checker sh-shellcheck :id "SC2068")
      '(4 8 warning "Declare and assign separately to avoid masking return 
values."
          :checker sh-shellcheck :id "SC2155")
-     '(4 11 info "Use $(...) notation instead of legacy backticked `...`."
-         :checker sh-shellcheck :id "SC2006")
-     '(4 12 info "which is non-standard. Use builtin 'command -v' instead."
-         :checker sh-shellcheck :id "SC2230"))))
+     '(4 11 warning "Quote this to prevent word splitting."
+         :checker sh-shellcheck :id "SC2046")
+     '(4 11 info "Use $(...) notation instead of legacy backticks `...`."
+         :checker sh-shellcheck :id "SC2006"))))
+
+(flycheck-ert-def-checker-test sh-shellcheck sh infer-shell
+  :tags '(checkstyle-xml)
+  (let ((inhibit-message t)
+        (flycheck-shellcheck-supported-shells '(bash ksh88 sh zsh))
+        (flycheck-shellcheck-infer-shell t))
+    (flycheck-ert-should-syntax-check
+     "language/sh/shellcheck-infer.sh" 'sh-mode
+     '(4 15 warning "Remove quotes from right-hand side of =~ to match as a 
regex rather than literally."
+         :checker sh-shellcheck :id "SC2076"))))
 
 (flycheck-ert-def-checker-test slim slim nil
   (flycheck-ert-should-syntax-check
diff --git a/test/resources/language/sh/shellcheck-infer.sh 
b/test/resources/language/sh/shellcheck-infer.sh
new file mode 100644
index 00000000000..73a28c117f3
--- /dev/null
+++ b/test/resources/language/sh/shellcheck-infer.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env zsh
+# shellcheck shell=bash
+var="foo bar"
+if [[ $var =~ "foo.*bar" ]]; then
+    echo "Match!"
+fi

Reply via email to