From 5ab35a7a30e4ce000840f9caba3deabf86d2d030 Mon Sep 17 00:00:00 2001 From: 0xHyoga <0xhyoga@cygnusdao.finance> Date: Tue, 27 Jun 2023 11:44:20 +0200 Subject: [PATCH] Update cairo linter to Cairo 1.0 (#4530) * update cairo linter * new cairo handler test * add another handler instead of replacing --- ale_linters/cairo/sierra.vim | 54 ++++++++++++++++++++++++++ ale_linters/cairo/starknet.vim | 4 +- test/handler/test_sierra_handler.vader | 20 ++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 ale_linters/cairo/sierra.vim create mode 100644 test/handler/test_sierra_handler.vader diff --git a/ale_linters/cairo/sierra.vim b/ale_linters/cairo/sierra.vim new file mode 100644 index 00000000..06eb87f4 --- /dev/null +++ b/ale_linters/cairo/sierra.vim @@ -0,0 +1,54 @@ +" Author: 0xHyoga <0xHyoga@gmx.com> +" Description: Report Starknet compile to sierra errors in cairo 1.0 code + +call ale#Set('cairo_sierra_executable', 'starknet-compile') +call ale#Set('cairo_sierra_options', '') + +function! ale_linters#cairo#sierra#Handle(buffer, lines) abort + " Matches patterns like the following: + " Error: Expected ';' but got '(' + " --> /path/to/file/file.cairo:1:10:) + let l:pattern = '\v(error|warning): (.*)$' + let l:line_and_column_pattern = '\v\.cairo:(\d+):(\d+)' + let l:output = [] + + for l:line in a:lines + let l:match = matchlist(l:line, l:pattern) + + if len(l:match) == 0 + let l:match = matchlist(l:line, l:line_and_column_pattern) + + if len(l:match) > 0 + let l:index = len(l:output) - 1 + let l:output[l:index]['lnum'] = l:match[1] + 0 + let l:output[l:index]['col'] = l:match[2] + 0 + endif + else + let l:isError = l:match[1] is? 'Error' + + call add(l:output, { + \ 'lnum': 0, + \ 'col': 0, + \ 'text': l:match[2], + \ 'type': l:isError ? 'E' : 'W', + \}) + endif + endfor + + return l:output +endfunction + +function! ale_linters#cairo#sierra#GetCommand(buffer) abort + let l:executable = ale#Var(a:buffer, 'cairo_sierra_executable') + + return l:executable . ale#Pad(ale#Var(a:buffer, 'cairo_sierra_options')) . ' %s' +endfunction + +call ale#linter#Define('cairo', { +\ 'name': 'sierra', +\ 'executable': {b -> ale#Var(b, 'cairo_sierra_executable')}, +\ 'command': function('ale_linters#cairo#sierra#GetCommand'), +\ 'callback': 'ale_linters#cairo#sierra#Handle', +\ 'output_stream': 'stderr', +\}) + diff --git a/ale_linters/cairo/starknet.vim b/ale_linters/cairo/starknet.vim index 990bda6d..d471cc89 100644 --- a/ale_linters/cairo/starknet.vim +++ b/ale_linters/cairo/starknet.vim @@ -1,5 +1,6 @@ " Author: 0xHyoga <0xHyoga@gmx.com> -" Description: Report starknet-compile errors in cairo code +" Description: Report starknet-compile errors in cairo code (pre-starknet +" 1.0). This is deprecated but kept for backwards compatability. call ale#Set('cairo_starknet_executable', 'starknet-compile') call ale#Set('cairo_starknet_options', '') @@ -35,3 +36,4 @@ call ale#linter#Define('cairo', { \ 'callback': 'ale_linters#cairo#starknet#Handle', \ 'output_stream': 'stderr', \}) + diff --git a/test/handler/test_sierra_handler.vader b/test/handler/test_sierra_handler.vader new file mode 100644 index 00000000..889ac49c --- /dev/null +++ b/test/handler/test_sierra_handler.vader @@ -0,0 +1,20 @@ +Before: + runtime ale_linters/cairo/sierra.vim + +After: + call ale#linter#Reset() + +Execute(The starknet handler should handle error messages correctly): + AssertEqual + \ [ + \ { + \ 'lnum': 16, + \ 'col': 25, + \ 'text': 'Plugin diagnostic: Type not found', + \ 'type': 'E', + \ }, + \ ], + \ ale_linters#cairo#sierra#Handle(bufnr(''), [ + \ 'error: Plugin diagnostic: Type not found', + \ ' --> lib.cairo:16:25', + \ ])