Search code examples
vimeslintvim-ale

vim ale shortcut to add eslint-ignore hint


I've been using vim for years, but I've just started integrating eslint (through ALE). I've found that sometimes I want to be able to quickly add an /* eslint-ignore-next-line */. For example:

 ...
❌    if (m = /^-l(\d+)$/.exec(args[i])) t.len = m[1];
 ...
~/some/dir/file.js [+]          
cond-assign: Expected a conditional expression and instead saw an assignment.

It's really handy that ALE gives you the code at the bottom of the window, but being lazy I want to automate adding the comment/hint:

/* eslint-ignore-next-line cond-assign */

Is there a way to access that information at the bottom of the screen in a vim script/function?


Solution

  • Though not documented, ALE stores lint information in the b:ale_highlight_items variable.

    David784's solution didn't work for me because I had customized the location-list text using ALE's g:ale_echo_msg_format configuration option. So I modified it to obtain the information directly from b:ale_highlight_items instead of parsing it out of the location-list.

    Here it is:

    command! ALEIgnoreEslint call AleIgnoreEslint()
    function! AleIgnoreEslint()
      " https://stackoverflow.com/questions/54961318/vim-ale-shortcut-to-add-eslint-ignore-hint
      let l:codes = []
      if (!exists('b:ale_highlight_items'))
        echo 'cannot ignore eslint rule without b:ale_highlight_items'
        return
      endif
      for l:item in b:ale_highlight_items
        if (l:item['lnum']==line('.') && l:item['linter_name']=='eslint')
          let l:code = l:item['code']
          call add(l:codes, l:code)
        endif
      endfor
      if len(l:codes)
        exec 'normal O/* eslint-disable-next-line ' . join(l:codes, ', ') . ' */'
      endif
    endfunction