Last active
November 18, 2015 03:09
-
-
Save airportyh/996c1f1afe7105e3eb07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var falafel = require('falafel'); | |
var fs = require('fs'); | |
var filePaths = process.argv.slice(2); | |
console.log('Processing files'); | |
console.log(filePaths.join('\n')); | |
filePaths.forEach(function(filepath) { | |
rewrite(filepath); | |
console.log('Rewrote', filepath); | |
}); | |
function rewrite(filepath) { | |
var code = fs.readFileSync(filepath).toString(); | |
var newCode = falafel(code, function(node) { | |
if (node.type === 'ExpressionStatement' || | |
node.type === 'ReturnStatement') { | |
ensureSemiColon(node); | |
} else if (node.type === 'VariableDeclaration') { | |
if (node.parent.type !== 'ForInStatement' && | |
node.parent.type !== 'ForStatement') { | |
ensureSemiColon(node); | |
} | |
} else if (node.type === 'ForStatement' || | |
node.type === 'ForInStatement' || | |
node.type === 'IfStatement' || | |
node.type === 'FunctionDeclaration' || | |
node.type === 'FunctionExpression') { | |
var src = node.source(); | |
src = src.replace(/\)\{/, ') {'); | |
src = src.replace(/\}else/g, '} else'); | |
src = src.replace(/else{/g, 'else {'); | |
node.update(src); | |
} | |
}); | |
fs.writeFileSync(filepath, newCode); | |
} | |
function ensureSemiColon(node) { | |
var src = node.source(); | |
if (src[src.length - 1] !== ';') { | |
node.update(node.source() + ';'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment