Skip to content

Commit

Permalink
fix(biome): find root when using biome.jsonc
Browse files Browse the repository at this point in the history
Since biome supports either `biome.json` or `biome.jsonc` config files,
we need to look for both when searching for the LSP project root. We can
also look for a package.json or .git folder to use. This uses mostly the
same logic as deno.
  • Loading branch information
redbmk committed Jun 21, 2024
1 parent f2aef2f commit 7fa1ab5
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
33 changes: 31 additions & 2 deletions autoload/ale/handlers/biome.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ call ale#Set('biome_executable', 'biome')
call ale#Set('biome_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('biome_options', '')
call ale#Set('biome_fixer_apply_unsafe', 0)
call ale#Set('biome_lsp_project_root', '')

function! ale#handlers#biome#GetExecutable(buffer) abort
return ale#path#FindExecutable(a:buffer, 'biome', [
Expand All @@ -26,7 +27,35 @@ function! ale#handlers#biome#GetCommand(buffer) abort
endfunction

function! ale#handlers#biome#GetProjectRoot(buffer) abort
let l:biome_file = ale#path#FindNearestFile(a:buffer, 'biome.json')
let l:project_root = ale#Var(a:buffer, 'biome_lsp_project_root')

return !empty(l:biome_file) ? fnamemodify(l:biome_file, ':h') : ''
if !empty(l:project_root)
return l:project_root
endif

let l:possible_project_roots = [
\ 'biome.json',
\ 'biome.jsonc',
\ 'package.json',
\ '.git',
\ bufname(a:buffer),
\]

for l:possible_root in l:possible_project_roots
let l:project_root = ale#path#FindNearestFile(a:buffer, l:possible_root)

if empty(l:project_root)
let l:project_root = ale#path#FindNearestDirectory(a:buffer, l:possible_root)
endif

if !empty(l:project_root)
" dir:p expands to /full/path/to/dir/ whereas
" file:p expands to /full/path/to/file (no trailing slash)
" Appending '/' ensures that :h:h removes the path's last segment
" regardless of whether it is a directory or not.
return fnamemodify(l:project_root . '/', ':p:h:h')
endif
endfor

return ''
endfunction
16 changes: 16 additions & 0 deletions doc/ale-typescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ g:ale_biome_fixer_apply_unsafe *g:ale_biome_fixer_apply_unsafe*
If set to `1`, biome will apply unsafe fixes along with safe fixes.


g:ale_biome_lsp_project_root *g:ale_biome_lsp_project_root*
*b:ale_biome_lsp_project_root*
Type: |String|
Default: `''`

If this variable is left unset, ALE will try to find the project root by
executing the following steps in the given order:

1. Find an ancestor directory containing a biome.json.
2. Find an ancestor directory containing a biome.jsonc.
3. Find an ancestor directory containing a package.json.
4. Find an ancestor directory containing a .git folder.
5. Use the directory of the current buffer (if the buffer was opened from
a file).


===============================================================================
cspell *ale-typescript-cspell*

Expand Down
19 changes: 19 additions & 0 deletions test/linter/test_typescript_biome.vader
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Before:
Save g:ale_biome_options
Save g:ale_biome_lsp_project_root

let g:ale_biome_options = ''
let g:ale_biome_lsp_project_root = ''

call ale#assert#SetUpLinterTest('typescript', 'biome')
call ale#test#SetFilename('test.ts')
Expand All @@ -16,3 +18,20 @@ Execute(The biome command should accept options):
let g:ale_biome_options = '--foobar'

AssertLinter 'biome', ale#Escape('biome') . ' lsp-proxy --foobar'

Execute(Should find project root containing biome.json):
call ale#test#SetFilename('../test-files/biome/json/src/test.ts')

AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/biome/json')

Execute(Should find project root containing biome.jsonc):
call ale#test#SetFilename('../test-files/biome/jsonc/src/test.ts')

AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/biome/jsonc')

Execute(Should use user-specified project root):
let g:ale_biome_lsp_project_root = '/'

call ale#test#SetFilename('../test-files/biome/jsonc/src/test.ts')

AssertLSPProject '/'
2 changes: 2 additions & 0 deletions test/linter/test_typescript_deno_lsp.vader
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Before:
Save g:ale_deno_import_map
Save g:ale_deno_importMap
Save g:ale_deno_unstable
Save g:ale_deno_executable
Save g:ale_deno_lsp_project_root

let g:ale_deno_import_map = 'import_map.json'
let g:ale_deno_importMap = ''
let g:ale_deno_unstable = 0
let g:ale_deno_executable = 'deno'
let g:ale_deno_lsp_project_root = ''
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 7fa1ab5

Please sign in to comment.