Skip to content

Instantly share code, notes, and snippets.

@airportyh
Last active November 18, 2015 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airportyh/996c1f1afe7105e3eb07 to your computer and use it in GitHub Desktop.
Save airportyh/996c1f1afe7105e3eb07 to your computer and use it in GitHub Desktop.
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