Permalink
Browse files

Simplify argument substitution

flycheck-substitute-argument always returns a list now, which reduces
the -flatten/-keep dance to a simple append/mapcar.
  • Loading branch information...
1 parent b9020a3 commit 8c44c5f620eca8f645e336af764411bd76e0aa8b @lunaryorn lunaryorn committed Jul 18, 2014
Showing with 39 additions and 34 deletions.
  1. +1 −0 CHANGES.rst
  2. +24 −20 flycheck.el
  3. +14 −14 test/flycheck-test.el
View
@@ -7,6 +7,7 @@ master (in development)
- The internal names of syntax checker properties changed. **All packages
depending on Flycheck must be recompiled!**
+ - `flycheck-substitute-argument` always returns a list now
- New syntax checkers:
View
@@ -1948,6 +1948,10 @@ configuration file was found."
(defun flycheck-substitute-argument (arg checker)
"Substitute ARG for CHECKER.
+Return a list of real arguments for the executable of CHECKER,
+substituted for the symbolic argument ARG. Single arguments,
+e.g. if ARG is a literal strings, are wrapped in a list.
+
ARG may be one of the following forms:
STRING
@@ -2046,16 +2050,16 @@ In all other cases, signal an error.
Note that substitution is *not* recursive. No symbols or cells
are substituted within the body of cells!"
(pcase arg
- ((pred stringp) arg)
+ ((pred stringp) (list arg))
(`source
- (flycheck-save-buffer-to-temp #'flycheck-temp-file-system))
+ (list (flycheck-save-buffer-to-temp #'flycheck-temp-file-system)))
(`source-inplace
- (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace))
- (`source-original (or (buffer-file-name) ""))
- (`temporary-directory (flycheck-temp-dir-system))
+ (list (flycheck-save-buffer-to-temp #'flycheck-temp-file-inplace)))
+ (`source-original (list (or (buffer-file-name) "")))
+ (`temporary-directory (list (flycheck-temp-dir-system)))
(`temporary-file-name
(let ((directory (flycheck-temp-dir-system)))
- (make-temp-name (expand-file-name "flycheck" directory))))
+ (list (make-temp-name (expand-file-name "flycheck" directory)))))
(`(config-file ,option-name ,file-name-var)
(-when-let* ((value (symbol-value file-name-var))
(file-name (flycheck-locate-config-file value checker)))
@@ -2092,14 +2096,13 @@ are substituted within the body of cells!"
(flycheck-prepend-with-option option-name value prepend-fn)))
(`(option-flag ,option-name ,variable)
(when (symbol-value variable)
- option-name))
+ (list option-name)))
(`(eval ,form)
(let ((result (eval form)))
- (if (or (null result)
- (stringp result)
- (and (listp result) (-all? #'stringp result)))
- result
- (error "Invalid result from evaluation of %S: %S" form result))))
+ (cond
+ ((and (listp result) (-all? #'stringp result)) result)
+ ((stringp result) (list result))
+ (t (error "Invalid result from evaluation of %S: %S" form result)))))
(_ (error "Unsupported argument %S" arg))))
(defun flycheck-checker-substituted-arguments (checker)
@@ -2108,8 +2111,9 @@ are substituted within the body of cells!"
Substitute each argument of CHECKER using
`flycheck-substitute-argument'. This replaces any special
symbols in the command."
- (-flatten (-keep (lambda (arg) (flycheck-substitute-argument arg checker))
- (flycheck-checker-arguments checker))))
+ (apply #'append
+ (mapcar (lambda (arg) (flycheck-substitute-argument arg checker))
+ (flycheck-checker-arguments checker))))
(defun flycheck-checker-shell-command (checker)
"Get a shell command for CHECKER.
@@ -2121,12 +2125,12 @@ Return the command of CHECKER as single string, suitable for
shell execution."
(combine-and-quote-strings
(cons (flycheck-checker-executable checker)
- (-flatten
- (-keep (lambda (arg)
- (if (memq arg '(source source-inplace source-original))
- (or (buffer-file-name) "")
- (flycheck-substitute-argument arg checker)))
- (flycheck-checker-arguments checker))))))
+ (apply #'append
+ (mapcar (lambda (arg)
+ (if (memq arg '(source source-inplace source-original))
+ (list (or (buffer-file-name) ""))
+ (flycheck-substitute-argument arg checker)))
+ (flycheck-checker-arguments checker))))))
(defun flycheck-check-modes (checker)
"Check the allowed modes of CHECKER.
View
@@ -1577,25 +1577,25 @@ check with. ERRORS is the list of expected errors."
(unwind-protect
(progn
(should (equal (flycheck-substitute-argument 'source-original 'emacs-lisp)
- (buffer-file-name)))
+ (list (buffer-file-name))))
(let ((filename (flycheck-substitute-argument 'source-inplace 'emacs-lisp)))
- (should (equal filename (flycheck-test-resource-filename
- (concat flycheck-temp-prefix
- "_substitute-dummy"))))
- (should (file-exists-p filename)))
+ (should (equal filename (list (flycheck-test-resource-filename
+ (concat flycheck-temp-prefix
+ "_substitute-dummy")))))
+ (should (file-exists-p (car filename))))
(let ((filename (flycheck-substitute-argument 'source 'emacs-lisp)))
- (should (string-prefix-p temporary-file-directory filename))
- (should (file-exists-p filename)))))
+ (should (string-prefix-p temporary-file-directory (car filename)))
+ (should (file-exists-p (car filename))))))
(mapc #'flycheck-safe-delete flycheck-temporaries)))
(ert-deftest flycheck-substitute-argument/temporary-directory ()
:tags '(checker-api)
(flycheck-test-with-temp-buffer
(unwind-protect
- (let ((dirname (flycheck-substitute-argument 'temporary-directory
- 'emacs-lisp)))
+ (let ((dirname (car (flycheck-substitute-argument 'temporary-directory
+ 'emacs-lisp))))
(should (file-directory-p dirname))
(should (string-prefix-p temporary-file-directory dirname)))
(mapc #'flycheck-safe-delete flycheck-temporaries))))
@@ -1604,8 +1604,8 @@ check with. ERRORS is the list of expected errors."
:tags '(checker-api)
(flycheck-test-with-temp-buffer
(unwind-protect
- (let ((filename (flycheck-substitute-argument 'temporary-file-name
- 'emacs-lisp)))
+ (let ((filename (car (flycheck-substitute-argument 'temporary-file-name
+ 'emacs-lisp))))
;; The filename should not exist, but it's parent directory should
(should-not (file-exists-p filename))
(should (file-directory-p (file-name-directory filename)))
@@ -1704,19 +1704,19 @@ check with. ERRORS is the list of expected errors."
(let ((flycheck-test-option-var t))
(should (equal (flycheck-substitute-argument
'(option-flag "--foo" flycheck-test-option-var) 'emacs-lisp)
- "--foo")))
+ (list "--foo"))))
(let ((flycheck-test-option-var (list "bar")))
(should (equal (flycheck-substitute-argument
'(option-flag "--foo" flycheck-test-option-var) 'emacs-lisp)
- "--foo"))))
+ (list "--foo")))))
(ert-deftest flycheck-substitute-argument/eval ()
:tags '(checker-api)
(let ((flycheck-test-option-var '("Hello " "World")))
(should (equal (flycheck-substitute-argument '(eval flycheck-test-option-var) 'emacs-lisp)
'("Hello " "World"))))
(should (equal (flycheck-substitute-argument '(eval (concat "Hello" "World")) 'emacs-lisp)
- "HelloWorld"))
+ '("HelloWorld")))
(should-not (flycheck-substitute-argument
'(eval (when (string= "foo" "bar") "yes")) 'emacs-lisp))
(should-error (flycheck-substitute-argument '(eval 200) 'emacs-lisp))

0 comments on commit 8c44c5f

Please sign in to comment.