summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHoracio Sanson <hsanson@gmail.com>2021-01-23 00:13:40 +0900
committerGitHub <noreply@github.com>2021-01-23 00:13:40 +0900
commitdb816b5c3fcf032e8053d23e6d48447eb9b08bb6 (patch)
tree970d8a2062372ee662b0e25c7538f4fc1135fcce
parenta1e6df987c28dd3e0efb7422f4ad85ee3bb3bebc (diff)
parent9bc4b468c20e91350cf1a6d3739f91f504e230ed (diff)
downloadale-db816b5c3fcf032e8053d23e6d48447eb9b08bb6.zip
Merge pull request #2782 from hsanson/2777-add-ibm-openapi-validator
Fix 2777 - Add IBM openapi validator
-rw-r--r--ale_linters/openapi/ibm_validator.vim58
-rw-r--r--ale_linters/openapi/yamllint.vim9
-rw-r--r--ale_linters/yaml/yamllint.vim43
-rw-r--r--autoload/ale/fix/registry.vim2
-rw-r--r--autoload/ale/fixers/prettier.vim1
-rw-r--r--autoload/ale/handlers/yamllint.vim39
-rw-r--r--doc/ale-openapi.txt74
-rw-r--r--doc/ale-supported-languages-and-tools.txt4
-rw-r--r--doc/ale.txt4
-rw-r--r--supported-tools.md4
-rw-r--r--test/command_callback/test_ibm_openapi_validator_command_callback.vader15
-rw-r--r--test/handler/test_ibm_openapi_validator_handler.vader49
-rw-r--r--test/handler/test_yamllint_handler.vader8
13 files changed, 264 insertions, 46 deletions
diff --git a/ale_linters/openapi/ibm_validator.vim b/ale_linters/openapi/ibm_validator.vim
new file mode 100644
index 00000000..446931a2
--- /dev/null
+++ b/ale_linters/openapi/ibm_validator.vim
@@ -0,0 +1,58 @@
+" Author: Horacio Sanson <hsanson@gmail.com>
+
+call ale#Set('openapi_ibm_validator_executable', 'lint-openapi')
+call ale#Set('openapi_ibm_validator_options', '')
+
+function! ale_linters#openapi#ibm_validator#GetCommand(buffer) abort
+ return '%e' . ale#Pad(ale#Var(a:buffer, 'openapi_ibm_validator_options'))
+ \ . ' %t'
+endfunction
+
+function! ale_linters#openapi#ibm_validator#Handle(buffer, lines) abort
+ let l:output = []
+ let l:type = 'E'
+ let l:message = ''
+ let l:nr = -1
+
+ for l:line in a:lines
+ let l:match = matchlist(l:line, '^errors$')
+
+ if !empty(l:match)
+ let l:type = 'E'
+ endif
+
+ let l:match = matchlist(l:line, '^warnings$')
+
+ if !empty(l:match)
+ let l:type = 'W'
+ endif
+
+ let l:match = matchlist(l:line, '^ *Message : *\(.\+\)$')
+
+ if !empty(l:match)
+ let l:message = l:match[1]
+ endif
+
+ let l:match = matchlist(l:line, '^ *Line *: *\(\d\+\)$')
+
+ if !empty(l:match)
+ let l:nr = l:match[1]
+
+ call add(l:output, {
+ \ 'lnum': l:nr + 0,
+ \ 'col': 0,
+ \ 'text': l:message,
+ \ 'type': l:type,
+ \})
+ endif
+ endfor
+
+ return l:output
+endfunction
+
+call ale#linter#Define('openapi', {
+\ 'name': 'ibm_validator',
+\ 'executable': {b -> ale#Var(b, 'openapi_ibm_validator_executable')},
+\ 'command': function('ale_linters#openapi#ibm_validator#GetCommand'),
+\ 'callback': 'ale_linters#openapi#ibm_validator#Handle',
+\})
diff --git a/ale_linters/openapi/yamllint.vim b/ale_linters/openapi/yamllint.vim
new file mode 100644
index 00000000..2b8952cc
--- /dev/null
+++ b/ale_linters/openapi/yamllint.vim
@@ -0,0 +1,9 @@
+call ale#Set('yaml_yamllint_executable', 'yamllint')
+call ale#Set('yaml_yamllint_options', '')
+
+call ale#linter#Define('openapi', {
+\ 'name': 'yamllint',
+\ 'executable': {b -> ale#Var(b, 'yaml_yamllint_executable')},
+\ 'command': function('ale#handlers#yamllint#GetCommand'),
+\ 'callback': 'ale#handlers#yamllint#Handle',
+\})
diff --git a/ale_linters/yaml/yamllint.vim b/ale_linters/yaml/yamllint.vim
index bedb7bf1..39011df1 100644
--- a/ale_linters/yaml/yamllint.vim
+++ b/ale_linters/yaml/yamllint.vim
@@ -3,48 +3,9 @@
call ale#Set('yaml_yamllint_executable', 'yamllint')
call ale#Set('yaml_yamllint_options', '')
-function! ale_linters#yaml#yamllint#GetCommand(buffer) abort
- return '%e' . ale#Pad(ale#Var(a:buffer, 'yaml_yamllint_options'))
- \ . ' -f parsable %t'
-endfunction
-
-function! ale_linters#yaml#yamllint#Handle(buffer, lines) abort
- " Matches patterns line the following:
- " something.yaml:1:1: [warning] missing document start "---" (document-start)
- " something.yml:2:1: [error] syntax error: expected the node content, but found '<stream end>'
- let l:pattern = '\v^.*:(\d+):(\d+): \[(error|warning)\] (.+)$'
- let l:output = []
-
- for l:match in ale#util#GetMatches(a:lines, l:pattern)
- let l:item = {
- \ 'lnum': l:match[1] + 0,
- \ 'col': l:match[2] + 0,
- \ 'text': l:match[4],
- \ 'type': l:match[3] is# 'error' ? 'E' : 'W',
- \}
-
- let l:code_match = matchlist(l:item.text, '\v^(.+) \(([^)]+)\)$')
-
- if !empty(l:code_match)
- if l:code_match[2] is# 'trailing-spaces'
- \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
- " Skip warnings for trailing whitespace if the option is off.
- continue
- endif
-
- let l:item.text = l:code_match[1]
- let l:item.code = l:code_match[2]
- endif
-
- call add(l:output, l:item)
- endfor
-
- return l:output
-endfunction
-
call ale#linter#Define('yaml', {
\ 'name': 'yamllint',
\ 'executable': {b -> ale#Var(b, 'yaml_yamllint_executable')},
-\ 'command': function('ale_linters#yaml#yamllint#GetCommand'),
-\ 'callback': 'ale_linters#yaml#yamllint#Handle',
+\ 'command': function('ale#handlers#yamllint#GetCommand'),
+\ 'callback': 'ale#handlers#yamllint#Handle',
\})
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
index f514466e..4564954b 100644
--- a/autoload/ale/fix/registry.vim
+++ b/autoload/ale/fix/registry.vim
@@ -102,7 +102,7 @@ let s:default_registry = {
\ },
\ 'prettier': {
\ 'function': 'ale#fixers#prettier#Fix',
-\ 'suggested_filetypes': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue', 'html', 'yaml'],
+\ 'suggested_filetypes': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue', 'html', 'yaml', 'openapi'],
\ 'description': 'Apply prettier to a file.',
\ },
\ 'prettier_eslint': {
diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim
index e0f4972e..12c018af 100644
--- a/autoload/ale/fixers/prettier.vim
+++ b/autoload/ale/fixers/prettier.vim
@@ -83,6 +83,7 @@ function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) abort
\ 'markdown': 'markdown',
\ 'vue': 'vue',
\ 'yaml': 'yaml',
+ \ 'openapi': 'yaml',
\ 'html': 'html',
\}
diff --git a/autoload/ale/handlers/yamllint.vim b/autoload/ale/handlers/yamllint.vim
new file mode 100644
index 00000000..5e04577d
--- /dev/null
+++ b/autoload/ale/handlers/yamllint.vim
@@ -0,0 +1,39 @@
+function! ale#handlers#yamllint#GetCommand(buffer) abort
+ return '%e' . ale#Pad(ale#Var(a:buffer, 'yaml_yamllint_options'))
+ \ . ' -f parsable %t'
+endfunction
+
+function! ale#handlers#yamllint#Handle(buffer, lines) abort
+ " Matches patterns line the following:
+ " something.yaml:1:1: [warning] missing document start "---" (document-start)
+ " something.yml:2:1: [error] syntax error: expected the node content, but found '<stream end>'
+ let l:pattern = '\v^.*:(\d+):(\d+): \[(error|warning)\] (.+)$'
+ let l:output = []
+
+ for l:match in ale#util#GetMatches(a:lines, l:pattern)
+ let l:item = {
+ \ 'lnum': l:match[1] + 0,
+ \ 'col': l:match[2] + 0,
+ \ 'text': l:match[4],
+ \ 'type': l:match[3] is# 'error' ? 'E' : 'W',
+ \}
+
+ let l:code_match = matchlist(l:item.text, '\v^(.+) \(([^)]+)\)$')
+
+ if !empty(l:code_match)
+ if l:code_match[2] is# 'trailing-spaces'
+ \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
+ " Skip warnings for trailing whitespace if the option is off.
+ continue
+ endif
+
+ let l:item.text = l:code_match[1]
+ let l:item.code = l:code_match[2]
+ endif
+
+ call add(l:output, l:item)
+ endfor
+
+ return l:output
+endfunction
+
diff --git a/doc/ale-openapi.txt b/doc/ale-openapi.txt
new file mode 100644
index 00000000..1fc41add
--- /dev/null
+++ b/doc/ale-openapi.txt
@@ -0,0 +1,74 @@
+===============================================================================
+ALE OpenApi Integration *ale-openapi-options*
+
+===============================================================================
+ibm_validator *ale-openapi-ibm-validator*
+
+Website: https://github.com/IBM/openapi-validator
+
+
+Installation
+-------------------------------------------------------------------------------
+
+Install ibm-openapi-validator either globally or locally: >
+
+ npm install ibm-openapi-validator -g # global
+ npm install ibm-openapi-validator # local
+<
+Configuration
+-------------------------------------------------------------------------------
+
+OpenAPI files can be written in YAML or JSON so in order for ALE plugins to
+work with these files we must set the buffer |filetype| to either |openapi.yaml|
+or |openapi.json| respectively. This causes ALE to lint the file with linters
+configured for openapi and yaml files or openapi and json files respectively.
+
+For example setting filetype to |openapi.yaml| on a buffer and the following
+|g:ale_linters| configuration will enable linting of openapi files using both
+|ibm_validator| and |yamlint|:
+
+>
+ let g:ale_linters = {
+ \ 'yaml': ['yamllint'],
+ \ 'openapi': ['ibm_validator']
+ \}
+<
+
+The following plugin will detect openapi files automatically and set the
+filetype to |openapi.yaml| or |openapi.json|:
+
+ https://github.com/hsanson/vim-openapi
+
+Options
+-------------------------------------------------------------------------------
+
+g:ale_openapi_ibm_validator_executable *g:ale_openapi_ibm_validator_executable*
+ *b:ale_openapi_ibm_validator_executable*
+ Type: |String|
+ Default: `'lint-openapi'`
+
+ This variable can be set to change the path to lint-openapi.
+
+
+g:ale_openapi_ibm_validator_options *g:ale_openapi_ibm_validator_options*
+ *b:ale_openapi_ibm_validator_options*
+ Type: |String|
+ Default: `''`
+
+ This variable can be set to pass additional options to lint-openapi.
+
+
+===============================================================================
+prettier *ale-openapi-prettier*
+
+See |ale-javascript-prettier| for information about the available options.
+
+
+===============================================================================
+yamllint *ale-openapi-yamllint*
+
+See |ale-yaml-yamllint| for information about the available options.
+
+
+===============================================================================
+ vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt
index 5476be47..13538b03 100644
--- a/doc/ale-supported-languages-and-tools.txt
+++ b/doc/ale-supported-languages-and-tools.txt
@@ -322,6 +322,10 @@ Notes:
* `ocamlformat`
* `ocp-indent`
* `ols`
+* OpenApi
+ * `ibm_validator`
+ * `prettier`
+ * `yamllint`
* Pawn
* `uncrustify`
* Perl
diff --git a/doc/ale.txt b/doc/ale.txt
index 721ca69d..cfb5beb4 100644
--- a/doc/ale.txt
+++ b/doc/ale.txt
@@ -2835,6 +2835,10 @@ documented in additional help files.
ols...................................|ale-ocaml-ols|
ocamlformat...........................|ale-ocaml-ocamlformat|
ocp-indent............................|ale-ocaml-ocp-indent|
+ openapi.................................|ale-openapi-options|
+ ibm_validator.........................|ale-openapi-ibm-validator|
+ prettier..............................|ale-openapi-prettier|
+ yamllint..............................|ale-openapi-yamllint|
pawn....................................|ale-pawn-options|
uncrustify............................|ale-pawn-uncrustify|
perl....................................|ale-perl-options|
diff --git a/supported-tools.md b/supported-tools.md
index b07f6acf..0cea2a1b 100644
--- a/supported-tools.md
+++ b/supported-tools.md
@@ -331,6 +331,10 @@ formatting.
* [ocamlformat](https://github.com/ocaml-ppx/ocamlformat)
* [ocp-indent](https://github.com/OCamlPro/ocp-indent)
* [ols](https://github.com/freebroccolo/ocaml-language-server)
+* OpenApi
+ * [ibm_validator](https://github.com/IBM/openapi-validator)
+ * [prettier](https://github.com/prettier/prettier)
+ * [yamllint](https://yamllint.readthedocs.io/)
* Pawn
* [uncrustify](https://github.com/uncrustify/uncrustify)
* Perl
diff --git a/test/command_callback/test_ibm_openapi_validator_command_callback.vader b/test/command_callback/test_ibm_openapi_validator_command_callback.vader
new file mode 100644
index 00000000..3484cc09
--- /dev/null
+++ b/test/command_callback/test_ibm_openapi_validator_command_callback.vader
@@ -0,0 +1,15 @@
+Before:
+ call ale#assert#SetUpLinterTest('openapi', 'ibm_validator')
+
+After:
+ call ale#assert#TearDownLinterTest()
+
+Execute(The yaml ibm-openapi-validator command callback should return the correct default string):
+ AssertLinter 'lint-openapi', ale#Escape('lint-openapi') . ' %t'
+
+Execute(The yaml ibm-openapi-validator command callback should be configurable):
+ let g:ale_openapi_ibm_validator_executable = '~/.local/bin/lint-openapi'
+ let g:ale_openapi_ibm_validator_options = '-c ~/.config'
+
+ AssertLinter '~/.local/bin/lint-openapi', ale#Escape('~/.local/bin/lint-openapi')
+ \ . ' -c ~/.config %t'
diff --git a/test/handler/test_ibm_openapi_validator_handler.vader b/test/handler/test_ibm_openapi_validator_handler.vader
new file mode 100644
index 00000000..e136d5d2
--- /dev/null
+++ b/test/handler/test_ibm_openapi_validator_handler.vader
@@ -0,0 +1,49 @@
+Before:
+ runtime! ale_linters/openapi/ibm_validator.vim
+
+After:
+ call ale#linter#Reset()
+
+Execute(Problems should be parsed correctly for openapi-ibm-validator):
+ AssertEqual
+ \ [
+ \ {
+ \ 'lnum': 54,
+ \ 'col': 0,
+ \ 'type': 'E',
+ \ 'text': 'Items with a description must have content in it.',
+ \ },
+ \ {
+ \ 'lnum': 24,
+ \ 'col': 0,
+ \ 'type': 'W',
+ \ 'text': 'Operations must have a non-empty `operationId`.',
+ \ },
+ \ {
+ \ 'lnum': 40,
+ \ 'col': 0,
+ \ 'type': 'W',
+ \ 'text': 'operationIds must follow case convention: lower_snake_case',
+ \ },
+ \ ],
+ \ ale_linters#openapi#ibm_validator#Handle(bufnr(''), [
+ \ '',
+ \ '[Warning] No .validaterc file found. The validator will run in default mode.',
+ \ 'To configure the validator, create a .validaterc file.',
+ \ '',
+ \ 'errors',
+ \ '',
+ \ ' Message : Items with a description must have content in it.',
+ \ ' Path : paths./settings.patch.description',
+ \ ' Line : 54',
+ \ '',
+ \ 'warnings',
+ \ '',
+ \ ' Message : Operations must have a non-empty `operationId`.',
+ \ ' Path : paths./stats.get.operationId',
+ \ ' Line : 24',
+ \ '',
+ \ ' Message : operationIds must follow case convention: lower_snake_case',
+ \ ' Path : paths./settings.get.operationId',
+ \ ' Line : 40'
+ \ ])
diff --git a/test/handler/test_yamllint_handler.vader b/test/handler/test_yamllint_handler.vader
index 1aa0b9f5..dd51119c 100644
--- a/test/handler/test_yamllint_handler.vader
+++ b/test/handler/test_yamllint_handler.vader
@@ -3,7 +3,7 @@ Before:
let g:ale_warn_about_trailing_whitespace = 1
- runtime! ale_linters/yaml/yamllint.vim
+ runtime! ale/handlers/yamllint.vim
After:
Restore
@@ -29,7 +29,7 @@ Execute(Problems should be parsed correctly for yamllint):
\ 'text': 'syntax error: expected the node content, but found ''<stream end>''',
\ },
\ ],
- \ ale_linters#yaml#yamllint#Handle(bufnr(''), [
+ \ ale#handlers#yamllint#Handle(bufnr(''), [
\ 'something.yaml:1:1: [warning] missing document start "---" (document-start)',
\ 'something.yml:2:1: [error] syntax error: expected the node content, but found ''<stream end>''',
\ ])
@@ -45,7 +45,7 @@ Execute(The yamllint handler should respect ale_warn_about_trailing_whitespace):
\ 'code': 'trailing-spaces',
\ },
\ ],
- \ ale_linters#yaml#yamllint#Handle(bufnr(''), [
+ \ ale#handlers#yamllint#Handle(bufnr(''), [
\ 'something.yml:5:18: [error] trailing spaces (trailing-spaces)',
\ ])
@@ -54,6 +54,6 @@ Execute(The yamllint handler should respect ale_warn_about_trailing_whitespace):
AssertEqual
\ [
\ ],
- \ ale_linters#yaml#yamllint#Handle(bufnr(''), [
+ \ ale#handlers#yamllint#Handle(bufnr(''), [
\ 'something.yml:5:18: [error] trailing spaces (trailing-spaces)',
\ ])