This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
ale/test/test_eslint_executable_detection.vader
Kevin Locke 106c27644b
eslint: Use cwd from executable location to fix nested projects (#3222)
* Split FindNearestExecutable from FindExecutable

The path searching in ale#node#FindExecutable() will be useful for
eslint.  Refactor it into a separate function so it can be used without
regard for the state of the _use_global and _executable variables.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>

* eslint: Set project root from local executable

Using the nearest directory with node_modules does not work correctly
for nested projects where the eslint dependencies are in the outer
project.  For example:
https://github.com/dense-analysis/ale/issues/3143#issuecomment-652452362

Adopt the behavior of SublimeLinter, which runs from project_root
determined by the presence of the eslint executable in node_modules/.bin
(or eslint in dependencies/devDependencies of package.json, which we can
add later as necessary).  See [NodeLinter#find_local_executable].

[NodeLinter#find_local_executable]: https://github.com/SublimeLinter/SublimeLinter/blob/056e6f6/lint/base_linter/node_linter.py#L109

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
2020-07-08 14:42:01 +01:00

100 lines
3.9 KiB
Text

Before:
let g:ale_javascript_eslint_executable = 'eslint_d'
call ale#test#SetDirectory('/testplugin/test')
runtime ale_linters/javascript/eslint.vim
After:
let g:ale_javascript_eslint_executable = 'eslint'
let g:ale_javascript_eslint_use_global = 0
call ale#test#RestoreDirectory()
call ale#linter#Reset()
Execute(create-react-app directories should be detected correctly):
call ale#test#SetFilename('eslint-test-files/react-app/subdir/testfile.js')
AssertEqual
\ ale#path#Simplify(g:dir . '/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js'),
\ ale#handlers#eslint#GetExecutable(bufnr(''))
Execute(use-global should override create-react-app detection):
let g:ale_javascript_eslint_use_global = 1
call ale#test#SetFilename('eslint-test-files/react-app/subdir/testfile.js')
AssertEqual
\ 'eslint_d',
\ ale#handlers#eslint#GetExecutable(bufnr(''))
Execute(other app directories should be detected correctly):
call ale#test#SetFilename('eslint-test-files/other-app/subdir/testfile.js')
AssertEqual
\ ale#path#Simplify(g:dir . '/eslint-test-files/node_modules/.bin/eslint'),
\ ale#handlers#eslint#GetExecutable(bufnr(''))
Execute(use-global should override other app directories):
let g:ale_javascript_eslint_use_global = 1
call ale#test#SetFilename('eslint-test-files/other-app/subdir/testfile.js')
AssertEqual
\ 'eslint_d',
\ ale#handlers#eslint#GetExecutable(bufnr(''))
Execute(eslint_d should be detected correctly):
call ale#test#SetFilename('eslint-test-files/app-with-eslint-d/testfile.js')
AssertEqual
\ ale#path#Simplify(g:dir . '/eslint-test-files/app-with-eslint-d/node_modules/.bin/eslint_d'),
\ ale#handlers#eslint#GetExecutable(bufnr(''))
Execute(eslint.js executables should be run with node on Windows):
call ale#test#SetFilename('eslint-test-files/react-app/subdir/testfile.js')
" We have to execute the file with node.
if has('win32')
AssertEqual
\ ale#path#CdString(ale#path#Simplify(g:dir . '/eslint-test-files/react-app'))
\ . ale#Escape('node.exe') . ' '
\ . ale#Escape(ale#path#Simplify(g:dir . '/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js'))
\ . ' -f json --stdin --stdin-filename %s',
\ ale#handlers#eslint#GetCommand(bufnr(''))
else
AssertEqual
\ ale#path#CdString(ale#path#Simplify(g:dir . '/eslint-test-files/react-app'))
\ . ale#Escape(ale#path#Simplify(g:dir . '/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js'))
\ . ' -f json --stdin --stdin-filename %s',
\ ale#handlers#eslint#GetCommand(bufnr(''))
endif
Execute(eslint.js should be run from containing project with eslint):
call ale#test#SetFilename('eslint-test-files/react-app/subdir-with-package-json/testfile.js')
" We have to execute the file with node.
if has('win32')
AssertEqual
\ ale#path#CdString(ale#path#Simplify(g:dir . '/eslint-test-files/react-app'))
\ . ale#Escape('node.exe') . ' '
\ . ale#Escape(ale#path#Simplify(g:dir . '/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js'))
\ . ' -f json --stdin --stdin-filename %s',
\ ale#handlers#eslint#GetCommand(bufnr(''))
else
AssertEqual
\ ale#path#CdString(ale#path#Simplify(g:dir . '/eslint-test-files/react-app'))
\ . ale#Escape(ale#path#Simplify(g:dir . '/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js'))
\ . ' -f json --stdin --stdin-filename %s',
\ ale#handlers#eslint#GetCommand(bufnr(''))
endif
Execute(eslint.js executables can be run outside project dir):
" Set filename above eslint-test-files (which contains node_modules)
call ale#test#SetFilename('testfile.js')
" cd "..." not present, since project root not found (from node_modules)
AssertEqual
\ ale#Escape(ale#handlers#eslint#GetExecutable(bufnr('')))
\ . ' -f json --stdin --stdin-filename %s',
\ ale#handlers#eslint#GetCommand(bufnr(''))