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 May 16, 2024
1 parent c88bddf commit 4da71a8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
33 changes: 31 additions & 2 deletions autoload/ale/handlers/biome.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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_lsp_project_root', '')

function! ale#handlers#biome#GetExecutable(buffer) abort
return ale#path#FindExecutable(a:buffer, 'biome', [
Expand All @@ -25,7 +26,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
24 changes: 24 additions & 0 deletions doc/ale-typescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ g:ale_biome_use_global *g:ale_biome_use_global*
See |ale-integrations-local-executables|


g:ale_biome_fixer_apply_unsafe *g:ale_biome_fixer_apply_unsafe*
*b:ale_biome_fixer_apply_unsafe*
Type: |Number|
Default: `0`

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
4 changes: 4 additions & 0 deletions test/fixers/test_biome_fixer_callback.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Before:
Save g:ale_biome_options

let g:ale_biome_options = ''

call ale#assert#SetUpFixerTest('typescript', 'biome')

After:
Expand Down
25 changes: 24 additions & 1 deletion test/linter/test_typescript_biome.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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 @@ -9,6 +15,23 @@ Execute(The default biome command should be correct):
AssertLinter 'biome', ale#Escape('biome') . ' lsp-proxy'

Execute(The biome command should accept options):
let b:ale_biome_options = '--foobar'
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/javascript/test.js')

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

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

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

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

call ale#test#SetFilename('../test-files/typescript/test.js')

AssertLSPProject '/'
Empty file.
Empty file.

0 comments on commit 4da71a8

Please sign in to comment.