Monday, May 20, 2024
126
rated 0 times [  132] [ 6]  / answers: 1 / hits: 6614  / 11 Years ago, mon, december 30, 2013, 12:00:00

I have a project of 100+ javascript files each with 1-5 missing semicolons according to JSHint (as a result multiple people working on the project with different conventions).



I'd like to bulk fix everything as it's not practical to fix each individually. I've found this: https://github.com/jshint/fixmyjs but I don't see a way only fix semicolons without 'fixing' other things (tabs -> spaces, for example).



Does anyone know of way to do this? I'm comfortable with the possibility that it might cause breakage/introduce bugs. I've look over the errors and they look pretty routine.


More From » command-line

 Answers
1

I really hope you like this as a solution. Be vary careful that you verify with jshint again after you've fixed the issues. And out of curiosity, how did you manage to get so many broken javascript files?



#!/bin/sh
function fixFile {
for i in `jshint $1 | grep -i Missing semicolon
| sed -e 's/([^0-9]*)([0-9]*)(.*$)/2/'`;
do
sed -i $1 -e $i's/(s*)$/;/'
done
}

fixFile $1


The above uses jshint to produce some error lines, greps them for the missing semicolon errors only, extracts the line number of each error, then seds the file in place on that line to remove any trailing whitespace and replace it with a semicolon.



The file...



var a = 5, c = 4

function helloWorld() {
if (this == doesntmakesense)
console.log('hello' + 'world');
}

console.log(a+c);
console.log('finished')


...becomes...



var a = 5, c = 4;

function helloWorld() {
if (this == doesntmakesense)
console.log('hello' + 'world');
}

console.log(a+c);
console.log('finished');


Where petty semantic errors are ignored, and only semicolons treated.



I'd save the bash script as something like fixFile.sh and then run find . -name *.js -exec ./fixFile.sh {} ;



But please commit beforehand. All commands are run at your own risk ;)


[#49123] Sunday, December 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;