diff options
author | Sam <sam.saffron@gmail.com> | 2024-02-23 15:30:12 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-23 13:30:12 +0900 |
commit | 5e8904cd3da4565130c09b77179ae7dddd07358f (patch) | |
tree | ef10ca0c59d3ab840e240769644cc6b028dc0fe4 | |
parent | f38a80217282005e15305bfa37d051bb580b63a1 (diff) | |
download | ale-5e8904cd3da4565130c09b77179ae7dddd07358f.zip |
Add support for gjs template linting using embertemplate lint (#4653)
* super hacky way to get ember template lint to work on gjs files
* Clean up code so we use a handler which means we reuse all the config
also moves handler to the glimmer directory so it only fires
for gjs files
* fix tests
-rw-r--r-- | ale_linters/glimmer/embertemplatelint.vim | 6 | ||||
-rw-r--r-- | ale_linters/handlebars/embertemplatelint.vim | 60 | ||||
-rw-r--r-- | autoload/ale/handlers/embertemplatelint.vim | 66 | ||||
-rw-r--r-- | test/handler/test_embertemplatelint_handler.vader | 10 |
4 files changed, 79 insertions, 63 deletions
diff --git a/ale_linters/glimmer/embertemplatelint.vim b/ale_linters/glimmer/embertemplatelint.vim new file mode 100644 index 00000000..10b54bb4 --- /dev/null +++ b/ale_linters/glimmer/embertemplatelint.vim @@ -0,0 +1,6 @@ +" Author: Sam Saffron <sam.saffron@gmail.com> +" Description: Ember-template-lint for checking GJS (Glimmer JS) files + +scriptencoding utf-8 + +call ale#handlers#embertemplatelint#DefineLinter('glimmer') diff --git a/ale_linters/handlebars/embertemplatelint.vim b/ale_linters/handlebars/embertemplatelint.vim index 14fa3b2e..62b5db03 100644 --- a/ale_linters/handlebars/embertemplatelint.vim +++ b/ale_linters/handlebars/embertemplatelint.vim @@ -1,62 +1,6 @@ " Author: Adrian Zalewski <aazalewski@hotmail.com> " Description: Ember-template-lint for checking Handlebars files -call ale#Set('handlebars_embertemplatelint_executable', 'ember-template-lint') -call ale#Set('handlebars_embertemplatelint_use_global', get(g:, 'ale_use_global_executables', 0)) +scriptencoding utf-8 -function! ale_linters#handlebars#embertemplatelint#GetExecutable(buffer) abort - return ale#path#FindExecutable(a:buffer, 'handlebars_embertemplatelint', [ - \ 'node_modules/.bin/ember-template-lint', - \]) -endfunction - -function! ale_linters#handlebars#embertemplatelint#GetCommand(buffer, version) abort - if ale#semver#GTE(a:version, [4, 0, 0]) - " --json was removed in favor of --format=json in ember-template-lint@4.0.0 - return '%e --format=json --filename %s' - endif - - return '%e --json --filename %s' -endfunction - -function! ale_linters#handlebars#embertemplatelint#GetCommandWithVersionCheck(buffer) abort - return ale#semver#RunWithVersionCheck( - \ a:buffer, - \ ale_linters#handlebars#embertemplatelint#GetExecutable(a:buffer), - \ '%e --version', - \ function('ale_linters#handlebars#embertemplatelint#GetCommand'), - \) -endfunction - -function! ale_linters#handlebars#embertemplatelint#Handle(buffer, lines) abort - let l:output = [] - let l:json = ale#util#FuzzyJSONDecode(a:lines, {}) - - for l:error in get(values(l:json), 0, []) - if has_key(l:error, 'fatal') - call add(l:output, { - \ 'lnum': get(l:error, 'line', 1), - \ 'col': get(l:error, 'column', 1), - \ 'text': l:error.message, - \ 'type': l:error.severity == 1 ? 'W' : 'E', - \}) - else - call add(l:output, { - \ 'lnum': l:error.line, - \ 'col': l:error.column, - \ 'text': l:error.rule . ': ' . l:error.message, - \ 'type': l:error.severity == 1 ? 'W' : 'E', - \}) - endif - endfor - - return l:output -endfunction - -call ale#linter#Define('handlebars', { -\ 'name': 'embertemplatelint', -\ 'aliases': ['ember-template-lint'], -\ 'executable': function('ale_linters#handlebars#embertemplatelint#GetExecutable'), -\ 'command': function('ale_linters#handlebars#embertemplatelint#GetCommandWithVersionCheck'), -\ 'callback': 'ale_linters#handlebars#embertemplatelint#Handle', -\}) +call ale#handlers#embertemplatelint#DefineLinter('handlebars') diff --git a/autoload/ale/handlers/embertemplatelint.vim b/autoload/ale/handlers/embertemplatelint.vim new file mode 100644 index 00000000..d2e83400 --- /dev/null +++ b/autoload/ale/handlers/embertemplatelint.vim @@ -0,0 +1,66 @@ +" Author: Adrian Zalewski <aazalewski@hotmail.com> +" Description: Ember-template-lint for checking Handlebars files + +function! ale#handlers#embertemplatelint#GetExecutable(buffer) abort + return ale#path#FindExecutable(a:buffer, 'handlebars_embertemplatelint', [ + \ 'node_modules/.bin/ember-template-lint', + \]) +endfunction + +function! ale#handlers#embertemplatelint#GetCommand(buffer, version) abort + if ale#semver#GTE(a:version, [4, 0, 0]) + " --json was removed in favor of --format=json in ember-template-lint@4.0.0 + return '%e --format=json --filename %s' + endif + + return '%e --json --filename %s' +endfunction + +function! ale#handlers#embertemplatelint#GetCommandWithVersionCheck(buffer) abort + return ale#semver#RunWithVersionCheck( + \ a:buffer, + \ ale#handlers#embertemplatelint#GetExecutable(a:buffer), + \ '%e --version', + \ function('ale#handlers#embertemplatelint#GetCommand'), + \) +endfunction + +function! ale#handlers#embertemplatelint#Handle(buffer, lines) abort + let l:output = [] + let l:json = ale#util#FuzzyJSONDecode(a:lines, {}) + + for l:error in get(values(l:json), 0, []) + if has_key(l:error, 'fatal') + call add(l:output, { + \ 'lnum': get(l:error, 'line', 1), + \ 'col': get(l:error, 'column', 1), + \ 'text': l:error.message, + \ 'type': l:error.severity == 1 ? 'W' : 'E', + \}) + else + call add(l:output, { + \ 'lnum': l:error.line, + \ 'col': l:error.column, + \ 'text': l:error.rule . ': ' . l:error.message, + \ 'type': l:error.severity == 1 ? 'W' : 'E', + \}) + endif + endfor + + return l:output +endfunction + +function! ale#handlers#embertemplatelint#DefineLinter(filetype) abort + call ale#Set('handlebars_embertemplatelint_executable', 'ember-template-lint') + call ale#Set('handlebars_embertemplatelint_use_global', get(g:, 'ale_use_global_executables', 0)) + + call ale#linter#Define(a:filetype, { + \ 'name': 'embertemplatelint', + \ 'aliases': ['ember-template-lint'], + \ 'executable': function('ale#handlers#embertemplatelint#GetExecutable'), + \ 'command': function('ale#handlers#embertemplatelint#GetCommandWithVersionCheck'), + \ 'callback': 'ale#handlers#embertemplatelint#Handle', + \}) +endfunction + + diff --git a/test/handler/test_embertemplatelint_handler.vader b/test/handler/test_embertemplatelint_handler.vader index 97ca4390..d5394baf 100644 --- a/test/handler/test_embertemplatelint_handler.vader +++ b/test/handler/test_embertemplatelint_handler.vader @@ -1,6 +1,6 @@ " Author: Adrian Zalewski <aazalewski@hotmail.com> Before: - runtime ale_linters/handlebars/embertemplatelint.vim + runtime autoload/ale/handlers/embertemplatelint.vim After: call ale#linter#Reset() @@ -44,7 +44,7 @@ Execute(The ember-template-lint handler should parse lines correctly): \ 'type': 'W', \ }, \ ], - \ ale_linters#handlebars#embertemplatelint#Handle(347, input_lines) + \ ale#handlers#embertemplatelint#Handle(347, input_lines) Execute(The ember-template-lint handler should handle template parsing error correctly): let input_lines = split('{ @@ -70,12 +70,12 @@ Execute(The ember-template-lint handler should handle template parsing error cor \ 'type': 'E', \ }, \ ], - \ ale_linters#handlebars#embertemplatelint#Handle(347, input_lines) + \ ale#handlers#embertemplatelint#Handle(347, input_lines) Execute(The ember-template-lint handler should handle no lint errors/warnings): AssertEqual \ [], - \ ale_linters#handlebars#embertemplatelint#Handle(347, []) + \ ale#handlers#embertemplatelint#Handle(347, []) AssertEqual \ [], - \ ale_linters#handlebars#embertemplatelint#Handle(347, ['{}']) + \ ale#handlers#embertemplatelint#Handle(347, ['{}']) |