summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-08-30 22:49:46 +0100
committerw0rp <devw0rp@gmail.com>2017-08-30 22:49:46 +0100
commitf36f38c960eab386ad1a2752ae3d6265875a3cff (patch)
treefcf568ebb9d39cb3c4058fac7fb1fbf84ddd203a
parent301d30229b10bf08094a6bd368c102cf25753dc9 (diff)
downloadale-f36f38c960eab386ad1a2752ae3d6265875a3cff.zip
Cover the prettier-eslint changes with tests, and fix some problems
-rw-r--r--autoload/ale/fixers/prettier_eslint.vim29
-rw-r--r--test/fixers/eslint-test-files/app-with-eslint-d/node_modules/.bin/eslint_d0
-rw-r--r--test/fixers/eslint-test-files/node_modules/.bin/eslint0
-rw-r--r--test/fixers/eslint-test-files/other-app/subdir/testfile.js0
-rw-r--r--test/fixers/eslint-test-files/react-app/.eslintrc.js0
-rw-r--r--test/fixers/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js0
-rw-r--r--test/fixers/eslint-test-files/react-app/node_modules/standard/bin/cmd.js0
-rw-r--r--test/fixers/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js0
-rw-r--r--test/fixers/eslint-test-files/react-app/subdir/testfile.css0
-rw-r--r--test/fixers/eslint-test-files/react-app/subdir/testfile.js0
-rw-r--r--test/fixers/test_prettier_eslint_fixer.callback.vader76
11 files changed, 93 insertions, 12 deletions
diff --git a/autoload/ale/fixers/prettier_eslint.vim b/autoload/ale/fixers/prettier_eslint.vim
index 6fe9f0bc..dbf0424c 100644
--- a/autoload/ale/fixers/prettier_eslint.vim
+++ b/autoload/ale/fixers/prettier_eslint.vim
@@ -2,6 +2,15 @@
" w0rp <devw0rp@gmail.com>, morhetz (Pavel Pertsev) <morhetz@gmail.com>
" Description: Integration between Prettier and ESLint.
+function! ale#fixers#prettier_eslint#SetOptionDefaults() abort
+ call ale#Set('javascript_prettier_eslint_executable', 'prettier-eslint')
+ call ale#Set('javascript_prettier_eslint_use_global', 0)
+ call ale#Set('javascript_prettier_eslint_options', '')
+ call ale#Set('javascript_prettier_eslint_legacy', 0)
+endfunction
+
+call ale#fixers#prettier_eslint#SetOptionDefaults()
+
function! s:FindConfig(buffer) abort
for l:filename in [
\ '.eslintrc.js',
@@ -21,11 +30,6 @@ function! s:FindConfig(buffer) abort
return ''
endfunction
-call ale#Set('javascript_prettier_eslint_executable', 'prettier-eslint')
-call ale#Set('javascript_prettier_eslint_use_global', 0)
-call ale#Set('javascript_prettier_eslint_options', '')
-call ale#Set('javascript_prettier_eslint_legacy', 0)
-
function! ale#fixers#prettier_eslint#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'javascript_prettier_eslint', [
\ 'node_modules/prettier-eslint-cli/dist/index.js',
@@ -33,21 +37,22 @@ function! ale#fixers#prettier_eslint#GetExecutable(buffer) abort
\])
endfunction
-function! ale#fixers#prettier_eslint#Fix(buffer, lines) abort
+function! ale#fixers#prettier_eslint#Fix(buffer) abort
let l:options = ale#Var(a:buffer, 'javascript_prettier_eslint_options')
let l:executable = ale#fixers#prettier_eslint#GetExecutable(a:buffer)
- let l:config = s:FindConfig(a:buffer)
- let l:eslint_config_option = ' --eslint-config-path ' . ale#Escape(l:config)
- if ale#Var(a:buffer, 'javascript_prettier_eslint_legacy')
- let l:eslint_config_option = ''
- endif
+ let l:config = !ale#Var(a:buffer, 'javascript_prettier_eslint_legacy')
+ \ ? s:FindConfig(a:buffer)
+ \ : ''
+ let l:eslint_config_option = !empty(l:config)
+ \ ? ' --eslint-config-path ' . ale#Escape(l:config)
+ \ : ''
return {
\ 'command': ale#Escape(l:executable)
\ . ' %t'
\ . l:eslint_config_option
- \ . ' ' . l:options
+ \ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' --write',
\ 'read_temporary_file': 1,
\}
diff --git a/test/fixers/eslint-test-files/app-with-eslint-d/node_modules/.bin/eslint_d b/test/fixers/eslint-test-files/app-with-eslint-d/node_modules/.bin/eslint_d
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/app-with-eslint-d/node_modules/.bin/eslint_d
diff --git a/test/fixers/eslint-test-files/node_modules/.bin/eslint b/test/fixers/eslint-test-files/node_modules/.bin/eslint
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/node_modules/.bin/eslint
diff --git a/test/fixers/eslint-test-files/other-app/subdir/testfile.js b/test/fixers/eslint-test-files/other-app/subdir/testfile.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/other-app/subdir/testfile.js
diff --git a/test/fixers/eslint-test-files/react-app/.eslintrc.js b/test/fixers/eslint-test-files/react-app/.eslintrc.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/.eslintrc.js
diff --git a/test/fixers/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js b/test/fixers/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/node_modules/eslint/bin/eslint.js
diff --git a/test/fixers/eslint-test-files/react-app/node_modules/standard/bin/cmd.js b/test/fixers/eslint-test-files/react-app/node_modules/standard/bin/cmd.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/node_modules/standard/bin/cmd.js
diff --git a/test/fixers/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js b/test/fixers/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js
diff --git a/test/fixers/eslint-test-files/react-app/subdir/testfile.css b/test/fixers/eslint-test-files/react-app/subdir/testfile.css
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/subdir/testfile.css
diff --git a/test/fixers/eslint-test-files/react-app/subdir/testfile.js b/test/fixers/eslint-test-files/react-app/subdir/testfile.js
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/fixers/eslint-test-files/react-app/subdir/testfile.js
diff --git a/test/fixers/test_prettier_eslint_fixer.callback.vader b/test/fixers/test_prettier_eslint_fixer.callback.vader
new file mode 100644
index 00000000..56daf93f
--- /dev/null
+++ b/test/fixers/test_prettier_eslint_fixer.callback.vader
@@ -0,0 +1,76 @@
+Before:
+ call ale#test#SetDirectory('/testplugin/test/fixers')
+
+ Save g:ale_javascript_prettier_eslint_executable
+ Save g:ale_javascript_prettier_eslint_use_global
+ Save g:ale_javascript_prettier_eslint_options
+ Save g:ale_javascript_prettier_eslint_legacy
+
+ unlet! g:ale_javascript_prettier_eslint_executable
+ unlet! g:ale_javascript_prettier_eslint_use_global
+ unlet! g:ale_javascript_prettier_eslint_options
+ unlet! g:ale_javascript_prettier_eslint_legacy
+
+ call ale#fixers#prettier_eslint#SetOptionDefaults()
+
+After:
+ Restore
+
+ unlet! b:ale_javascript_prettier_eslint_executable
+ unlet! b:ale_javascript_prettier_eslint_use_global
+ unlet! b:ale_javascript_prettier_eslint_options
+ unlet! b:ale_javascript_prettier_eslint_legacy
+
+ call ale#test#RestoreDirectory()
+
+Execute(The default command should be correct):
+ AssertEqual
+ \ {
+ \ 'read_temporary_file': 1,
+ \ 'command':
+ \ ale#Escape('prettier-eslint')
+ \ . ' %t'
+ \ . ' --write'
+ \ },
+ \ ale#fixers#prettier_eslint#Fix(bufnr(''))
+
+Execute(Additional options should be used when set):
+ let b:ale_javascript_prettier_eslint_options = '--foobar'
+
+ AssertEqual
+ \ {
+ \ 'read_temporary_file': 1,
+ \ 'command':
+ \ ale#Escape('prettier-eslint')
+ \ . ' %t'
+ \ . ' --foobar --write'
+ \ },
+ \ ale#fixers#prettier_eslint#Fix(bufnr(''))
+
+Execute(Configuration files should be detected):
+ call ale#test#SetFilename('eslint-test-files/react-app/foo/bar.js')
+
+ AssertEqual
+ \ {
+ \ 'read_temporary_file': 1,
+ \ 'command':
+ \ ale#Escape('prettier-eslint')
+ \ . ' %t'
+ \ . ' --eslint-config-path ' . ale#Escape(g:dir . '/eslint-test-files/react-app/.eslintrc.js')
+ \ . ' --write'
+ \ },
+ \ ale#fixers#prettier_eslint#Fix(bufnr(''))
+
+Execute(Configuration files should be disabled if the legacy option is on):
+ call ale#test#SetFilename('eslint-test-files/react-app/foo/bar.js')
+ let b:ale_javascript_prettier_eslint_legacy = 1
+
+ AssertEqual
+ \ {
+ \ 'read_temporary_file': 1,
+ \ 'command':
+ \ ale#Escape('prettier-eslint')
+ \ . ' %t'
+ \ . ' --write'
+ \ },
+ \ ale#fixers#prettier_eslint#Fix(bufnr(''))