Support fixing JSON files with fixjson

This commit is contained in:
rhysd 2018-01-24 10:36:02 +00:00 committed by w0rp
parent d562d53102
commit b28a6ddbe4
7 changed files with 118 additions and 2 deletions

View File

@ -113,7 +113,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), [google-java-format](https://github.com/google/google-java-format) | | Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html), [google-java-format](https://github.com/google/google-java-format) |
| JavaScript | [eslint](http://eslint.org/), [flow](https://flowtype.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [prettier](https://github.com/prettier/prettier), [prettier-eslint](https://github.com/prettier/prettier-eslint), [prettier-standard](https://github.com/sheerun/prettier-standard), [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo) | JavaScript | [eslint](http://eslint.org/), [flow](https://flowtype.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [prettier](https://github.com/prettier/prettier), [prettier-eslint](https://github.com/prettier/prettier-eslint), [prettier-standard](https://github.com/sheerun/prettier-standard), [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo)
| JSON | [jsonlint](http://zaa.ch/jsonlint/), [jq](https://stedolan.github.io/jq/), [prettier](https://github.com/prettier/prettier) | | JSON | [fixjson](https://github.com/rhysd/fixjson), [jsonlint](http://zaa.ch/jsonlint/), [jq](https://stedolan.github.io/jq/), [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 | [alex](https://github.com/wooorm/alex) !!, [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/), [redpen](http://redpen.cc/), [vale](https://github.com/ValeLint/vale), [write-good](https://github.com/btford/write-good) | | LaTeX | [alex](https://github.com/wooorm/alex) !!, [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/), [redpen](http://redpen.cc/), [vale](https://github.com/ValeLint/vale), [write-good](https://github.com/btford/write-good) |
| Less | [lessc](https://www.npmjs.com/package/less), [prettier](https://github.com/prettier/prettier), [stylelint](https://github.com/stylelint/stylelint) | | Less | [lessc](https://www.npmjs.com/package/less), [prettier](https://github.com/prettier/prettier), [stylelint](https://github.com/stylelint/stylelint) |

View File

@ -159,6 +159,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['java'], \ 'suggested_filetypes': ['java'],
\ 'description': 'Fix Java files with google-java-format.', \ 'description': 'Fix Java files with google-java-format.',
\ }, \ },
\ 'fixjson': {
\ 'function': 'ale#fixers#fixjson#Fix',
\ 'suggested_filetypes': ['json'],
\ 'description': 'Fix JSON files with fixjson.',
\ },
\ 'jq': { \ 'jq': {
\ 'function': 'ale#fixers#jq#Fix', \ 'function': 'ale#fixers#jq#Fix',
\ 'suggested_filetypes': ['json'], \ 'suggested_filetypes': ['json'],

View File

@ -0,0 +1,20 @@
" Author: rhysd <https://rhysd.github.io>
" Description: Integration of fixjson with ALE.
call ale#Set('json_fixjson_executable', 'fixjson')
call ale#Set('json_fixjson_options', '')
function! ale#fixers#fixjson#Fix(buffer) abort
let l:executable = ale#Escape(ale#Var(a:buffer, 'json_fixjson_executable'))
let l:filename = ale#Escape(bufname(a:buffer))
let l:command = l:executable . ' --stdin-filename ' . l:filename
let l:options = ale#Var(a:buffer, 'json_fixjson_options')
if l:options isnot# ''
let l:command .= ' ' . l:options
endif
return {
\ 'command': l:command
\}
endfunction

View File

@ -2,6 +2,45 @@
ALE JSON Integration *ale-json-options* ALE JSON Integration *ale-json-options*
===============================================================================
fixjson *ale-json-fixjson*
fixjson is a JSON file fixer/formatter for humans using (relaxed) JSON5.
It provides:
- Pretty-prints JSON input
- Fixes various failures while humans writing JSON
- Fixes trailing commas objects or arrays
- Fixes missing commas for elements of objects or arrays
- Adds quotes to keys in objects
- Newlines in strings
- Hex numbers
- Fixes single quotes to double quotes
You can install it using npm:
>
$ npm install -g fixjson
<
ALE provides fixjson integration as a fixer. See |ale-fix|.
g:ale_json_fixjson_executable *g:ale_json_fixjson_executable*
*b:ale_json_fixjson_executable*
Type: |String|
Default: `'fixjson'`
The executable that will be run for fixjson.
g:ale_json_fixjson_options *g:ale_json_fixjson_options*
*b:ale_json_fixjson_options*
Type: |String|
Default: `''`
This variable can add extra options to the command executed for running
fixjson.
=============================================================================== ===============================================================================
jsonlint *ale-json-jsonlint* jsonlint *ale-json-jsonlint*

View File

@ -112,6 +112,7 @@ CONTENTS *ale-contents*
standard............................|ale-javascript-standard| standard............................|ale-javascript-standard|
xo..................................|ale-javascript-xo| xo..................................|ale-javascript-xo|
json..................................|ale-json-options| json..................................|ale-json-options|
fixjson.............................|ale-json-fixjson|
jsonlint............................|ale-json-jsonlint| jsonlint............................|ale-json-jsonlint|
jq..................................|ale-json-jq| jq..................................|ale-json-jq|
prettier............................|ale-json-prettier| prettier............................|ale-json-prettier|
@ -322,7 +323,7 @@ Notes:
* Idris: `idris` * Idris: `idris`
* Java: `checkstyle`, `javac`, `google-java-format` * Java: `checkstyle`, `javac`, `google-java-format`
* JavaScript: `eslint`, `flow`, `jscs`, `jshint`, `prettier`, `prettier-eslint`, `prettier-standard`, `standard`, `xo` * JavaScript: `eslint`, `flow`, `jscs`, `jshint`, `prettier`, `prettier-eslint`, `prettier-standard`, `standard`, `xo`
* JSON: `jsonlint`, `jq`, `prettier` * JSON: `fixjson`, `jsonlint`, `jq`, `prettier`
* Kotlin: `kotlinc`, `ktlint` * Kotlin: `kotlinc`, `ktlint`
* LaTeX (tex): `alex`!!, `chktex`, `lacheck`, `proselint`, `redpen`, `vale`, `write-good` * LaTeX (tex): `alex`!!, `chktex`, `lacheck`, `proselint`, `redpen`, `vale`, `write-good`
* Less: `lessc`, `prettier`, `stylelint` * Less: `lessc`, `prettier`, `stylelint`

View File

@ -0,0 +1,50 @@
Before:
Save g:ale_json_fixjson_executable
Save g:ale_json_fixjson_options
let g:ale_json_fixjson_executable = '/path/to/fixjson'
let g:ale_json_fixjson_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
Execute(The fixjson callback should return the correct default command):
AssertEqual
\ {
\ 'command': ale#Escape('/path/to/fixjson')
\ . ' --stdin-filename '
\ . ale#Escape(bufname(bufnr('')))
\ },
\ ale#fixers#fixjson#Fix(bufnr(''))
Execute(The fixjson callback should set the buffer name as file name):
call ale#test#SetFilename('../json_files/testfile.json')
AssertEqual
\ {
\ 'command': ale#Escape('/path/to/fixjson')
\ . ' --stdin-filename '
\ . ale#Escape(bufname(bufnr('')))
\ },
\ ale#fixers#fixjson#Fix(bufnr(''))
AssertNotEqual
\ stridx(
\ ale#fixers#fixjson#Fix(bufnr('')).command,
\ 'testfile.json',
\ ),
\ -1
Execute(The fixjson callback should include additional options):
let g:ale_json_fixjson_options = '-i 2'
AssertEqual
\ {
\ 'command': ale#Escape('/path/to/fixjson')
\ . ' --stdin-filename '
\ . ale#Escape(bufname(bufnr('')))
\ . ' -i 2'
\ },
\ ale#fixers#fixjson#Fix(bufnr(''))

View File

@ -0,0 +1 @@
{"answer":42}