combocodegen.js 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. /**
  2. * combocodegen.js ( based on escodegen )
  3. * @author dron
  4. * @create 2013-03-09
  5. */
  6. void function( factory, global ) {
  7. var parseConf, generateConf, host, escodegen;
  8. host = global.document;
  9. if( host.combocodegen )
  10. return ;
  11. parseConf = { raw: true, loc: true };
  12. generateConf = {
  13. format: { indent: { style: " " }, quotes: "double" }
  14. };
  15. factory( escodegen = {}, global );
  16. var trialHtmlCommentError = function( content ){
  17. var errorRegx, cutFlag1, cutFlag2, lineSpliter;
  18. errorRegx = /^Line (\d+): Unexpected token (<|>)$/;
  19. cutFlag1 = "<!--";
  20. cutFlag2 = "-->";
  21. lineSpliter = /\r\n|[\r\n]/;
  22. var test = function( content ){
  23. var ast;
  24. try{
  25. ast = host.esprima.parse( content, parseConf );
  26. return { status: "OK", ast: ast };
  27. }catch( e ){
  28. // console.log( "【Tracker】 " + e.message );
  29. if( errorRegx.test( e.message ) ){
  30. return {
  31. status: "MaybeHtmlCommentError",
  32. errorMode: RegExp.$2,
  33. line: RegExp.$1 - 0,
  34. error: e
  35. }
  36. }
  37. return {
  38. status: "OtherError",
  39. error: e
  40. };
  41. }
  42. };
  43. return function( content ){
  44. var ret, status, c, b, l;
  45. c = content;
  46. while( true ){
  47. ret = test( c );
  48. status = ret.status;
  49. if( status == "OK" ){
  50. return ret.ast;
  51. }else if( status == "OtherError" ){
  52. throw ret.error;
  53. }else if( status == "MaybeHtmlCommentError" ){
  54. c = c.split( lineSpliter );
  55. l = ret.line - 1;
  56. b = c[ l ];
  57. if( ret.errorMode == "<" && b.indexOf( cutFlag1 ) > -1 ){
  58. c[ l ] = b.slice( 0, b.lastIndexOf( cutFlag1 ) );
  59. }else if( ret.errorMode == ">" && b.indexOf( cutFlag2 ) > -1 ){
  60. c[ l ] = b.slice( 0, b.lastIndexOf( cutFlag2 ) );
  61. }else{
  62. throw ret.error;
  63. }
  64. c = c.join( "\n" );
  65. }
  66. }
  67. };
  68. }();
  69. host.combocodegen = function( code ){
  70. var ast;
  71. ast = trialHtmlCommentError( code.origContent );
  72. return escodegen.generate( ast, generateConf, code );
  73. };
  74. }(function ( exports, global ) {
  75. 'use strict';
  76. var Syntax,
  77. Precedence,
  78. BinaryPrecedence,
  79. Regex,
  80. VisitorKeys,
  81. VisitorOption,
  82. SourceNode,
  83. isArray,
  84. base,
  85. indent,
  86. json,
  87. renumber,
  88. hexadecimal,
  89. quotes,
  90. escapeless,
  91. newline,
  92. space,
  93. parentheses,
  94. semicolons,
  95. safeConcatenation,
  96. extra,
  97. parse,
  98. sourceMap;
  99. var _slice = [].slice, _push = [].push, _join = [].join, guid, currentCode,
  100. trackerDelimiterRegx;
  101. // NOTE: 以下的代码新增了 idBuffer 和 entrustedTraceId 的设计
  102. // idBuffer: 上一个语法环境里预置的 id 数组,在本语法环增中会追加新的代码片断 id 到其后,一般统一在外层语法环境里去做 trace 跟踪
  103. // entrustedTraceId: 上一个语法环境里预置的 id 数组,用于委托到本语法环境里做 trace 跟踪(正好与上面相反)
  104. trackerDelimiterRegx = /\{<\}.*?\{>\}/g;
  105. // NOTE: 用于生成代码关键 token 的唯一标识数字
  106. guid = function(){
  107. var index = 0;
  108. return function( length, joinWith ){
  109. if( !length )
  110. return index ++;
  111. for(var i = 0, result = []; i < length; i ++)
  112. result.push( index ++ );
  113. if( joinWith )
  114. _push.apply( result, joinWith );
  115. return result;
  116. }
  117. }();
  118. Syntax = {
  119. AssignmentExpression: 'AssignmentExpression',
  120. ArrayExpression: 'ArrayExpression',
  121. BlockStatement: 'BlockStatement',
  122. BinaryExpression: 'BinaryExpression',
  123. BreakStatement: 'BreakStatement',
  124. CallExpression: 'CallExpression',
  125. CatchClause: 'CatchClause',
  126. ConditionalExpression: 'ConditionalExpression',
  127. ContinueStatement: 'ContinueStatement',
  128. DoWhileStatement: 'DoWhileStatement',
  129. DebuggerStatement: 'DebuggerStatement',
  130. EmptyStatement: 'EmptyStatement',
  131. ExpressionStatement: 'ExpressionStatement',
  132. ForStatement: 'ForStatement',
  133. ForInStatement: 'ForInStatement',
  134. FunctionDeclaration: 'FunctionDeclaration',
  135. FunctionExpression: 'FunctionExpression',
  136. Identifier: 'Identifier',
  137. IfStatement: 'IfStatement',
  138. Literal: 'Literal',
  139. LabeledStatement: 'LabeledStatement',
  140. LogicalExpression: 'LogicalExpression',
  141. MemberExpression: 'MemberExpression',
  142. NewExpression: 'NewExpression',
  143. ObjectExpression: 'ObjectExpression',
  144. Program: 'Program',
  145. Property: 'Property',
  146. ReturnStatement: 'ReturnStatement',
  147. SequenceExpression: 'SequenceExpression',
  148. SwitchStatement: 'SwitchStatement',
  149. SwitchCase: 'SwitchCase',
  150. ThisExpression: 'ThisExpression',
  151. ThrowStatement: 'ThrowStatement',
  152. TryStatement: 'TryStatement',
  153. UnaryExpression: 'UnaryExpression',
  154. UpdateExpression: 'UpdateExpression',
  155. VariableDeclaration: 'VariableDeclaration',
  156. VariableDeclarator: 'VariableDeclarator',
  157. WhileStatement: 'WhileStatement',
  158. WithStatement: 'WithStatement'
  159. };
  160. Precedence = {
  161. Sequence: 0,
  162. Assignment: 1,
  163. Conditional: 2,
  164. LogicalOR: 3,
  165. LogicalAND: 4,
  166. BitwiseOR: 5,
  167. BitwiseXOR: 6,
  168. BitwiseAND: 7,
  169. Equality: 8,
  170. Relational: 9,
  171. BitwiseSHIFT: 10,
  172. Additive: 11,
  173. Multiplicative: 12,
  174. Unary: 13,
  175. Postfix: 14,
  176. Call: 15,
  177. New: 16,
  178. Member: 17,
  179. Primary: 18
  180. };
  181. BinaryPrecedence = {
  182. '||': Precedence.LogicalOR,
  183. '&&': Precedence.LogicalAND,
  184. '|': Precedence.BitwiseOR,
  185. '^': Precedence.BitwiseXOR,
  186. '&': Precedence.BitwiseAND,
  187. '==': Precedence.Equality,
  188. '!=': Precedence.Equality,
  189. '===': Precedence.Equality,
  190. '!==': Precedence.Equality,
  191. '<': Precedence.Relational,
  192. '>': Precedence.Relational,
  193. '<=': Precedence.Relational,
  194. '>=': Precedence.Relational,
  195. 'in': Precedence.Relational,
  196. 'instanceof': Precedence.Relational,
  197. '<<': Precedence.BitwiseSHIFT,
  198. '>>': Precedence.BitwiseSHIFT,
  199. '>>>': Precedence.BitwiseSHIFT,
  200. '+': Precedence.Additive,
  201. '-': Precedence.Additive,
  202. '*': Precedence.Multiplicative,
  203. '%': Precedence.Multiplicative,
  204. '/': Precedence.Multiplicative
  205. };
  206. Regex = {
  207. NonAsciiIdentifierPart: new RegExp('[\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0300-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u0483-\u0487\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u05d0-\u05ea\u05f0-\u05f2\u0610-\u061a\u0620-\u0669\u066e-\u06d3\u06d5-\u06dc\u06df-\u06e8\u06ea-\u06fc\u06ff\u0710-\u074a\u074d-\u07b1\u07c0-\u07f5\u07fa\u0800-\u082d\u0840-\u085b\u08a0\u08a2-\u08ac\u08e4-\u08fe\u0900-\u0963\u0966-\u096f\u0971-\u0977\u0979-\u097f\u0981-\u0983\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bc-\u09c4\u09c7\u09c8\u09cb-\u09ce\u09d7\u09dc\u09dd\u09df-\u09e3\u09e6-\u09f1\u0a01-\u0a03\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a59-\u0a5c\u0a5e\u0a66-\u0a75\u0a81-\u0a83\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abc-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ad0\u0ae0-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3c-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5c\u0b5d\u0b5f-\u0b63\u0b66-\u0b6f\u0b71\u0b82\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd0\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c58\u0c59\u0c60-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbc-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0cde\u0ce0-\u0ce3\u0ce6-\u0cef\u0cf1\u0cf2\u0d02\u0d03\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d-\u0d44\u0d46-\u0d48\u0d4a-\u0d4e\u0d57\u0d60-\u0d63\u0d66-\u0d6f\u0d7a-\u0d7f\u0d82\u0d83\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e01-\u0e3a\u0e40-\u0e4e\u0e50-\u0e59\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb9\u0ebb-\u0ebd\u0ec0-\u0ec4\u0ec6\u0ec8-\u0ecd\u0ed0-\u0ed9\u0edc-\u0edf\u0f00\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e-\u0f47\u0f49-\u0f6c\u0f71-\u0f84\u0f86-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1049\u1050-\u109d\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u135d-\u135f\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176c\u176e-\u1770\u1772\u1773\u1780-\u17d3\u17d7\u17dc\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1820-\u1877\u1880-\u18aa\u18b0-\u18f5\u1900-\u191c\u1920-\u192b\u1930-\u193b\u1946-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u19d0-\u19d9\u1a00-\u1a1b\u1a20-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1aa7\u1b00-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1bf3\u1c00-\u1c37\u1c40-\u1c49\u1c4d-\u1c7d\u1cd0-\u1cd2\u1cd4-\u1cf6\u1d00-\u1de6\u1dfc-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u200c\u200d\u203f\u2040\u2054\u2071\u207f\u2090-\u209c\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d7f-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2de0-\u2dff\u2e2f\u3005-\u3007\u3021-\u302f\u3031-\u3035\u3038-\u303c\u3041-\u3096\u3099\u309a\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua62b\ua640-\ua66f\ua674-\ua67d\ua67f-\ua697\ua69f-\ua6f1\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua827\ua840-\ua873\ua880-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f7\ua8fb\ua900-\ua92d\ua930-\ua953\ua960-\ua97c\ua980-\ua9c0\ua9cf-\ua9d9\uaa00-\uaa36\uaa40-\uaa4d\uaa50-\uaa59\uaa60-\uaa76\uaa7a\uaa7b\uaa80-\uaac2\uaadb-\uaadd\uaae0-\uaaef\uaaf2-\uaaf6\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabea\uabec\uabed\uabf0-\uabf9\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\ufe70-\ufe74\ufe76-\ufefc\uff10-\uff19\uff21-\uff3a\uff3f\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc]')
  208. };
  209. function returnSelf( string ){
  210. return string;
  211. }
  212. function getDefaultOptions() {
  213. // default options
  214. return {
  215. indent: null,
  216. base: null,
  217. parse: null,
  218. comment: false,
  219. format: {
  220. indent: {
  221. style: ' ',
  222. base: 0,
  223. adjustMultilineComment: false
  224. },
  225. json: false,
  226. renumber: false,
  227. hexadecimal: false,
  228. quotes: 'single',
  229. escapeless: false,
  230. compact: false,
  231. parentheses: true,
  232. semicolons: true,
  233. safeConcatenation: false
  234. },
  235. sourceMap: null,
  236. sourceMapWithCode: false
  237. };
  238. }
  239. function stringToArray(str) {
  240. var length = str.length,
  241. result = [],
  242. i;
  243. for (i = 0; i < length; i += 1) {
  244. result[i] = str.charAt(i);
  245. }
  246. return result;
  247. }
  248. function stringRepeat(str, num) {
  249. var result = '';
  250. for (num |= 0; num > 0; num >>>= 1, str += str) {
  251. if (num & 1) {
  252. result += str;
  253. }
  254. }
  255. return result;
  256. }
  257. // NOTE: 新增各种包装函数,用于辅助实现代码入侵和代码跟踪展示
  258. function wrapTrackerDelimiter( string ){
  259. return "{<}" + string + "{>}";
  260. }
  261. function injectAssistedCode( content ){
  262. return wrapTrackerDelimiter( "/* TRACKERINJECTJS */" + content );
  263. }
  264. function injectCodeFragmentTrace( idArray ){
  265. var groupId = Tracker.StatusPool.snippetGroupCreate.call( Tracker.StatusPool, currentCode, idArray );
  266. return injectAssistedCode( "__tracker__(" + groupId + ");" );
  267. }
  268. function injectCodeFragmentTraceWithReturn( idArray ){
  269. var groupId = Tracker.StatusPool.snippetGroupCreate.call( Tracker.StatusPool, currentCode, idArray );
  270. return injectAssistedCode( "__tracker__(" + groupId + ")" );
  271. }
  272. function wrapCodeFragmentHtml( fragment, id ){
  273. // if( !snippetsIdSet[ id ] )
  274. // snippetsIdSet[ id ] = 1;
  275. // Tracker.StatusPool.snippetToCodePut( id, currentCode );
  276. return wrapTrackerDelimiter( "<!-- TRACKERINJECTHTML --><span id=ckey-" + id + ">" ) + fragment +
  277. wrapTrackerDelimiter( "<!-- TRACKERINJECTHTML --></span>" );
  278. }
  279. isArray = Array.isArray;
  280. if (!isArray)
  281. isArray = function isArray(array) {
  282. return Object.prototype.toString.call(array) === '[object Array]';
  283. };
  284. // Fallback for the non SourceMap environment
  285. function SourceNodeMock(line, column, filename, chunk) {
  286. var result = [];
  287. function flatten(input) {
  288. var i, iz;
  289. if (isArray(input)) {
  290. for (i = 0, iz = input.length; i < iz; ++i) {
  291. flatten(input[i]);
  292. }
  293. } else if (input instanceof SourceNodeMock) {
  294. result.push(input);
  295. } else if (typeof input === 'string' && input) {
  296. result.push(input);
  297. }
  298. }
  299. flatten(chunk);
  300. this.children = result;
  301. }
  302. SourceNodeMock.prototype.toString = function toString() {
  303. var res = '', i, iz, node;
  304. for (i = 0, iz = this.children.length; i < iz; ++i) {
  305. node = this.children[i];
  306. if (node instanceof SourceNodeMock) {
  307. res += node.toString();
  308. } else {
  309. res += node;
  310. }
  311. }
  312. return res;
  313. };
  314. SourceNodeMock.prototype.replaceRight = function replaceRight(pattern, replacement) {
  315. var last = this.children[this.children.length - 1];
  316. if (last instanceof SourceNodeMock) {
  317. last.replaceRight(pattern, replacement);
  318. } else if (typeof last === 'string') {
  319. this.children[this.children.length - 1] = last.replace(pattern, replacement);
  320. } else {
  321. this.children.push(''.replace(pattern, replacement));
  322. }
  323. return this;
  324. };
  325. SourceNodeMock.prototype.join = function join(sep) {
  326. var i, iz, result;
  327. result = [];
  328. iz = this.children.length;
  329. if (iz > 0) {
  330. for (i = 0, iz -= 1; i < iz; ++i) {
  331. result.push(this.children[i], sep);
  332. }
  333. result.push(this.children[iz]);
  334. this.children = result;
  335. }
  336. return this;
  337. };
  338. function endsWithLineTerminator(str) {
  339. var ch = str.charAt(str.length - 1);
  340. return ch === '\r' || ch === '\n';
  341. }
  342. function shallowCopy(obj) {
  343. var ret = {}, key;
  344. for (key in obj) {
  345. if (obj.hasOwnProperty(key)) {
  346. ret[key] = obj[key];
  347. }
  348. }
  349. return ret;
  350. }
  351. function deepCopy(obj) {
  352. var ret = {}, key, val;
  353. for (key in obj) {
  354. if (obj.hasOwnProperty(key)) {
  355. val = obj[key];
  356. if (typeof val === 'object' && val !== null) {
  357. ret[key] = deepCopy(val);
  358. } else {
  359. ret[key] = val;
  360. }
  361. }
  362. }
  363. return ret;
  364. }
  365. function updateDeeply(target, override) {
  366. var key, val;
  367. function isHashObject(target) {
  368. return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp);
  369. }
  370. for (key in override) {
  371. if (override.hasOwnProperty(key)) {
  372. val = override[key];
  373. if (isHashObject(val)) {
  374. if (isHashObject(target[key])) {
  375. updateDeeply(target[key], val);
  376. } else {
  377. target[key] = updateDeeply({}, val);
  378. }
  379. } else {
  380. target[key] = val;
  381. }
  382. }
  383. }
  384. return target;
  385. }
  386. function generateNumber(value) {
  387. var result, point, temp, exponent, pos;
  388. if (value !== value) {
  389. throw new Error('Numeric literal whose value is NaN');
  390. }
  391. if (value < 0 || (value === 0 && 1 / value < 0)) {
  392. throw new Error('Numeric literal whose value is negative');
  393. }
  394. if (value === 1 / 0) {
  395. return json ? 'null' : renumber ? '1e400' : '1e+400';
  396. }
  397. result = '' + value;
  398. if (!renumber || result.length < 3) {
  399. return result;
  400. }
  401. point = result.indexOf('.');
  402. if (!json && result.charAt(0) === '0' && point === 1) {
  403. point = 0;
  404. result = result.slice(1);
  405. }
  406. temp = result;
  407. result = result.replace('e+', 'e');
  408. exponent = 0;
  409. if ((pos = temp.indexOf('e')) > 0) {
  410. exponent = +temp.slice(pos + 1);
  411. temp = temp.slice(0, pos);
  412. }
  413. if (point >= 0) {
  414. exponent -= temp.length - point - 1;
  415. temp = +(temp.slice(0, point) + temp.slice(point + 1)) + '';
  416. }
  417. pos = 0;
  418. while (temp.charAt(temp.length + pos - 1) === '0') {
  419. pos -= 1;
  420. }
  421. if (pos !== 0) {
  422. exponent -= pos;
  423. temp = temp.slice(0, pos);
  424. }
  425. if (exponent !== 0) {
  426. temp += 'e' + exponent;
  427. }
  428. if ((temp.length < result.length ||
  429. (hexadecimal && value > 1e12 && Math.floor(value) === value && (temp = '0x' + value.toString(16)).length < result.length)) &&
  430. +temp === value) {
  431. result = temp;
  432. }
  433. return result;
  434. }
  435. function escapeAllowedCharacter(ch, next) {
  436. var code = ch.charCodeAt(0), hex = code.toString(16), result = '\\';
  437. switch (ch) {
  438. case '\b':
  439. result += 'b';
  440. break;
  441. case '\f':
  442. result += 'f';
  443. break;
  444. case '\t':
  445. result += 't';
  446. break;
  447. default:
  448. if (json || code > 0xff) {
  449. result += 'u' + '0000'.slice(hex.length) + hex;
  450. } else if (ch === '\u0000' && '0123456789'.indexOf(next) < 0) {
  451. result += '0';
  452. } else if (ch === '\v') {
  453. result += 'v';
  454. } else {
  455. result += 'x' + '00'.slice(hex.length) + hex;
  456. }
  457. break;
  458. }
  459. return result;
  460. }
  461. function escapeDisallowedCharacter(ch) {
  462. var result = '\\';
  463. switch (ch) {
  464. case '\\':
  465. result += '\\';
  466. break;
  467. case '\n':
  468. result += 'n';
  469. break;
  470. case '\r':
  471. result += 'r';
  472. break;
  473. case '\u2028':
  474. result += 'u2028';
  475. break;
  476. case '\u2029':
  477. result += 'u2029';
  478. break;
  479. default:
  480. throw new Error('Incorrectly classified character');
  481. }
  482. return result;
  483. }
  484. function escapeString(str) {
  485. var result = '', i, len, ch, next, singleQuotes = 0, doubleQuotes = 0, single;
  486. if (typeof str[0] === 'undefined') {
  487. str = stringToArray(str);
  488. }
  489. for (i = 0, len = str.length; i < len; i += 1) {
  490. ch = str[i];
  491. if (ch === '\'') {
  492. singleQuotes += 1;
  493. } else if (ch === '"') {
  494. doubleQuotes += 1;
  495. } else if (ch === '/' && json) {
  496. result += '\\';
  497. } else if ('\\\n\r\u2028\u2029'.indexOf(ch) >= 0) {
  498. result += escapeDisallowedCharacter(ch);
  499. continue;
  500. } else if ((json && ch < ' ') || !(json || escapeless || (ch >= ' ' && ch <= '~'))) {
  501. result += escapeAllowedCharacter(ch, str[i + 1]);
  502. continue;
  503. }
  504. result += ch;
  505. }
  506. single = !(quotes === 'double' || (quotes === 'auto' && doubleQuotes < singleQuotes));
  507. str = result;
  508. result = single ? '\'' : '"';
  509. if (typeof str[0] === 'undefined') {
  510. str = stringToArray(str);
  511. }
  512. for (i = 0, len = str.length; i < len; i += 1) {
  513. ch = str[i];
  514. if ((ch === '\'' && single) || (ch === '"' && !single)) {
  515. result += '\\';
  516. }
  517. result += ch;
  518. }
  519. return result + (single ? '\'' : '"');
  520. }
  521. function isWhiteSpace(ch) {
  522. return '\t\v\f \xa0'.indexOf(ch) >= 0 || (ch.charCodeAt(0) >= 0x1680 && '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\ufeff'.indexOf(ch) >= 0);
  523. }
  524. function isLineTerminator(ch) {
  525. return '\n\r\u2028\u2029'.indexOf(ch) >= 0;
  526. }
  527. function isIdentifierPart(ch) {
  528. return (ch === '$') || (ch === '_') || (ch === '\\') ||
  529. (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
  530. ((ch >= '0') && (ch <= '9')) ||
  531. ((ch.charCodeAt(0) >= 0x80) && Regex.NonAsciiIdentifierPart.test(ch));
  532. }
  533. function join(left, right) {
  534. var leftSource = toSourceNode(left).toString(),
  535. rightSource = toSourceNode(right).toString(),
  536. leftChar = leftSource.charAt(leftSource.length - 1),
  537. rightChar = rightSource.charAt(0);
  538. if (((leftChar === '+' || leftChar === '-') && leftChar === rightChar) || (isIdentifierPart(leftChar) && isIdentifierPart(rightChar))) {
  539. return [left, ' ', right];
  540. } else if (isWhiteSpace(leftChar) || isLineTerminator(leftChar) || isWhiteSpace(rightChar) || isLineTerminator(rightChar)) {
  541. return [left, right];
  542. }
  543. return [left, space, right];
  544. }
  545. function addIndent(stmt) {
  546. return [base, stmt];
  547. }
  548. function withIndent(fn) {
  549. var previousBase;
  550. previousBase = base;
  551. base += indent;
  552. var result = fn.call(this, base);
  553. base = previousBase;
  554. return result;
  555. }
  556. function calculateSpaces(str) {
  557. var i;
  558. for (i = str.length - 1; i >= 0; i -= 1) {
  559. if (isLineTerminator(str.charAt(i))) {
  560. break;
  561. }
  562. }
  563. return (str.length - 1) - i;
  564. }
  565. function toSourceNode(generated, node) {
  566. if (node == null) {
  567. if (generated instanceof SourceNode) {
  568. return generated;
  569. } else {
  570. node = {};
  571. }
  572. }
  573. if (node.loc == null) {
  574. return new SourceNode(null, null, sourceMap, generated);
  575. }
  576. return new SourceNode(node.loc.start.line, node.loc.start.column, sourceMap, generated);
  577. }
  578. function adjustMultilineComment(value, specialBase) {
  579. var array, i, len, line, j, ch, spaces, previousBase;
  580. array = value.split(/\r\n|[\r\n]/);
  581. spaces = Number.MAX_VALUE;
  582. // first line doesn't have indentation
  583. for (i = 1, len = array.length; i < len; i += 1) {
  584. line = array[i];
  585. j = 0;
  586. while (j < line.length && isWhiteSpace(line[j])) {
  587. j += 1;
  588. }
  589. if (spaces > j) {
  590. spaces = j;
  591. }
  592. }
  593. if (typeof specialBase !== 'undefined') {
  594. // pattern like
  595. // {
  596. // var t = 20; /*
  597. // * this is comment
  598. // */
  599. // }
  600. previousBase = base;
  601. if (array[1][spaces] === '*') {
  602. specialBase += ' ';
  603. }
  604. base = specialBase;
  605. } else {
  606. if (spaces & 1) {
  607. // /*
  608. // *
  609. // */
  610. // If spaces are odd number, above pattern is considered.
  611. // We waste 1 space.
  612. spaces -= 1;
  613. }
  614. previousBase = base;
  615. }
  616. for (i = 1, len = array.length; i < len; i += 1) {
  617. array[i] = toSourceNode(addIndent(array[i].slice(spaces))).join('');
  618. }
  619. base = previousBase;
  620. return array.join('\n');
  621. }
  622. function generateComment(comment, specialBase) {
  623. if (comment.type === 'Line') {
  624. if (endsWithLineTerminator(comment.value)) {
  625. return '//' + comment.value;
  626. } else {
  627. // Always use LineTerminator
  628. return '//' + comment.value + '\n';
  629. }
  630. }
  631. if (extra.format.indent.adjustMultilineComment && /[\n\r]/.test(comment.value)) {
  632. return adjustMultilineComment('/*' + comment.value + '*/', specialBase);
  633. }
  634. return '/*' + comment.value + '*/';
  635. }
  636. function addCommentsToStatement(stmt, result) {
  637. var i, len, comment, save, node, tailingToStatement, specialBase, fragment;
  638. if (stmt.leadingComments && stmt.leadingComments.length > 0) {
  639. save = result;
  640. comment = stmt.leadingComments[0];
  641. result = [];
  642. if (safeConcatenation && stmt.type === Syntax.Program && stmt.body.length === 0) {
  643. result.push('\n');
  644. }
  645. result.push(generateComment(comment));
  646. if (!endsWithLineTerminator(toSourceNode(result).toString())) {
  647. result.push('\n');
  648. }
  649. for (i = 1, len = stmt.leadingComments.length; i < len; i += 1) {
  650. comment = stmt.leadingComments[i];
  651. fragment = [generateComment(comment)];
  652. if (!endsWithLineTerminator(toSourceNode(fragment).toString())) {
  653. fragment.push('\n');
  654. }
  655. result.push(addIndent(fragment));
  656. }
  657. result.push(addIndent(save));
  658. }
  659. if (stmt.trailingComments) {
  660. tailingToStatement = !endsWithLineTerminator(toSourceNode(result).toString());
  661. specialBase = stringRepeat(' ', calculateSpaces(toSourceNode([base, result, indent]).toString()));
  662. for (i = 0, len = stmt.trailingComments.length; i < len; i += 1) {
  663. comment = stmt.trailingComments[i];
  664. if (tailingToStatement) {
  665. // We assume target like following script
  666. //
  667. // var t = 20; /**
  668. // * This is comment of t
  669. // */
  670. if (i === 0) {
  671. // first case
  672. result.push(indent);
  673. } else {
  674. result.push(specialBase);
  675. }
  676. result.push(generateComment(comment, specialBase));
  677. } else {
  678. result.push(addIndent(generateComment(comment)));
  679. }
  680. if (i !== len - 1 && !endsWithLineTerminator(toSourceNode(result).toString())) {
  681. result.push('\n');
  682. }
  683. }
  684. }
  685. return result;
  686. }
  687. function parenthesize( text, current, should, wrapHtml ) {
  688. wrapHtml = wrapHtml || returnSelf;
  689. if ( current < should ) {
  690. return [ wrapHtml( '(' ), ' ', text, ' ', wrapHtml( ')' ) ];
  691. }
  692. return text;
  693. }
  694. function maybeBlock( stmt, semicolonOptional, entrustedTraceId, idBuffer, dontBlock ) {
  695. var result, noLeadingComment;
  696. // NOTE: entrustedTraceId 用于接受外部语法的委托 traceId
  697. noLeadingComment = !extra.comment || !stmt.leadingComments;
  698. if (stmt.type === Syntax.BlockStatement && noLeadingComment) {
  699. return [ space, generateStatement( stmt, {
  700. entrustedTraceId: entrustedTraceId,
  701. idBuffer: idBuffer
  702. } ) ];
  703. }
  704. if (stmt.type === Syntax.EmptyStatement && noLeadingComment) {
  705. return ';';
  706. }
  707. // dontBlock = false;
  708. if( !dontBlock )
  709. withIndent(function (indent) {
  710. var id = guid( 2, entrustedTraceId );
  711. result = [
  712. " ",
  713. wrapCodeFragmentHtml( "{", id[ 0 ] ),
  714. injectCodeFragmentTrace( id ),
  715. newline,
  716. addIndent(
  717. generateStatement( stmt, { semicolonOptional: semicolonOptional } ) ),
  718. newline,
  719. base.slice( 4 ),
  720. wrapCodeFragmentHtml( "}", id[ 1 ] )
  721. ];
  722. if( idBuffer )
  723. idBuffer.push( id[0], id[1] );
  724. });
  725. else
  726. withIndent(function (indent) {
  727. // var id = guid( 2, entrustedTraceId );
  728. result = [
  729. " ",
  730. // wrapCodeFragmentHtml( "{", id[ 0 ] ),
  731. // injectCodeFragmentTrace( id ),
  732. newline,
  733. addIndent(
  734. generateStatement( stmt, { semicolonOptional: semicolonOptional } ) ),
  735. // newline,
  736. // base.slice( 4 ),
  737. // wrapCodeFragmentHtml( "}", id[ 1 ] )
  738. ];
  739. // if( idBuffer )
  740. // idBuffer.push( id[0], id[1] );
  741. });
  742. return result;
  743. }
  744. function maybeBlockSuffix( stmt, result ) {
  745. var ends = endsWithLineTerminator(toSourceNode(result).toString());
  746. if (stmt.type === Syntax.BlockStatement && (!extra.comment || !stmt.leadingComments) && !ends) {
  747. return [result, space];
  748. }
  749. if (ends) {
  750. return [result, base];
  751. }
  752. return [result, newline, base];
  753. }
  754. function generateFunctionBody( node, idBuffer ) {
  755. var result, i, len, wrapHtml;
  756. wrapHtml = idBuffer ? function( string ){
  757. var id;
  758. idBuffer.push( id = guid() );
  759. return wrapCodeFragmentHtml( string, id );
  760. } : returnSelf;
  761. len = node.params.length;
  762. result = [ wrapHtml( '(' ), len ? space : '' ];
  763. for (i = 0; i < len; i += 1) {
  764. result.push( wrapHtml( node.params[i].name ) );
  765. if (i + 1 < len) {
  766. result.push( ',' + space );
  767. }
  768. }
  769. result.push(
  770. len ? space : '',
  771. wrapHtml( ')' ),
  772. maybeBlock( node.body, null, null, idBuffer )
  773. );
  774. return result;
  775. }
  776. function generateExpression( expr, option ) {
  777. var result, precedence, currentPrecedence, i, len, raw, fragment, multiline, leftChar, leftSource, rightChar, rightSource, allowIn, allowCall, idBuffer, wrapHtml, allowUnparenthesizedNew;
  778. precedence = option.precedence;
  779. allowIn = option.allowIn;
  780. allowCall = option.allowCall;
  781. idBuffer = option.idBuffer;
  782. wrapHtml = idBuffer ? function( string ){
  783. var id;
  784. idBuffer.push( id = guid() );
  785. return wrapCodeFragmentHtml( string, id );
  786. } : returnSelf;
  787. switch (expr.type) {
  788. case Syntax.SequenceExpression:
  789. result = [];
  790. allowIn |= ( Precedence.Sequence < precedence );
  791. for (i = 0, len = expr.expressions.length; i < len; i += 1) {
  792. result.push( generateExpression( expr.expressions[i], {
  793. precedence: Precedence.Assignment,
  794. allowIn: allowIn,
  795. allowCall: true,
  796. idBuffer: idBuffer
  797. }) );
  798. if (i + 1 < len) {
  799. result.push( ',' + space );
  800. }
  801. }
  802. result = parenthesize( result, Precedence.Sequence, precedence, wrapHtml );
  803. break;
  804. case Syntax.AssignmentExpression:
  805. allowIn |= (Precedence.Assignment < precedence);
  806. result = parenthesize(
  807. [
  808. generateExpression( expr.left, {
  809. precedence: Precedence.Call,
  810. allowIn: allowIn,
  811. allowCall: true,
  812. idBuffer: idBuffer
  813. }),
  814. space + wrapHtml( expr.operator ) + space,
  815. generateExpression( expr.right, {
  816. precedence: Precedence.Assignment,
  817. allowIn: allowIn,
  818. allowCall: true,
  819. idBuffer: idBuffer
  820. })
  821. ],
  822. Precedence.Assignment,
  823. precedence,
  824. wrapHtml
  825. );
  826. break;
  827. case Syntax.ConditionalExpression:
  828. allowIn |= (Precedence.Conditional < precedence);
  829. result = parenthesize(
  830. [
  831. generateExpression(expr.test, {
  832. precedence: Precedence.LogicalOR,
  833. allowIn: allowIn,
  834. allowCall: true,
  835. idBuffer: idBuffer
  836. }),
  837. space + wrapHtml( '?' ) + space,
  838. generateExpression(expr.consequent, {
  839. precedence: Precedence.Assignment,
  840. allowIn: allowIn,
  841. allowCall: true,
  842. idBuffer: idBuffer
  843. }),
  844. space + wrapHtml( ':' ) + space,
  845. generateExpression(expr.alternate, {
  846. precedence: Precedence.Assignment,
  847. allowIn: allowIn,
  848. allowCall: true,
  849. idBuffer: idBuffer
  850. })
  851. ],
  852. Precedence.Conditional,
  853. precedence,
  854. wrapHtml
  855. );
  856. break;
  857. case Syntax.LogicalExpression:
  858. case Syntax.BinaryExpression:
  859. currentPrecedence = BinaryPrecedence[expr.operator];
  860. allowIn |= (currentPrecedence < precedence);
  861. result = join(
  862. generateExpression(expr.left, {
  863. precedence: currentPrecedence,
  864. allowIn: allowIn,
  865. allowCall: true,
  866. idBuffer: idBuffer
  867. }),
  868. wrapHtml( expr.operator )
  869. );
  870. fragment = generateExpression(expr.right, {
  871. precedence: currentPrecedence + 1,
  872. allowIn: allowIn,
  873. allowCall: true,
  874. idBuffer: idBuffer
  875. });
  876. if (expr.operator === '/' && fragment.toString().charAt(0) === '/') {
  877. // If '/' concats with '/', it is interpreted as comment start
  878. result.push(' ', wrapHtml( fragment ));
  879. } else {
  880. result = join(result, wrapHtml( fragment ));
  881. }
  882. if (expr.operator === 'in' && !allowIn) {
  883. result = [ wrapHtml('('), result, wrapHtml( ')' ) ];
  884. } else {
  885. result = parenthesize( result, currentPrecedence, precedence, wrapHtml );
  886. }
  887. break;
  888. case Syntax.CallExpression:
  889. result = [ generateExpression( expr.callee, {
  890. precedence: Precedence.Call,
  891. allowIn: true,
  892. allowCall: true,
  893. allowUnparenthesizedNew: false,
  894. idBuffer: idBuffer
  895. }) ];
  896. result.push( wrapHtml( '(' ) );
  897. for (i = 0, len = expr['arguments'].length; i < len; i += 1) {
  898. result.push(
  899. space,
  900. generateExpression( expr['arguments'][i], {
  901. precedence: Precedence.Assignment,
  902. allowIn: true,
  903. allowCall: true,
  904. idBuffer: idBuffer
  905. })
  906. );
  907. result.push( i + 1 < len ? ',' : space );
  908. }
  909. result.push( wrapHtml( ')' ) );
  910. if ( !allowCall ) {
  911. result = [ wrapHtml( '(' ), result, wrapHtml( ')' ) ];
  912. } else {
  913. result = parenthesize( result, Precedence.Call, precedence, wrapHtml );
  914. }
  915. break;
  916. case Syntax.NewExpression:
  917. len = expr['arguments'].length;
  918. allowUnparenthesizedNew = option.allowUnparenthesizedNew === undefined || option.allowUnparenthesizedNew;
  919. result = join(
  920. wrapHtml( 'new' ),
  921. generateExpression(expr.callee, {
  922. precedence: Precedence.New,
  923. allowIn: true,
  924. allowCall: false,
  925. allowUnparenthesizedNew: allowUnparenthesizedNew && !parentheses && len === 0,
  926. idBuffer: idBuffer
  927. })
  928. );
  929. if (!allowUnparenthesizedNew || parentheses || len > 0) {
  930. result.push( wrapHtml( '(' ) );
  931. for (i = 0; i < len; i += 1) {
  932. result.push( space, generateExpression(expr['arguments'][i], {
  933. precedence: Precedence.Assignment,
  934. allowIn: true,
  935. allowCall: true,
  936. idBuffer: idBuffer
  937. } ));
  938. result.push( i + 1 < len ? ',' : space );
  939. }
  940. result.push( wrapHtml( ')' ) );
  941. }
  942. result = parenthesize( result, Precedence.New, precedence, wrapHtml );
  943. break;
  944. case Syntax.MemberExpression:
  945. result = [ generateExpression( expr.object, {
  946. precedence: Precedence.Call,
  947. allowIn: true,
  948. allowCall: allowCall,
  949. allowUnparenthesizedNew: false,
  950. idBuffer: idBuffer
  951. } ) ];
  952. if (expr.computed) {
  953. result.push( wrapHtml( '[' ), generateExpression(expr.property, {
  954. precedence: Precedence.Sequence,
  955. allowIn: true,
  956. allowCall: allowCall,
  957. idBuffer: idBuffer
  958. }), wrapHtml( ']' ) );
  959. } else {
  960. if (expr.object.type === Syntax.Literal && typeof expr.object.value === 'number') {
  961. if (result.indexOf('.') < 0) {
  962. if (!/[eExX]/.test(result) && !(result.length >= 2 && result[0] === '0')) {
  963. result.push( wrapHtml( '.' ) );
  964. }
  965. }
  966. }
  967. result.push( '.' + wrapHtml( expr.property.name ) );
  968. }
  969. result = parenthesize( result, Precedence.Member, precedence, wrapHtml );
  970. break;
  971. case Syntax.UnaryExpression:
  972. fragment = generateExpression(expr.argument, {
  973. precedence: Precedence.Unary,
  974. allowIn: true,
  975. allowCall: true,
  976. idBuffer: idBuffer
  977. });
  978. if (space === '') {
  979. result = join( wrapHtml( expr.operator ), fragment );
  980. } else {
  981. result = [ wrapHtml( expr.operator ) ];
  982. if (expr.operator.length > 2) {
  983. // delete, void, typeof
  984. // get `typeof []`, not `typeof[]`
  985. result = join(result, fragment);
  986. } else {
  987. // Prevent inserting spaces between operator and argument if it is unnecessary
  988. // like, `!cond`
  989. leftSource = toSourceNode(result).toString();
  990. leftChar = leftSource.charAt(leftSource.length - 1);
  991. rightChar = fragment.toString().charAt(0);
  992. if (((leftChar === '+' || leftChar === '-') && leftChar === rightChar) || (isIdentifierPart(leftChar) && isIdentifierPart(rightChar))) {
  993. result.push( ' ', fragment );
  994. } else {
  995. result.push( fragment );
  996. }
  997. }
  998. }
  999. result = parenthesize( result, Precedence.Unary, precedence, wrapHtml );
  1000. break;
  1001. case Syntax.UpdateExpression:
  1002. if (expr.prefix) {
  1003. result = parenthesize(
  1004. [
  1005. wrapHtml( expr.operator ),
  1006. generateExpression(expr.argument, {
  1007. precedence: Precedence.Unary,
  1008. allowIn: true,
  1009. allowCall: true,
  1010. idBuffer: idBuffer
  1011. })
  1012. ],
  1013. Precedence.Unary,
  1014. precedence,
  1015. wrapHtml
  1016. );
  1017. } else {
  1018. result = parenthesize(
  1019. [
  1020. generateExpression(expr.argument, {
  1021. precedence: Precedence.Postfix,
  1022. allowIn: true,
  1023. allowCall: true,
  1024. idBuffer: idBuffer
  1025. }),
  1026. wrapHtml( expr.operator )
  1027. ],
  1028. Precedence.Postfix,
  1029. precedence,
  1030. wrapHtml
  1031. );
  1032. }
  1033. break;
  1034. case Syntax.FunctionExpression:
  1035. result = wrapHtml( 'function' );
  1036. if ( expr.id ) {
  1037. result += ' ' + wrapHtml( expr.id.name );
  1038. } else {
  1039. result += space;
  1040. }
  1041. result = [ result, generateFunctionBody( expr, idBuffer ) ];
  1042. break;
  1043. case Syntax.ArrayExpression:
  1044. if ( !expr.elements.length ) {
  1045. result = wrapHtml( '[]' );
  1046. break;
  1047. }
  1048. multiline = expr.elements.length > 1;
  1049. result = [
  1050. wrapHtml( '[' ), multiline ? newline : ''
  1051. ];
  1052. withIndent(function (indent) {
  1053. for (i = 0, len = expr.elements.length; i < len; i += 1) {
  1054. if (!expr.elements[i]) {
  1055. if(multiline) result.push(base);
  1056. if (i + 1 === len) {
  1057. result.push( ',' );
  1058. }
  1059. } else {
  1060. result.push( multiline ? base : '', generateExpression( expr.elements[i], {
  1061. precedence: Precedence.Assignment,
  1062. allowIn: true,
  1063. allowCall: true,
  1064. idBuffer: idBuffer,
  1065. addLine: true
  1066. } ) );
  1067. }
  1068. if (i + 1 < len) {
  1069. result.push( ',' + ( multiline ? newline : space ) );
  1070. }
  1071. }
  1072. });
  1073. if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
  1074. result.push(newline);
  1075. }
  1076. result.push( multiline ? base : '', wrapHtml( ']' ) );
  1077. break;
  1078. case Syntax.Property:
  1079. // NOTE: get 和 set 在初版的时候暂不考虑
  1080. if (expr.kind === 'get' || expr.kind === 'set') {
  1081. result = [
  1082. expr.kind + ' ',
  1083. generateExpression(expr.key, {
  1084. precedence: Precedence.Sequence,
  1085. allowIn: true,
  1086. allowCall: true,
  1087. idBuffer: idBuffer
  1088. }),
  1089. generateFunctionBody( expr.value )
  1090. ];
  1091. } else {
  1092. result = [
  1093. generateExpression( expr.key, {
  1094. precedence: Precedence.Sequence,
  1095. allowIn: true,
  1096. allowCall: true,
  1097. idBuffer: idBuffer,
  1098. addLine: true
  1099. } ),
  1100. ':' + space,
  1101. generateExpression( expr.value, {
  1102. precedence: Precedence.Assignment,
  1103. allowIn: true,
  1104. allowCall: true,
  1105. idBuffer: idBuffer
  1106. } )
  1107. ];
  1108. }
  1109. break;
  1110. case Syntax.ObjectExpression:
  1111. if ( !expr.properties.length ) {
  1112. result = wrapHtml( '{}' );
  1113. break;
  1114. }
  1115. multiline = expr.properties.length > 1;
  1116. result = [ wrapHtml( '{' ), multiline ? newline : ''];
  1117. withIndent(function (indent) {
  1118. for (i = 0, len = expr.properties.length; i < len; i += 1) {
  1119. result.push( multiline ? base : '', generateExpression(expr.properties[i], {
  1120. precedence: Precedence.Sequence,
  1121. allowIn: true,
  1122. allowCall: true,
  1123. idBuffer: idBuffer
  1124. }));
  1125. if (i + 1 < len) {
  1126. result.push( ',' + (multiline ? newline : space));
  1127. }
  1128. }
  1129. });
  1130. if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
  1131. result.push(newline);
  1132. }
  1133. result.push( multiline ? base : '', wrapHtml( '}' ) );
  1134. break;
  1135. case Syntax.ThisExpression:
  1136. result = wrapHtml( 'this' );
  1137. break;
  1138. case Syntax.Identifier:
  1139. result = wrapHtml( expr.name );
  1140. break;
  1141. case Syntax.Literal:
  1142. if (expr.hasOwnProperty('raw') && parse) {
  1143. try {
  1144. raw = parse(expr.raw).body[0].expression;
  1145. if (raw.type === Syntax.Literal) {
  1146. if (raw.value === expr.value) {
  1147. result = wrapHtml( expr.raw );
  1148. break;
  1149. }
  1150. }
  1151. } catch (e) {
  1152. // not use raw property
  1153. }
  1154. }
  1155. if (expr.value === null) {
  1156. result = wrapHtml( 'null' );
  1157. break;
  1158. }
  1159. if (typeof expr.value === 'string') {
  1160. result = wrapHtml( escapeString( expr.value ) );
  1161. break;
  1162. }
  1163. if (typeof expr.value === 'number') {
  1164. result = wrapHtml( generateNumber( expr.value ) );
  1165. break;
  1166. }
  1167. result = wrapHtml( expr.value.toString() );
  1168. break;
  1169. default:
  1170. throw new Error('Unknown expression type: ' + expr.type);
  1171. }
  1172. return toSourceNode(result, expr);
  1173. }
  1174. function generateStatement( stmt, option ) {
  1175. var i, len, result, node, allowIn, fragment, semicolon, idBuffer, entrustedTraceId, id,
  1176. tid, resultString;
  1177. allowIn = true;
  1178. semicolon = ';';
  1179. if (option) {
  1180. allowIn = option.allowIn === undefined || option.allowIn;
  1181. idBuffer = option.idBuffer;
  1182. entrustedTraceId = option.entrustedTraceId; // NOTE: 上一个语法委托 trace 的 id 数组
  1183. if (!semicolons && option.semicolonOptional === true) {
  1184. semicolon = '';
  1185. }
  1186. }
  1187. switch (stmt.type) {
  1188. case Syntax.BlockStatement:
  1189. id = guid( 2, entrustedTraceId );
  1190. result = [
  1191. wrapCodeFragmentHtml( '{', id[ 0 ] ),
  1192. injectCodeFragmentTrace( id ),
  1193. newline,
  1194. ];
  1195. withIndent(function (indent) {
  1196. for (i = 0, len = stmt.body.length; i < len; i += 1) {
  1197. fragment = addIndent(generateStatement(stmt.body[i], {semicolonOptional: i === len - 1}));
  1198. result.push(fragment);
  1199. if (!endsWithLineTerminator(toSourceNode(fragment).toString())) {
  1200. result.push(newline);
  1201. }
  1202. }
  1203. });
  1204. result.push(addIndent(
  1205. wrapCodeFragmentHtml( '}', id[ 1 ] )
  1206. ));
  1207. if( idBuffer )
  1208. idBuffer.push( id[0], id[1] );
  1209. break;
  1210. case Syntax.BreakStatement:
  1211. if (stmt.label) {
  1212. // NOTE: 居然可以跟 label,这种情况暂时不考虑了
  1213. result = 'break ' + stmt.label.name + semicolon;
  1214. } else {
  1215. id = guid( 1 );
  1216. result = [
  1217. injectCodeFragmentTrace( id ),
  1218. wrapCodeFragmentHtml( 'break', id[ 0 ] ),
  1219. semicolon
  1220. ];
  1221. }
  1222. break;
  1223. case Syntax.ContinueStatement:
  1224. if ( stmt.label ) {
  1225. // TODO: break/continue label 需要处理,否则无法高亮
  1226. result = 'continue ' + stmt.label.name + semicolon;
  1227. } else {
  1228. id = guid( 1 );
  1229. result = [
  1230. injectCodeFragmentTrace( id ),
  1231. wrapCodeFragmentHtml( 'continue', id[ 0 ] ),
  1232. semicolon
  1233. ];
  1234. }
  1235. break;
  1236. case Syntax.DoWhileStatement:
  1237. // Because `do 42 while (cond)` is Syntax Error. We need semicolon.
  1238. id = guid( 4 );
  1239. result = [
  1240. wrapCodeFragmentHtml( 'do', id[ 0 ] ),
  1241. maybeBlock( stmt.body )
  1242. ];
  1243. result = maybeBlockSuffix(stmt.body, result);
  1244. result = join(result, [
  1245. wrapCodeFragmentHtml( 'while', id[ 1 ] ),
  1246. space,
  1247. wrapCodeFragmentHtml( '(', id[ 2 ] ),
  1248. space,
  1249. generateExpression(stmt.test, {
  1250. precedence: Precedence.Sequence,
  1251. allowIn: true,
  1252. allowCall: true,
  1253. idBuffer: id
  1254. }),
  1255. space,
  1256. wrapCodeFragmentHtml( ')', id[ 3 ] ),
  1257. semicolon,
  1258. injectCodeFragmentTrace( id )
  1259. ]);
  1260. break;
  1261. case Syntax.CatchClause:
  1262. id = guid( 3 );
  1263. withIndent(function (indent) {
  1264. result = [
  1265. wrapCodeFragmentHtml( 'catch', id[ 0 ] ),
  1266. space,
  1267. wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1268. space,
  1269. generateExpression( stmt.param, {
  1270. precedence: Precedence.Sequence,
  1271. allowIn: true,
  1272. allowCall: true,
  1273. idBuffer: id
  1274. } ),
  1275. space,
  1276. wrapCodeFragmentHtml( ')', id[ 2 ] )
  1277. ];
  1278. });
  1279. result.push( maybeBlock( stmt.body, null, id ) );
  1280. break;
  1281. case Syntax.DebuggerStatement:
  1282. result = 'debugger' + semicolon;
  1283. break;
  1284. case Syntax.EmptyStatement:
  1285. result = ';';
  1286. break;
  1287. case Syntax.ExpressionStatement:
  1288. id = [];
  1289. result = [generateExpression(stmt.expression, {
  1290. precedence: Precedence.Sequence,
  1291. allowIn: true,
  1292. allowCall: true,
  1293. fromStatement: true,
  1294. idBuffer: id
  1295. })];
  1296. // 12.4 '{', 'function' is not allowed in this position.
  1297. // wrap expression with parentheses
  1298. resultString = result.toString().replace( trackerDelimiterRegx, "" );
  1299. if ( resultString.charAt(0) === '{' ||
  1300. ( resultString.slice(0, 8) === 'function' && " (".indexOf( resultString.charAt(8) ) >= 0 ) ) {
  1301. _push.apply( id, tid = guid(2) );
  1302. result = [
  1303. wrapCodeFragmentHtml( '(', tid[0] ),
  1304. result,
  1305. wrapCodeFragmentHtml( ')', tid[1] ),
  1306. semicolon,
  1307. injectCodeFragmentTrace( id )
  1308. ];
  1309. } else {
  1310. result.push(
  1311. semicolon,
  1312. injectCodeFragmentTrace( id )
  1313. );
  1314. }
  1315. break;
  1316. case Syntax.VariableDeclarator:
  1317. if ( stmt.init ){
  1318. id = guid( 2 );
  1319. idBuffer && _push.apply( idBuffer, id );
  1320. result = [
  1321. wrapCodeFragmentHtml( stmt.id.name, id[ 0 ] ),
  1322. space,
  1323. wrapCodeFragmentHtml( '=', id[ 1 ] ),
  1324. space,
  1325. generateExpression( stmt.init, {
  1326. precedence: Precedence.Assignment,
  1327. allowIn: allowIn,
  1328. allowCall: true,
  1329. idBuffer: idBuffer
  1330. } )
  1331. ];
  1332. } else {
  1333. id = guid();
  1334. idBuffer && idBuffer.push( id );
  1335. result = wrapCodeFragmentHtml( stmt.id.name, id );
  1336. }
  1337. break;
  1338. case Syntax.VariableDeclaration:
  1339. id = [];
  1340. id.push( tid = guid() );
  1341. result = [
  1342. wrapCodeFragmentHtml( stmt.kind, tid )
  1343. ];
  1344. // special path for
  1345. // var x = function () {
  1346. // };
  1347. if ( stmt.declarations.length === 1 && stmt.declarations[0].init &&
  1348. stmt.declarations[0].init.type === Syntax.FunctionExpression ) {
  1349. result.push( ' ', generateStatement( stmt.declarations[0], {
  1350. allowIn: allowIn,
  1351. idBuffer: id
  1352. } ) );
  1353. } else {
  1354. // VariableDeclarator is typed as Statement,
  1355. // but joined with comma (not LineTerminator).
  1356. // So if comment is attached to target node, we should specialize.
  1357. withIndent(function (indent) {
  1358. node = stmt.declarations[0];
  1359. if ( extra.comment && node.leadingComments ) {
  1360. result.push( '\n', addIndent( generateStatement( node, {
  1361. allowIn: allowIn
  1362. } ) ) );
  1363. } else {
  1364. result.push( ' ', generateStatement(node, {
  1365. allowIn: allowIn,
  1366. idBuffer: id
  1367. } ) );
  1368. }
  1369. for ( i = 1, len = stmt.declarations.length; i < len; i += 1 ) {
  1370. node = stmt.declarations[i];
  1371. if (extra.comment && node.leadingComments) {
  1372. result.push( ',' + newline, addIndent( generateStatement( node, {
  1373. allowIn: allowIn,
  1374. idBuffer: id
  1375. } ) ) );
  1376. } else {
  1377. result.push( ',' + space, generateStatement( node, {
  1378. allowIn: allowIn,
  1379. idBuffer: id
  1380. } ) );
  1381. }
  1382. }
  1383. });
  1384. }
  1385. result.push(
  1386. injectAssistedCode( ", __trackerTempVariable__ = " ),
  1387. injectCodeFragmentTraceWithReturn( id ),
  1388. // injectCodeFragmentTrace( id ),
  1389. semicolon
  1390. );
  1391. break;
  1392. case Syntax.ThrowStatement:
  1393. // TODO: throw 后面如果哪一个复杂的表达式,整段都会被绿色块包含起来,因为直接用了 wrapCodeFragmentHtml,
  1394. // 应该改为 idBuffer 的实现
  1395. // 但由于 throw 完成之后,代码会中断,没有机会执行 trace,暂时也无比较好的办法(用 try..finally ? )
  1396. // 参考 ReturnStatement 中加 try 的实现
  1397. id = guid( 2 );
  1398. result = [
  1399. injectCodeFragmentTrace( id ),
  1400. join(
  1401. wrapCodeFragmentHtml( 'throw', id[ 0 ] ),
  1402. wrapCodeFragmentHtml( generateExpression(stmt.argument, {
  1403. precedence: Precedence.Sequence,
  1404. allowIn: true,
  1405. allowCall: true
  1406. }), id[ 1 ] )
  1407. ),
  1408. semicolon
  1409. ];
  1410. break;
  1411. case Syntax.TryStatement:
  1412. id = guid( 1 );
  1413. result = [
  1414. wrapCodeFragmentHtml( 'try', id[ 0 ] ),
  1415. maybeBlock( stmt.block, null, id )
  1416. ];
  1417. result = maybeBlockSuffix(stmt.block, result);
  1418. for (i = 0, len = stmt.handlers.length; i < len; i += 1) {
  1419. result = join(result, generateStatement(stmt.handlers[i]));
  1420. if (stmt.finalizer || i + 1 !== len) {
  1421. result = maybeBlockSuffix(stmt.handlers[i].body, result);
  1422. }
  1423. }
  1424. id = guid( 1 );
  1425. if (stmt.finalizer) {
  1426. result = join(result, [
  1427. wrapCodeFragmentHtml( 'finally', id[ 0 ] ),
  1428. maybeBlock( stmt.finalizer, null, id )
  1429. ]);
  1430. }
  1431. break;
  1432. case Syntax.SwitchStatement:
  1433. id = guid( 5 );
  1434. withIndent(function (indent) {
  1435. result = [
  1436. wrapCodeFragmentHtml( 'switch', id[ 0 ]),
  1437. space,
  1438. wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1439. space,
  1440. generateExpression(stmt.discriminant, {
  1441. precedence: Precedence.Sequence,
  1442. allowIn: true,
  1443. allowCall: true,
  1444. idBuffer: id
  1445. }),
  1446. space,
  1447. wrapCodeFragmentHtml( ')', id[ 2 ] ),
  1448. space,
  1449. wrapCodeFragmentHtml( '{', id[ 3 ] ),
  1450. newline
  1451. ];
  1452. });
  1453. if (stmt.cases) {
  1454. for (i = 0, len = stmt.cases.length; i < len; i += 1) {
  1455. fragment = addIndent(generateStatement(stmt.cases[i], {semicolonOptional: i === len - 1}));
  1456. result.push(fragment);
  1457. if (!endsWithLineTerminator(toSourceNode(fragment).toString())) {
  1458. result.push(newline);
  1459. }
  1460. }
  1461. }
  1462. result.push(
  1463. addIndent( wrapCodeFragmentHtml( '}', id[ 4 ] ) ),
  1464. injectCodeFragmentTrace( id )
  1465. );
  1466. break;
  1467. case Syntax.SwitchCase:
  1468. withIndent(function (indent) {
  1469. if (stmt.test) {
  1470. id = guid( 1 );
  1471. result = [
  1472. join(
  1473. wrapCodeFragmentHtml( 'case', id[ 0 ] ),
  1474. generateExpression(stmt.test, {
  1475. precedence: Precedence.Sequence,
  1476. allowIn: true,
  1477. allowCall: true,
  1478. idBuffer: id
  1479. })
  1480. ),
  1481. ':',
  1482. injectCodeFragmentTrace( id )
  1483. ];
  1484. } else {
  1485. id = guid( 1 );
  1486. result = [
  1487. wrapCodeFragmentHtml( 'default' ),
  1488. ':',
  1489. injectCodeFragmentTrace( id )
  1490. ];
  1491. }
  1492. i = 0;
  1493. len = stmt.consequent.length;
  1494. if (len && stmt.consequent[0].type === Syntax.BlockStatement) {
  1495. fragment = maybeBlock(stmt.consequent[0]);
  1496. result.push(fragment);
  1497. i = 1;
  1498. }
  1499. if (i !== len && !endsWithLineTerminator(toSourceNode(result).toString())) {
  1500. result.push(newline);
  1501. }
  1502. for (; i < len; i += 1) {
  1503. fragment = addIndent(generateStatement(stmt.consequent[i], {semicolonOptional: i === len - 1 && semicolon === ''}));
  1504. result.push(fragment);
  1505. if (i + 1 !== len && !endsWithLineTerminator(toSourceNode(fragment).toString())) {
  1506. result.push(newline);
  1507. }
  1508. }
  1509. });
  1510. break;
  1511. case Syntax.IfStatement:
  1512. id = guid( 3, entrustedTraceId );
  1513. withIndent(function (indent) {
  1514. result = [
  1515. // NOTE: 加了 wrap
  1516. wrapCodeFragmentHtml( 'if', id[ 0 ] ),
  1517. space,
  1518. wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1519. space,
  1520. generateExpression(stmt.test, {
  1521. precedence: Precedence.Sequence,
  1522. allowIn: true,
  1523. allowCall: true,
  1524. idBuffer: id
  1525. }),
  1526. space,
  1527. wrapCodeFragmentHtml( ')', id[ 2 ] )
  1528. ];
  1529. });
  1530. if ( stmt.alternate ) {
  1531. result.push( maybeBlock( stmt.consequent, null, id ) );
  1532. result = maybeBlockSuffix( stmt.consequent, result );
  1533. if ( stmt.alternate.type === Syntax.IfStatement ) {
  1534. id = guid( 1 );
  1535. result = join(
  1536. result, [
  1537. wrapCodeFragmentHtml( "else", id[ 0 ] ) + ' ',
  1538. generateStatement( stmt.alternate, {
  1539. semicolonOptional: semicolon === '',
  1540. entrustedTraceId: id
  1541. } )
  1542. ]
  1543. );
  1544. } else {
  1545. id = guid( 1 );
  1546. result = join(
  1547. result,
  1548. join(
  1549. wrapCodeFragmentHtml( 'else', id[ 0 ] ),
  1550. maybeBlock(stmt.alternate, semicolon === '', id)
  1551. )
  1552. );
  1553. }
  1554. } else {
  1555. result.push( maybeBlock(stmt.consequent, semicolon === '', id ));
  1556. }
  1557. break;
  1558. case Syntax.ForStatement:
  1559. id = guid( 4 );
  1560. withIndent(function (indent) {
  1561. result = [
  1562. wrapCodeFragmentHtml( 'for', id[ 0 ] ),
  1563. space,
  1564. wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1565. space
  1566. ];
  1567. if (stmt.init) {
  1568. if (stmt.init.type === Syntax.VariableDeclaration) {
  1569. result.push(
  1570. generateStatement(
  1571. stmt.init, { allowIn: false, idBuffer: id }
  1572. )
  1573. );
  1574. } else {
  1575. result.push( generateExpression(stmt.init, {
  1576. precedence: Precedence.Sequence,
  1577. allowIn: false,
  1578. allowCall: true,
  1579. idBuffer: id
  1580. } ), ';' );
  1581. }
  1582. } else {
  1583. result.push( ';' );
  1584. }
  1585. if (stmt.test) {
  1586. result.push( space, generateExpression(stmt.test, {
  1587. precedence: Precedence.Sequence,
  1588. allowIn: true,
  1589. allowCall: true,
  1590. idBuffer: id
  1591. }), ';');
  1592. } else {
  1593. result.push( ';' );
  1594. }
  1595. if ( stmt.update ) {
  1596. result.push(space, generateExpression(stmt.update, {
  1597. precedence: Precedence.Sequence,
  1598. allowIn: true,
  1599. allowCall: true,
  1600. idBuffer: id
  1601. }), space, wrapCodeFragmentHtml( ')', id[ 2 ] ) );
  1602. } else {
  1603. result.push( space, wrapCodeFragmentHtml( ')', id[ 3 ] ) );
  1604. }
  1605. });
  1606. result.push(
  1607. maybeBlock(stmt.body, semicolon === ''),
  1608. injectAssistedCode( ";" ),
  1609. injectCodeFragmentTrace( id )
  1610. );
  1611. break;
  1612. case Syntax.ForInStatement:
  1613. id = guid( 5 );
  1614. result = [
  1615. wrapCodeFragmentHtml( 'for', id[ 0 ] ),
  1616. space,
  1617. wrapCodeFragmentHtml( '(', space, id[ 1 ] )
  1618. ];
  1619. withIndent(function (indent) {
  1620. if (stmt.left.type === Syntax.VariableDeclaration) {
  1621. withIndent(function (indent) {
  1622. result.push(
  1623. wrapCodeFragmentHtml( stmt.left.kind, id[ 2 ] ) + ' ',
  1624. generateStatement(stmt.left.declarations[0], {
  1625. allowIn: false,
  1626. idBuffer: id
  1627. }));
  1628. });
  1629. } else {
  1630. result.push( generateExpression(stmt.left, {
  1631. precedence: Precedence.Call,
  1632. allowIn: true,
  1633. allowCall: true,
  1634. idBuffer: id
  1635. }));
  1636. }
  1637. result = join( result, wrapCodeFragmentHtml( 'in', id[ 3 ] ) );
  1638. result = [ join(
  1639. result,
  1640. generateExpression(stmt.right, {
  1641. precedence: Precedence.Sequence,
  1642. allowIn: true,
  1643. allowCall: true,
  1644. idBuffer: id
  1645. })
  1646. ), space, wrapCodeFragmentHtml( ')', id[ 4 ] ) ];
  1647. });
  1648. result.push(
  1649. maybeBlock(stmt.body, semicolon === ''),
  1650. injectAssistedCode( ";" ),
  1651. injectCodeFragmentTrace( id )
  1652. );
  1653. break;
  1654. case Syntax.LabeledStatement:
  1655. id = guid( 1 );
  1656. result = [
  1657. injectCodeFragmentTrace( id ),
  1658. wrapCodeFragmentHtml( stmt.label.name, id[ 0 ] ),
  1659. ':',
  1660. maybeBlock( stmt.body, semicolon === '', null, null, true )
  1661. ];
  1662. break;
  1663. case Syntax.Program:
  1664. len = stmt.body.length;
  1665. result = [safeConcatenation && len > 0 ? '\n' : ''];
  1666. for (i = 0; i < len; i += 1) {
  1667. fragment = addIndent(generateStatement(stmt.body[i], {semicolonOptional: !safeConcatenation && i === len - 1}));
  1668. result.push(fragment);
  1669. if (i + 1 < len && !endsWithLineTerminator(toSourceNode(fragment).toString())) {
  1670. result.push(newline);
  1671. }
  1672. }
  1673. break;
  1674. case Syntax.FunctionDeclaration:
  1675. id = guid( 2 );
  1676. result = [
  1677. wrapCodeFragmentHtml( "function", id[ 0 ] ),
  1678. ' ',
  1679. wrapCodeFragmentHtml( stmt.id.name, id[ 1 ] ),
  1680. generateFunctionBody( stmt, id ),
  1681. injectAssistedCode( ";" ),
  1682. injectCodeFragmentTrace( id )
  1683. ];
  1684. break;
  1685. case Syntax.ReturnStatement:
  1686. id = guid( 1 );
  1687. if (stmt.argument) {
  1688. result = [
  1689. injectAssistedCode( "try{" ),
  1690. join(
  1691. wrapCodeFragmentHtml( 'return', id[ 0 ] ),
  1692. generateExpression(stmt.argument, {
  1693. precedence: Precedence.Sequence,
  1694. allowIn: true,
  1695. allowCall: true,
  1696. idBuffer: id
  1697. })
  1698. ),
  1699. injectAssistedCode( "}catch(__trackerErrorData__){throw __trackerErrorData__;}finally{" ),
  1700. injectCodeFragmentTrace( id ),
  1701. injectAssistedCode( "}" ),
  1702. semicolon
  1703. ];
  1704. } else {
  1705. result = [
  1706. injectCodeFragmentTrace( id ),
  1707. wrapCodeFragmentHtml( 'return', id[ 0 ] ),
  1708. semicolon
  1709. ];
  1710. }
  1711. break;
  1712. case Syntax.WhileStatement:
  1713. id = guid( 3 );
  1714. withIndent(function (indent) {
  1715. result = [
  1716. wrapCodeFragmentHtml( 'while', id[ 0 ] ) + space + wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1717. space,
  1718. generateExpression(stmt.test, {
  1719. precedence: Precedence.Sequence,
  1720. allowIn: true,
  1721. allowCall: true,
  1722. idBuffer: id
  1723. }),
  1724. space,
  1725. wrapCodeFragmentHtml( ')', id[ 2 ] )
  1726. ];
  1727. });
  1728. result.push(
  1729. maybeBlock(stmt.body, semicolon === ''),
  1730. injectAssistedCode( ";" ),
  1731. injectCodeFragmentTrace( id )
  1732. );
  1733. break;
  1734. case Syntax.WithStatement:
  1735. id = guid( 3 );
  1736. withIndent(function (indent) {
  1737. result = [
  1738. wrapCodeFragmentHtml( 'with', id[ 0 ] ),
  1739. space,
  1740. wrapCodeFragmentHtml( '(', id[ 1 ] ),
  1741. space,
  1742. generateExpression(stmt.object, {
  1743. precedence: Precedence.Sequence,
  1744. allowIn: true,
  1745. allowCall: true,
  1746. idBuffer: id
  1747. }),
  1748. space,
  1749. wrapCodeFragmentHtml( ')', id[ 2 ] )
  1750. ];
  1751. });
  1752. result.push(
  1753. maybeBlock(stmt.body, semicolon === '', id)
  1754. );
  1755. break;
  1756. default:
  1757. throw new Error('Unknown statement type: ' + stmt.type);
  1758. }
  1759. // Attach comments
  1760. if (extra.comment) {
  1761. result = addCommentsToStatement(stmt, result);
  1762. }
  1763. var fragment = toSourceNode(result).toString();
  1764. if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') {
  1765. result = toSourceNode(result).replaceRight(/\s+$/, '');
  1766. }
  1767. return toSourceNode(result, stmt);
  1768. }
  1769. function generate( node, options, currentCodeInstance ) {
  1770. var defaultOptions = getDefaultOptions(), result, pair;
  1771. currentCode = currentCodeInstance;
  1772. // snippetsIdSet = currentCode.snippetsIdSet;
  1773. if (options != null) {
  1774. // Obsolete options
  1775. //
  1776. // `options.indent`
  1777. // `options.base`
  1778. //
  1779. // Instead of them, we can use `option.format.indent`.
  1780. if (typeof options.indent === 'string') {
  1781. defaultOptions.format.indent.style = options.indent;
  1782. }
  1783. if (typeof options.base === 'number') {
  1784. defaultOptions.format.indent.base = options.base;
  1785. }
  1786. options = updateDeeply(defaultOptions, options);
  1787. indent = options.format.indent.style;
  1788. if (typeof options.base === 'string') {
  1789. base = options.base;
  1790. } else {
  1791. base = stringRepeat(indent, options.format.indent.base);
  1792. }
  1793. } else {
  1794. options = defaultOptions;
  1795. indent = options.format.indent.style;
  1796. base = stringRepeat(indent, options.format.indent.base);
  1797. }
  1798. json = options.format.json;
  1799. renumber = options.format.renumber;
  1800. hexadecimal = json ? false : options.format.hexadecimal;
  1801. quotes = json ? 'double' : options.format.quotes;
  1802. escapeless = options.format.escapeless;
  1803. if (options.format.compact) {
  1804. newline = space = indent = base = '';
  1805. } else {
  1806. newline = '\n';
  1807. space = ' ';
  1808. }
  1809. parentheses = options.format.parentheses;
  1810. semicolons = options.format.semicolons;
  1811. safeConcatenation = options.format.safeConcatenation;
  1812. parse = json ? null : options.parse;
  1813. sourceMap = options.sourceMap;
  1814. extra = options;
  1815. if (sourceMap) {
  1816. if (typeof process !== 'undefined') {
  1817. // We assume environment is node.js
  1818. SourceNode = require('source-map').SourceNode;
  1819. } else {
  1820. SourceNode = global.sourceMap.SourceNode;
  1821. }
  1822. } else {
  1823. SourceNode = SourceNodeMock;
  1824. }
  1825. switch (node.type) {
  1826. case Syntax.BlockStatement:
  1827. case Syntax.BreakStatement:
  1828. case Syntax.CatchClause:
  1829. case Syntax.ContinueStatement:
  1830. case Syntax.DoWhileStatement:
  1831. case Syntax.DebuggerStatement:
  1832. case Syntax.EmptyStatement:
  1833. case Syntax.ExpressionStatement:
  1834. case Syntax.ForStatement:
  1835. case Syntax.ForInStatement:
  1836. case Syntax.FunctionDeclaration:
  1837. case Syntax.IfStatement:
  1838. case Syntax.LabeledStatement:
  1839. case Syntax.Program:
  1840. case Syntax.ReturnStatement:
  1841. case Syntax.SwitchStatement:
  1842. case Syntax.SwitchCase:
  1843. case Syntax.ThrowStatement:
  1844. case Syntax.TryStatement:
  1845. case Syntax.VariableDeclaration:
  1846. case Syntax.VariableDeclarator:
  1847. case Syntax.WhileStatement:
  1848. case Syntax.WithStatement:
  1849. result = generateStatement(node);
  1850. break;
  1851. case Syntax.AssignmentExpression:
  1852. case Syntax.ArrayExpression:
  1853. case Syntax.BinaryExpression:
  1854. case Syntax.CallExpression:
  1855. case Syntax.ConditionalExpression:
  1856. case Syntax.FunctionExpression:
  1857. case Syntax.Identifier:
  1858. case Syntax.Literal:
  1859. case Syntax.LogicalExpression:
  1860. case Syntax.MemberExpression:
  1861. case Syntax.NewExpression:
  1862. case Syntax.ObjectExpression:
  1863. case Syntax.Property:
  1864. case Syntax.SequenceExpression:
  1865. case Syntax.ThisExpression:
  1866. case Syntax.UnaryExpression:
  1867. case Syntax.UpdateExpression:
  1868. result = generateExpression(node, {
  1869. precedence: Precedence.Sequence,
  1870. allowIn: true,
  1871. allowCall: true
  1872. });
  1873. break;
  1874. default:
  1875. throw new Error('Unknown node type: ' + node.type);
  1876. }
  1877. if (!sourceMap) {
  1878. return result.toString();
  1879. }
  1880. pair = result.toStringWithSourceMap({file: options.sourceMap});
  1881. if (options.sourceMapWithCode) {
  1882. return pair;
  1883. }
  1884. return pair.map.toString();
  1885. }
  1886. // simple visitor implementation
  1887. VisitorKeys = {
  1888. AssignmentExpression: ['left', 'right'],
  1889. ArrayExpression: ['elements'],
  1890. BlockStatement: ['body'],
  1891. BinaryExpression: ['left', 'right'],
  1892. BreakStatement: ['label'],
  1893. CallExpression: ['callee', 'arguments'],
  1894. CatchClause: ['param', 'body'],
  1895. ConditionalExpression: ['test', 'consequent', 'alternate'],
  1896. ContinueStatement: ['label'],
  1897. DoWhileStatement: ['body', 'test'],
  1898. DebuggerStatement: [],
  1899. EmptyStatement: [],
  1900. ExpressionStatement: ['expression'],
  1901. ForStatement: ['init', 'test', 'update', 'body'],
  1902. ForInStatement: ['left', 'right', 'body'],
  1903. FunctionDeclaration: ['id', 'params', 'body'],
  1904. FunctionExpression: ['id', 'params', 'body'],
  1905. Identifier: [],
  1906. IfStatement: ['test', 'consequent', 'alternate'],
  1907. Literal: [],
  1908. LabeledStatement: ['label', 'body'],
  1909. LogicalExpression: ['left', 'right'],
  1910. MemberExpression: ['object', 'property'],
  1911. NewExpression: ['callee', 'arguments'],
  1912. ObjectExpression: ['properties'],
  1913. Program: ['body'],
  1914. Property: ['key', 'value'],
  1915. ReturnStatement: ['argument'],
  1916. SequenceExpression: ['expressions'],
  1917. SwitchStatement: ['discriminant', 'cases'],
  1918. SwitchCase: ['test', 'consequent'],
  1919. ThisExpression: [],
  1920. ThrowStatement: ['argument'],
  1921. TryStatement: ['block', 'handlers', 'finalizer'],
  1922. UnaryExpression: ['argument'],
  1923. UpdateExpression: ['argument'],
  1924. VariableDeclaration: ['declarations'],
  1925. VariableDeclarator: ['id', 'init'],
  1926. WhileStatement: ['test', 'body'],
  1927. WithStatement: ['object', 'body']
  1928. };
  1929. VisitorOption = {
  1930. Break: 1,
  1931. Skip: 2
  1932. };
  1933. function traverse(top, visitor) {
  1934. var worklist, leavelist, node, ret, current, current2, candidates, candidate, marker = {};
  1935. worklist = [ top ];
  1936. leavelist = [ null ];
  1937. while (worklist.length) {
  1938. node = worklist.pop();
  1939. if (node === marker) {
  1940. node = leavelist.pop();
  1941. if (visitor.leave) {
  1942. ret = visitor.leave(node, leavelist[leavelist.length - 1]);
  1943. } else {
  1944. ret = undefined;
  1945. }
  1946. if (ret === VisitorOption.Break) {
  1947. return;
  1948. }
  1949. } else if (node) {
  1950. if (visitor.enter) {
  1951. ret = visitor.enter(node, leavelist[leavelist.length - 1]);
  1952. } else {
  1953. ret = undefined;
  1954. }
  1955. if (ret === VisitorOption.Break) {
  1956. return;
  1957. }
  1958. worklist.push(marker);
  1959. leavelist.push(node);
  1960. if (ret !== VisitorOption.Skip) {
  1961. candidates = VisitorKeys[node.type];
  1962. current = candidates.length;
  1963. while ((current -= 1) >= 0) {
  1964. candidate = node[candidates[current]];
  1965. if (candidate) {
  1966. if (isArray(candidate)) {
  1967. current2 = candidate.length;
  1968. while ((current2 -= 1) >= 0) {
  1969. if (candidate[current2]) {
  1970. worklist.push(candidate[current2]);
  1971. }
  1972. }
  1973. } else {
  1974. worklist.push(candidate);
  1975. }
  1976. }
  1977. }
  1978. }
  1979. }
  1980. }
  1981. }
  1982. // based on LLVM libc++ upper_bound / lower_bound
  1983. // MIT License
  1984. function upperBound(array, func) {
  1985. var diff, len, i, current;
  1986. len = array.length;
  1987. i = 0;
  1988. while (len) {
  1989. diff = len >>> 1;
  1990. current = i + diff;
  1991. if (func(array[current])) {
  1992. len = diff;
  1993. } else {
  1994. i = current + 1;
  1995. len -= diff + 1;
  1996. }
  1997. }
  1998. return i;
  1999. }
  2000. function lowerBound(array, func) {
  2001. var diff, len, i, current;
  2002. len = array.length;
  2003. i = 0;
  2004. while (len) {
  2005. diff = len >>> 1;
  2006. current = i + diff;
  2007. if (func(array[current])) {
  2008. i = current + 1;
  2009. len -= diff + 1;
  2010. } else {
  2011. len = diff;
  2012. }
  2013. }
  2014. return i;
  2015. }
  2016. function extendCommentRange(comment, tokens) {
  2017. var target, token;
  2018. target = upperBound(tokens, function search(token) {
  2019. return token.range[0] > comment.range[0];
  2020. });
  2021. comment.extendedRange = [comment.range[0], comment.range[1]];
  2022. if (target !== tokens.length) {
  2023. comment.extendedRange[1] = tokens[target].range[0];
  2024. }
  2025. target -= 1;
  2026. if (target >= 0) {
  2027. if (target < tokens.length) {
  2028. comment.extendedRange[0] = tokens[target].range[1];
  2029. } else if (token.length) {
  2030. comment.extendedRange[1] = tokens[tokens.length - 1].range[0];
  2031. }
  2032. }
  2033. return comment;
  2034. }
  2035. function attachComments(tree, providedComments, tokens) {
  2036. // At first, we should calculate extended comment ranges.
  2037. var comments = [], comment, len, i;
  2038. if (!tree.range) {
  2039. throw new Error('attachComments needs range information');
  2040. }
  2041. // tokens array is empty, we attach comments to tree as 'leadingComments'
  2042. if (!tokens.length) {
  2043. if (providedComments.length) {
  2044. for (i = 0, len = providedComments.length; i < len; i += 1) {
  2045. comment = deepCopy(providedComments[i]);
  2046. comment.extendedRange = [0, tree.range[0]];
  2047. comments.push(comment);
  2048. }
  2049. tree.leadingComments = comments;
  2050. }
  2051. return tree;
  2052. }
  2053. for (i = 0, len = providedComments.length; i < len; i += 1) {
  2054. comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
  2055. }
  2056. // This is based on John Freeman's implementation.
  2057. traverse(tree, {
  2058. cursor: 0,
  2059. enter: function (node) {
  2060. var comment;
  2061. while (this.cursor < comments.length) {
  2062. comment = comments[this.cursor];
  2063. if (comment.extendedRange[1] > node.range[0]) {
  2064. break;
  2065. }
  2066. if (comment.extendedRange[1] === node.range[0]) {
  2067. if (!node.leadingComments) {
  2068. node.leadingComments = [];
  2069. }
  2070. node.leadingComments.push(comment);
  2071. comments.splice(this.cursor, 1);
  2072. } else {
  2073. this.cursor += 1;
  2074. }
  2075. }
  2076. // already out of owned node
  2077. if (this.cursor === comments.length) {
  2078. return VisitorOption.Break;
  2079. }
  2080. if (comments[this.cursor].extendedRange[0] > node.range[1]) {
  2081. return VisitorOption.Skip;
  2082. }
  2083. }
  2084. });
  2085. traverse(tree, {
  2086. cursor: 0,
  2087. leave: function (node) {
  2088. var comment;
  2089. while (this.cursor < comments.length) {
  2090. comment = comments[this.cursor];
  2091. if (node.range[1] < comment.extendedRange[0]) {
  2092. break;
  2093. }
  2094. if (node.range[1] === comment.extendedRange[0]) {
  2095. if (!node.trailingComments) {
  2096. node.trailingComments = [];
  2097. }
  2098. node.trailingComments.push(comment);
  2099. comments.splice(this.cursor, 1);
  2100. } else {
  2101. this.cursor += 1;
  2102. }
  2103. }
  2104. // already out of owned node
  2105. if (this.cursor === comments.length) {
  2106. return VisitorOption.Break;
  2107. }
  2108. if (comments[this.cursor].extendedRange[0] > node.range[1]) {
  2109. return VisitorOption.Skip;
  2110. }
  2111. }
  2112. });
  2113. return tree;
  2114. }
  2115. // Sync with package.json.
  2116. exports.version = '0.0.9-dev';
  2117. exports.generate = generate;
  2118. exports.traverse = traverse;
  2119. exports.attachComments = attachComments;
  2120. }, this );