From 03f1c1e81bdf1fd41b535f6a0260d47ba1bf60dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Renstr=C3=B6m?= Date: Wed, 6 Sep 2017 16:21:26 +0200 Subject: [PATCH] Add 'prettier' fixer support to TypeScript, CSS, SCSS and JSON (#910) * Add prettier fixer support for typescript * Add prettier fixer support for css and scss * Add prettier fixer support for json * Use getbufvar() to get &filetype --- README.md | 8 +-- autoload/ale/fix/registry.vim | 2 +- autoload/ale/fixers/prettier.vim | 16 +++++ doc/ale-css.txt | 6 ++ doc/ale-json.txt | 18 ++++++ doc/ale-scss.txt | 6 ++ doc/ale-typescript.txt | 6 ++ doc/ale.txt | 14 +++-- .../fixers/test_prettier_fixer_callback.vader | 60 ++++++++++++++++++- test/prettier-test-files/testfile.css | 0 test/prettier-test-files/testfile.json | 0 test/prettier-test-files/testfile.scss | 0 test/prettier-test-files/testfile.ts | 0 13 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 doc/ale-json.txt create mode 100644 test/prettier-test-files/testfile.css create mode 100644 test/prettier-test-files/testfile.json create mode 100644 test/prettier-test-files/testfile.scss create mode 100644 test/prettier-test-files/testfile.ts diff --git a/README.md b/README.md index 863815f..ee5c156 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ formatting. | CMake | [cmakelint](https://github.com/richq/cmake-lint) | | CoffeeScript | [coffee](http://coffeescript.org/), [coffeelint](https://www.npmjs.com/package/coffeelint) | | Crystal | [crystal](https://crystal-lang.org/) !! | -| CSS | [csslint](http://csslint.net/), [stylelint](https://github.com/stylelint/stylelint) | +| CSS | [csslint](http://csslint.net/), [stylelint](https://github.com/stylelint/stylelint), [prettier](https://github.com/prettier/prettier) | | Cython (pyrex filetype) | [cython](http://cython.org/) | | D | [dmd](https://dlang.org/dmd-linux.html) | | Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) | @@ -100,7 +100,7 @@ formatting. | Idris | [idris](http://www.idris-lang.org/) | | Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) | | JavaScript | [eslint](http://eslint.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [flow](https://flowtype.org/), [prettier](https://github.com/prettier/prettier), prettier-eslint >= 4.2.0, prettier-standard, [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo) -| JSON | [jsonlint](http://zaa.ch/jsonlint/) | +| JSON | [jsonlint](http://zaa.ch/jsonlint/), [prettier](https://github.com/prettier/prettier) | | Kotlin | [kotlinc](https://kotlinlang.org) !!, [ktlint](https://ktlint.github.io) !! see `:help ale-integration-kotlin` for configuration instructions | LaTeX | [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/) | | Lua | [luacheck](https://github.com/mpeterv/luacheck) | @@ -125,7 +125,7 @@ formatting. | Ruby | [brakeman](http://brakemanscanner.org/) !!, [rails_best_practices](https://github.com/flyerhzm/rails_best_practices) !!, [reek](https://github.com/troessner/reek), [rubocop](https://github.com/bbatsov/rubocop), [ruby](https://www.ruby-lang.org) | | Rust | cargo !! (see `:help ale-integration-rust` for configuration instructions), [rls](https://github.com/rust-lang-nursery/rls), [rustc](https://www.rust-lang.org/) | | SASS | [sass-lint](https://www.npmjs.com/package/sass-lint), [stylelint](https://github.com/stylelint/stylelint) | -| SCSS | [sass-lint](https://www.npmjs.com/package/sass-lint), [scss-lint](https://github.com/brigade/scss-lint), [stylelint](https://github.com/stylelint/stylelint) | +| SCSS | [sass-lint](https://www.npmjs.com/package/sass-lint), [scss-lint](https://github.com/brigade/scss-lint), [stylelint](https://github.com/stylelint/stylelint), [prettier](https://github.com/prettier/prettier) | | Scala | [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) | | Slim | [slim-lint](https://github.com/sds/slim-lint) | SML | [smlnj](http://www.smlnj.org/) | @@ -136,7 +136,7 @@ formatting. | Texinfo | [proselint](http://proselint.com/)| | Text^ | [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale) | | Thrift | [thrift](http://thrift.apache.org/) | -| TypeScript | [eslint](http://eslint.org/), [tslint](https://github.com/palantir/tslint), tsserver, typecheck | +| TypeScript | [eslint](http://eslint.org/), [tslint](https://github.com/palantir/tslint), tsserver, typecheck, [prettier](https://github.com/prettier/prettier) | | Verilog | [iverilog](https://github.com/steveicarus/iverilog), [verilator](http://www.veripool.org/projects/verilator/wiki/Intro) | | Vim | [vint](https://github.com/Kuniwak/vint) | | Vim help^ | [proselint](http://proselint.com/)| diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index b77ac03..5b1030d 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -34,7 +34,7 @@ let s:default_registry = { \ }, \ 'prettier': { \ 'function': 'ale#fixers#prettier#Fix', -\ 'suggested_filetypes': ['javascript'], +\ 'suggested_filetypes': ['javascript', 'typescript', 'json', 'css', 'scss'], \ 'description': 'Apply prettier to a file.', \ }, \ 'prettier_eslint': { diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index 581536e..ca1bf60 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -38,6 +38,22 @@ function! ale#fixers#prettier#Fix(buffer) abort let l:config = s:FindConfig(a:buffer) let l:use_config = ale#Var(a:buffer, 'javascript_prettier_use_local_config') \ && !empty(l:config) + let l:filetype = getbufvar(a:buffer, '&filetype') + + " Append the --parser flag depending on the current filetype (unless it's + " already set in g:javascript_prettier_options). + if match(l:options, '--parser') == -1 + if l:filetype is# 'typescript' + let l:parser = 'typescript' + elseif l:filetype =~# 'css\|scss' + let l:parser = 'postcss' + elseif l:filetype is# 'json' + let l:parser = 'json' + else + let l:parser = 'babylon' + endif + let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser + endif return { \ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer)) diff --git a/doc/ale-css.txt b/doc/ale-css.txt index b1ab8eb..979be88 100644 --- a/doc/ale-css.txt +++ b/doc/ale-css.txt @@ -29,5 +29,11 @@ g:ale_css_stylelint_use_global *g:ale_css_stylelint_use_global* See |ale-integrations-local-executables| +=============================================================================== +prettier *ale-css-prettier* + +See |ale-javascript-prettier| for information about the available options. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-json.txt b/doc/ale-json.txt new file mode 100644 index 0000000..1d052d5 --- /dev/null +++ b/doc/ale-json.txt @@ -0,0 +1,18 @@ +=============================================================================== +ALE JSON Integration *ale-json-options* + + +=============================================================================== +jsonlint *ale-json-jsonlint* + +There are no options available. + + +=============================================================================== +prettier *ale-json-prettier* + +See |ale-javascript-prettier| for information about the available options. + + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-scss.txt b/doc/ale-scss.txt index c7b7919..e0b0a11 100644 --- a/doc/ale-scss.txt +++ b/doc/ale-scss.txt @@ -21,5 +21,11 @@ g:ale_scss_stylelint_use_global *g:ale_scss_stylelint_use_global* See |ale-integrations-local-executables| +=============================================================================== +prettier *ale-scss-prettier* + +See |ale-javascript-prettier| for information about the available options. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-typescript.txt b/doc/ale-typescript.txt index 949b80d..887766f 100644 --- a/doc/ale-typescript.txt +++ b/doc/ale-typescript.txt @@ -93,5 +93,11 @@ g:ale_typescript_tsserver_use_global *g:ale_typescript_tsserver_use_global* tsserver in node_modules. +=============================================================================== +prettier *ale-typescript-prettier* + +See |ale-javascript-prettier| for information about the available options. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 899e8c8..3c02af3 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -33,6 +33,7 @@ CONTENTS *ale-contents* gcc.................................|ale-cpp-gcc| clang-format........................|ale-cpp-clangformat| css...................................|ale-css-options| + prettier............................|ale-css-prettier| stylelint...........................|ale-css-stylelint| cmake.................................|ale-cmake-options| cmakelint...........................|ale-cmake-cmakelint| @@ -71,6 +72,9 @@ CONTENTS *ale-contents* prettier-standard...................|ale-javascript-prettier-standard| standard............................|ale-javascript-standard| xo..................................|ale-javascript-xo| + json..................................|ale-json-options| + jsonlint............................|ale-json-jsonlint| + prettier............................|ale-json-prettier| kotlin................................|ale-kotlin-options| kotlinc.............................|ale-kotlin-kotlinc| lua...................................|ale-lua-options| @@ -117,6 +121,7 @@ CONTENTS *ale-contents* scala.................................|ale-scala-options| scalastyle..........................|ale-scala-scalastyle| scss..................................|ale-scss-options| + prettier............................|ale-scss-prettier| stylelint...........................|ale-scss-stylelint| sh....................................|ale-sh-options| shell...............................|ale-sh-shell| @@ -136,6 +141,7 @@ CONTENTS *ale-contents* thrift..............................|ale-thrift-thrift| typescript............................|ale-typescript-options| eslint..............................|ale-typescript-eslint| + prettier............................|ale-typescript-prettier| tslint..............................|ale-typescript-tslint| tsserver............................|ale-typescript-tsserver| verilog/systemverilog.................|ale-verilog-options| @@ -200,7 +206,7 @@ Notes: * CMake: `cmakelint` * CoffeeScript: `coffee`, `coffeelint` * Crystal: `crystal`!! -* CSS: `csslint`, `stylelint` +* CSS: `csslint`, `stylelint`, `prettier` * Cython (pyrex filetype): `cython` * D: `dmd` * Dart: `dartanalyzer` @@ -220,7 +226,7 @@ Notes: * Idris: `idris` * Java: `checkstyle`, `javac` * JavaScript: `eslint`, `jscs`, `jshint`, `flow`, `prettier`, `prettier-eslint` >= 4.2.0, `prettier-standard`, `standard`, `xo` -* JSON: `jsonlint` +* JSON: `jsonlint`, `prettier` * Kotlin: `kotlinc`, `ktlint` * LaTeX (tex): `chktex`, `lacheck`, `proselint` * Lua: `luacheck` @@ -245,7 +251,7 @@ Notes: * Ruby: `brakeman`, `rails_best_practices`!!, `reek`, `rubocop`, `ruby` * Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|) * SASS: `sass-lint`, `stylelint` -* SCSS: `sass-lint`, `scss-lint`, `stylelint` +* SCSS: `sass-lint`, `scss-lint`, `stylelint`, `prettier` * Scala: `scalac`, `scalastyle` * Slim: `slim-lint` * SML: `smlnj` @@ -256,7 +262,7 @@ Notes: * Texinfo: `proselint` * Text^: `proselint`, `vale` * Thrift: `thrift` -* TypeScript: `eslint`, `tslint`, `tsserver`, `typecheck` +* TypeScript: `eslint`, `tslint`, `tsserver`, `typecheck`, `prettier` * Verilog: `iverilog`, `verilator` * Vim: `vint` * Vim help^: `proselint` diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader index 1eb24da..a684ad0 100644 --- a/test/fixers/test_prettier_fixer_callback.vader +++ b/test/fixers/test_prettier_fixer_callback.vader @@ -24,6 +24,7 @@ Execute(The prettier callback should return the correct default values): \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' + \ . ' --parser babylon' \ . ' --write', \ }, \ ale#fixers#prettier#Fix(bufnr('')) @@ -37,6 +38,7 @@ Execute(The prettier callback should include configuration files when the option \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' + \ . ' --parser babylon' \ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) \ . ' --write', \ }, @@ -51,8 +53,64 @@ Execute(The prettier callback should include custom prettier options): \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' - \ . ' --no-semi' + \ . ' --no-semi --parser babylon' \ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) \ . ' --write', \ }, \ ale#fixers#prettier#Fix(bufnr('')) + +Execute(Append '--parser typescript' for filetype=typescript): + set filetype=typescript + call ale#test#SetFilename('../prettier-test-files/testfile.ts') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape(g:ale_javascript_prettier_executable) + \ . ' %t' + \ . ' --parser typescript' + \ . ' --write', + \ }, + \ ale#fixers#prettier#Fix(bufnr('')) + +Execute(Append '--parser json' for filetype=json): + set filetype=json + call ale#test#SetFilename('../prettier-test-files/testfile.json') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape(g:ale_javascript_prettier_executable) + \ . ' %t' + \ . ' --parser json' + \ . ' --write', + \ }, + \ ale#fixers#prettier#Fix(bufnr('')) + +Execute(Append '--parser postcss' for filetype=scss): + set filetype=scss + call ale#test#SetFilename('../prettier-test-files/testfile.scss') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape(g:ale_javascript_prettier_executable) + \ . ' %t' + \ . ' --parser postcss' + \ . ' --write', + \ }, + \ ale#fixers#prettier#Fix(bufnr('')) + +Execute(Append '--parser postcss' for filetype=css): + set filetype=css + call ale#test#SetFilename('../prettier-test-files/testfile.css') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape(g:ale_javascript_prettier_executable) + \ . ' %t' + \ . ' --parser postcss' + \ . ' --write', + \ }, + \ ale#fixers#prettier#Fix(bufnr('')) diff --git a/test/prettier-test-files/testfile.css b/test/prettier-test-files/testfile.css new file mode 100644 index 0000000..e69de29 diff --git a/test/prettier-test-files/testfile.json b/test/prettier-test-files/testfile.json new file mode 100644 index 0000000..e69de29 diff --git a/test/prettier-test-files/testfile.scss b/test/prettier-test-files/testfile.scss new file mode 100644 index 0000000..e69de29 diff --git a/test/prettier-test-files/testfile.ts b/test/prettier-test-files/testfile.ts new file mode 100644 index 0000000..e69de29