From 2760cf7018893b42c4ae5c750a7f1d38669aa821 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 6 Dec 2018 19:23:31 +0100 Subject: [PATCH] refactor sasslint linter (#2077) Previous implementation required one to have sass-lint globally. This allows you to have it locally, override the executable and add options. --- ale_linters/sass/sasslint.vim | 27 ++++++++++-- ale_linters/scss/sasslint.vim | 28 ++++++++---- autoload/ale/handlers/sasslint.vim | 8 ---- doc/ale-sass.txt | 6 +++ doc/ale-scss.txt | 27 ++++++++++++ doc/ale.txt | 2 + .../with-bin/node_modules/.bin/sass-lint | 0 .../node_modules/sass-lint/bin/sass-lint.js | 0 .../test_sass_sasslint_command_callback.vader | 43 +++++++++++++++++++ .../test_sasslint_command_callback.vader | 11 ----- .../test_scss_sasslint_command_callback.vader | 43 +++++++++++++++++++ 11 files changed, 163 insertions(+), 32 deletions(-) delete mode 100644 autoload/ale/handlers/sasslint.vim create mode 100755 test/command_callback/sasslint-test-files/with-bin/node_modules/.bin/sass-lint create mode 100755 test/command_callback/sasslint-test-files/with-source/node_modules/sass-lint/bin/sass-lint.js create mode 100644 test/command_callback/test_sass_sasslint_command_callback.vader delete mode 100644 test/command_callback/test_sasslint_command_callback.vader create mode 100644 test/command_callback/test_scss_sasslint_command_callback.vader diff --git a/ale_linters/sass/sasslint.vim b/ale_linters/sass/sasslint.vim index 4df56dfd..8d24185d 100644 --- a/ale_linters/sass/sasslint.vim +++ b/ale_linters/sass/sasslint.vim @@ -1,9 +1,28 @@ -" Author: KabbAmine - https://github.com/KabbAmine, -" Ben Falconer +" Author: sQVe - https://github.com/sQVe + +call ale#Set('sass_sasslint_executable', 'sass-lint') +call ale#Set('sass_sasslint_options', '') +call ale#Set('sass_sasslint_use_global', get(g:, 'ale_use_global_executables', 0)) + +function! ale_linters#sass#sasslint#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'sass_sasslint', [ + \ 'node_modules/sass-lint/bin/sass-lint.js', + \ 'node_modules/.bin/sass-lint', + \]) +endfunction + +function! ale_linters#sass#sasslint#GetCommand(buffer) abort + let l:executable = ale_linters#sass#sasslint#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'sass_sasslint_options') + + return ale#node#Executable(a:buffer, l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' -v -q -f compact %t' +endfunction call ale#linter#Define('sass', { \ 'name': 'sasslint', -\ 'executable': 'sass-lint', -\ 'command_callback': 'ale#handlers#sasslint#GetCommand', +\ 'executable_callback': 'ale_linters#sass#sasslint#GetExecutable', +\ 'command_callback': 'ale_linters#sass#sasslint#GetCommand', \ 'callback': 'ale#handlers#css#HandleCSSLintFormat', \}) diff --git a/ale_linters/scss/sasslint.vim b/ale_linters/scss/sasslint.vim index f6075001..8b725ba6 100644 --- a/ale_linters/scss/sasslint.vim +++ b/ale_linters/scss/sasslint.vim @@ -1,18 +1,28 @@ -" Author: KabbAmine - https://github.com/KabbAmine, Ben Falconer -" +" Author: sQVe - https://github.com/sQVe + +call ale#Set('scss_sasslint_executable', 'sass-lint') +call ale#Set('scss_sasslint_options', '') +call ale#Set('scss_sasslint_use_global', get(g:, 'ale_use_global_executables', 0)) + +function! ale_linters#scss#sasslint#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'scss_sasslint', [ + \ 'node_modules/sass-lint/bin/sass-lint.js', + \ 'node_modules/.bin/sass-lint', + \]) +endfunction function! ale_linters#scss#sasslint#GetCommand(buffer) abort - return ale#path#BufferCdString(a:buffer) - \ . ale#Escape('sass-lint') - \ . ' -v' - \ . ' -q' - \ . ' -f compact' - \ . ' %t' + let l:executable = ale_linters#scss#sasslint#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'scss_sasslint_options') + + return ale#node#Executable(a:buffer, l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' -v -q -f compact %t' endfunction call ale#linter#Define('scss', { \ 'name': 'sasslint', -\ 'executable': 'sass-lint', +\ 'executable_callback': 'ale_linters#scss#sasslint#GetExecutable', \ 'command_callback': 'ale_linters#scss#sasslint#GetCommand', \ 'callback': 'ale#handlers#css#HandleCSSLintFormat', \}) diff --git a/autoload/ale/handlers/sasslint.vim b/autoload/ale/handlers/sasslint.vim deleted file mode 100644 index 399bf47c..00000000 --- a/autoload/ale/handlers/sasslint.vim +++ /dev/null @@ -1,8 +0,0 @@ -" Author: KabbAmine - https://github.com/KabbAmine, -" Ben Falconer - -function! ale#handlers#sasslint#GetCommand(buffer) abort - return ale#path#BufferCdString(a:buffer) - \ . ale#Escape('sass-lint') - \ . ' -v -q -f compact %t' -endfunction diff --git a/doc/ale-sass.txt b/doc/ale-sass.txt index 08e9a381..735f44b2 100644 --- a/doc/ale-sass.txt +++ b/doc/ale-sass.txt @@ -2,6 +2,12 @@ ALE SASS Integration *ale-sass-options* +=============================================================================== +sasslint *ale-sass-sasslint* + +See |ale-scss-sasslint| for information about the available options. + + =============================================================================== stylelint *ale-sass-stylelint* diff --git a/doc/ale-scss.txt b/doc/ale-scss.txt index 3ad84fc1..07a94fe1 100644 --- a/doc/ale-scss.txt +++ b/doc/ale-scss.txt @@ -8,6 +8,33 @@ prettier *ale-scss-prettier* See |ale-javascript-prettier| for information about the available options. +=============================================================================== +sasslint *ale-scss-sasslint* + +g:ale_scss_sasslint_executable *g:ale_scss_sasslint_executable* + *b:ale_scss_sasslint_executable* + Type: |String| + Default: `'sass-lint'` + + See |ale-integrations-local-executables| + + +g:ale_scss_sasslint_options *g:ale_scss_sasslint_options* + *b:ale_scss_sasslint_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to sass-lint. + + +g:ale_scss_sasslint_use_global *g:ale_scss_sasslint_use_global* + *b:ale_scss_sasslint_use_global* + Type: |Number| + Default: `get(g:, 'ale_use_global_executables', 0)` + + See |ale-integrations-local-executables| + + =============================================================================== stylelint *ale-scss-stylelint* diff --git a/doc/ale.txt b/doc/ale.txt index 1a37f73f..92a100f9 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -288,6 +288,7 @@ CONTENTS *ale-contents* rustc...............................|ale-rust-rustc| rustfmt.............................|ale-rust-rustfmt| sass..................................|ale-sass-options| + sasslint............................|ale-sass-sasslint| stylelint...........................|ale-sass-stylelint| scala.................................|ale-scala-options| sbtserver...........................|ale-scala-sbtserver| @@ -295,6 +296,7 @@ CONTENTS *ale-contents* scalastyle..........................|ale-scala-scalastyle| scss..................................|ale-scss-options| prettier............................|ale-scss-prettier| + sasslint............................|ale-scss-sasslint| stylelint...........................|ale-scss-stylelint| sh....................................|ale-sh-options| sh-language-server..................|ale-sh-language-server| diff --git a/test/command_callback/sasslint-test-files/with-bin/node_modules/.bin/sass-lint b/test/command_callback/sasslint-test-files/with-bin/node_modules/.bin/sass-lint new file mode 100755 index 00000000..e69de29b diff --git a/test/command_callback/sasslint-test-files/with-source/node_modules/sass-lint/bin/sass-lint.js b/test/command_callback/sasslint-test-files/with-source/node_modules/sass-lint/bin/sass-lint.js new file mode 100755 index 00000000..e69de29b diff --git a/test/command_callback/test_sass_sasslint_command_callback.vader b/test/command_callback/test_sass_sasslint_command_callback.vader new file mode 100644 index 00000000..9b9bf906 --- /dev/null +++ b/test/command_callback/test_sass_sasslint_command_callback.vader @@ -0,0 +1,43 @@ +Before: + call ale#assert#SetUpLinterTest('sass', 'sasslint') + call ale#test#SetFilename('test.sass') + unlet! b:executable + +After: + call ale#assert#TearDownLinterTest() + +Execute(should default to source, bin/sass-lint.js): + call ale#test#SetFilename('sasslint-test-files/with-source/test.sass') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/sasslint-test-files/with-source/node_modules/sass-lint/bin/sass-lint.js' + \) + + AssertLinter b:executable, + \ (has('win32') ? 'node.exe ' : '') + \ . ale#Escape(b:executable) + \ . ' -v -q -f compact %t' + +Execute(should fallback to bin, .bin/sass-lint): + call ale#test#SetFilename('sasslint-test-files/with-bin/test.sass') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/sasslint-test-files/with-bin/node_modules/.bin/sass-lint' + \) + + AssertLinter b:executable, ale#Escape(b:executable) . ' -v -q -f compact %t' + +Execute(should fallback to global bin): + AssertLinter 'sass-lint', ale#Escape('sass-lint') . ' -v -q -f compact %t' + +Execute(The global executable should be configurable): + let b:ale_sass_sasslint_executable = 'foo' + + AssertLinter 'foo', ale#Escape('foo') . ' -v -q -f compact %t' + +Execute(The options should be configurable): + let b:ale_sass_sasslint_options = '--bar' + + AssertLinter 'sass-lint', ale#Escape('sass-lint') . ' --bar -v -q -f compact %t' diff --git a/test/command_callback/test_sasslint_command_callback.vader b/test/command_callback/test_sasslint_command_callback.vader deleted file mode 100644 index 9142c441..00000000 --- a/test/command_callback/test_sasslint_command_callback.vader +++ /dev/null @@ -1,11 +0,0 @@ -Before: - call ale#assert#SetUpLinterTest('sass', 'sasslint') - call ale#test#SetFilename('test.sass') - -After: - call ale#assert#TearDownLinterTest() - -Execute(The default sasslint command should be correct): - AssertLinter 'sass-lint', - \ ale#path#CdString(expand('%:p:h')) - \ . ale#Escape('sass-lint') . ' -v -q -f compact %t' diff --git a/test/command_callback/test_scss_sasslint_command_callback.vader b/test/command_callback/test_scss_sasslint_command_callback.vader new file mode 100644 index 00000000..1695190a --- /dev/null +++ b/test/command_callback/test_scss_sasslint_command_callback.vader @@ -0,0 +1,43 @@ +Before: + call ale#assert#SetUpLinterTest('scss', 'sasslint') + call ale#test#SetFilename('test.scss') + unlet! b:executable + +After: + call ale#assert#TearDownLinterTest() + +Execute(should default to source, bin/sass-lint.js): + call ale#test#SetFilename('sasslint-test-files/with-source/test.scss') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/sasslint-test-files/with-source/node_modules/sass-lint/bin/sass-lint.js' + \) + + AssertLinter b:executable, + \ (has('win32') ? 'node.exe ' : '') + \ . ale#Escape(b:executable) + \ . ' -v -q -f compact %t' + +Execute(should fallback to bin, .bin/sass-lint): + call ale#test#SetFilename('sasslint-test-files/with-bin/test.scss') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/sasslint-test-files/with-bin/node_modules/.bin/sass-lint' + \) + + AssertLinter b:executable, ale#Escape(b:executable) . ' -v -q -f compact %t' + +Execute(should fallback to global bin): + AssertLinter 'sass-lint', ale#Escape('sass-lint') . ' -v -q -f compact %t' + +Execute(The global executable should be configurable): + let b:ale_scss_sasslint_executable = 'foo' + + AssertLinter 'foo', ale#Escape('foo') . ' -v -q -f compact %t' + +Execute(The options should be configurable): + let b:ale_scss_sasslint_options = '--bar' + + AssertLinter 'sass-lint', ale#Escape('sass-lint') . ' --bar -v -q -f compact %t'