summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlvin Chan <chaucerbao@users.noreply.github.com>2018-06-18 14:40:57 -0700
committerw0rp <w0rp@users.noreply.github.com>2018-06-18 22:40:57 +0100
commitb8a1038a411e266c07a7e91c6561126680854bef (patch)
treee3ac53c453af413a7e40a4b9f1d8062fdf8cd0f0
parent43ce8d761035e221b735b73597b482b9f2764b42 (diff)
downloadale-b8a1038a411e266c07a7e91c6561126680854bef.zip
Set `--parser` option in Prettier's fixer (#1620)
* Set `--parser` option in Prettier's fixer * Add expected `--parser` option to tests * Disable Prettier `--parser` detection if file extension exists * Manually default Prettier `--parser` to "babylon" * Add `--parser` test for TypeScript * Add tests for Prettier `--parser` * Add JSON5 to the suggested fixer for Prettier
-rw-r--r--autoload/ale/fix/registry.vim2
-rw-r--r--autoload/ale/fixers/prettier.vim20
-rw-r--r--test/fixers/test_prettier_fixer_callback.vader140
-rw-r--r--test/prettier-test-files/testfile0
4 files changed, 160 insertions, 2 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
index 7b55acfc..e99b8d2e 100644
--- a/autoload/ale/fix/registry.vim
+++ b/autoload/ale/fix/registry.vim
@@ -51,7 +51,7 @@ let s:default_registry = {
\ },
\ 'prettier': {
\ 'function': 'ale#fixers#prettier#Fix',
-\ 'suggested_filetypes': ['javascript', 'typescript', 'json', 'css', 'scss', 'less', 'markdown', 'graphql', 'vue'],
+\ 'suggested_filetypes': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue'],
\ 'description': 'Apply prettier to a file.',
\ },
\ 'prettier_eslint': {
diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim
index dcc35813..e8f4e92e 100644
--- a/autoload/ale/fixers/prettier.vim
+++ b/autoload/ale/fixers/prettier.vim
@@ -30,8 +30,26 @@ endfunction
function! ale#fixers#prettier#ApplyFixForVersion(buffer, version_output) abort
let l:executable = ale#fixers#prettier#GetExecutable(a:buffer)
let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
-
let l:version = ale#semver#GetVersion(l:executable, a:version_output)
+ let l:parser = ''
+
+ " Append the --parser flag depending on the current filetype (unless it's
+ " already set in g:javascript_prettier_options).
+ if empty(expand('#' . a:buffer . ':e')) && match(l:options, '--parser') == -1
+ let l:prettier_parsers = ['typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue']
+ let l:parser = 'babylon'
+
+ for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.')
+ if index(l:prettier_parsers, l:filetype) > -1
+ let l:parser = l:filetype
+ break
+ endif
+ endfor
+ endif
+
+ if !empty(l:parser)
+ let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser
+ endif
" 1.4.0 is the first version with --stdin-filepath
if ale#semver#GTE(l:version, [1, 4, 0])
diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader
index c4f36f52..2018c3a6 100644
--- a/test/fixers/test_prettier_fixer_callback.vader
+++ b/test/fixers/test_prettier_fixer_callback.vader
@@ -95,3 +95,143 @@ Execute(The version number should be cached):
\ . ' --stdin-filepath %s --stdin',
\ },
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), [])
+
+Execute(Should set --parser based on filetype, TypeScript):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=typescript
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser typescript'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, CSS):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=css
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser css'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, LESS):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=less
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser less'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, SCSS):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=scss
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser scss'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, JSON):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=json
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser json'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, JSON5):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=json5
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser json5'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, GraphQL):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=graphql
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser graphql'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, Markdown):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=markdown
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser markdown'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on filetype, Vue):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=vue
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser vue'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
+
+Execute(Should set --parser based on first filetype of multiple filetypes):
+ call ale#test#SetFilename('../prettier-test-files/testfile')
+
+ set filetype=css.scss
+
+ AssertEqual
+ \ {
+ \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
+ \ . ale#Escape(g:ale_javascript_prettier_executable)
+ \ . ' --parser css'
+ \ . ' --stdin-filepath %s --stdin',
+ \ },
+ \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
diff --git a/test/prettier-test-files/testfile b/test/prettier-test-files/testfile
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/prettier-test-files/testfile