LoginSignup
i_rei
@i_rei

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ESLintがVimで動かない

解決したいこと

ESLintでコードのチェックをしたいです。
Vimで使用できるように設定したのですが、なぜか使えません。
解決方法を教えてください。

状況/自分で試したこと

環境

MacBook Air (Apple Silicon)

ESLint

ESLintをインストールして設定し、Vimの設定もしたのですが、できません。
mkdir myappをしたあと、
npx create-react-app my app
を行いました。
その後、myappディレクトリで、
npm install eslintしました。

その後eslint --initできず、

にを参考にnpx eslint --initにより初期設定を行いました。
その後作った.eslintrc.jsonをいじり、次のようになりました。

.eslintrc.json
{
  "env": {
    "browser": true,
    "es2021": true,
    "es6": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2019,
    "sourceType": "module"
  },
  "plugins": [
    "react"
  ],
  "settings": {
    "react"{
      "version":"detect"
    }
  },
  "rules": {
    "no-undef":"error",
    "semi":"warn",
    "no-extra-semi":"warn",
    "arrow-body-style": "error",
    "arrow-parens": "error",
    "arrow-spacing": "error",
    "generator-star-spacing": "error",
    "no-duplicate-imports": "error",
    "no-useless-computed-key": "error",
    "no-useless-constructor": "error",
    "no-useless-rename": "error",
    "no-var": "error",
    "object-shorthand": "error",
    "prefer-arrow-callback": "error",
    "prefer-const": "error",
    "prefer-rest-params": "error",
    "prefer-spread": "error",
    "prefer-template": "error",
    "rest-spread-spacing": "error",
    "template-curly-spacing": "error",
    "yield-star-spacing": "error"
  }
}

ESLintのバージョンはv7.22.0です。

Vim

コードをチェックするために、下記のプラグインを導入しました。

これに関する~/.vimrcはこのようになっています。

~/.vimrc
call dein#add('vim-syntastic/syntastic')

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_enable_signs = 1
let g:syntastic_always_populate_loc_list = 0
let g:syntastic_auto_loc_list = 0
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
let g:syntastic_mode_map = {'mode':'active','active_filetypes':[
  \ 'javascript','html','css','ejavascript','python','vim','zsh']}

let g:syntastic_javascript_checkers = ['eslint']
let g:syntastic_python_checkers= ['flake8']

noremap <C-c> :w<CR>:SyntasticCheck<CR>

上記のように設定しました。

エラーメッセージなど

その後一度ターミナルを再起動し、myappディレクトリに次のようなサンプルファイルを作りました。

a.js
console.log(hello)

その後、npx eslint a.jsを行うと、次のように実行できました。

/Users/ユーザー名/myapp/a.js
  1:13  error    'hello' is not defined  no-undef
  1:19  warning  Missing semicolon       semi

✖ 2 problems (1 error, 1 warning)
  0 errors and 1 warning potentially fixable with the `--fix` option.

しかし、~/.vimrcの設定では、ファイルを開いた時にエラーがあれば教えてくると思うのですが、何もありません。:SyntasticInfoを行うと、次のようになりました。

Syntastic version: 3.10.0-22(Vim 802, Darwin)
Info for filetype:javascript
Global mode:active
Filetype javascript is active
The current file will be checked automatically
Available checkers: -
Currently enabled checkers: - 
Press ENTER or type command to continue

おそらくAvailableとCureentlyの部分が-になっていることが原因だと思うのですが、なぜそうなっているのかがわかりません。

教えていただけると嬉しいです。

ちなみにですが、Pythonでは設定しているflake8はVimで正しく作動しました。

0

1Answer

恐らく syntastic は、eslint コマンドが $PATH 上に存在しないと動かないのだと思います。

調べてみたところ、推測が正しければ以下のような設定で動くかもしれません。

let g:syntastic_javascript_eslint_exe = 'npx eslint'

ちなみに、syntastic はかなり古い方のプラグインで、現在だと ALEvim-lsp + efm-langserver (こちらは少々大掛かりで設定も大変です) などの組み合わせを利用する方法もあります。

0

Comments

  1. @i_rei

    Questioner
    試してみましたが、できませんでした...
    自分でも他の方法を調べてみましたが、同じく無理でした。
    ALEを使った方が無難でしょうか?
  2. `npx` はご存知の通りカレントディレクトリによって動作が変わるので、Vim 内のカレントディレクトリがプロジェクト内になっている必要があります。プロジェクトルートで Vim を起動していれば(他に変な設定さえなければ)この点は大丈夫かと思いますが、心当たりがあれば確認してみてください。


    余裕があるのであれば ALE を試してみるのも一つの手かと思います。軽く調べてみたところ、以下のページに設定方法の説明が見つかりました。(私自身は ALE は使っていないため、実際に確認はしていません)

    https://www.chunkhang.com/blog/using-eslint-in-vim-for-react
  3. @i_rei

    Questioner
    ALEを使うとあっさりできました。
    syntasticについての問題は解決できていませんが、当初の目的はESLintをVimで使うことだったので、この質問はクローズしたいと思います。
    色々と教えていただきありがとうございます。

Your answer might help someone💌