when I edit javascript, I'm usually actually editing someone else's code. Is there a way to dynamically set the value of js-indent-level from a .eslintrc file?

  • Considering that .eslintrc doesn't have to be a JSON, getting that information out will be hacky. An easy solution could be to bind running --fix to a key, or you could look into something like EditorConfig.
    – user12563
    Nov 13 '18 at 20:50

I know nothing about eslint or it's rc file, but TMTOWTDI as the Perl folks are fond of saying. Here's is how I solved a similar problem for myself using shell-command-to-string. I needed to get the version of CMake that was installed on whatever system I happened to be on.

(defvar cmake-mode-cmake-version
  (let (l)
    (setq l (shell-command-to-string
             (concat cmake-mode-cmake-executable " --version")))
    (string-match  (rx bow
                       (one-or-more digit)
                       (one-or-more digit)
                       (zero-or-more "." (one-or-more digit))
    (match-string 0 l))
 "The version of CMake from `cmake-mode-cmake-executable'")

First, we define a new variable cmake-mode-cmake-version. Then we run shell-command-to-string with a string that I concat together. At this point you're going to want to use something like "grep indent-level .eslint". (Again, don't know anything about eslint. Just working off the cuff here). Next, in string-match we look for a version string using a regex constructed with the rx package. With match-string we return the result of the last match (the one we just did with string-match), and cmake-mode-cmake-version now contains something like '3.11.1'. It's not the most lispy code, but hopefully it's clear.

Now, you could of course forego the concats, and the string-matchs, etc. Assuming your rc file is list of "things = values" then we could imagine something like:

(eval-after-load "js-mode"
  (defvar my/eslint-indent
    '(let (l)
      (setq l (shell-command-to-string "grep indent-level .eslintrc | egrep -o '\\d+$' | tr -d '\\n'"))
   "The indent level from .eslintrc"))
  (setq js-indent-level my/eslint-indent))

where we set js-indent-level after loading our chosen JavaScript mode. Of course this is just an example, and you'll have to tweak it some for your own needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.