From 80bd2e18d65e335492cc4f6ba3fe15cc98740ef3 Mon Sep 17 00:00:00 2001 From: Sorin Iclanzan Date: Fri, 28 Aug 2020 03:14:50 -0400 Subject: [PATCH] Set prettier working directory to where .prettierignore is (#3101) Prettier does not use `.prettierignore` unless the current directory is the root where the `.prettierignore` file resides. * Update Prettier tests * Look for prettierignore to determine project root --- autoload/ale/fixers/prettier.vim | 17 ++++++++++++++++- test/fixers/test_prettier_fixer_callback.vader | 11 +++++++++++ .../with_prettierignore/.prettierignore | 0 .../with_prettierignore/src/testfile.js | 0 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/prettier-test-files/with_prettierignore/.prettierignore create mode 100644 test/prettier-test-files/with_prettierignore/src/testfile.js diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index 23120777..e0f4972e 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -34,6 +34,21 @@ function! ale#fixers#prettier#ProcessPrettierDOutput(buffer, output) abort return a:output endfunction +function! ale#fixers#prettier#GetProjectRoot(buffer) abort + let l:config = ale#path#FindNearestFile(a:buffer, '.prettierignore') + + if !empty(l:config) + return fnamemodify(l:config, ':h') + endif + + " Fall back to the directory of the buffer + return fnamemodify(bufname(a:buffer), ':p:h') +endfunction + +function! ale#fixers#prettier#CdProjectRoot(buffer) abort + return ale#path#CdString(ale#fixers#prettier#GetProjectRoot(a:buffer)) +endfunction + function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) abort let l:executable = ale#fixers#prettier#GetExecutable(a:buffer) let l:options = ale#Var(a:buffer, 'javascript_prettier_options') @@ -97,7 +112,7 @@ function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) abort " 1.4.0 is the first version with --stdin-filepath if ale#semver#GTE(a:version, [1, 4, 0]) return { - \ 'command': ale#path#BufferCdString(a:buffer) + \ 'command': ale#fixers#prettier#CdProjectRoot(a:buffer) \ . ale#Escape(l:executable) \ . (!empty(l:options) ? ' ' . l:options : '') \ . ' --stdin-filepath %s --stdin', diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader index 1087a160..fdd97df3 100644 --- a/test/fixers/test_prettier_fixer_callback.vader +++ b/test/fixers/test_prettier_fixer_callback.vader @@ -297,6 +297,17 @@ Execute(Should set --parser for experimental language, Handlebars): \ . ' --stdin-filepath %s --stdin', \ } +Execute(Changes to directory where .prettierignore is found): + call ale#test#SetFilename('../prettier-test-files/with_prettierignore/src/testfile.js') + + GivenCommandOutput ['1.6.0'] + AssertFixer + \ { + \ 'command': ale#path#CdString(expand('%:p:h:h')) + \ . ale#Escape(g:ale_javascript_prettier_executable) + \ . ' --stdin-filepath %s --stdin', + \ } + Execute(The prettier_d post-processor should permit regular JavaScript content): AssertEqual \ [ diff --git a/test/prettier-test-files/with_prettierignore/.prettierignore b/test/prettier-test-files/with_prettierignore/.prettierignore new file mode 100644 index 00000000..e69de29b diff --git a/test/prettier-test-files/with_prettierignore/src/testfile.js b/test/prettier-test-files/with_prettierignore/src/testfile.js new file mode 100644 index 00000000..e69de29b