From b28a6ddbe4cf573ea993288a6ad4db569d535adf Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 24 Jan 2018 10:36:02 +0000 Subject: [PATCH] Support fixing JSON files with fixjson --- README.md | 2 +- autoload/ale/fix/registry.vim | 5 ++ autoload/ale/fixers/fixjson.vim | 20 ++++++++ doc/ale-json.txt | 39 +++++++++++++++ doc/ale.txt | 3 +- test/fixers/test_fixjson_fixer_callback.vader | 50 +++++++++++++++++++ test/json_files/testfile.json | 1 + 7 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 autoload/ale/fixers/fixjson.vim create mode 100644 test/fixers/test_fixjson_fixer_callback.vader create mode 100644 test/json_files/testfile.json diff --git a/README.md b/README.md index 6ac88cc9..ff46a402 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,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), [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) -| 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 | | 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) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 2e8e6e06..2d0b4a53 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -159,6 +159,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['java'], \ 'description': 'Fix Java files with google-java-format.', \ }, +\ 'fixjson': { +\ 'function': 'ale#fixers#fixjson#Fix', +\ 'suggested_filetypes': ['json'], +\ 'description': 'Fix JSON files with fixjson.', +\ }, \ 'jq': { \ 'function': 'ale#fixers#jq#Fix', \ 'suggested_filetypes': ['json'], diff --git a/autoload/ale/fixers/fixjson.vim b/autoload/ale/fixers/fixjson.vim new file mode 100644 index 00000000..84728f24 --- /dev/null +++ b/autoload/ale/fixers/fixjson.vim @@ -0,0 +1,20 @@ +" Author: rhysd +" 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 diff --git a/doc/ale-json.txt b/doc/ale-json.txt index 92c4609e..b8e13cdb 100644 --- a/doc/ale-json.txt +++ b/doc/ale-json.txt @@ -2,6 +2,45 @@ 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* diff --git a/doc/ale.txt b/doc/ale.txt index f25e8846..ddda902e 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -112,6 +112,7 @@ CONTENTS *ale-contents* standard............................|ale-javascript-standard| xo..................................|ale-javascript-xo| json..................................|ale-json-options| + fixjson.............................|ale-json-fixjson| jsonlint............................|ale-json-jsonlint| jq..................................|ale-json-jq| prettier............................|ale-json-prettier| @@ -322,7 +323,7 @@ Notes: * Idris: `idris` * Java: `checkstyle`, `javac`, `google-java-format` * JavaScript: `eslint`, `flow`, `jscs`, `jshint`, `prettier`, `prettier-eslint`, `prettier-standard`, `standard`, `xo` -* JSON: `jsonlint`, `jq`, `prettier` +* JSON: `fixjson`, `jsonlint`, `jq`, `prettier` * Kotlin: `kotlinc`, `ktlint` * LaTeX (tex): `alex`!!, `chktex`, `lacheck`, `proselint`, `redpen`, `vale`, `write-good` * Less: `lessc`, `prettier`, `stylelint` diff --git a/test/fixers/test_fixjson_fixer_callback.vader b/test/fixers/test_fixjson_fixer_callback.vader new file mode 100644 index 00000000..1a3bdcfc --- /dev/null +++ b/test/fixers/test_fixjson_fixer_callback.vader @@ -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('')) diff --git a/test/json_files/testfile.json b/test/json_files/testfile.json new file mode 100644 index 00000000..fe317ebb --- /dev/null +++ b/test/json_files/testfile.json @@ -0,0 +1 @@ +{"answer":42}