Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ale_linters/typescript/tsc.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
call ale#Set('typescript_tsc_config_path', '')
call ale#Set('typescript_tsc_use_global', get(g:, 'ale_use_global_executables', 0))

call ale#linter#Define('typescript', {
\ 'name': 'tsc',
\ 'lsp': 'stdio',
\ 'executable': function('ale#handlers#typescript#GetExecutable'),
\ 'command': '%e --lsp --stdio',
\ 'project_root': function('ale#handlers#typescript#GetProjectRoot'),
\})
44 changes: 44 additions & 0 deletions autoload/ale/handlers/typescript.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
call ale#Set('typescript_tsc_executable', 'tsc')
call ale#Set('typescript_lsp_project_root', '')

function! ale#handlers#typescript#GetExecutable(buffer) abort
return ale#path#FindExecutable(a:buffer, 'typescript_tsc', [
\ 'node_modules/typescript/bin/tsc',
\ '.yarn/sdks/typescript/bin/tsc',
\ 'node_modules/.bin/tsc',
\])
endfunction

function! ale#handlers#typescript#GetProjectRoot(buffer) abort
let l:project_root = ale#Var(a:buffer, 'typescript_lsp_project_root')

if !empty(l:project_root)
return l:project_root
endif

let l:possible_project_roots = [
\ 'tsconfig.json',
\ '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