|
| 1 | +fs = require 'fs' |
| 2 | +vm = require 'vm' |
| 3 | +util = require 'util' |
| 4 | +parser = require("uglify-js").parser |
| 5 | +uglify = require("uglify-js").uglify |
| 6 | +ctx = vm.createContext() |
| 7 | +vm.runInContext fs.readFileSync(__dirname + "/jslint.js"), ctx |
| 8 | +JSLINT = ctx.JSLINT |
| 9 | + |
| 10 | +script = fs.readFileSync process.argv[2], 'utf8' |
| 11 | + |
| 12 | +details = null |
| 13 | +re = /\/\*\!(?:FLO|P)IM_PLUGIN([\s\S]+)\*\// |
| 14 | +script = script.replace re, (header) -> |
| 15 | + matches = header.match re |
| 16 | + if matches |
| 17 | + try |
| 18 | + details = JSON.parse matches[1] |
| 19 | + return "" |
| 20 | +unless details? |
| 21 | + process.exit 1 |
| 22 | +console.log details |
| 23 | + |
| 24 | +globals = ['document','twttr'] |
| 25 | +if details.access? and Array.isArray details.access |
| 26 | + for a in details.access |
| 27 | + if typeof a is 'string' and a.match /^[a-zA-Z]+$/ |
| 28 | + globals.push a |
| 29 | + |
| 30 | +walk = (ast) -> |
| 31 | + if Array.isArray(ast) and ast.length > 0 |
| 32 | + type = ast[0] |
| 33 | + if type is 'toplevel' |
| 34 | + for ast2, i in ast[1] |
| 35 | + ast[1][i] = walk ast2 |
| 36 | + else if type is 'var' |
| 37 | + #skip |
| 38 | + else if type is 'new' |
| 39 | + #skip |
| 40 | + ast[1] = walk ast[1] |
| 41 | + for ast2,i in ast[2] |
| 42 | + ast[2][i] = walk ast[2][i] |
| 43 | + if ast[2].length is 0 |
| 44 | + ast[2].push ['name','null'] |
| 45 | + else if type is 'num' |
| 46 | + #skip |
| 47 | + else if type is 'regexp' |
| 48 | + #skip |
| 49 | + else if type is 'unary-prefix' |
| 50 | + ast[2] = walk ast[2] |
| 51 | + else if type is 'name' |
| 52 | + if globals.indexOf(ast[1]) isnt -1 |
| 53 | + ast = [ |
| 54 | + 'call' |
| 55 | + [ |
| 56 | + 'dot' |
| 57 | + ['name','lib'] |
| 58 | + 'global' |
| 59 | + ] |
| 60 | + [['string',ast[1]]] |
| 61 | + ] |
| 62 | + #skip |
| 63 | + else if type is 'sub' |
| 64 | + ast[1] = walk ast[1] |
| 65 | + base = ast[1] |
| 66 | + property = walk ast[2] |
| 67 | + if property[0] isnt 'num' |
| 68 | + ast = [ |
| 69 | + 'call' |
| 70 | + [ |
| 71 | + 'dot', |
| 72 | + ['name', 'ADSAFE'], |
| 73 | + 'get' |
| 74 | + ] |
| 75 | + [ |
| 76 | + base |
| 77 | + property |
| 78 | + ] |
| 79 | + ] |
| 80 | + else if type is 'return' |
| 81 | + ast[1] = walk ast[1] |
| 82 | + else if type is 'binary' |
| 83 | + operation = ast[1] |
| 84 | + ast[2] = walk ast[2] |
| 85 | + ast[3] = walk ast[3] |
| 86 | + else if type is 'call' |
| 87 | + ast[1] = walk ast[1] |
| 88 | + funcName = ast[1] |
| 89 | + params = ast[2] |
| 90 | + for ast2, i in ast[2] |
| 91 | + ast[2][i] = walk ast2 |
| 92 | + if funcName[0] is 'name' |
| 93 | + if funcName[1] is 'parseInt' |
| 94 | + if params.length is 1 |
| 95 | + ast[2].push ['num', 10] |
| 96 | + else if funcName[1] is 'setTimeout' |
| 97 | + ast[1] = [ |
| 98 | + 'dot' |
| 99 | + ['name', 'ADSAFE'] |
| 100 | + 'later' |
| 101 | + ] |
| 102 | + else if type is 'if' |
| 103 | + condition = ast[1] |
| 104 | + ast[1] = walk ast[1] |
| 105 | + ast[2] = walk ast[2] |
| 106 | + ast[3] = walk ast[3] |
| 107 | + else if type is 'while' |
| 108 | + #skip |
| 109 | + ast[2] = walk ast[2] |
| 110 | + if ast[1][0] is 'assign' |
| 111 | + # Rewrite this as a for loop |
| 112 | + if ast[2][0] isnt 'block' |
| 113 | + ast[2] = [ |
| 114 | + 'block' |
| 115 | + [ |
| 116 | + ast[2] |
| 117 | + ] |
| 118 | + ] |
| 119 | + ast[2][1].unshift [ |
| 120 | + 'stat' |
| 121 | + ast[1] |
| 122 | + ] |
| 123 | + ast[2][1].push [ |
| 124 | + 'if' |
| 125 | + [ |
| 126 | + 'unary-prefix' |
| 127 | + '!' |
| 128 | + ast[1][2] |
| 129 | + ] |
| 130 | + [ |
| 131 | + 'block' |
| 132 | + [ |
| 133 | + [ |
| 134 | + 'break' |
| 135 | + null |
| 136 | + ] |
| 137 | + ] |
| 138 | + ] |
| 139 | + undefined |
| 140 | + ] |
| 141 | + ast = [ |
| 142 | + 'for' |
| 143 | + null |
| 144 | + null |
| 145 | + null |
| 146 | + ast[2] |
| 147 | + ] |
| 148 | + else if type is 'block' |
| 149 | + for ast2, i in ast[1] |
| 150 | + ast[1][i] = walk ast2 |
| 151 | + else if type is 'function' |
| 152 | + #skip ('function', name, arguments, ast) |
| 153 | + for ast2, i in ast[3] |
| 154 | + ast[3][i] = walk ast2 |
| 155 | + else if type is 'stat' |
| 156 | + ast[1] = walk ast[1] |
| 157 | + else if type is 'assign' |
| 158 | + if ast[2][0] is 'dot' |
| 159 | + # Base: |
| 160 | + base = walk ast[2][1] |
| 161 | + # property: |
| 162 | + property = ast[2][2] |
| 163 | + # value |
| 164 | + value = walk ast[3] |
| 165 | + ast = [ |
| 166 | + 'call' |
| 167 | + [ |
| 168 | + 'dot', |
| 169 | + ['name', 'ADSAFE'], |
| 170 | + 'set' |
| 171 | + ] |
| 172 | + [ |
| 173 | + base |
| 174 | + ['string', property] |
| 175 | + value |
| 176 | + ] |
| 177 | + ] |
| 178 | + else |
| 179 | + ast[2] = walk ast[2] |
| 180 | + ast[3] = walk ast[3] |
| 181 | + else if type is 'dot' |
| 182 | + # Base: |
| 183 | + base = walk ast[1] |
| 184 | + # property: |
| 185 | + property = ast[2] |
| 186 | + ast = [ |
| 187 | + 'call' |
| 188 | + [ |
| 189 | + 'dot', |
| 190 | + ['name', 'ADSAFE'], |
| 191 | + 'get' |
| 192 | + ] |
| 193 | + [ |
| 194 | + base |
| 195 | + ['string', property] |
| 196 | + ] |
| 197 | + ] |
| 198 | + else if type is 'string' |
| 199 | + ast[1] = ast[1].replace(/\<\//g,"<\\/") |
| 200 | + console.log "STRING: #{ast[1]}" |
| 201 | + else |
| 202 | + console.error "Unknown type: '#{type}'" |
| 203 | + console.log util.inspect ast, false, null, true |
| 204 | + process.exit 1 |
| 205 | + return ast |
| 206 | +ast = parser.parse script |
| 207 | +console.log util.inspect ast, false, null, true |
| 208 | +walk ast, 0 |
| 209 | +ast = uglify.ast_mangle ast, { |
| 210 | + mangle: true |
| 211 | + toplevel: true |
| 212 | + defines: {'this':['name', "null"]} |
| 213 | +} |
| 214 | +#ast = uglify.ast_squeeze ast |
| 215 | +script = uglify.gen_code ast, beautify: true, indent_start:2, indent_level:2, quote_keys: true |
| 216 | +console.log script |
| 217 | + |
| 218 | +script = script.replace /(\n|^)\/\/.*(\n|$)/g, "$2" |
| 219 | +adsafeId = "APPAAAAFF_" |
| 220 | +script = "<div id=\"#{adsafeId}\"><script>ADSAFE.go(\"#{adsafeId}\", function (dom, lib) {\n\"use strict\";\n#{script}});</script></div>" |
| 221 | +ok = JSLINT script, { |
| 222 | + adsafe: true, fragment: true, predef: globals, browser: true, safe: true, bitwise: true, continue: true, eqeq: true, es5: true, evil: false, forin: true, newcap: true, nomen: true, plusplus: true, regexp: true, undef: true, unparam: true, sloppy: true, stupid: true, sub: true, vars: true, white: true, css: true |
| 223 | +}, { |
| 224 | + plugin: false |
| 225 | +} |
| 226 | +unless ok |
| 227 | + result = JSLINT.data() |
| 228 | + console.dir result |
0 commit comments