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
This commit is contained in:
Peter Renström 2017-09-06 16:21:26 +02:00 committed by w0rp
parent c277cdef8c
commit 03f1c1e81b
13 changed files with 126 additions and 10 deletions

View File

@ -80,7 +80,7 @@ formatting.
| CMake | [cmakelint](https://github.com/richq/cmake-lint) | | CMake | [cmakelint](https://github.com/richq/cmake-lint) |
| CoffeeScript | [coffee](http://coffeescript.org/), [coffeelint](https://www.npmjs.com/package/coffeelint) | | CoffeeScript | [coffee](http://coffeescript.org/), [coffeelint](https://www.npmjs.com/package/coffeelint) |
| Crystal | [crystal](https://crystal-lang.org/) !! | | 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/) | | Cython (pyrex filetype) | [cython](http://cython.org/) |
| D | [dmd](https://dlang.org/dmd-linux.html) | | D | [dmd](https://dlang.org/dmd-linux.html) |
| Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) | | 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/) | | Idris | [idris](http://www.idris-lang.org/) |
| Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) | | 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) | 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 | 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/) | | 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) | | 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) | | 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/) | | 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) | | 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) | | Scala | [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) |
| Slim | [slim-lint](https://github.com/sds/slim-lint) | Slim | [slim-lint](https://github.com/sds/slim-lint)
| SML | [smlnj](http://www.smlnj.org/) | | SML | [smlnj](http://www.smlnj.org/) |
@ -136,7 +136,7 @@ formatting.
| Texinfo | [proselint](http://proselint.com/)| | Texinfo | [proselint](http://proselint.com/)|
| Text^ | [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale) | | Text^ | [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale) |
| Thrift | [thrift](http://thrift.apache.org/) | | 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) | | Verilog | [iverilog](https://github.com/steveicarus/iverilog), [verilator](http://www.veripool.org/projects/verilator/wiki/Intro) |
| Vim | [vint](https://github.com/Kuniwak/vint) | | Vim | [vint](https://github.com/Kuniwak/vint) |
| Vim help^ | [proselint](http://proselint.com/)| | Vim help^ | [proselint](http://proselint.com/)|

View File

@ -34,7 +34,7 @@ let s:default_registry = {
\ }, \ },
\ 'prettier': { \ 'prettier': {
\ 'function': 'ale#fixers#prettier#Fix', \ 'function': 'ale#fixers#prettier#Fix',
\ 'suggested_filetypes': ['javascript'], \ 'suggested_filetypes': ['javascript', 'typescript', 'json', 'css', 'scss'],
\ 'description': 'Apply prettier to a file.', \ 'description': 'Apply prettier to a file.',
\ }, \ },
\ 'prettier_eslint': { \ 'prettier_eslint': {

View File

@ -38,6 +38,22 @@ function! ale#fixers#prettier#Fix(buffer) abort
let l:config = s:FindConfig(a:buffer) let l:config = s:FindConfig(a:buffer)
let l:use_config = ale#Var(a:buffer, 'javascript_prettier_use_local_config') let l:use_config = ale#Var(a:buffer, 'javascript_prettier_use_local_config')
\ && !empty(l: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 { return {
\ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer)) \ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer))

View File

@ -29,5 +29,11 @@ g:ale_css_stylelint_use_global *g:ale_css_stylelint_use_global*
See |ale-integrations-local-executables| 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: vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

18
doc/ale-json.txt Normal file
View File

@ -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:

View File

@ -21,5 +21,11 @@ g:ale_scss_stylelint_use_global *g:ale_scss_stylelint_use_global*
See |ale-integrations-local-executables| 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: vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -93,5 +93,11 @@ g:ale_typescript_tsserver_use_global *g:ale_typescript_tsserver_use_global*
tsserver in node_modules. 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: vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -33,6 +33,7 @@ CONTENTS *ale-contents*
gcc.................................|ale-cpp-gcc| gcc.................................|ale-cpp-gcc|
clang-format........................|ale-cpp-clangformat| clang-format........................|ale-cpp-clangformat|
css...................................|ale-css-options| css...................................|ale-css-options|
prettier............................|ale-css-prettier|
stylelint...........................|ale-css-stylelint| stylelint...........................|ale-css-stylelint|
cmake.................................|ale-cmake-options| cmake.................................|ale-cmake-options|
cmakelint...........................|ale-cmake-cmakelint| cmakelint...........................|ale-cmake-cmakelint|
@ -71,6 +72,9 @@ CONTENTS *ale-contents*
prettier-standard...................|ale-javascript-prettier-standard| prettier-standard...................|ale-javascript-prettier-standard|
standard............................|ale-javascript-standard| standard............................|ale-javascript-standard|
xo..................................|ale-javascript-xo| xo..................................|ale-javascript-xo|
json..................................|ale-json-options|
jsonlint............................|ale-json-jsonlint|
prettier............................|ale-json-prettier|
kotlin................................|ale-kotlin-options| kotlin................................|ale-kotlin-options|
kotlinc.............................|ale-kotlin-kotlinc| kotlinc.............................|ale-kotlin-kotlinc|
lua...................................|ale-lua-options| lua...................................|ale-lua-options|
@ -117,6 +121,7 @@ CONTENTS *ale-contents*
scala.................................|ale-scala-options| scala.................................|ale-scala-options|
scalastyle..........................|ale-scala-scalastyle| scalastyle..........................|ale-scala-scalastyle|
scss..................................|ale-scss-options| scss..................................|ale-scss-options|
prettier............................|ale-scss-prettier|
stylelint...........................|ale-scss-stylelint| stylelint...........................|ale-scss-stylelint|
sh....................................|ale-sh-options| sh....................................|ale-sh-options|
shell...............................|ale-sh-shell| shell...............................|ale-sh-shell|
@ -136,6 +141,7 @@ CONTENTS *ale-contents*
thrift..............................|ale-thrift-thrift| thrift..............................|ale-thrift-thrift|
typescript............................|ale-typescript-options| typescript............................|ale-typescript-options|
eslint..............................|ale-typescript-eslint| eslint..............................|ale-typescript-eslint|
prettier............................|ale-typescript-prettier|
tslint..............................|ale-typescript-tslint| tslint..............................|ale-typescript-tslint|
tsserver............................|ale-typescript-tsserver| tsserver............................|ale-typescript-tsserver|
verilog/systemverilog.................|ale-verilog-options| verilog/systemverilog.................|ale-verilog-options|
@ -200,7 +206,7 @@ Notes:
* CMake: `cmakelint` * CMake: `cmakelint`
* CoffeeScript: `coffee`, `coffeelint` * CoffeeScript: `coffee`, `coffeelint`
* Crystal: `crystal`!! * Crystal: `crystal`!!
* CSS: `csslint`, `stylelint` * CSS: `csslint`, `stylelint`, `prettier`
* Cython (pyrex filetype): `cython` * Cython (pyrex filetype): `cython`
* D: `dmd` * D: `dmd`
* Dart: `dartanalyzer` * Dart: `dartanalyzer`
@ -220,7 +226,7 @@ Notes:
* Idris: `idris` * Idris: `idris`
* Java: `checkstyle`, `javac` * Java: `checkstyle`, `javac`
* JavaScript: `eslint`, `jscs`, `jshint`, `flow`, `prettier`, `prettier-eslint` >= 4.2.0, `prettier-standard`, `standard`, `xo` * JavaScript: `eslint`, `jscs`, `jshint`, `flow`, `prettier`, `prettier-eslint` >= 4.2.0, `prettier-standard`, `standard`, `xo`
* JSON: `jsonlint` * JSON: `jsonlint`, `prettier`
* Kotlin: `kotlinc`, `ktlint` * Kotlin: `kotlinc`, `ktlint`
* LaTeX (tex): `chktex`, `lacheck`, `proselint` * LaTeX (tex): `chktex`, `lacheck`, `proselint`
* Lua: `luacheck` * Lua: `luacheck`
@ -245,7 +251,7 @@ Notes:
* Ruby: `brakeman`, `rails_best_practices`!!, `reek`, `rubocop`, `ruby` * Ruby: `brakeman`, `rails_best_practices`!!, `reek`, `rubocop`, `ruby`
* Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|) * Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|)
* SASS: `sass-lint`, `stylelint` * SASS: `sass-lint`, `stylelint`
* SCSS: `sass-lint`, `scss-lint`, `stylelint` * SCSS: `sass-lint`, `scss-lint`, `stylelint`, `prettier`
* Scala: `scalac`, `scalastyle` * Scala: `scalac`, `scalastyle`
* Slim: `slim-lint` * Slim: `slim-lint`
* SML: `smlnj` * SML: `smlnj`
@ -256,7 +262,7 @@ Notes:
* Texinfo: `proselint` * Texinfo: `proselint`
* Text^: `proselint`, `vale` * Text^: `proselint`, `vale`
* Thrift: `thrift` * Thrift: `thrift`
* TypeScript: `eslint`, `tslint`, `tsserver`, `typecheck` * TypeScript: `eslint`, `tslint`, `tsserver`, `typecheck`, `prettier`
* Verilog: `iverilog`, `verilator` * Verilog: `iverilog`, `verilator`
* Vim: `vint` * Vim: `vint`
* Vim help^: `proselint` * Vim help^: `proselint`

View File

@ -24,6 +24,7 @@ Execute(The prettier callback should return the correct default values):
\ 'read_temporary_file': 1, \ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t' \ . ' %t'
\ . ' --parser babylon'
\ . ' --write', \ . ' --write',
\ }, \ },
\ ale#fixers#prettier#Fix(bufnr('')) \ ale#fixers#prettier#Fix(bufnr(''))
@ -37,6 +38,7 @@ Execute(The prettier callback should include configuration files when the option
\ 'read_temporary_file': 1, \ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t' \ . ' %t'
\ . ' --parser babylon'
\ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) \ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
\ . ' --write', \ . ' --write',
\ }, \ },
@ -51,8 +53,64 @@ Execute(The prettier callback should include custom prettier options):
\ 'read_temporary_file': 1, \ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t' \ . ' %t'
\ . ' --no-semi' \ . ' --no-semi --parser babylon'
\ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) \ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
\ . ' --write', \ . ' --write',
\ }, \ },
\ ale#fixers#prettier#Fix(bufnr('')) \ 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(''))

View File

View File

View File

View File