Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit c44cb15

Browse files
committed
Cleanup: JSON decoding.
1 parent eba7ae6 commit c44cb15

File tree

2 files changed

+44
-63
lines changed

2 files changed

+44
-63
lines changed

autoload/syntastic/preprocess.vim

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,12 @@ function! syntastic#preprocess#cppcheck(errors) abort " {{{2
5959
return map(copy(a:errors), 'substitute(v:val, ''\v^\[[^]]+\]\zs( -\> \[[^]]+\])+\ze:'', "", "")')
6060
endfunction " }}}2
6161

62-
" @vimlint(EVL102, 1, l:true)
63-
" @vimlint(EVL102, 1, l:false)
64-
" @vimlint(EVL102, 1, l:null)
6562
function! syntastic#preprocess#flow(errors) abort " {{{2
66-
" JSON artifacts
67-
let true = 1
68-
let false = 0
69-
let null = ''
70-
7163
let idx = 0
72-
while idx < len(a:errors) && a:errors[idx][0] != '{'
64+
while idx < len(a:errors) && a:errors[idx][0] !=# '{'
7365
let idx += 1
7466
endwhile
75-
76-
" A hat tip to Marc Weber for this trick
77-
" http://stackoverflow.com/questions/17751186/iterating-over-a-string-in-vimscript-or-parse-a-json-file/19105763#19105763
78-
try
79-
let errs = eval(join(a:errors[idx :], ''))
80-
catch
81-
let errs = {}
82-
endtry
67+
let errs = s:_decode_JSON(join(a:errors[idx :], ''))
8368

8469
let out = []
8570
if type(errs) == type({}) && has_key(errs, 'errors') && type(errs['errors']) == type([])
@@ -108,25 +93,22 @@ function! syntastic#preprocess#flow(errors) abort " {{{2
10893

10994
call add(out, msg)
11095
catch /\m^Vim\%((\a\+)\)\=:E716/
111-
call syntastic#log#warn('checker javascript/flow: unknown error format')
96+
call syntastic#log#warn('checker javascript/flow: unrecognized error format')
11297
let out = []
11398
break
11499
endtry
115100
else
116-
call syntastic#log#warn('checker javascript/flow: unknown error format')
101+
call syntastic#log#warn('checker javascript/flow: unrecognized error format')
117102
let out = []
118103
break
119104
endif
120105
endfor
121106
else
122-
call syntastic#log#warn('checker javascript/flow: unknown error format')
107+
call syntastic#log#warn('checker javascript/flow: unrecognized error format')
123108
endif
124109

125110
return out
126111
endfunction " }}}2
127-
" @vimlint(EVL102, 0, l:true)
128-
" @vimlint(EVL102, 0, l:false)
129-
" @vimlint(EVL102, 0, l:null)
130112

131113
function! syntastic#preprocess#iconv(errors) abort " {{{2
132114
return
@@ -152,22 +134,8 @@ function! syntastic#preprocess#perl(errors) abort " {{{2
152134
return syntastic#util#unique(out)
153135
endfunction " }}}2
154136

155-
" @vimlint(EVL102, 1, l:true)
156-
" @vimlint(EVL102, 1, l:false)
157-
" @vimlint(EVL102, 1, l:null)
158137
function! syntastic#preprocess#prospector(errors) abort " {{{2
159-
" JSON artifacts
160-
let true = 1
161-
let false = 0
162-
let null = ''
163-
164-
" A hat tip to Marc Weber for this trick
165-
" http://stackoverflow.com/questions/17751186/iterating-over-a-string-in-vimscript-or-parse-a-json-file/19105763#19105763
166-
try
167-
let errs = eval(join(a:errors, ''))
168-
catch
169-
let errs = {}
170-
endtry
138+
let errs = s:_decode_JSON(join(a:errors, ''))
171139

172140
let out = []
173141
if type(errs) == type({}) && has_key(errs, 'messages')
@@ -189,26 +157,23 @@ function! syntastic#preprocess#prospector(errors) abort " {{{2
189157

190158
call add(out, msg)
191159
catch /\m^Vim\%((\a\+)\)\=:E716/
192-
call syntastic#log#warn('checker python/prospector: unknown error format')
160+
call syntastic#log#warn('checker python/prospector: unrecognized error format')
193161
let out = []
194162
break
195163
endtry
196164
else
197-
call syntastic#log#warn('checker python/prospector: unknown error format')
165+
call syntastic#log#warn('checker python/prospector: unrecognized error format')
198166
let out = []
199167
break
200168
endif
201169
endfor
202170
else
203-
call syntastic#log#warn('checker python/prospector: unknown error format')
171+
call syntastic#log#warn('checker python/prospector: unrecognized error format')
204172
endif
205173
endif
206174

207175
return out
208176
endfunction " }}}2
209-
" @vimlint(EVL102, 0, l:true)
210-
" @vimlint(EVL102, 0, l:false)
211-
" @vimlint(EVL102, 0, l:null)
212177

213178
function! syntastic#preprocess#rparse(errors) abort " {{{2
214179
let errlist = copy(a:errors)
@@ -268,22 +233,8 @@ function! syntastic#preprocess#validator(errors) abort " {{{2
268233
return out
269234
endfunction " }}}2
270235

271-
" @vimlint(EVL102, 1, l:true)
272-
" @vimlint(EVL102, 1, l:false)
273-
" @vimlint(EVL102, 1, l:null)
274236
function! syntastic#preprocess#vint(errors) abort " {{{2
275-
" JSON artifacts
276-
let true = 1
277-
let false = 0
278-
let null = ''
279-
280-
" A hat tip to Marc Weber for this trick
281-
" http://stackoverflow.com/questions/17751186/iterating-over-a-string-in-vimscript-or-parse-a-json-file/19105763#19105763
282-
try
283-
let errs = eval(join(a:errors, ''))
284-
catch
285-
let errs = []
286-
endtry
237+
let errs = s:_decode_JSON(join(a:errors, ''))
287238

288239
let out = []
289240
if type(errs) == type([])
@@ -300,22 +251,52 @@ function! syntastic#preprocess#vint(errors) abort " {{{2
300251

301252
call add(out, msg)
302253
catch /\m^Vim\%((\a\+)\)\=:E716/
303-
call syntastic#log#warn('checker vim/vint: unknown error format')
254+
call syntastic#log#warn('checker vim/vint: unrecognized error format')
304255
let out = []
305256
break
306257
endtry
307258
else
308-
call syntastic#log#warn('checker vim/vint: unknown error format')
259+
call syntastic#log#warn('checker vim/vint: unrecognized error format')
309260
let out = []
310261
break
311262
endif
312263
endfor
313264
else
314-
call syntastic#log#warn('checker vim/vint: unknown error format')
265+
call syntastic#log#warn('checker vim/vint: unrecognized error format')
315266
endif
316267

317268
return out
318269
endfunction " }}}2
270+
271+
" }}}1
272+
273+
" Private functions {{{1
274+
275+
" @vimlint(EVL102, 1, l:true)
276+
" @vimlint(EVL102, 1, l:false)
277+
" @vimlint(EVL102, 1, l:null)
278+
function! s:_decode_JSON(json) abort " {{{2
279+
" The following is inspired by https://github.com/MarcWeber/vim-addon-manager and
280+
" http://stackoverflow.com/questions/17751186/iterating-over-a-string-in-vimscript-or-parse-a-json-file/19105763#19105763
281+
" A hat tip to Marc Weber for this trick
282+
if substitute(a:json, '\v\"%(\\.|[^"\\])*\"|true|false|null|[+-]?\d+%(\.\d+%([Ee][+-]?\d+)?)?', '', 'g') !~# "[^,:{}[\\] \t]"
283+
" JSON artifacts
284+
let true = 1
285+
let false = 0
286+
let null = ''
287+
288+
try
289+
let object = eval(a:json)
290+
catch
291+
" malformed JSON
292+
let object = ''
293+
endtry
294+
else
295+
let object = ''
296+
endif
297+
298+
return object
299+
endfunction " }}}2
319300
" @vimlint(EVL102, 0, l:true)
320301
" @vimlint(EVL102, 0, l:false)
321302
" @vimlint(EVL102, 0, l:null)

plugin/syntastic.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if has('reltime')
1919
lockvar! g:_SYNTASTIC_START
2020
endif
2121

22-
let g:_SYNTASTIC_VERSION = '3.6.0-122'
22+
let g:_SYNTASTIC_VERSION = '3.6.0-123'
2323
lockvar g:_SYNTASTIC_VERSION
2424

2525
" Sanity checks {{{1

0 commit comments

Comments
 (0)