diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 6568a47..ce30c36 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -67,6 +67,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['javascript'], \ 'description': 'Fix JavaScript files using standard --fix', \ }, +\ 'stylelint': { +\ 'function': 'ale#fixers#stylelint#Fix', +\ 'suggested_filetypes': ['css', 'sass', 'scss', 'stylus'], +\ 'description': 'Fix stylesheet files using stylelint --fix.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/stylelint.vim b/autoload/ale/fixers/stylelint.vim new file mode 100644 index 0000000..7d5abb7 --- /dev/null +++ b/autoload/ale/fixers/stylelint.vim @@ -0,0 +1,31 @@ +" Author: Mahmoud Mostafa +" Description: Fixing files with stylelint. + +call ale#Set('stylelint_executable', 'stylelint') +call ale#Set('stylelint_use_global', 0) + +function! ale#fixers#stylelint#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'stylelint', [ + \ 'node_modules/stylelint/bin/stylelint.js', + \ 'node_modules/.bin/stylelint', + \]) +endfunction + + +function! ale#fixers#stylelint#Fix(buffer) abort + let l:executable = ale#fixers#stylelint#GetExecutable(a:buffer) + + if ale#Has('win32') && l:executable =~? 'stylelint\.js$' + " For Windows, if we detect an stylelint.js script, we need to execute + " it with node, or the file can be opened with a text editor. + let l:head = 'node ' . ale#Escape(l:executable) + else + let l:head = ale#Escape(l:executable) + endif + + return { + \ 'command': l:head + \ . ' --fix %t', + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/test/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js b/test/eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js new file mode 100644 index 0000000..e69de29 diff --git a/test/eslint-test-files/react-app/subdir/testfile.css b/test/eslint-test-files/react-app/subdir/testfile.css new file mode 100644 index 0000000..e69de29 diff --git a/test/fixers/test_stylelint_fixer_callback.vader b/test/fixers/test_stylelint_fixer_callback.vader new file mode 100644 index 0000000..6c99196 --- /dev/null +++ b/test/fixers/test_stylelint_fixer_callback.vader @@ -0,0 +1,32 @@ +Before: + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + let g:ale_has_override = {} + call ale#test#RestoreDirectory() + +Execute(The path to stylelint.js should be run on Unix): + call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.css') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': + \ ale#Escape(simplify(g:dir . '/../eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js')) + \ . ' --fix %t', + \ }, + \ ale#fixers#stylelint#Fix(bufnr('')) + +Execute(The stylelint fixer with stylelint.js should be run with node on Windows): + call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.css') + let g:ale_has_override['win32'] = 1 + + " We have to execute the file with node. + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': 'node ' + \ . ale#Escape(simplify(g:dir . '/../eslint-test-files/react-app/node_modules/stylelint/bin/stylelint.js')) + \ . ' --fix %t', + \ }, + \ ale#fixers#stylelint#Fix(bufnr(''))