From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#57884: [PATCH] Flymake backend using the shellcheck program Date: Sat, 17 Sep 2022 19:32:44 +0200 Message-ID: <871qs9c3er.fsf@gmail.com> References: <87a66yaqwc.fsf@gmail.com> <83bkre0w4m.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21249"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: 57884@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 17 19:33:13 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oZbgm-0005K5-6r for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Sep 2022 19:33:12 +0200 Original-Received: from localhost ([::1]:38282 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oZbgk-00029b-Pd for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Sep 2022 13:33:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60726) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oZbgd-00029R-8P for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 13:33:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:48039) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oZbgc-0002gS-Ua for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 13:33:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oZbgc-0007az-HY for bug-gnu-emacs@gnu.org; Sat, 17 Sep 2022 13:33:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 17 Sep 2022 17:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 57884 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 57884-submit@debbugs.gnu.org id=B57884.166343597829182 (code B ref 57884); Sat, 17 Sep 2022 17:33:02 +0000 Original-Received: (at 57884) by debbugs.gnu.org; 17 Sep 2022 17:32:58 +0000 Original-Received: from localhost ([127.0.0.1]:47114 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZbgX-0007ab-P7 for submit@debbugs.gnu.org; Sat, 17 Sep 2022 13:32:58 -0400 Original-Received: from mail-ed1-f49.google.com ([209.85.208.49]:46864) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oZbgS-0007aJ-SC for 57884@debbugs.gnu.org; Sat, 17 Sep 2022 13:32:56 -0400 Original-Received: by mail-ed1-f49.google.com with SMTP id z13so21726411edb.13 for <57884@debbugs.gnu.org>; Sat, 17 Sep 2022 10:32:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date; bh=Io6BLmas5crugaFhsvZ8PVTqZdoeRmk0tOPHYPFbzYs=; b=eRm1AcAZE0wAhb/b2jpsXPy9L3B1ESGjNxehnEAuN+jY9x1TH1nRgm60XIs5veP7JK KtAZlnpo3JZRC9KCEZPDfu2gf//MpKLZz97iIHDWxdOa47xdwUS3qv0cwZF6Q1K+8hIL pdIyR/Ssw6rZJvQC8bxSl8LZuuCyMJP+B59CZuHbq72+fwdKGRx3DBQT3kzlVwN5qYHg zZctkYZmjnmR+uqcnHY+rQt0qUF2NOWSlWDz6mvQQ3lz1WWQ17SLbgaVag/6CAoZri1H 8gsW+mAD+E6CCyCEKgwvQgmSLdi6sOobHcacvFfojb6o1xsFAMNalcR2KxXMLseJCjqB FYyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date; bh=Io6BLmas5crugaFhsvZ8PVTqZdoeRmk0tOPHYPFbzYs=; b=HTRjrqg48KbSESpa7Py7gkG2Qc3yu+9IynTQAOLWDTiIYKk1gSMzIahrluVV8L2iCe Tx1+fDpRTYWX+bWGN/l93TEg+IPluc/ExLK9OKiUZzuutmz+3rQsx3O4C89mokuN2nOm q7RgF7w1GIS2EMoeKvG/bwoyz877SDGF7wXeZys3hak3vN+3rMliXl3vQhPp1Mi8wbZr 8aXQtCCqC8eFrZiSuWyN2bMEjELg22yxXfAbN/NC6SqiOOnJ6AD6Z/1uklJJdZ42mbmZ e8S1mB9RnKktROtXtjU2AgveU0prP8qKSlRx62uxo8MBbElxpT06Rle4dFXAO61qzryt wZTA== X-Gm-Message-State: ACrzQf1LkvUrtlWEAGFqD16usIE9mRNcQW69rolgcnH+2UGHqMePgNKS guDllFgCR4GHyN37KSBxfiuRjsxVyBKswA== X-Google-Smtp-Source: AMsMyM55jQP48qFlHmqa49et1ejvQCUY+xq1+mMMGP4cW+0433XwXnIG0cQA16eL0O30doMrRfQYUA== X-Received: by 2002:a05:6402:1945:b0:446:692:8aeb with SMTP id f5-20020a056402194500b0044606928aebmr8436939edz.403.1663435966447; Sat, 17 Sep 2022 10:32:46 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::8510]) by smtp.gmail.com with ESMTPSA id i20-20020aa7c9d4000000b00448176872f7sm15965358edt.81.2022.09.17.10.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Sep 2022 10:32:45 -0700 (PDT) In-Reply-To: <83bkre0w4m.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 17 Sep 2022 20:05:29 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:242891 Archived-At: --=-=-= Content-Type: text/plain I've attached a new patch. See my comments below. On Sat, 17 Sep 2022 at 20:05, Eli Zaretskii wrote: >> From: Augusto Stoffel >> Date: Sat, 17 Sep 2022 18:48:19 +0200 >> >> See https://www.shellcheck.net/ for details on the ShellCheck program. >> It seems to be the most usual linter for shell scripts. > > Thanks. > >> * lisp/progmodes/sh-script.el (sh-shellcheck-program, >> sh--shellcheck-process, sh-shellcheck-flymake): Variables and function >> defining a Flymake backend. >> (sh-mode): Add it to 'flymake-diagnostic-functions'. > > Please mention the bug number in the log message. Done. >> +(defcustom sh-shellcheck-command '("shellcheck") >> + "The shellcheck program followed by extra arguments." >> + :type 'string) > > This lacks the :version tag. Done (and fixed the :type). > Also, wouldn't it be better to have separate user options for the > program and the switches? That way, there won't be a need to require > users to provide a list as the value of the option. I could split this into two defcustoms if you feel strongly about it, but it seems a bit of overengineering to me. Not many people will want to customize the program name, and the switches would have to be provided as a list anyway, since we're not going to call this through a shell. Let me know what you think. >> +(defun sh-shellcheck-flymake (report-fn &rest _args) >> + "Flymake backend using the shellcheck program. > > The first line of a function's doc string should mention its > arguments. It didn't fit the first line, so as per standard procedure it's in the body of the docstring. > And I think this warrants a NEWS entry. Done. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-New-Flymake-backend-using-the-shellcheck-program.patch >From 7598653c31bc72a30bc7ed7ba28cc9b6c6b6c843 Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Sat, 17 Sep 2022 18:30:04 +0200 Subject: [PATCH] New Flymake backend using the shellcheck program See bug#57884. * lisp/progmodes/sh-script.el (sh-shellcheck-program, sh--shellcheck-process, sh-shellcheck-flymake): Variables and function defining a Flymake backend. (sh-mode): Add it to 'flymake-diagnostic-functions'. --- etc/NEWS | 5 +++ lisp/progmodes/sh-script.el | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index a6a8883593..60e171ad13 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1320,6 +1320,11 @@ This controls how statements like the following are indented: foo && bar ++++ +*** New Flymake backend using the ShellCheck program +It is enable by default, only requiring that ShellCheck is installed. +See https://www.shellcheck.net/ for details. + ** Cperl Mode --- diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 517fbbd8e7..9c0f6dabd5 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -31,6 +31,9 @@ ;; available for filenames, variables known from the script, the shell and ;; the environment as well as commands. +;; A Flymake backend using the ShellCheck program is provided. See +;; https://www.shellcheck.net/ for installation instructions. + ;;; Known Bugs: ;; - In Bourne the keyword `in' is not anchored to case, for, select ... @@ -1580,6 +1583,7 @@ sh-mode ((equal (file-name-nondirectory buffer-file-name) ".profile") "sh") (t sh-shell-file)) nil nil) + (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t) (add-hook 'hack-local-variables-hook #'sh-after-hack-local-variables nil t)) @@ -3103,6 +3107,73 @@ sh-delete-backslash (delete-region (1+ (point)) (progn (skip-chars-backward " \t") (point))))))) +;;; Flymake backend + +(defcustom sh-shellcheck-command '("shellcheck") + "The shellcheck program followed by extra arguments." + :type '(repeat string) + :version "29.1") + +(defvar-local sh--shellcheck-process nil) + +(defun sh-shellcheck-flymake (report-fn &rest _args) + "Flymake backend using the shellcheck program. +Takes a Flymake callback REPORT-FN as argument, as expected of a +member of `flymake-diagnostic-functions'." + (when (process-live-p sh--shellcheck-process) + (kill-process sh--shellcheck-process)) + (let* ((source (current-buffer)) + (dialect (named-let dialect ((s sh-shell)) + (pcase s + ((or 'bash 'dash 'sh) (symbol-name s)) + ('ksh88 "ksh") + ((guard s) + (dialect (alist-get s sh-ancestor-alist)))))) + (pattern "^-:\\([0-9]+\\):\\([0-9]+\\): \\([^:]+\\): \\(.*\\)$") + (sentinel + (lambda (proc _event) + (when (memq (process-status proc) '(exit signal)) + (unwind-protect + (if (with-current-buffer source + (not (eq proc sh--shellcheck-process))) + (flymake-log :warning "Canceling obsolete check %s" proc) + (with-current-buffer (process-buffer proc) + (goto-char (point-min)) + (cl-loop + while (search-forward-regexp pattern nil t) + for msg = (match-string 4) + for (beg . end) = (flymake-diag-region + source + (string-to-number (match-string 1)) + (string-to-number (match-string 2))) + for type = (pcase (match-string 3) + ("error" :error) + ("warning" :warning) + (_ :note)) + when (and beg end) + collect (flymake-make-diagnostic source beg end type msg) + into diags + finally (funcall report-fn diags)))) + (kill-buffer (process-buffer proc))))))) + (unless dialect + (error "`sh-shellcheck-flymake' is not suitable for shell type `%s'" + sh-shell)) + (setq sh--shellcheck-process + (make-process + :name "luacheck" :noquery t :connection-type 'pipe + :buffer (generate-new-buffer " *flymake-luacheck*") + :command `(,(car sh-shellcheck-command) + "--format=gcc" + "--color=never" + "-s" ,dialect + ,@(cdr sh-shellcheck-command) + "-") + :sentinel sentinel)) + (save-restriction + (widen) + (process-send-region sh--shellcheck-process (point-min) (point-max)) + (process-send-eof sh--shellcheck-process)))) + (provide 'sh-script) ;;; sh-script.el ends here -- 2.37.3 --=-=-=--