📜 ⬆️ ⬇️

Remove extra commas from javascript code

When you write on js, there are often situations when here, then there are lines of the form ",)" or ",}" or ",]". In ff, chrome code with such fragments works, but not in IE.

Vim medicine:

autocmd BufWritePre *.js :%s/\(.*\),\(\s*\n*\s*\)\(\}\|\]\|)\)/\1\2\3/e

The regular is written on the knee, but it works and fixes most of the problems with commas on the code.
')
Additions are welcome!

UPD:

Although this approach fixes most of the ordinary cases, it can also spoil the code with regulars or, for example, strings:

var a = "[,]";
var a = /[,]/;


will turn into:

var a = "[]";
var a = /[]/;


If your code has regulars and / or strings with similar constructions, it is better to use jslint for validation. You can use github.com/hallettj/jslint.vim for VIM

Source: https://habr.com/ru/post/114435/


All Articles