|
| 1 | + |
| 2 | +function formatSource(src) { |
| 3 | + var literals = {} |
| 4 | + , idc = 0; |
| 5 | + |
| 6 | + function stash(literal) { |
| 7 | + var id = idc++; |
| 8 | + literals[id] = literal; |
| 9 | + return "__unstash(" + id + ")"; |
| 10 | + } |
| 11 | + |
| 12 | + function unstash(macro) { |
| 13 | + return literals[macro.replace(/[^0-9]+/g, '')]; |
| 14 | + } |
| 15 | + |
| 16 | + return ( |
| 17 | + src |
| 18 | + |
| 19 | + // stash string literals, comments and regexps |
| 20 | + .replace(/\/\/[^\n]*/g, stash) |
| 21 | + .replace(/\/\*[^]*?\*\//g, stash) |
| 22 | + |
| 23 | + .replace(/'[^\n']*?(\\'[^\n']*?)*'/g, stash) |
| 24 | + .replace(/"[^\n"]*?(\\"[^\n"]*?)*"/g, stash) |
| 25 | + .replace(/\/[^\n\/]*?(\\\/[^\n\/]*?)*\/[a-z]*/g, stash) |
| 26 | + |
| 27 | + .replace(/\/\/[^\n]*/g, stash) |
| 28 | + .replace(/\/\*[^]*?\*\//g, stash) |
| 29 | + |
| 30 | + .replace(/'[^\n']*?(\\'[^\n']*?)*'/g, stash) |
| 31 | + .replace(/"[^\n"]*?(\\"[^\n"]*?)*"/g, stash) |
| 32 | + .replace(/\/[^\n\/]*?(\\\/[^\n\/]*?)*\/[a-z]*/g, stash) |
| 33 | + |
| 34 | + // assume 4 space indent style |
| 35 | + .replace(/ /g, "\t") |
| 36 | + .replace(/ +/g, " ") |
| 37 | + |
| 38 | + // remove spaces around parens |
| 39 | + .replace(/ *([[({})\]]) */g, "$1") |
| 40 | + .replace(/ *([[({})\]]) */g, "$1") |
| 41 | + .replace(/ *([[({})\]]) */g, "$1") |
| 42 | + |
| 43 | + // till unstash |
| 44 | + |
| 45 | + // keep spaces around keywords/identifiers |
| 46 | + .replace(/(if|else|while|for|return|do)([({[])/g, "$1 $2") |
| 47 | + .replace(/([)}])([a-zA-Z])/g, "$1 $2") |
| 48 | + |
| 49 | + // fix ){ |
| 50 | + .replace(/\)\{/g, ") {") |
| 51 | + // fix one liner functions |
| 52 | + .replace(/\{([\S])/g, "{ $1") |
| 53 | + .replace(/([\S])\}/g, "$1 }") |
| 54 | + |
| 55 | + // fix { } |
| 56 | + .replace(/\{ \}/g, "{}") |
| 57 | + |
| 58 | + // move opening { on same line |
| 59 | + .replace(/\n\s*\{\s*\n/g, " {\n") |
| 60 | + |
| 61 | + // move opening ( on same line |
| 62 | + .replace(/\n\s*\(\s*\n/g, "(\n") |
| 63 | + |
| 64 | + // fix : spacing |
| 65 | + .replace(/\s*:\s*/g, ": ") |
| 66 | + |
| 67 | + // fix ternary operator : spacing |
| 68 | + .replace(/(\?[^\n:]+):/g, "$1 :") |
| 69 | + |
| 70 | + // fix operator spacing |
| 71 | + .replace(/\s*(\+\+|--|\|\||&&|\+=|-=|\*=|\/=|\+|-|<=|>=|<+|>+|!==|===|=|%)[^\S\n]*/g, " $1 ") |
| 72 | + .replace(/\s*(\+\+|--|\|\||&&|\+=|-=|\*=|\/=|\+|-|<=|>=|<+|>+|!==|===|=|%)[^\S\n]*/g, " $1 ") |
| 73 | + .replace(/\s*(\+\+|--) */g, "$1") |
| 74 | + |
| 75 | + // unstash stuff |
| 76 | + .replace(/__unstash\([0-9]+\)/g, unstash) |
| 77 | + .replace(/__unstash\([0-9]+\)/g, unstash) |
| 78 | + .replace(/__unstash\([0-9]+\)/g, unstash) |
| 79 | + .replace(/__unstash\([0-9]+\)/g, unstash) |
| 80 | + |
| 81 | + // from here on only patterns that include newlines are safe |
| 82 | + |
| 83 | + // fix var indent and move var , on next line |
| 84 | + .replace(/var [^\;]+;/g, function(vars) { |
| 85 | + return vars.replace(/,(\n(?:\t*\n)*\t+)/g, "$1, "); |
| 86 | + }) |
| 87 | + |
| 88 | + // move obj literal commas for comma-first style |
| 89 | + .replace(/,(\n(?:\t*(?:\/\/[^\n]*)?\n)*)\t([a-zA-Z$_])/g, "$1, $2") |
| 90 | + |
| 91 | + // fix non-whitespace followed commas |
| 92 | + .replace(/,([\S])/g, ", $1") |
| 93 | + |
| 94 | + // indent = 2 spaces |
| 95 | + .replace(/\t/g, " ") |
| 96 | + |
| 97 | + // fix the stupid comment style |
| 98 | + .replace(/ \/\/\/\/ */g, "// ") |
| 99 | + |
| 100 | + // remove more than two line long instances of whitespace |
| 101 | + .replace(/\n\n\n\n+/g, "\n\n\n") |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +var fs = require('fs'); |
| 106 | +function formatFile(filename) { |
| 107 | + fs.readFile(filename + '.unfmt.js', 'utf8', function(err, src) { |
| 108 | + if (err) |
| 109 | + throw err; |
| 110 | + |
| 111 | + fs.writeFile(filename + '.js', formatSource(src), function(err) { |
| 112 | + if (err) |
| 113 | + throw err; |
| 114 | + }); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +var filenames = ["main", "lib/backend", "lib/connection", "lib/helpers"]; |
| 119 | +filenames.forEach(formatFile); |
0 commit comments