Skip to content

Commit 993e3fe

Browse files
committed
Vim: fix linting of new buffer
In Vim, if the user creates a new buffer with no name, quick-lint-js searches for a configuration file starting in the parent of the current working directory. This behavior is unintentional. Change the Vim plugin to only pass the --path-for-config-search option for buffers which represent files. THis means that new buffers will always use the default configuration.
1 parent df467d6 commit 993e3fe

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

plugin/vim/quick-lint-js.vim/autoload/quick_lint_js_ale.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
" https://github.com/dense-analysis/ale
66

77
function! quick_lint_js_ale#get_command(buffer_number) abort
8-
return '%e --output-format=vim-qflist-json --vim-file-bufnr '.string(a:buffer_number).' --path-for-config-search=%s %t'
8+
let l:extra_options = ''
9+
if quick_lint_js_ale#is_buffer_associated_with_file(a:buffer_number)
10+
let l:extra_options .= ' --path-for-config-search=%s'
11+
endif
12+
return '%e --output-format=vim-qflist-json --vim-file-bufnr '.string(a:buffer_number).l:extra_options.' %t'
913
endfunction
1014

1115
function! quick_lint_js_ale#get_executable(buffer_number) abort
@@ -36,6 +40,11 @@ function! quick_lint_js_ale#get_lsp_project_root(_buffer_number) abort
3640
return '/'
3741
endfunction
3842

43+
function! quick_lint_js_ale#is_buffer_associated_with_file(buffer_number) abort
44+
return bufname(a:buffer_number) !=# ''
45+
\ && getbufvar(a:buffer_number, '&buftype') ==# ''
46+
endfunction
47+
3948
" quick-lint-js finds bugs in JavaScript programs.
4049
" Copyright (C) 2020 Matthew "strager" Glazar
4150
"

plugin/vim/test.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function! s:main() abort
1515
endfunction
1616

1717
function! s:test_all() abort
18+
call s:test_parse_command_output()
19+
call s:test_buffer_is_associated_with_file()
20+
endfunction
21+
22+
function! s:test_parse_command_output() abort
1823
let l:qflist = s:parse([''])
1924
call assert_equal([], l:qflist)
2025

@@ -72,6 +77,33 @@ function! s:parse(lines) abort
7277
return quick_lint_js_ale#parse_command_output(0, a:lines)
7378
endfunction
7479

80+
function! s:test_buffer_is_associated_with_file() abort
81+
%bwipeout!
82+
call assert_false(quick_lint_js_ale#is_buffer_associated_with_file(bufnr('%')))
83+
84+
%bwipeout!
85+
silent edit file-name.txt
86+
call assert_true(quick_lint_js_ale#is_buffer_associated_with_file(bufnr('%')))
87+
88+
%bwipeout!
89+
silent edit file-name.txt
90+
set buftype=nofile
91+
call assert_false(quick_lint_js_ale#is_buffer_associated_with_file(bufnr('%')))
92+
93+
%bwipeout!
94+
help
95+
set filetype=javascript
96+
call assert_false(quick_lint_js_ale#is_buffer_associated_with_file(bufnr('%')))
97+
98+
" Check a buffer different from the current buffer:
99+
%bwipeout!
100+
silent edit file.txt
101+
let l:file_buffer_number = bufnr('%')
102+
new
103+
call assert_false(quick_lint_js_ale#is_buffer_associated_with_file(bufnr('%')))
104+
call assert_true(quick_lint_js_ale#is_buffer_associated_with_file(l:file_buffer_number))
105+
endfunction
106+
75107
function! s:check_for_errors() abort
76108
if len(v:errors) > 0
77109
for l:error in v:errors

0 commit comments

Comments
 (0)