add spectral linter for yaml

ci
This commit is contained in:
tatsuya 2020-12-13 17:06:23 +09:00
parent c374736301
commit 5a47d878fb
9 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,42 @@
" Author: t2h5 <https://github.com/t2h5>
" Description: Integration of Stoplight Spectral CLI with ALE.
call ale#Set('yaml_spectral_executable', 'spectral')
call ale#Set('yaml_spectral_use_global', get(g:, 'ale_use_global_executables', 0))
function! ale_linters#yaml#spectral#Handle(buffer, lines) abort
" Matches patterns like the following:
" openapi.yml:1:1 error oas3-schema "Object should have required property `info`."
" openapi.yml:1:1 warning oas3-api-servers "OpenAPI `servers` must be present and non-empty array."
let l:pattern = '\v^.*:(\d+):(\d+) (error|warning) (.*)$'
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
let l:obj = {
\ 'lnum': l:match[1] + 0,
\ 'col': l:match[2] + 0,
\ 'type': l:match[3] is# 'error' ? 'E' : 'W',
\ 'text': l:match[4],
\}
let l:code_match = matchlist(l:obj.text, '\v^(.+) "(.+)"$')
if !empty(l:code_match)
let l:obj.code = l:code_match[1]
let l:obj.text = l:code_match[2]
endif
call add(l:output, l:obj)
endfor
return l:output
endfunction
call ale#linter#Define('yaml', {
\ 'name': 'spectral',
\ 'executable': {b -> ale#node#FindExecutable(b, 'yaml_spectral', [
\ 'node_modules/.bin/spectral',
\ ])},
\ 'command': '%e lint --ignore-unknown-format -q -f text %t',
\ 'callback': 'ale_linters#yaml#spectral#Handle'
\})

View File

@ -537,6 +537,7 @@ Notes:
* `xmllint`
* YAML
* `prettier`
* `spectral`
* `swaglint`
* `yamlfix`
* `yamllint`

View File

@ -15,6 +15,38 @@ Install prettier either globally or locally: >
npm install prettier -g # global
npm install prettier # local
<
===============================================================================
spectral *ale-yaml-spectral*
Website: https://github.com/stoplightio/spectral
Installation
-------------------------------------------------------------------------------
Install spectral either globally or locally: >
npm install @stoplight/spectral -g # global
npm install @stoplight/spectral # local
<
Options
-------------------------------------------------------------------------------
g:ale_yaml_spectral_executable *g:ale_yaml_spectral_executable*
*b:ale_yaml_spectral_executable*
Type: |String|
Default: `'spectral'`
This variable can be set to change the path to spectral.
g:ale_yaml_spectral_use_global *g:ale_yaml_spectral_use_global*
*b:ale_yaml_spectral_use_global*
Type: |String|
Default: `get(g:, 'ale_use_global_executables', 0)`
See |ale-integrations-local-executables|
===============================================================================
swaglint *ale-yaml-swaglint*

View File

@ -3028,6 +3028,7 @@ documented in additional help files.
xmllint...............................|ale-xml-xmllint|
yaml....................................|ale-yaml-options|
prettier..............................|ale-yaml-prettier|
spectral..............................|ale-yaml-spectral|
swaglint..............................|ale-yaml-swaglint|
yamlfix...............................|ale-yaml-yamlfix|
yamllint..............................|ale-yaml-yamllint|

View File

@ -546,6 +546,7 @@ formatting.
* [xmllint](http://xmlsoft.org/xmllint.html)
* YAML
* [prettier](https://github.com/prettier/prettier)
* [spectral](https://github.com/stoplightio/spectral)
* [swaglint](https://github.com/byCedric/swaglint)
* [yamlfix](https://lyz-code.github.io/yamlfix)
* [yamllint](https://yamllint.readthedocs.io/)

View File

View File

@ -0,0 +1,31 @@
Before:
call ale#assert#SetUpLinterTest('yaml', 'spectral')
After:
call ale#assert#TearDownLinterTest()
Execute(The yaml spectral command callback should return the correct default string):
AssertLinter 'spectral', ale#Escape('spectral') . ' lint --ignore-unknown-format -q -f text %t'
Execute(The yaml spectral command callback should be configurable):
let g:ale_yaml_spectral_executable = '~/.local/bin/spectral'
AssertLinter '~/.local/bin/spectral',
\ ale#Escape('~/.local/bin/spectral')
\ . ' lint --ignore-unknown-format -q -f text %t'
Execute(The yaml spectral command callback should allow a global installation to be used):
let g:ale_yaml_spectral_executable = '/usr/local/bin/spectral'
let g:ale_yaml_spectral_use_global = 1
AssertLinter '/usr/local/bin/spectral',
\ ale#Escape('/usr/local/bin/spectral')
\ . ' lint --ignore-unknown-format -q -f text %t'
Execute(The yaml spectral command callback should allow a local installation to be used):
call ale#test#SetFilename('spectral_paths/openapi.yaml')
AssertLinter
\ ale#path#Simplify(g:dir . '/spectral_paths/node_modules/.bin/spectral'),
\ ale#Escape(ale#path#Simplify(g:dir . '/spectral_paths/node_modules/.bin/spectral'))
\ . ' lint --ignore-unknown-format -q -f text %t'

View File

@ -0,0 +1,52 @@
Before:
runtime ale_linters/yaml/spectral.vim
After:
call ale#linter#Reset()
Execute(spectral handler should parse lines correctly):
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'col': 1,
\ 'code': 'oas3-api-servers',
\ 'text': 'OpenAPI `servers` must be present and non-empty array.',
\ 'type': 'W'
\ },
\ {
\ 'lnum': 1,
\ 'col': 1,
\ 'code': 'oas3-schema',
\ 'text': 'Object should have required property `paths`.',
\ 'type': 'E'
\ },
\ {
\ 'lnum': 1,
\ 'col': 1,
\ 'code': 'openapi-tags',
\ 'text': 'OpenAPI object should have non-empty `tags` array.',
\ 'type': 'W'
\ },
\ {
\ 'lnum': 3,
\ 'col': 6,
\ 'code': 'info-contact',
\ 'text': 'Info object should contain `contact` object.',
\ 'type': 'W'
\ },
\ {
\ 'lnum': 3,
\ 'col': 6,
\ 'code': 'oas3-schema',
\ 'text': '`info` property should have required property `version`.',
\ 'type': 'E'
\ },
\ ],
\ ale_linters#yaml#spectral#Handle(bufnr(''), [
\ 'openapi.yml:1:1 warning oas3-api-servers "OpenAPI `servers` must be present and non-empty array."',
\ 'openapi.yml:1:1 error oas3-schema "Object should have required property `paths`."',
\ 'openapi.yml:1:1 warning openapi-tags "OpenAPI object should have non-empty `tags` array."',
\ 'openapi.yml:3:6 warning info-contact "Info object should contain `contact` object."',
\ 'openapi.yml:3:6 error oas3-schema "`info` property should have required property `version`."',
\ ])