beautify.js 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420
  1. /* AUTO-GENERATED. DO NOT MODIFY. */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. JS Beautifier
  23. ---------------
  24. Written by Einar Lielmanis, <einar@beautifier.io>
  25. https://beautifier.io/
  26. Originally converted to javascript by Vital, <vital76@gmail.com>
  27. "End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
  28. Parsing improvements for brace-less statements by Liam Newman <bitwiseman@beautifier.io>
  29. Usage:
  30. js_beautify(js_source_text);
  31. js_beautify(js_source_text, options);
  32. The options are:
  33. indent_size (default 4) - indentation size,
  34. indent_char (default space) - character to indent with,
  35. preserve_newlines (default true) - whether existing line breaks should be preserved,
  36. max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk,
  37. jslint_happy (default false) - if true, then jslint-stricter mode is enforced.
  38. jslint_happy !jslint_happy
  39. ---------------------------------
  40. function () function()
  41. switch () { switch() {
  42. case 1: case 1:
  43. break; break;
  44. } }
  45. space_after_anon_function (default false) - should the space before an anonymous function's parens be added, "function()" vs "function ()",
  46. NOTE: This option is overriden by jslint_happy (i.e. if jslint_happy is true, space_after_anon_function is true by design)
  47. brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none" | any of the former + ",preserve-inline"
  48. put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
  49. preserve-inline will try to preserve inline blocks of curly braces
  50. space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)",
  51. unescape_strings (default false) - should printable characters in strings encoded in \xNN notation be unescaped, "example" vs "\x65\x78\x61\x6d\x70\x6c\x65"
  52. wrap_line_length (default unlimited) - lines should wrap at next opportunity after this number of characters.
  53. NOTE: This is not a hard limit. Lines will continue until a point where a newline would
  54. be preserved if it were present.
  55. end_with_newline (default false) - end output with a newline
  56. e.g
  57. js_beautify(js_source_text, {
  58. 'indent_size': 1,
  59. 'indent_char': '\t'
  60. });
  61. */
  62. (function() {
  63. /* GENERATED_BUILD_OUTPUT */
  64. var legacy_beautify_js =
  65. /******/ (function(modules) { // webpackBootstrap
  66. /******/ // The module cache
  67. /******/ var installedModules = {};
  68. /******/
  69. /******/ // The require function
  70. /******/ function __webpack_require__(moduleId) {
  71. /******/
  72. /******/ // Check if module is in cache
  73. /******/ if(installedModules[moduleId]) {
  74. /******/ return installedModules[moduleId].exports;
  75. /******/ }
  76. /******/ // Create a new module (and put it into the cache)
  77. /******/ var module = installedModules[moduleId] = {
  78. /******/ i: moduleId,
  79. /******/ l: false,
  80. /******/ exports: {}
  81. /******/ };
  82. /******/
  83. /******/ // Execute the module function
  84. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  85. /******/
  86. /******/ // Flag the module as loaded
  87. /******/ module.l = true;
  88. /******/
  89. /******/ // Return the exports of the module
  90. /******/ return module.exports;
  91. /******/ }
  92. /******/
  93. /******/
  94. /******/ // expose the modules object (__webpack_modules__)
  95. /******/ __webpack_require__.m = modules;
  96. /******/
  97. /******/ // expose the module cache
  98. /******/ __webpack_require__.c = installedModules;
  99. /******/
  100. /******/ // define getter function for harmony exports
  101. /******/ __webpack_require__.d = function(exports, name, getter) {
  102. /******/ if(!__webpack_require__.o(exports, name)) {
  103. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  104. /******/ }
  105. /******/ };
  106. /******/
  107. /******/ // define __esModule on exports
  108. /******/ __webpack_require__.r = function(exports) {
  109. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  110. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  111. /******/ }
  112. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  113. /******/ };
  114. /******/
  115. /******/ // create a fake namespace object
  116. /******/ // mode & 1: value is a module id, require it
  117. /******/ // mode & 2: merge all properties of value into the ns
  118. /******/ // mode & 4: return value when already ns object
  119. /******/ // mode & 8|1: behave like require
  120. /******/ __webpack_require__.t = function(value, mode) {
  121. /******/ if(mode & 1) value = __webpack_require__(value);
  122. /******/ if(mode & 8) return value;
  123. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  124. /******/ var ns = Object.create(null);
  125. /******/ __webpack_require__.r(ns);
  126. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  127. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  128. /******/ return ns;
  129. /******/ };
  130. /******/
  131. /******/ // getDefaultExport function for compatibility with non-harmony modules
  132. /******/ __webpack_require__.n = function(module) {
  133. /******/ var getter = module && module.__esModule ?
  134. /******/ function getDefault() { return module['default']; } :
  135. /******/ function getModuleExports() { return module; };
  136. /******/ __webpack_require__.d(getter, 'a', getter);
  137. /******/ return getter;
  138. /******/ };
  139. /******/
  140. /******/ // Object.prototype.hasOwnProperty.call
  141. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  142. /******/
  143. /******/ // __webpack_public_path__
  144. /******/ __webpack_require__.p = "";
  145. /******/
  146. /******/
  147. /******/ // Load entry module and return exports
  148. /******/ return __webpack_require__(__webpack_require__.s = 0);
  149. /******/ })
  150. /************************************************************************/
  151. /******/ ([
  152. /* 0 */
  153. /***/ (function(module, exports, __webpack_require__) {
  154. "use strict";
  155. /*jshint node:true */
  156. /*
  157. The MIT License (MIT)
  158. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  159. Permission is hereby granted, free of charge, to any person
  160. obtaining a copy of this software and associated documentation files
  161. (the "Software"), to deal in the Software without restriction,
  162. including without limitation the rights to use, copy, modify, merge,
  163. publish, distribute, sublicense, and/or sell copies of the Software,
  164. and to permit persons to whom the Software is furnished to do so,
  165. subject to the following conditions:
  166. The above copyright notice and this permission notice shall be
  167. included in all copies or substantial portions of the Software.
  168. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  169. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  170. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  171. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  172. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  173. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  174. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  175. SOFTWARE.
  176. */
  177. var Beautifier = __webpack_require__(1).Beautifier,
  178. Options = __webpack_require__(5).Options;
  179. function js_beautify(js_source_text, options) {
  180. var beautifier = new Beautifier(js_source_text, options);
  181. return beautifier.beautify();
  182. }
  183. module.exports = js_beautify;
  184. module.exports.defaultOptions = function() {
  185. return new Options();
  186. };
  187. /***/ }),
  188. /* 1 */
  189. /***/ (function(module, exports, __webpack_require__) {
  190. "use strict";
  191. /*jshint node:true */
  192. /*
  193. The MIT License (MIT)
  194. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  195. Permission is hereby granted, free of charge, to any person
  196. obtaining a copy of this software and associated documentation files
  197. (the "Software"), to deal in the Software without restriction,
  198. including without limitation the rights to use, copy, modify, merge,
  199. publish, distribute, sublicense, and/or sell copies of the Software,
  200. and to permit persons to whom the Software is furnished to do so,
  201. subject to the following conditions:
  202. The above copyright notice and this permission notice shall be
  203. included in all copies or substantial portions of the Software.
  204. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  205. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  206. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  207. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  208. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  209. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  210. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  211. SOFTWARE.
  212. */
  213. var Output = __webpack_require__(2).Output;
  214. var Token = __webpack_require__(3).Token;
  215. var acorn = __webpack_require__(4);
  216. var Options = __webpack_require__(5).Options;
  217. var Tokenizer = __webpack_require__(7).Tokenizer;
  218. var line_starters = __webpack_require__(7).line_starters;
  219. var positionable_operators = __webpack_require__(7).positionable_operators;
  220. var TOKEN = __webpack_require__(7).TOKEN;
  221. function remove_redundant_indentation(output, frame) {
  222. // This implementation is effective but has some issues:
  223. // - can cause line wrap to happen too soon due to indent removal
  224. // after wrap points are calculated
  225. // These issues are minor compared to ugly indentation.
  226. if (frame.multiline_frame ||
  227. frame.mode === MODE.ForInitializer ||
  228. frame.mode === MODE.Conditional) {
  229. return;
  230. }
  231. // remove one indent from each line inside this section
  232. output.remove_indent(frame.start_line_index);
  233. }
  234. function in_array(what, arr) {
  235. return arr.indexOf(what) !== -1;
  236. }
  237. function ltrim(s) {
  238. return s.replace(/^\s+/g, '');
  239. }
  240. function generateMapFromStrings(list) {
  241. var result = {};
  242. for (var x = 0; x < list.length; x++) {
  243. // make the mapped names underscored instead of dash
  244. result[list[x].replace(/-/g, '_')] = list[x];
  245. }
  246. return result;
  247. }
  248. function reserved_word(token, word) {
  249. return token && token.type === TOKEN.RESERVED && token.text === word;
  250. }
  251. function reserved_array(token, words) {
  252. return token && token.type === TOKEN.RESERVED && in_array(token.text, words);
  253. }
  254. // Unsure of what they mean, but they work. Worth cleaning up in future.
  255. var special_words = ['case', 'return', 'do', 'if', 'throw', 'else', 'await', 'break', 'continue', 'async'];
  256. var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
  257. // Generate map from array
  258. var OPERATOR_POSITION = generateMapFromStrings(validPositionValues);
  259. var OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION.before_newline, OPERATOR_POSITION.preserve_newline];
  260. var MODE = {
  261. BlockStatement: 'BlockStatement', // 'BLOCK'
  262. Statement: 'Statement', // 'STATEMENT'
  263. ObjectLiteral: 'ObjectLiteral', // 'OBJECT',
  264. ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]',
  265. ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)',
  266. Conditional: 'Conditional', //'(COND-EXPRESSION)',
  267. Expression: 'Expression' //'(EXPRESSION)'
  268. };
  269. // we could use just string.split, but
  270. // IE doesn't like returning empty strings
  271. function split_linebreaks(s) {
  272. //return s.split(/\x0d\x0a|\x0a/);
  273. s = s.replace(acorn.allLineBreaks, '\n');
  274. var out = [],
  275. idx = s.indexOf("\n");
  276. while (idx !== -1) {
  277. out.push(s.substring(0, idx));
  278. s = s.substring(idx + 1);
  279. idx = s.indexOf("\n");
  280. }
  281. if (s.length) {
  282. out.push(s);
  283. }
  284. return out;
  285. }
  286. function is_array(mode) {
  287. return mode === MODE.ArrayLiteral;
  288. }
  289. function is_expression(mode) {
  290. return in_array(mode, [MODE.Expression, MODE.ForInitializer, MODE.Conditional]);
  291. }
  292. function all_lines_start_with(lines, c) {
  293. for (var i = 0; i < lines.length; i++) {
  294. var line = lines[i].trim();
  295. if (line.charAt(0) !== c) {
  296. return false;
  297. }
  298. }
  299. return true;
  300. }
  301. function each_line_matches_indent(lines, indent) {
  302. var i = 0,
  303. len = lines.length,
  304. line;
  305. for (; i < len; i++) {
  306. line = lines[i];
  307. // allow empty lines to pass through
  308. if (line && line.indexOf(indent) !== 0) {
  309. return false;
  310. }
  311. }
  312. return true;
  313. }
  314. function Beautifier(source_text, options) {
  315. options = options || {};
  316. this._source_text = source_text || '';
  317. this._output = null;
  318. this._tokens = null;
  319. this._last_last_text = null;
  320. this._flags = null;
  321. this._previous_flags = null;
  322. this._flag_store = null;
  323. this._options = new Options(options);
  324. }
  325. Beautifier.prototype.create_flags = function(flags_base, mode) {
  326. var next_indent_level = 0;
  327. if (flags_base) {
  328. next_indent_level = flags_base.indentation_level;
  329. if (!this._output.just_added_newline() &&
  330. flags_base.line_indent_level > next_indent_level) {
  331. next_indent_level = flags_base.line_indent_level;
  332. }
  333. }
  334. var next_flags = {
  335. mode: mode,
  336. parent: flags_base,
  337. last_token: flags_base ? flags_base.last_token : new Token(TOKEN.START_BLOCK, ''), // last token text
  338. last_word: flags_base ? flags_base.last_word : '', // last TOKEN.WORD passed
  339. declaration_statement: false,
  340. declaration_assignment: false,
  341. multiline_frame: false,
  342. inline_frame: false,
  343. if_block: false,
  344. else_block: false,
  345. do_block: false,
  346. do_while: false,
  347. import_block: false,
  348. in_case_statement: false, // switch(..){ INSIDE HERE }
  349. in_case: false, // we're on the exact line with "case 0:"
  350. case_body: false, // the indented case-action block
  351. indentation_level: next_indent_level,
  352. line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
  353. start_line_index: this._output.get_line_number(),
  354. ternary_depth: 0
  355. };
  356. return next_flags;
  357. };
  358. Beautifier.prototype._reset = function(source_text) {
  359. var baseIndentString = source_text.match(/^[\t ]*/)[0];
  360. this._last_last_text = ''; // pre-last token text
  361. this._output = new Output(this._options, baseIndentString);
  362. // If testing the ignore directive, start with output disable set to true
  363. this._output.raw = this._options.test_output_raw;
  364. // Stack of parsing/formatting states, including MODE.
  365. // We tokenize, parse, and output in an almost purely a forward-only stream of token input
  366. // and formatted output. This makes the beautifier less accurate than full parsers
  367. // but also far more tolerant of syntax errors.
  368. //
  369. // For example, the default mode is MODE.BlockStatement. If we see a '{' we push a new frame of type
  370. // MODE.BlockStatement on the the stack, even though it could be object literal. If we later
  371. // encounter a ":", we'll switch to to MODE.ObjectLiteral. If we then see a ";",
  372. // most full parsers would die, but the beautifier gracefully falls back to
  373. // MODE.BlockStatement and continues on.
  374. this._flag_store = [];
  375. this.set_mode(MODE.BlockStatement);
  376. var tokenizer = new Tokenizer(source_text, this._options);
  377. this._tokens = tokenizer.tokenize();
  378. return source_text;
  379. };
  380. Beautifier.prototype.beautify = function() {
  381. // if disabled, return the input unchanged.
  382. if (this._options.disabled) {
  383. return this._source_text;
  384. }
  385. var sweet_code;
  386. var source_text = this._reset(this._source_text);
  387. var eol = this._options.eol;
  388. if (this._options.eol === 'auto') {
  389. eol = '\n';
  390. if (source_text && acorn.lineBreak.test(source_text || '')) {
  391. eol = source_text.match(acorn.lineBreak)[0];
  392. }
  393. }
  394. var current_token = this._tokens.next();
  395. while (current_token) {
  396. this.handle_token(current_token);
  397. this._last_last_text = this._flags.last_token.text;
  398. this._flags.last_token = current_token;
  399. current_token = this._tokens.next();
  400. }
  401. sweet_code = this._output.get_code(eol);
  402. return sweet_code;
  403. };
  404. Beautifier.prototype.handle_token = function(current_token, preserve_statement_flags) {
  405. if (current_token.type === TOKEN.START_EXPR) {
  406. this.handle_start_expr(current_token);
  407. } else if (current_token.type === TOKEN.END_EXPR) {
  408. this.handle_end_expr(current_token);
  409. } else if (current_token.type === TOKEN.START_BLOCK) {
  410. this.handle_start_block(current_token);
  411. } else if (current_token.type === TOKEN.END_BLOCK) {
  412. this.handle_end_block(current_token);
  413. } else if (current_token.type === TOKEN.WORD) {
  414. this.handle_word(current_token);
  415. } else if (current_token.type === TOKEN.RESERVED) {
  416. this.handle_word(current_token);
  417. } else if (current_token.type === TOKEN.SEMICOLON) {
  418. this.handle_semicolon(current_token);
  419. } else if (current_token.type === TOKEN.STRING) {
  420. this.handle_string(current_token);
  421. } else if (current_token.type === TOKEN.EQUALS) {
  422. this.handle_equals(current_token);
  423. } else if (current_token.type === TOKEN.OPERATOR) {
  424. this.handle_operator(current_token);
  425. } else if (current_token.type === TOKEN.COMMA) {
  426. this.handle_comma(current_token);
  427. } else if (current_token.type === TOKEN.BLOCK_COMMENT) {
  428. this.handle_block_comment(current_token, preserve_statement_flags);
  429. } else if (current_token.type === TOKEN.COMMENT) {
  430. this.handle_comment(current_token, preserve_statement_flags);
  431. } else if (current_token.type === TOKEN.DOT) {
  432. this.handle_dot(current_token);
  433. } else if (current_token.type === TOKEN.EOF) {
  434. this.handle_eof(current_token);
  435. } else if (current_token.type === TOKEN.UNKNOWN) {
  436. this.handle_unknown(current_token, preserve_statement_flags);
  437. } else {
  438. this.handle_unknown(current_token, preserve_statement_flags);
  439. }
  440. };
  441. Beautifier.prototype.handle_whitespace_and_comments = function(current_token, preserve_statement_flags) {
  442. var newlines = current_token.newlines;
  443. var keep_whitespace = this._options.keep_array_indentation && is_array(this._flags.mode);
  444. if (current_token.comments_before) {
  445. var comment_token = current_token.comments_before.next();
  446. while (comment_token) {
  447. // The cleanest handling of inline comments is to treat them as though they aren't there.
  448. // Just continue formatting and the behavior should be logical.
  449. // Also ignore unknown tokens. Again, this should result in better behavior.
  450. this.handle_whitespace_and_comments(comment_token, preserve_statement_flags);
  451. this.handle_token(comment_token, preserve_statement_flags);
  452. comment_token = current_token.comments_before.next();
  453. }
  454. }
  455. if (keep_whitespace) {
  456. for (var i = 0; i < newlines; i += 1) {
  457. this.print_newline(i > 0, preserve_statement_flags);
  458. }
  459. } else {
  460. if (this._options.max_preserve_newlines && newlines > this._options.max_preserve_newlines) {
  461. newlines = this._options.max_preserve_newlines;
  462. }
  463. if (this._options.preserve_newlines) {
  464. if (newlines > 1) {
  465. this.print_newline(false, preserve_statement_flags);
  466. for (var j = 1; j < newlines; j += 1) {
  467. this.print_newline(true, preserve_statement_flags);
  468. }
  469. }
  470. }
  471. }
  472. };
  473. var newline_restricted_tokens = ['async', 'break', 'continue', 'return', 'throw', 'yield'];
  474. Beautifier.prototype.allow_wrap_or_preserved_newline = function(current_token, force_linewrap) {
  475. force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
  476. // Never wrap the first token on a line
  477. if (this._output.just_added_newline()) {
  478. return;
  479. }
  480. var shouldPreserveOrForce = (this._options.preserve_newlines && current_token.newlines) || force_linewrap;
  481. var operatorLogicApplies = in_array(this._flags.last_token.text, positionable_operators) ||
  482. in_array(current_token.text, positionable_operators);
  483. if (operatorLogicApplies) {
  484. var shouldPrintOperatorNewline = (
  485. in_array(this._flags.last_token.text, positionable_operators) &&
  486. in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)
  487. ) ||
  488. in_array(current_token.text, positionable_operators);
  489. shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline;
  490. }
  491. if (shouldPreserveOrForce) {
  492. this.print_newline(false, true);
  493. } else if (this._options.wrap_line_length) {
  494. if (reserved_array(this._flags.last_token, newline_restricted_tokens)) {
  495. // These tokens should never have a newline inserted
  496. // between them and the following expression.
  497. return;
  498. }
  499. var proposed_line_length = this._output.current_line.get_character_count() + current_token.text.length +
  500. (this._output.space_before_token ? 1 : 0);
  501. if (proposed_line_length >= this._options.wrap_line_length) {
  502. this.print_newline(false, true);
  503. }
  504. }
  505. };
  506. Beautifier.prototype.print_newline = function(force_newline, preserve_statement_flags) {
  507. if (!preserve_statement_flags) {
  508. if (this._flags.last_token.text !== ';' && this._flags.last_token.text !== ',' && this._flags.last_token.text !== '=' && (this._flags.last_token.type !== TOKEN.OPERATOR || this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) {
  509. var next_token = this._tokens.peek();
  510. while (this._flags.mode === MODE.Statement &&
  511. !(this._flags.if_block && reserved_word(next_token, 'else')) &&
  512. !this._flags.do_block) {
  513. this.restore_mode();
  514. }
  515. }
  516. }
  517. if (this._output.add_new_line(force_newline)) {
  518. this._flags.multiline_frame = true;
  519. }
  520. };
  521. Beautifier.prototype.print_token_line_indentation = function(current_token) {
  522. if (this._output.just_added_newline()) {
  523. if (this._options.keep_array_indentation && is_array(this._flags.mode) && current_token.newlines) {
  524. this._output.current_line.push(current_token.whitespace_before);
  525. this._output.space_before_token = false;
  526. } else if (this._output.set_indent(this._flags.indentation_level)) {
  527. this._flags.line_indent_level = this._flags.indentation_level;
  528. }
  529. }
  530. };
  531. Beautifier.prototype.print_token = function(current_token, printable_token) {
  532. if (this._output.raw) {
  533. this._output.add_raw_token(current_token);
  534. return;
  535. }
  536. if (this._options.comma_first && current_token.previous && current_token.previous.type === TOKEN.COMMA &&
  537. this._output.just_added_newline()) {
  538. if (this._output.previous_line.last() === ',') {
  539. var popped = this._output.previous_line.pop();
  540. // if the comma was already at the start of the line,
  541. // pull back onto that line and reprint the indentation
  542. if (this._output.previous_line.is_empty()) {
  543. this._output.previous_line.push(popped);
  544. this._output.trim(true);
  545. this._output.current_line.pop();
  546. this._output.trim();
  547. }
  548. // add the comma in front of the next token
  549. this.print_token_line_indentation(current_token);
  550. this._output.add_token(',');
  551. this._output.space_before_token = true;
  552. }
  553. }
  554. printable_token = printable_token || current_token.text;
  555. this.print_token_line_indentation(current_token);
  556. this._output.add_token(printable_token);
  557. };
  558. Beautifier.prototype.indent = function() {
  559. this._flags.indentation_level += 1;
  560. };
  561. Beautifier.prototype.deindent = function() {
  562. if (this._flags.indentation_level > 0 &&
  563. ((!this._flags.parent) || this._flags.indentation_level > this._flags.parent.indentation_level)) {
  564. this._flags.indentation_level -= 1;
  565. }
  566. };
  567. Beautifier.prototype.set_mode = function(mode) {
  568. if (this._flags) {
  569. this._flag_store.push(this._flags);
  570. this._previous_flags = this._flags;
  571. } else {
  572. this._previous_flags = this.create_flags(null, mode);
  573. }
  574. this._flags = this.create_flags(this._previous_flags, mode);
  575. };
  576. Beautifier.prototype.restore_mode = function() {
  577. if (this._flag_store.length > 0) {
  578. this._previous_flags = this._flags;
  579. this._flags = this._flag_store.pop();
  580. if (this._previous_flags.mode === MODE.Statement) {
  581. remove_redundant_indentation(this._output, this._previous_flags);
  582. }
  583. }
  584. };
  585. Beautifier.prototype.start_of_object_property = function() {
  586. return this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement && (
  587. (this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || (reserved_array(this._flags.last_token, ['get', 'set'])));
  588. };
  589. Beautifier.prototype.start_of_statement = function(current_token) {
  590. var start = false;
  591. start = start || reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD;
  592. start = start || reserved_word(this._flags.last_token, 'do');
  593. start = start || (!(this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement)) && reserved_array(this._flags.last_token, newline_restricted_tokens) && !current_token.newlines;
  594. start = start || reserved_word(this._flags.last_token, 'else') &&
  595. !(reserved_word(current_token, 'if') && !current_token.comments_before);
  596. start = start || (this._flags.last_token.type === TOKEN.END_EXPR && (this._previous_flags.mode === MODE.ForInitializer || this._previous_flags.mode === MODE.Conditional));
  597. start = start || (this._flags.last_token.type === TOKEN.WORD && this._flags.mode === MODE.BlockStatement &&
  598. !this._flags.in_case &&
  599. !(current_token.text === '--' || current_token.text === '++') &&
  600. this._last_last_text !== 'function' &&
  601. current_token.type !== TOKEN.WORD && current_token.type !== TOKEN.RESERVED);
  602. start = start || (this._flags.mode === MODE.ObjectLiteral && (
  603. (this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || reserved_array(this._flags.last_token, ['get', 'set'])));
  604. if (start) {
  605. this.set_mode(MODE.Statement);
  606. this.indent();
  607. this.handle_whitespace_and_comments(current_token, true);
  608. // Issue #276:
  609. // If starting a new statement with [if, for, while, do], push to a new line.
  610. // if (a) if (b) if(c) d(); else e(); else f();
  611. if (!this.start_of_object_property()) {
  612. this.allow_wrap_or_preserved_newline(current_token,
  613. reserved_array(current_token, ['do', 'for', 'if', 'while']));
  614. }
  615. return true;
  616. }
  617. return false;
  618. };
  619. Beautifier.prototype.handle_start_expr = function(current_token) {
  620. // The conditional starts the statement if appropriate.
  621. if (!this.start_of_statement(current_token)) {
  622. this.handle_whitespace_and_comments(current_token);
  623. }
  624. var next_mode = MODE.Expression;
  625. if (current_token.text === '[') {
  626. if (this._flags.last_token.type === TOKEN.WORD || this._flags.last_token.text === ')') {
  627. // this is array index specifier, break immediately
  628. // a[x], fn()[x]
  629. if (reserved_array(this._flags.last_token, line_starters)) {
  630. this._output.space_before_token = true;
  631. }
  632. this.set_mode(next_mode);
  633. this.print_token(current_token);
  634. this.indent();
  635. if (this._options.space_in_paren) {
  636. this._output.space_before_token = true;
  637. }
  638. return;
  639. }
  640. next_mode = MODE.ArrayLiteral;
  641. if (is_array(this._flags.mode)) {
  642. if (this._flags.last_token.text === '[' ||
  643. (this._flags.last_token.text === ',' && (this._last_last_text === ']' || this._last_last_text === '}'))) {
  644. // ], [ goes to new line
  645. // }, [ goes to new line
  646. if (!this._options.keep_array_indentation) {
  647. this.print_newline();
  648. }
  649. }
  650. }
  651. if (!in_array(this._flags.last_token.type, [TOKEN.START_EXPR, TOKEN.END_EXPR, TOKEN.WORD, TOKEN.OPERATOR])) {
  652. this._output.space_before_token = true;
  653. }
  654. } else {
  655. if (this._flags.last_token.type === TOKEN.RESERVED) {
  656. if (this._flags.last_token.text === 'for') {
  657. this._output.space_before_token = this._options.space_before_conditional;
  658. next_mode = MODE.ForInitializer;
  659. } else if (in_array(this._flags.last_token.text, ['if', 'while'])) {
  660. this._output.space_before_token = this._options.space_before_conditional;
  661. next_mode = MODE.Conditional;
  662. } else if (in_array(this._flags.last_word, ['await', 'async'])) {
  663. // Should be a space between await and an IIFE, or async and an arrow function
  664. this._output.space_before_token = true;
  665. } else if (this._flags.last_token.text === 'import' && current_token.whitespace_before === '') {
  666. this._output.space_before_token = false;
  667. } else if (in_array(this._flags.last_token.text, line_starters) || this._flags.last_token.text === 'catch') {
  668. this._output.space_before_token = true;
  669. }
  670. } else if (this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
  671. // Support of this kind of newline preservation.
  672. // a = (b &&
  673. // (c || d));
  674. if (!this.start_of_object_property()) {
  675. this.allow_wrap_or_preserved_newline(current_token);
  676. }
  677. } else if (this._flags.last_token.type === TOKEN.WORD) {
  678. this._output.space_before_token = false;
  679. // function name() vs function name ()
  680. // function* name() vs function* name ()
  681. // async name() vs async name ()
  682. // In ES6, you can also define the method properties of an object
  683. // var obj = {a: function() {}}
  684. // It can be abbreviated
  685. // var obj = {a() {}}
  686. // var obj = { a() {}} vs var obj = { a () {}}
  687. // var obj = { * a() {}} vs var obj = { * a () {}}
  688. var peek_back_two = this._tokens.peek(-3);
  689. if (this._options.space_after_named_function && peek_back_two) {
  690. // peek starts at next character so -1 is current token
  691. var peek_back_three = this._tokens.peek(-4);
  692. if (reserved_array(peek_back_two, ['async', 'function']) ||
  693. (peek_back_two.text === '*' && reserved_array(peek_back_three, ['async', 'function']))) {
  694. this._output.space_before_token = true;
  695. } else if (this._flags.mode === MODE.ObjectLiteral) {
  696. if ((peek_back_two.text === '{' || peek_back_two.text === ',') ||
  697. (peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
  698. this._output.space_before_token = true;
  699. }
  700. }
  701. }
  702. } else {
  703. // Support preserving wrapped arrow function expressions
  704. // a.b('c',
  705. // () => d.e
  706. // )
  707. this.allow_wrap_or_preserved_newline(current_token);
  708. }
  709. // function() vs function ()
  710. // yield*() vs yield* ()
  711. // function*() vs function* ()
  712. if ((this._flags.last_token.type === TOKEN.RESERVED && (this._flags.last_word === 'function' || this._flags.last_word === 'typeof')) ||
  713. (this._flags.last_token.text === '*' &&
  714. (in_array(this._last_last_text, ['function', 'yield']) ||
  715. (this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
  716. this._output.space_before_token = this._options.space_after_anon_function;
  717. }
  718. }
  719. if (this._flags.last_token.text === ';' || this._flags.last_token.type === TOKEN.START_BLOCK) {
  720. this.print_newline();
  721. } else if (this._flags.last_token.type === TOKEN.END_EXPR || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.END_BLOCK || this._flags.last_token.text === '.' || this._flags.last_token.type === TOKEN.COMMA) {
  722. // do nothing on (( and )( and ][ and ]( and .(
  723. // TODO: Consider whether forcing this is required. Review failing tests when removed.
  724. this.allow_wrap_or_preserved_newline(current_token, current_token.newlines);
  725. }
  726. this.set_mode(next_mode);
  727. this.print_token(current_token);
  728. if (this._options.space_in_paren) {
  729. this._output.space_before_token = true;
  730. }
  731. // In all cases, if we newline while inside an expression it should be indented.
  732. this.indent();
  733. };
  734. Beautifier.prototype.handle_end_expr = function(current_token) {
  735. // statements inside expressions are not valid syntax, but...
  736. // statements must all be closed when their container closes
  737. while (this._flags.mode === MODE.Statement) {
  738. this.restore_mode();
  739. }
  740. this.handle_whitespace_and_comments(current_token);
  741. if (this._flags.multiline_frame) {
  742. this.allow_wrap_or_preserved_newline(current_token,
  743. current_token.text === ']' && is_array(this._flags.mode) && !this._options.keep_array_indentation);
  744. }
  745. if (this._options.space_in_paren) {
  746. if (this._flags.last_token.type === TOKEN.START_EXPR && !this._options.space_in_empty_paren) {
  747. // () [] no inner space in empty parens like these, ever, ref #320
  748. this._output.trim();
  749. this._output.space_before_token = false;
  750. } else {
  751. this._output.space_before_token = true;
  752. }
  753. }
  754. if (current_token.text === ']' && this._options.keep_array_indentation) {
  755. this.print_token(current_token);
  756. this.restore_mode();
  757. } else {
  758. this.restore_mode();
  759. this.print_token(current_token);
  760. }
  761. remove_redundant_indentation(this._output, this._previous_flags);
  762. // do {} while () // no statement required after
  763. if (this._flags.do_while && this._previous_flags.mode === MODE.Conditional) {
  764. this._previous_flags.mode = MODE.Expression;
  765. this._flags.do_block = false;
  766. this._flags.do_while = false;
  767. }
  768. };
  769. Beautifier.prototype.handle_start_block = function(current_token) {
  770. this.handle_whitespace_and_comments(current_token);
  771. // Check if this is should be treated as a ObjectLiteral
  772. var next_token = this._tokens.peek();
  773. var second_token = this._tokens.peek(1);
  774. if (this._flags.last_word === 'switch' && this._flags.last_token.type === TOKEN.END_EXPR) {
  775. this.set_mode(MODE.BlockStatement);
  776. this._flags.in_case_statement = true;
  777. } else if (second_token && (
  778. (in_array(second_token.text, [':', ',']) && in_array(next_token.type, [TOKEN.STRING, TOKEN.WORD, TOKEN.RESERVED])) ||
  779. (in_array(next_token.text, ['get', 'set', '...']) && in_array(second_token.type, [TOKEN.WORD, TOKEN.RESERVED]))
  780. )) {
  781. // We don't support TypeScript,but we didn't break it for a very long time.
  782. // We'll try to keep not breaking it.
  783. if (!in_array(this._last_last_text, ['class', 'interface'])) {
  784. this.set_mode(MODE.ObjectLiteral);
  785. } else {
  786. this.set_mode(MODE.BlockStatement);
  787. }
  788. } else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
  789. // arrow function: (param1, paramN) => { statements }
  790. this.set_mode(MODE.BlockStatement);
  791. } else if (in_array(this._flags.last_token.type, [TOKEN.EQUALS, TOKEN.START_EXPR, TOKEN.COMMA, TOKEN.OPERATOR]) ||
  792. reserved_array(this._flags.last_token, ['return', 'throw', 'import', 'default'])
  793. ) {
  794. // Detecting shorthand function syntax is difficult by scanning forward,
  795. // so check the surrounding context.
  796. // If the block is being returned, imported, export default, passed as arg,
  797. // assigned with = or assigned in a nested object, treat as an ObjectLiteral.
  798. this.set_mode(MODE.ObjectLiteral);
  799. } else {
  800. this.set_mode(MODE.BlockStatement);
  801. }
  802. var empty_braces = !next_token.comments_before && next_token.text === '}';
  803. var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
  804. this._flags.last_token.type === TOKEN.END_EXPR;
  805. if (this._options.brace_preserve_inline) // check for inline, set inline_frame if so
  806. {
  807. // search forward for a newline wanted inside this block
  808. var index = 0;
  809. var check_token = null;
  810. this._flags.inline_frame = true;
  811. do {
  812. index += 1;
  813. check_token = this._tokens.peek(index - 1);
  814. if (check_token.newlines) {
  815. this._flags.inline_frame = false;
  816. break;
  817. }
  818. } while (check_token.type !== TOKEN.EOF &&
  819. !(check_token.type === TOKEN.END_BLOCK && check_token.opened === current_token));
  820. }
  821. if ((this._options.brace_style === "expand" ||
  822. (this._options.brace_style === "none" && current_token.newlines)) &&
  823. !this._flags.inline_frame) {
  824. if (this._flags.last_token.type !== TOKEN.OPERATOR &&
  825. (empty_anonymous_function ||
  826. this._flags.last_token.type === TOKEN.EQUALS ||
  827. (reserved_array(this._flags.last_token, special_words) && this._flags.last_token.text !== 'else'))) {
  828. this._output.space_before_token = true;
  829. } else {
  830. this.print_newline(false, true);
  831. }
  832. } else { // collapse || inline_frame
  833. if (is_array(this._previous_flags.mode) && (this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.COMMA)) {
  834. if (this._flags.last_token.type === TOKEN.COMMA || this._options.space_in_paren) {
  835. this._output.space_before_token = true;
  836. }
  837. if (this._flags.last_token.type === TOKEN.COMMA || (this._flags.last_token.type === TOKEN.START_EXPR && this._flags.inline_frame)) {
  838. this.allow_wrap_or_preserved_newline(current_token);
  839. this._previous_flags.multiline_frame = this._previous_flags.multiline_frame || this._flags.multiline_frame;
  840. this._flags.multiline_frame = false;
  841. }
  842. }
  843. if (this._flags.last_token.type !== TOKEN.OPERATOR && this._flags.last_token.type !== TOKEN.START_EXPR) {
  844. if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) {
  845. this.print_newline();
  846. } else {
  847. this._output.space_before_token = true;
  848. }
  849. }
  850. }
  851. this.print_token(current_token);
  852. this.indent();
  853. // Except for specific cases, open braces are followed by a new line.
  854. if (!empty_braces && !(this._options.brace_preserve_inline && this._flags.inline_frame)) {
  855. this.print_newline();
  856. }
  857. };
  858. Beautifier.prototype.handle_end_block = function(current_token) {
  859. // statements must all be closed when their container closes
  860. this.handle_whitespace_and_comments(current_token);
  861. while (this._flags.mode === MODE.Statement) {
  862. this.restore_mode();
  863. }
  864. var empty_braces = this._flags.last_token.type === TOKEN.START_BLOCK;
  865. if (this._flags.inline_frame && !empty_braces) { // try inline_frame (only set if this._options.braces-preserve-inline) first
  866. this._output.space_before_token = true;
  867. } else if (this._options.brace_style === "expand") {
  868. if (!empty_braces) {
  869. this.print_newline();
  870. }
  871. } else {
  872. // skip {}
  873. if (!empty_braces) {
  874. if (is_array(this._flags.mode) && this._options.keep_array_indentation) {
  875. // we REALLY need a newline here, but newliner would skip that
  876. this._options.keep_array_indentation = false;
  877. this.print_newline();
  878. this._options.keep_array_indentation = true;
  879. } else {
  880. this.print_newline();
  881. }
  882. }
  883. }
  884. this.restore_mode();
  885. this.print_token(current_token);
  886. };
  887. Beautifier.prototype.handle_word = function(current_token) {
  888. if (current_token.type === TOKEN.RESERVED) {
  889. if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
  890. current_token.type = TOKEN.WORD;
  891. } else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
  892. current_token.type = TOKEN.WORD;
  893. } else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
  894. current_token.type = TOKEN.WORD;
  895. } else if (this._flags.mode === MODE.ObjectLiteral) {
  896. var next_token = this._tokens.peek();
  897. if (next_token.text === ':') {
  898. current_token.type = TOKEN.WORD;
  899. }
  900. }
  901. }
  902. if (this.start_of_statement(current_token)) {
  903. // The conditional starts the statement if appropriate.
  904. if (reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD) {
  905. this._flags.declaration_statement = true;
  906. }
  907. } else if (current_token.newlines && !is_expression(this._flags.mode) &&
  908. (this._flags.last_token.type !== TOKEN.OPERATOR || (this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) &&
  909. this._flags.last_token.type !== TOKEN.EQUALS &&
  910. (this._options.preserve_newlines || !reserved_array(this._flags.last_token, ['var', 'let', 'const', 'set', 'get']))) {
  911. this.handle_whitespace_and_comments(current_token);
  912. this.print_newline();
  913. } else {
  914. this.handle_whitespace_and_comments(current_token);
  915. }
  916. if (this._flags.do_block && !this._flags.do_while) {
  917. if (reserved_word(current_token, 'while')) {
  918. // do {} ## while ()
  919. this._output.space_before_token = true;
  920. this.print_token(current_token);
  921. this._output.space_before_token = true;
  922. this._flags.do_while = true;
  923. return;
  924. } else {
  925. // do {} should always have while as the next word.
  926. // if we don't see the expected while, recover
  927. this.print_newline();
  928. this._flags.do_block = false;
  929. }
  930. }
  931. // if may be followed by else, or not
  932. // Bare/inline ifs are tricky
  933. // Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
  934. if (this._flags.if_block) {
  935. if (!this._flags.else_block && reserved_word(current_token, 'else')) {
  936. this._flags.else_block = true;
  937. } else {
  938. while (this._flags.mode === MODE.Statement) {
  939. this.restore_mode();
  940. }
  941. this._flags.if_block = false;
  942. this._flags.else_block = false;
  943. }
  944. }
  945. if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
  946. this.print_newline();
  947. if (this._flags.case_body || this._options.jslint_happy) {
  948. // switch cases following one another
  949. this.deindent();
  950. this._flags.case_body = false;
  951. }
  952. this.print_token(current_token);
  953. this._flags.in_case = true;
  954. return;
  955. }
  956. if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
  957. if (!this.start_of_object_property()) {
  958. this.allow_wrap_or_preserved_newline(current_token);
  959. }
  960. }
  961. if (reserved_word(current_token, 'function')) {
  962. if (in_array(this._flags.last_token.text, ['}', ';']) ||
  963. (this._output.just_added_newline() && !(in_array(this._flags.last_token.text, ['(', '[', '{', ':', '=', ',']) || this._flags.last_token.type === TOKEN.OPERATOR))) {
  964. // make sure there is a nice clean space of at least one blank line
  965. // before a new function definition
  966. if (!this._output.just_added_blankline() && !current_token.comments_before) {
  967. this.print_newline();
  968. this.print_newline(true);
  969. }
  970. }
  971. if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD) {
  972. if (reserved_array(this._flags.last_token, ['get', 'set', 'new', 'export']) ||
  973. reserved_array(this._flags.last_token, newline_restricted_tokens)) {
  974. this._output.space_before_token = true;
  975. } else if (reserved_word(this._flags.last_token, 'default') && this._last_last_text === 'export') {
  976. this._output.space_before_token = true;
  977. } else if (this._flags.last_token.text === 'declare') {
  978. // accomodates Typescript declare function formatting
  979. this._output.space_before_token = true;
  980. } else {
  981. this.print_newline();
  982. }
  983. } else if (this._flags.last_token.type === TOKEN.OPERATOR || this._flags.last_token.text === '=') {
  984. // foo = function
  985. this._output.space_before_token = true;
  986. } else if (!this._flags.multiline_frame && (is_expression(this._flags.mode) || is_array(this._flags.mode))) {
  987. // (function
  988. } else {
  989. this.print_newline();
  990. }
  991. this.print_token(current_token);
  992. this._flags.last_word = current_token.text;
  993. return;
  994. }
  995. var prefix = 'NONE';
  996. if (this._flags.last_token.type === TOKEN.END_BLOCK) {
  997. if (this._previous_flags.inline_frame) {
  998. prefix = 'SPACE';
  999. } else if (!reserved_array(current_token, ['else', 'catch', 'finally', 'from'])) {
  1000. prefix = 'NEWLINE';
  1001. } else {
  1002. if (this._options.brace_style === "expand" ||
  1003. this._options.brace_style === "end-expand" ||
  1004. (this._options.brace_style === "none" && current_token.newlines)) {
  1005. prefix = 'NEWLINE';
  1006. } else {
  1007. prefix = 'SPACE';
  1008. this._output.space_before_token = true;
  1009. }
  1010. }
  1011. } else if (this._flags.last_token.type === TOKEN.SEMICOLON && this._flags.mode === MODE.BlockStatement) {
  1012. // TODO: Should this be for STATEMENT as well?
  1013. prefix = 'NEWLINE';
  1014. } else if (this._flags.last_token.type === TOKEN.SEMICOLON && is_expression(this._flags.mode)) {
  1015. prefix = 'SPACE';
  1016. } else if (this._flags.last_token.type === TOKEN.STRING) {
  1017. prefix = 'NEWLINE';
  1018. } else if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD ||
  1019. (this._flags.last_token.text === '*' &&
  1020. (in_array(this._last_last_text, ['function', 'yield']) ||
  1021. (this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
  1022. prefix = 'SPACE';
  1023. } else if (this._flags.last_token.type === TOKEN.START_BLOCK) {
  1024. if (this._flags.inline_frame) {
  1025. prefix = 'SPACE';
  1026. } else {
  1027. prefix = 'NEWLINE';
  1028. }
  1029. } else if (this._flags.last_token.type === TOKEN.END_EXPR) {
  1030. this._output.space_before_token = true;
  1031. prefix = 'NEWLINE';
  1032. }
  1033. if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
  1034. if (this._flags.inline_frame || this._flags.last_token.text === 'else' || this._flags.last_token.text === 'export') {
  1035. prefix = 'SPACE';
  1036. } else {
  1037. prefix = 'NEWLINE';
  1038. }
  1039. }
  1040. if (reserved_array(current_token, ['else', 'catch', 'finally'])) {
  1041. if ((!(this._flags.last_token.type === TOKEN.END_BLOCK && this._previous_flags.mode === MODE.BlockStatement) ||
  1042. this._options.brace_style === "expand" ||
  1043. this._options.brace_style === "end-expand" ||
  1044. (this._options.brace_style === "none" && current_token.newlines)) &&
  1045. !this._flags.inline_frame) {
  1046. this.print_newline();
  1047. } else {
  1048. this._output.trim(true);
  1049. var line = this._output.current_line;
  1050. // If we trimmed and there's something other than a close block before us
  1051. // put a newline back in. Handles '} // comment' scenario.
  1052. if (line.last() !== '}') {
  1053. this.print_newline();
  1054. }
  1055. this._output.space_before_token = true;
  1056. }
  1057. } else if (prefix === 'NEWLINE') {
  1058. if (reserved_array(this._flags.last_token, special_words)) {
  1059. // no newline between 'return nnn'
  1060. this._output.space_before_token = true;
  1061. } else if (this._flags.last_token.text === 'declare' && reserved_array(current_token, ['var', 'let', 'const'])) {
  1062. // accomodates Typescript declare formatting
  1063. this._output.space_before_token = true;
  1064. } else if (this._flags.last_token.type !== TOKEN.END_EXPR) {
  1065. if ((this._flags.last_token.type !== TOKEN.START_EXPR || !reserved_array(current_token, ['var', 'let', 'const'])) && this._flags.last_token.text !== ':') {
  1066. // no need to force newline on 'var': for (var x = 0...)
  1067. if (reserved_word(current_token, 'if') && reserved_word(current_token.previous, 'else')) {
  1068. // no newline for } else if {
  1069. this._output.space_before_token = true;
  1070. } else {
  1071. this.print_newline();
  1072. }
  1073. }
  1074. } else if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
  1075. this.print_newline();
  1076. }
  1077. } else if (this._flags.multiline_frame && is_array(this._flags.mode) && this._flags.last_token.text === ',' && this._last_last_text === '}') {
  1078. this.print_newline(); // }, in lists get a newline treatment
  1079. } else if (prefix === 'SPACE') {
  1080. this._output.space_before_token = true;
  1081. }
  1082. if (current_token.previous && (current_token.previous.type === TOKEN.WORD || current_token.previous.type === TOKEN.RESERVED)) {
  1083. this._output.space_before_token = true;
  1084. }
  1085. this.print_token(current_token);
  1086. this._flags.last_word = current_token.text;
  1087. if (current_token.type === TOKEN.RESERVED) {
  1088. if (current_token.text === 'do') {
  1089. this._flags.do_block = true;
  1090. } else if (current_token.text === 'if') {
  1091. this._flags.if_block = true;
  1092. } else if (current_token.text === 'import') {
  1093. this._flags.import_block = true;
  1094. } else if (this._flags.import_block && reserved_word(current_token, 'from')) {
  1095. this._flags.import_block = false;
  1096. }
  1097. }
  1098. };
  1099. Beautifier.prototype.handle_semicolon = function(current_token) {
  1100. if (this.start_of_statement(current_token)) {
  1101. // The conditional starts the statement if appropriate.
  1102. // Semicolon can be the start (and end) of a statement
  1103. this._output.space_before_token = false;
  1104. } else {
  1105. this.handle_whitespace_and_comments(current_token);
  1106. }
  1107. var next_token = this._tokens.peek();
  1108. while (this._flags.mode === MODE.Statement &&
  1109. !(this._flags.if_block && reserved_word(next_token, 'else')) &&
  1110. !this._flags.do_block) {
  1111. this.restore_mode();
  1112. }
  1113. // hacky but effective for the moment
  1114. if (this._flags.import_block) {
  1115. this._flags.import_block = false;
  1116. }
  1117. this.print_token(current_token);
  1118. };
  1119. Beautifier.prototype.handle_string = function(current_token) {
  1120. if (this.start_of_statement(current_token)) {
  1121. // The conditional starts the statement if appropriate.
  1122. // One difference - strings want at least a space before
  1123. this._output.space_before_token = true;
  1124. } else {
  1125. this.handle_whitespace_and_comments(current_token);
  1126. if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD || this._flags.inline_frame) {
  1127. this._output.space_before_token = true;
  1128. } else if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
  1129. if (!this.start_of_object_property()) {
  1130. this.allow_wrap_or_preserved_newline(current_token);
  1131. }
  1132. } else {
  1133. this.print_newline();
  1134. }
  1135. }
  1136. this.print_token(current_token);
  1137. };
  1138. Beautifier.prototype.handle_equals = function(current_token) {
  1139. if (this.start_of_statement(current_token)) {
  1140. // The conditional starts the statement if appropriate.
  1141. } else {
  1142. this.handle_whitespace_and_comments(current_token);
  1143. }
  1144. if (this._flags.declaration_statement) {
  1145. // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
  1146. this._flags.declaration_assignment = true;
  1147. }
  1148. this._output.space_before_token = true;
  1149. this.print_token(current_token);
  1150. this._output.space_before_token = true;
  1151. };
  1152. Beautifier.prototype.handle_comma = function(current_token) {
  1153. this.handle_whitespace_and_comments(current_token, true);
  1154. this.print_token(current_token);
  1155. this._output.space_before_token = true;
  1156. if (this._flags.declaration_statement) {
  1157. if (is_expression(this._flags.parent.mode)) {
  1158. // do not break on comma, for(var a = 1, b = 2)
  1159. this._flags.declaration_assignment = false;
  1160. }
  1161. if (this._flags.declaration_assignment) {
  1162. this._flags.declaration_assignment = false;
  1163. this.print_newline(false, true);
  1164. } else if (this._options.comma_first) {
  1165. // for comma-first, we want to allow a newline before the comma
  1166. // to turn into a newline after the comma, which we will fixup later
  1167. this.allow_wrap_or_preserved_newline(current_token);
  1168. }
  1169. } else if (this._flags.mode === MODE.ObjectLiteral ||
  1170. (this._flags.mode === MODE.Statement && this._flags.parent.mode === MODE.ObjectLiteral)) {
  1171. if (this._flags.mode === MODE.Statement) {
  1172. this.restore_mode();
  1173. }
  1174. if (!this._flags.inline_frame) {
  1175. this.print_newline();
  1176. }
  1177. } else if (this._options.comma_first) {
  1178. // EXPR or DO_BLOCK
  1179. // for comma-first, we want to allow a newline before the comma
  1180. // to turn into a newline after the comma, which we will fixup later
  1181. this.allow_wrap_or_preserved_newline(current_token);
  1182. }
  1183. };
  1184. Beautifier.prototype.handle_operator = function(current_token) {
  1185. var isGeneratorAsterisk = current_token.text === '*' &&
  1186. (reserved_array(this._flags.last_token, ['function', 'yield']) ||
  1187. (in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.COMMA, TOKEN.END_BLOCK, TOKEN.SEMICOLON]))
  1188. );
  1189. var isUnary = in_array(current_token.text, ['-', '+']) && (
  1190. in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.START_EXPR, TOKEN.EQUALS, TOKEN.OPERATOR]) ||
  1191. in_array(this._flags.last_token.text, line_starters) ||
  1192. this._flags.last_token.text === ','
  1193. );
  1194. if (this.start_of_statement(current_token)) {
  1195. // The conditional starts the statement if appropriate.
  1196. } else {
  1197. var preserve_statement_flags = !isGeneratorAsterisk;
  1198. this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
  1199. }
  1200. if (reserved_array(this._flags.last_token, special_words)) {
  1201. // "return" had a special handling in TK_WORD. Now we need to return the favor
  1202. this._output.space_before_token = true;
  1203. this.print_token(current_token);
  1204. return;
  1205. }
  1206. // hack for actionscript's import .*;
  1207. if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
  1208. this.print_token(current_token);
  1209. return;
  1210. }
  1211. if (current_token.text === '::') {
  1212. // no spaces around exotic namespacing syntax operator
  1213. this.print_token(current_token);
  1214. return;
  1215. }
  1216. // Allow line wrapping between operators when operator_position is
  1217. // set to before or preserve
  1218. if (this._flags.last_token.type === TOKEN.OPERATOR && in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) {
  1219. this.allow_wrap_or_preserved_newline(current_token);
  1220. }
  1221. if (current_token.text === ':' && this._flags.in_case) {
  1222. this._flags.case_body = true;
  1223. this.indent();
  1224. this.print_token(current_token);
  1225. this.print_newline();
  1226. this._flags.in_case = false;
  1227. return;
  1228. }
  1229. var space_before = true;
  1230. var space_after = true;
  1231. var in_ternary = false;
  1232. if (current_token.text === ':') {
  1233. if (this._flags.ternary_depth === 0) {
  1234. // Colon is invalid javascript outside of ternary and object, but do our best to guess what was meant.
  1235. space_before = false;
  1236. } else {
  1237. this._flags.ternary_depth -= 1;
  1238. in_ternary = true;
  1239. }
  1240. } else if (current_token.text === '?') {
  1241. this._flags.ternary_depth += 1;
  1242. }
  1243. // let's handle the operator_position option prior to any conflicting logic
  1244. if (!isUnary && !isGeneratorAsterisk && this._options.preserve_newlines && in_array(current_token.text, positionable_operators)) {
  1245. var isColon = current_token.text === ':';
  1246. var isTernaryColon = (isColon && in_ternary);
  1247. var isOtherColon = (isColon && !in_ternary);
  1248. switch (this._options.operator_position) {
  1249. case OPERATOR_POSITION.before_newline:
  1250. // if the current token is : and it's not a ternary statement then we set space_before to false
  1251. this._output.space_before_token = !isOtherColon;
  1252. this.print_token(current_token);
  1253. if (!isColon || isTernaryColon) {
  1254. this.allow_wrap_or_preserved_newline(current_token);
  1255. }
  1256. this._output.space_before_token = true;
  1257. return;
  1258. case OPERATOR_POSITION.after_newline:
  1259. // if the current token is anything but colon, or (via deduction) it's a colon and in a ternary statement,
  1260. // then print a newline.
  1261. this._output.space_before_token = true;
  1262. if (!isColon || isTernaryColon) {
  1263. if (this._tokens.peek().newlines) {
  1264. this.print_newline(false, true);
  1265. } else {
  1266. this.allow_wrap_or_preserved_newline(current_token);
  1267. }
  1268. } else {
  1269. this._output.space_before_token = false;
  1270. }
  1271. this.print_token(current_token);
  1272. this._output.space_before_token = true;
  1273. return;
  1274. case OPERATOR_POSITION.preserve_newline:
  1275. if (!isOtherColon) {
  1276. this.allow_wrap_or_preserved_newline(current_token);
  1277. }
  1278. // if we just added a newline, or the current token is : and it's not a ternary statement,
  1279. // then we set space_before to false
  1280. space_before = !(this._output.just_added_newline() || isOtherColon);
  1281. this._output.space_before_token = space_before;
  1282. this.print_token(current_token);
  1283. this._output.space_before_token = true;
  1284. return;
  1285. }
  1286. }
  1287. if (isGeneratorAsterisk) {
  1288. this.allow_wrap_or_preserved_newline(current_token);
  1289. space_before = false;
  1290. var next_token = this._tokens.peek();
  1291. space_after = next_token && in_array(next_token.type, [TOKEN.WORD, TOKEN.RESERVED]);
  1292. } else if (current_token.text === '...') {
  1293. this.allow_wrap_or_preserved_newline(current_token);
  1294. space_before = this._flags.last_token.type === TOKEN.START_BLOCK;
  1295. space_after = false;
  1296. } else if (in_array(current_token.text, ['--', '++', '!', '~']) || isUnary) {
  1297. // unary operators (and binary +/- pretending to be unary) special cases
  1298. if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR) {
  1299. this.allow_wrap_or_preserved_newline(current_token);
  1300. }
  1301. space_before = false;
  1302. space_after = false;
  1303. // http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
  1304. // if there is a newline between -- or ++ and anything else we should preserve it.
  1305. if (current_token.newlines && (current_token.text === '--' || current_token.text === '++')) {
  1306. this.print_newline(false, true);
  1307. }
  1308. if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
  1309. // for (;; ++i)
  1310. // ^^^
  1311. space_before = true;
  1312. }
  1313. if (this._flags.last_token.type === TOKEN.RESERVED) {
  1314. space_before = true;
  1315. } else if (this._flags.last_token.type === TOKEN.END_EXPR) {
  1316. space_before = !(this._flags.last_token.text === ']' && (current_token.text === '--' || current_token.text === '++'));
  1317. } else if (this._flags.last_token.type === TOKEN.OPERATOR) {
  1318. // a++ + ++b;
  1319. // a - -b
  1320. space_before = in_array(current_token.text, ['--', '-', '++', '+']) && in_array(this._flags.last_token.text, ['--', '-', '++', '+']);
  1321. // + and - are not unary when preceeded by -- or ++ operator
  1322. // a-- + b
  1323. // a * +b
  1324. // a - -b
  1325. if (in_array(current_token.text, ['+', '-']) && in_array(this._flags.last_token.text, ['--', '++'])) {
  1326. space_after = true;
  1327. }
  1328. }
  1329. if (((this._flags.mode === MODE.BlockStatement && !this._flags.inline_frame) || this._flags.mode === MODE.Statement) &&
  1330. (this._flags.last_token.text === '{' || this._flags.last_token.text === ';')) {
  1331. // { foo; --i }
  1332. // foo(); --bar;
  1333. this.print_newline();
  1334. }
  1335. }
  1336. this._output.space_before_token = this._output.space_before_token || space_before;
  1337. this.print_token(current_token);
  1338. this._output.space_before_token = space_after;
  1339. };
  1340. Beautifier.prototype.handle_block_comment = function(current_token, preserve_statement_flags) {
  1341. if (this._output.raw) {
  1342. this._output.add_raw_token(current_token);
  1343. if (current_token.directives && current_token.directives.preserve === 'end') {
  1344. // If we're testing the raw output behavior, do not allow a directive to turn it off.
  1345. this._output.raw = this._options.test_output_raw;
  1346. }
  1347. return;
  1348. }
  1349. if (current_token.directives) {
  1350. this.print_newline(false, preserve_statement_flags);
  1351. this.print_token(current_token);
  1352. if (current_token.directives.preserve === 'start') {
  1353. this._output.raw = true;
  1354. }
  1355. this.print_newline(false, true);
  1356. return;
  1357. }
  1358. // inline block
  1359. if (!acorn.newline.test(current_token.text) && !current_token.newlines) {
  1360. this._output.space_before_token = true;
  1361. this.print_token(current_token);
  1362. this._output.space_before_token = true;
  1363. return;
  1364. }
  1365. var lines = split_linebreaks(current_token.text);
  1366. var j; // iterator for this case
  1367. var javadoc = false;
  1368. var starless = false;
  1369. var lastIndent = current_token.whitespace_before;
  1370. var lastIndentLength = lastIndent.length;
  1371. // block comment starts with a new line
  1372. this.print_newline(false, preserve_statement_flags);
  1373. if (lines.length > 1) {
  1374. javadoc = all_lines_start_with(lines.slice(1), '*');
  1375. starless = each_line_matches_indent(lines.slice(1), lastIndent);
  1376. }
  1377. // first line always indented
  1378. this.print_token(current_token, lines[0]);
  1379. for (j = 1; j < lines.length; j++) {
  1380. this.print_newline(false, true);
  1381. if (javadoc) {
  1382. // javadoc: reformat and re-indent
  1383. this.print_token(current_token, ' ' + ltrim(lines[j]));
  1384. } else if (starless && lines[j].length > lastIndentLength) {
  1385. // starless: re-indent non-empty content, avoiding trim
  1386. this.print_token(current_token, lines[j].substring(lastIndentLength));
  1387. } else {
  1388. // normal comments output raw
  1389. this._output.add_token(lines[j]);
  1390. }
  1391. }
  1392. // for comments of more than one line, make sure there's a new line after
  1393. this.print_newline(false, preserve_statement_flags);
  1394. };
  1395. Beautifier.prototype.handle_comment = function(current_token, preserve_statement_flags) {
  1396. if (current_token.newlines) {
  1397. this.print_newline(false, preserve_statement_flags);
  1398. } else {
  1399. this._output.trim(true);
  1400. }
  1401. this._output.space_before_token = true;
  1402. this.print_token(current_token);
  1403. this.print_newline(false, preserve_statement_flags);
  1404. };
  1405. Beautifier.prototype.handle_dot = function(current_token) {
  1406. if (this.start_of_statement(current_token)) {
  1407. // The conditional starts the statement if appropriate.
  1408. } else {
  1409. this.handle_whitespace_and_comments(current_token, true);
  1410. }
  1411. if (reserved_array(this._flags.last_token, special_words)) {
  1412. this._output.space_before_token = false;
  1413. } else {
  1414. // allow preserved newlines before dots in general
  1415. // force newlines on dots after close paren when break_chained - for bar().baz()
  1416. this.allow_wrap_or_preserved_newline(current_token,
  1417. this._flags.last_token.text === ')' && this._options.break_chained_methods);
  1418. }
  1419. // Only unindent chained method dot if this dot starts a new line.
  1420. // Otherwise the automatic extra indentation removal will handle the over indent
  1421. if (this._options.unindent_chained_methods && this._output.just_added_newline()) {
  1422. this.deindent();
  1423. }
  1424. this.print_token(current_token);
  1425. };
  1426. Beautifier.prototype.handle_unknown = function(current_token, preserve_statement_flags) {
  1427. this.print_token(current_token);
  1428. if (current_token.text[current_token.text.length - 1] === '\n') {
  1429. this.print_newline(false, preserve_statement_flags);
  1430. }
  1431. };
  1432. Beautifier.prototype.handle_eof = function(current_token) {
  1433. // Unwind any open statements
  1434. while (this._flags.mode === MODE.Statement) {
  1435. this.restore_mode();
  1436. }
  1437. this.handle_whitespace_and_comments(current_token);
  1438. };
  1439. module.exports.Beautifier = Beautifier;
  1440. /***/ }),
  1441. /* 2 */
  1442. /***/ (function(module, exports, __webpack_require__) {
  1443. "use strict";
  1444. /*jshint node:true */
  1445. /*
  1446. The MIT License (MIT)
  1447. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1448. Permission is hereby granted, free of charge, to any person
  1449. obtaining a copy of this software and associated documentation files
  1450. (the "Software"), to deal in the Software without restriction,
  1451. including without limitation the rights to use, copy, modify, merge,
  1452. publish, distribute, sublicense, and/or sell copies of the Software,
  1453. and to permit persons to whom the Software is furnished to do so,
  1454. subject to the following conditions:
  1455. The above copyright notice and this permission notice shall be
  1456. included in all copies or substantial portions of the Software.
  1457. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1458. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1459. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1460. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1461. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1462. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1463. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1464. SOFTWARE.
  1465. */
  1466. function OutputLine(parent) {
  1467. this.__parent = parent;
  1468. this.__character_count = 0;
  1469. // use indent_count as a marker for this.__lines that have preserved indentation
  1470. this.__indent_count = -1;
  1471. this.__alignment_count = 0;
  1472. this.__items = [];
  1473. }
  1474. OutputLine.prototype.item = function(index) {
  1475. if (index < 0) {
  1476. return this.__items[this.__items.length + index];
  1477. } else {
  1478. return this.__items[index];
  1479. }
  1480. };
  1481. OutputLine.prototype.has_match = function(pattern) {
  1482. for (var lastCheckedOutput = this.__items.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) {
  1483. if (this.__items[lastCheckedOutput].match(pattern)) {
  1484. return true;
  1485. }
  1486. }
  1487. return false;
  1488. };
  1489. OutputLine.prototype.set_indent = function(indent, alignment) {
  1490. this.__indent_count = indent || 0;
  1491. this.__alignment_count = alignment || 0;
  1492. this.__character_count = this.__parent.baseIndentLength + this.__alignment_count + this.__indent_count * this.__parent.indent_length;
  1493. };
  1494. OutputLine.prototype.get_character_count = function() {
  1495. return this.__character_count;
  1496. };
  1497. OutputLine.prototype.is_empty = function() {
  1498. return this.__items.length === 0;
  1499. };
  1500. OutputLine.prototype.last = function() {
  1501. if (!this.is_empty()) {
  1502. return this.__items[this.__items.length - 1];
  1503. } else {
  1504. return null;
  1505. }
  1506. };
  1507. OutputLine.prototype.push = function(item) {
  1508. this.__items.push(item);
  1509. this.__character_count += item.length;
  1510. };
  1511. OutputLine.prototype.push_raw = function(item) {
  1512. this.push(item);
  1513. var last_newline_index = item.lastIndexOf('\n');
  1514. if (last_newline_index !== -1) {
  1515. this.__character_count = item.length - last_newline_index;
  1516. }
  1517. };
  1518. OutputLine.prototype.pop = function() {
  1519. var item = null;
  1520. if (!this.is_empty()) {
  1521. item = this.__items.pop();
  1522. this.__character_count -= item.length;
  1523. }
  1524. return item;
  1525. };
  1526. OutputLine.prototype.remove_indent = function() {
  1527. if (this.__indent_count > 0) {
  1528. this.__indent_count -= 1;
  1529. this.__character_count -= this.__parent.indent_length;
  1530. }
  1531. };
  1532. OutputLine.prototype.trim = function() {
  1533. while (this.last() === ' ') {
  1534. this.__items.pop();
  1535. this.__character_count -= 1;
  1536. }
  1537. };
  1538. OutputLine.prototype.toString = function() {
  1539. var result = '';
  1540. if (!this.is_empty()) {
  1541. if (this.__indent_count >= 0) {
  1542. result = this.__parent.get_indent_string(this.__indent_count);
  1543. }
  1544. if (this.__alignment_count >= 0) {
  1545. result += this.__parent.get_alignment_string(this.__alignment_count);
  1546. }
  1547. result += this.__items.join('');
  1548. }
  1549. return result;
  1550. };
  1551. function IndentCache(base_string, level_string) {
  1552. this.__cache = [base_string];
  1553. this.__level_string = level_string;
  1554. }
  1555. IndentCache.prototype.__ensure_cache = function(level) {
  1556. while (level >= this.__cache.length) {
  1557. this.__cache.push(this.__cache[this.__cache.length - 1] + this.__level_string);
  1558. }
  1559. };
  1560. IndentCache.prototype.get_level_string = function(level) {
  1561. this.__ensure_cache(level);
  1562. return this.__cache[level];
  1563. };
  1564. function Output(options, baseIndentString) {
  1565. var indent_string = options.indent_char;
  1566. if (options.indent_size > 1) {
  1567. indent_string = new Array(options.indent_size + 1).join(options.indent_char);
  1568. }
  1569. // Set to null to continue support for auto detection of base indent level.
  1570. baseIndentString = baseIndentString || '';
  1571. if (options.indent_level > 0) {
  1572. baseIndentString = new Array(options.indent_level + 1).join(indent_string);
  1573. }
  1574. this.__indent_cache = new IndentCache(baseIndentString, indent_string);
  1575. this.__alignment_cache = new IndentCache('', ' ');
  1576. this.baseIndentLength = baseIndentString.length;
  1577. this.indent_length = indent_string.length;
  1578. this.raw = false;
  1579. this._end_with_newline = options.end_with_newline;
  1580. this.__lines = [];
  1581. this.previous_line = null;
  1582. this.current_line = null;
  1583. this.space_before_token = false;
  1584. // initialize
  1585. this.__add_outputline();
  1586. }
  1587. Output.prototype.__add_outputline = function() {
  1588. this.previous_line = this.current_line;
  1589. this.current_line = new OutputLine(this);
  1590. this.__lines.push(this.current_line);
  1591. };
  1592. Output.prototype.get_line_number = function() {
  1593. return this.__lines.length;
  1594. };
  1595. Output.prototype.get_indent_string = function(level) {
  1596. return this.__indent_cache.get_level_string(level);
  1597. };
  1598. Output.prototype.get_alignment_string = function(level) {
  1599. return this.__alignment_cache.get_level_string(level);
  1600. };
  1601. Output.prototype.is_empty = function() {
  1602. return !this.previous_line && this.current_line.is_empty();
  1603. };
  1604. Output.prototype.add_new_line = function(force_newline) {
  1605. // never newline at the start of file
  1606. // otherwise, newline only if we didn't just add one or we're forced
  1607. if (this.is_empty() ||
  1608. (!force_newline && this.just_added_newline())) {
  1609. return false;
  1610. }
  1611. // if raw output is enabled, don't print additional newlines,
  1612. // but still return True as though you had
  1613. if (!this.raw) {
  1614. this.__add_outputline();
  1615. }
  1616. return true;
  1617. };
  1618. Output.prototype.get_code = function(eol) {
  1619. var sweet_code = this.__lines.join('\n').replace(/[\r\n\t ]+$/, '');
  1620. if (this._end_with_newline) {
  1621. sweet_code += '\n';
  1622. }
  1623. if (eol !== '\n') {
  1624. sweet_code = sweet_code.replace(/[\n]/g, eol);
  1625. }
  1626. return sweet_code;
  1627. };
  1628. Output.prototype.set_indent = function(indent, alignment) {
  1629. indent = indent || 0;
  1630. alignment = alignment || 0;
  1631. // Never indent your first output indent at the start of the file
  1632. if (this.__lines.length > 1) {
  1633. this.current_line.set_indent(indent, alignment);
  1634. return true;
  1635. }
  1636. this.current_line.set_indent();
  1637. return false;
  1638. };
  1639. Output.prototype.add_raw_token = function(token) {
  1640. for (var x = 0; x < token.newlines; x++) {
  1641. this.__add_outputline();
  1642. }
  1643. this.current_line.push(token.whitespace_before);
  1644. this.current_line.push_raw(token.text);
  1645. this.space_before_token = false;
  1646. };
  1647. Output.prototype.add_token = function(printable_token) {
  1648. this.add_space_before_token();
  1649. this.current_line.push(printable_token);
  1650. };
  1651. Output.prototype.add_space_before_token = function() {
  1652. if (this.space_before_token && !this.just_added_newline()) {
  1653. this.current_line.push(' ');
  1654. }
  1655. this.space_before_token = false;
  1656. };
  1657. Output.prototype.remove_indent = function(index) {
  1658. var output_length = this.__lines.length;
  1659. while (index < output_length) {
  1660. this.__lines[index].remove_indent();
  1661. index++;
  1662. }
  1663. };
  1664. Output.prototype.trim = function(eat_newlines) {
  1665. eat_newlines = (eat_newlines === undefined) ? false : eat_newlines;
  1666. this.current_line.trim(this.indent_string, this.baseIndentString);
  1667. while (eat_newlines && this.__lines.length > 1 &&
  1668. this.current_line.is_empty()) {
  1669. this.__lines.pop();
  1670. this.current_line = this.__lines[this.__lines.length - 1];
  1671. this.current_line.trim();
  1672. }
  1673. this.previous_line = this.__lines.length > 1 ?
  1674. this.__lines[this.__lines.length - 2] : null;
  1675. };
  1676. Output.prototype.just_added_newline = function() {
  1677. return this.current_line.is_empty();
  1678. };
  1679. Output.prototype.just_added_blankline = function() {
  1680. return this.is_empty() ||
  1681. (this.current_line.is_empty() && this.previous_line.is_empty());
  1682. };
  1683. Output.prototype.ensure_empty_line_above = function(starts_with, ends_with) {
  1684. var index = this.__lines.length - 2;
  1685. while (index >= 0) {
  1686. var potentialEmptyLine = this.__lines[index];
  1687. if (potentialEmptyLine.is_empty()) {
  1688. break;
  1689. } else if (potentialEmptyLine.item(0).indexOf(starts_with) !== 0 &&
  1690. potentialEmptyLine.item(-1) !== ends_with) {
  1691. this.__lines.splice(index + 1, 0, new OutputLine(this));
  1692. this.previous_line = this.__lines[this.__lines.length - 2];
  1693. break;
  1694. }
  1695. index--;
  1696. }
  1697. };
  1698. module.exports.Output = Output;
  1699. /***/ }),
  1700. /* 3 */
  1701. /***/ (function(module, exports, __webpack_require__) {
  1702. "use strict";
  1703. /*jshint node:true */
  1704. /*
  1705. The MIT License (MIT)
  1706. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1707. Permission is hereby granted, free of charge, to any person
  1708. obtaining a copy of this software and associated documentation files
  1709. (the "Software"), to deal in the Software without restriction,
  1710. including without limitation the rights to use, copy, modify, merge,
  1711. publish, distribute, sublicense, and/or sell copies of the Software,
  1712. and to permit persons to whom the Software is furnished to do so,
  1713. subject to the following conditions:
  1714. The above copyright notice and this permission notice shall be
  1715. included in all copies or substantial portions of the Software.
  1716. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1717. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1718. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1719. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1720. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1721. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1722. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1723. SOFTWARE.
  1724. */
  1725. function Token(type, text, newlines, whitespace_before) {
  1726. this.type = type;
  1727. this.text = text;
  1728. // comments_before are
  1729. // comments that have a new line before them
  1730. // and may or may not have a newline after
  1731. // this is a set of comments before
  1732. this.comments_before = null; /* inline comment*/
  1733. // this.comments_after = new TokenStream(); // no new line before and newline after
  1734. this.newlines = newlines || 0;
  1735. this.whitespace_before = whitespace_before || '';
  1736. this.parent = null;
  1737. this.next = null;
  1738. this.previous = null;
  1739. this.opened = null;
  1740. this.closed = null;
  1741. this.directives = null;
  1742. }
  1743. module.exports.Token = Token;
  1744. /***/ }),
  1745. /* 4 */
  1746. /***/ (function(module, exports, __webpack_require__) {
  1747. "use strict";
  1748. /* jshint node: true, curly: false */
  1749. // Parts of this section of code is taken from acorn.
  1750. //
  1751. // Acorn was written by Marijn Haverbeke and released under an MIT
  1752. // license. The Unicode regexps (for identifiers and whitespace) were
  1753. // taken from [Esprima](http://esprima.org) by Ariya Hidayat.
  1754. //
  1755. // Git repositories for Acorn are available at
  1756. //
  1757. // http://marijnhaverbeke.nl/git/acorn
  1758. // https://github.com/marijnh/acorn.git
  1759. // ## Character categories
  1760. // acorn used char codes to squeeze the last bit of performance out
  1761. // Beautifier is okay without that, so we're using regex
  1762. // permit $ (36) and @ (64). @ is used in ES7 decorators.
  1763. // 65 through 91 are uppercase letters.
  1764. // permit _ (95).
  1765. // 97 through 123 are lowercase letters.
  1766. var baseASCIIidentifierStartChars = "\x24\x40\x41-\x5a\x5f\x61-\x7a";
  1767. // inside an identifier @ is not allowed but 0-9 are.
  1768. var baseASCIIidentifierChars = "\x24\x30-\x39\x41-\x5a\x5f\x61-\x7a";
  1769. // Big ugly regular expressions that match characters in the
  1770. // whitespace, identifier, and identifier-start categories. These
  1771. // are only applied when a character is found to actually have a
  1772. // code point above 128.
  1773. var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\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\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\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\u2071\u207f\u2090-\u209c\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-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\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-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1774. var nonASCIIidentifierChars = "\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1775. //var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1776. //var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1777. var identifierStart = "[" + baseASCIIidentifierStartChars + nonASCIIidentifierStartChars + "]";
  1778. var identifierChars = "[" + baseASCIIidentifierChars + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]*";
  1779. exports.identifier = new RegExp(identifierStart + identifierChars, 'g');
  1780. var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/; // jshint ignore:line
  1781. // Whether a single character denotes a newline.
  1782. exports.newline = /[\n\r\u2028\u2029]/;
  1783. // Matches a whole line break (where CRLF is considered a single
  1784. // line break). Used to count lines.
  1785. // in javascript, these two differ
  1786. // in python they are the same, different methods are called on them
  1787. exports.lineBreak = new RegExp('\r\n|' + exports.newline.source);
  1788. exports.allLineBreaks = new RegExp(exports.lineBreak.source, 'g');
  1789. /***/ }),
  1790. /* 5 */
  1791. /***/ (function(module, exports, __webpack_require__) {
  1792. "use strict";
  1793. /*jshint node:true */
  1794. /*
  1795. The MIT License (MIT)
  1796. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1797. Permission is hereby granted, free of charge, to any person
  1798. obtaining a copy of this software and associated documentation files
  1799. (the "Software"), to deal in the Software without restriction,
  1800. including without limitation the rights to use, copy, modify, merge,
  1801. publish, distribute, sublicense, and/or sell copies of the Software,
  1802. and to permit persons to whom the Software is furnished to do so,
  1803. subject to the following conditions:
  1804. The above copyright notice and this permission notice shall be
  1805. included in all copies or substantial portions of the Software.
  1806. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1807. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1808. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1809. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1810. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1811. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1812. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1813. SOFTWARE.
  1814. */
  1815. var BaseOptions = __webpack_require__(6).Options;
  1816. var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
  1817. function Options(options) {
  1818. BaseOptions.call(this, options, 'js');
  1819. // compatibility, re
  1820. var raw_brace_style = this.raw_options.brace_style || null;
  1821. if (raw_brace_style === "expand-strict") { //graceful handling of deprecated option
  1822. this.raw_options.brace_style = "expand";
  1823. } else if (raw_brace_style === "collapse-preserve-inline") { //graceful handling of deprecated option
  1824. this.raw_options.brace_style = "collapse,preserve-inline";
  1825. } else if (this.raw_options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
  1826. this.raw_options.brace_style = this.raw_options.braces_on_own_line ? "expand" : "collapse";
  1827. // } else if (!raw_brace_style) { //Nothing exists to set it
  1828. // raw_brace_style = "collapse";
  1829. }
  1830. //preserve-inline in delimited string will trigger brace_preserve_inline, everything
  1831. //else is considered a brace_style and the last one only will have an effect
  1832. var brace_style_split = this._get_selection_list('brace_style', ['collapse', 'expand', 'end-expand', 'none', 'preserve-inline']);
  1833. this.brace_preserve_inline = false; //Defaults in case one or other was not specified in meta-option
  1834. this.brace_style = "collapse";
  1835. for (var bs = 0; bs < brace_style_split.length; bs++) {
  1836. if (brace_style_split[bs] === "preserve-inline") {
  1837. this.brace_preserve_inline = true;
  1838. } else {
  1839. this.brace_style = brace_style_split[bs];
  1840. }
  1841. }
  1842. this.unindent_chained_methods = this._get_boolean('unindent_chained_methods');
  1843. this.break_chained_methods = this._get_boolean('break_chained_methods');
  1844. this.space_in_paren = this._get_boolean('space_in_paren');
  1845. this.space_in_empty_paren = this._get_boolean('space_in_empty_paren');
  1846. this.jslint_happy = this._get_boolean('jslint_happy');
  1847. this.space_after_anon_function = this._get_boolean('space_after_anon_function');
  1848. this.space_after_named_function = this._get_boolean('space_after_named_function');
  1849. this.keep_array_indentation = this._get_boolean('keep_array_indentation');
  1850. this.space_before_conditional = this._get_boolean('space_before_conditional', true);
  1851. this.unescape_strings = this._get_boolean('unescape_strings');
  1852. this.e4x = this._get_boolean('e4x');
  1853. this.comma_first = this._get_boolean('comma_first');
  1854. this.operator_position = this._get_selection('operator_position', validPositionValues);
  1855. // For testing of beautify preserve:start directive
  1856. this.test_output_raw = this._get_boolean('test_output_raw');
  1857. // force this._options.space_after_anon_function to true if this._options.jslint_happy
  1858. if (this.jslint_happy) {
  1859. this.space_after_anon_function = true;
  1860. }
  1861. }
  1862. Options.prototype = new BaseOptions();
  1863. module.exports.Options = Options;
  1864. /***/ }),
  1865. /* 6 */
  1866. /***/ (function(module, exports, __webpack_require__) {
  1867. "use strict";
  1868. /*jshint node:true */
  1869. /*
  1870. The MIT License (MIT)
  1871. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  1872. Permission is hereby granted, free of charge, to any person
  1873. obtaining a copy of this software and associated documentation files
  1874. (the "Software"), to deal in the Software without restriction,
  1875. including without limitation the rights to use, copy, modify, merge,
  1876. publish, distribute, sublicense, and/or sell copies of the Software,
  1877. and to permit persons to whom the Software is furnished to do so,
  1878. subject to the following conditions:
  1879. The above copyright notice and this permission notice shall be
  1880. included in all copies or substantial portions of the Software.
  1881. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1882. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1883. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1884. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  1885. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1886. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  1887. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1888. SOFTWARE.
  1889. */
  1890. function Options(options, merge_child_field) {
  1891. this.raw_options = _mergeOpts(options, merge_child_field);
  1892. // Support passing the source text back with no change
  1893. this.disabled = this._get_boolean('disabled');
  1894. this.eol = this._get_characters('eol', 'auto');
  1895. this.end_with_newline = this._get_boolean('end_with_newline');
  1896. this.indent_size = this._get_number('indent_size', 4);
  1897. this.indent_char = this._get_characters('indent_char', ' ');
  1898. this.indent_level = this._get_number('indent_level');
  1899. this.preserve_newlines = this._get_boolean('preserve_newlines', true);
  1900. this.max_preserve_newlines = this._get_number('max_preserve_newlines', 32786);
  1901. if (!this.preserve_newlines) {
  1902. this.max_preserve_newlines = 0;
  1903. }
  1904. this.indent_with_tabs = this._get_boolean('indent_with_tabs');
  1905. if (this.indent_with_tabs) {
  1906. this.indent_char = '\t';
  1907. this.indent_size = 1;
  1908. }
  1909. // Backwards compat with 1.3.x
  1910. this.wrap_line_length = this._get_number('wrap_line_length', this._get_number('max_char'));
  1911. }
  1912. Options.prototype._get_array = function(name, default_value) {
  1913. var option_value = this.raw_options[name];
  1914. var result = default_value || [];
  1915. if (typeof option_value === 'object') {
  1916. if (option_value !== null && typeof option_value.concat === 'function') {
  1917. result = option_value.concat();
  1918. }
  1919. } else if (typeof option_value === 'string') {
  1920. result = option_value.split(/[^a-zA-Z0-9_\/\-]+/);
  1921. }
  1922. return result;
  1923. };
  1924. Options.prototype._get_boolean = function(name, default_value) {
  1925. var option_value = this.raw_options[name];
  1926. var result = option_value === undefined ? !!default_value : !!option_value;
  1927. return result;
  1928. };
  1929. Options.prototype._get_characters = function(name, default_value) {
  1930. var option_value = this.raw_options[name];
  1931. var result = default_value || '';
  1932. if (typeof option_value === 'string') {
  1933. result = option_value.replace(/\\r/, '\r').replace(/\\n/, '\n').replace(/\\t/, '\t');
  1934. }
  1935. return result;
  1936. };
  1937. Options.prototype._get_number = function(name, default_value) {
  1938. var option_value = this.raw_options[name];
  1939. default_value = parseInt(default_value, 10);
  1940. if (isNaN(default_value)) {
  1941. default_value = 0;
  1942. }
  1943. var result = parseInt(option_value, 10);
  1944. if (isNaN(result)) {
  1945. result = default_value;
  1946. }
  1947. return result;
  1948. };
  1949. Options.prototype._get_selection = function(name, selection_list, default_value) {
  1950. var result = this._get_selection_list(name, selection_list, default_value);
  1951. if (result.length !== 1) {
  1952. throw new Error(
  1953. "Invalid Option Value: The option '" + name + "' can only be one of the following values:\n" +
  1954. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  1955. }
  1956. return result[0];
  1957. };
  1958. Options.prototype._get_selection_list = function(name, selection_list, default_value) {
  1959. if (!selection_list || selection_list.length === 0) {
  1960. throw new Error("Selection list cannot be empty.");
  1961. }
  1962. default_value = default_value || [selection_list[0]];
  1963. if (!this._is_valid_selection(default_value, selection_list)) {
  1964. throw new Error("Invalid Default Value!");
  1965. }
  1966. var result = this._get_array(name, default_value);
  1967. if (!this._is_valid_selection(result, selection_list)) {
  1968. throw new Error(
  1969. "Invalid Option Value: The option '" + name + "' can contain only the following values:\n" +
  1970. selection_list + "\nYou passed in: '" + this.raw_options[name] + "'");
  1971. }
  1972. return result;
  1973. };
  1974. Options.prototype._is_valid_selection = function(result, selection_list) {
  1975. return result.length && selection_list.length &&
  1976. !result.some(function(item) { return selection_list.indexOf(item) === -1; });
  1977. };
  1978. // merges child options up with the parent options object
  1979. // Example: obj = {a: 1, b: {a: 2}}
  1980. // mergeOpts(obj, 'b')
  1981. //
  1982. // Returns: {a: 2}
  1983. function _mergeOpts(allOptions, childFieldName) {
  1984. var finalOpts = {};
  1985. allOptions = _normalizeOpts(allOptions);
  1986. var name;
  1987. for (name in allOptions) {
  1988. if (name !== childFieldName) {
  1989. finalOpts[name] = allOptions[name];
  1990. }
  1991. }
  1992. //merge in the per type settings for the childFieldName
  1993. if (childFieldName && allOptions[childFieldName]) {
  1994. for (name in allOptions[childFieldName]) {
  1995. finalOpts[name] = allOptions[childFieldName][name];
  1996. }
  1997. }
  1998. return finalOpts;
  1999. }
  2000. function _normalizeOpts(options) {
  2001. var convertedOpts = {};
  2002. var key;
  2003. for (key in options) {
  2004. var newKey = key.replace(/-/g, "_");
  2005. convertedOpts[newKey] = options[key];
  2006. }
  2007. return convertedOpts;
  2008. }
  2009. module.exports.Options = Options;
  2010. module.exports.normalizeOpts = _normalizeOpts;
  2011. module.exports.mergeOpts = _mergeOpts;
  2012. /***/ }),
  2013. /* 7 */
  2014. /***/ (function(module, exports, __webpack_require__) {
  2015. "use strict";
  2016. /*jshint node:true */
  2017. /*
  2018. The MIT License (MIT)
  2019. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2020. Permission is hereby granted, free of charge, to any person
  2021. obtaining a copy of this software and associated documentation files
  2022. (the "Software"), to deal in the Software without restriction,
  2023. including without limitation the rights to use, copy, modify, merge,
  2024. publish, distribute, sublicense, and/or sell copies of the Software,
  2025. and to permit persons to whom the Software is furnished to do so,
  2026. subject to the following conditions:
  2027. The above copyright notice and this permission notice shall be
  2028. included in all copies or substantial portions of the Software.
  2029. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2030. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2031. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2032. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2033. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2034. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2035. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2036. SOFTWARE.
  2037. */
  2038. var InputScanner = __webpack_require__(8).InputScanner;
  2039. var BaseTokenizer = __webpack_require__(9).Tokenizer;
  2040. var BASETOKEN = __webpack_require__(9).TOKEN;
  2041. var Directives = __webpack_require__(11).Directives;
  2042. var acorn = __webpack_require__(4);
  2043. function in_array(what, arr) {
  2044. return arr.indexOf(what) !== -1;
  2045. }
  2046. var TOKEN = {
  2047. START_EXPR: 'TK_START_EXPR',
  2048. END_EXPR: 'TK_END_EXPR',
  2049. START_BLOCK: 'TK_START_BLOCK',
  2050. END_BLOCK: 'TK_END_BLOCK',
  2051. WORD: 'TK_WORD',
  2052. RESERVED: 'TK_RESERVED',
  2053. SEMICOLON: 'TK_SEMICOLON',
  2054. STRING: 'TK_STRING',
  2055. EQUALS: 'TK_EQUALS',
  2056. OPERATOR: 'TK_OPERATOR',
  2057. COMMA: 'TK_COMMA',
  2058. BLOCK_COMMENT: 'TK_BLOCK_COMMENT',
  2059. COMMENT: 'TK_COMMENT',
  2060. DOT: 'TK_DOT',
  2061. UNKNOWN: 'TK_UNKNOWN',
  2062. START: BASETOKEN.START,
  2063. RAW: BASETOKEN.RAW,
  2064. EOF: BASETOKEN.EOF
  2065. };
  2066. var directives_core = new Directives(/\/\*/, /\*\//);
  2067. var number_pattern = /0[xX][0123456789abcdefABCDEF]*|0[oO][01234567]*|0[bB][01]*|\d+n|(?:\.\d+|\d+\.?\d*)(?:[eE][+-]?\d+)?/g;
  2068. var digit = /[0-9]/;
  2069. // Dot "." must be distinguished from "..." and decimal
  2070. var dot_pattern = /[^\d\.]/;
  2071. var positionable_operators = (
  2072. ">>> === !== " +
  2073. "<< && >= ** != == <= >> || " +
  2074. "< / - + > : & % ? ^ | *").split(' ');
  2075. // IMPORTANT: this must be sorted longest to shortest or tokenizing many not work.
  2076. // Also, you must update possitionable operators separately from punct
  2077. var punct =
  2078. ">>>= " +
  2079. "... >>= <<= === >>> !== **= " +
  2080. "=> ^= :: /= << <= == && -= >= >> != -- += ** || ++ %= &= *= |= " +
  2081. "= ! ? > < : / ^ - + * & % ~ |";
  2082. punct = punct.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&");
  2083. punct = punct.replace(/ /g, '|');
  2084. var punct_pattern = new RegExp(punct, 'g');
  2085. var shebang_pattern = /#![^\n\r\u2028\u2029]*(?:\r\n|[\n\r\u2028\u2029])?/g;
  2086. var include_pattern = /#include[^\n\r\u2028\u2029]*(?:\r\n|[\n\r\u2028\u2029])?/g;
  2087. // words which should always start on new line.
  2088. var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
  2089. var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
  2090. var reserved_word_pattern = new RegExp('^(?:' + reserved_words.join('|') + ')$');
  2091. // /* ... */ comment ends with nearest */ or end of file
  2092. var block_comment_pattern = /\/\*(?:[\s\S]*?)((?:\*\/)|$)/g;
  2093. // comment ends just before nearest linefeed or end of file
  2094. var comment_pattern = /\/\/(?:[^\n\r\u2028\u2029]*)/g;
  2095. var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
  2096. var in_html_comment;
  2097. var Tokenizer = function(input_string, options) {
  2098. BaseTokenizer.call(this, input_string, options);
  2099. this._whitespace_pattern = /[\n\r\u2028\u2029\t\u000B\u00A0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff ]+/g;
  2100. this._newline_pattern = /([^\n\r\u2028\u2029]*)(\r\n|[\n\r\u2028\u2029])?/g;
  2101. };
  2102. Tokenizer.prototype = new BaseTokenizer();
  2103. Tokenizer.prototype._is_comment = function(current_token) {
  2104. return current_token.type === TOKEN.COMMENT || current_token.type === TOKEN.BLOCK_COMMENT || current_token.type === TOKEN.UNKNOWN;
  2105. };
  2106. Tokenizer.prototype._is_opening = function(current_token) {
  2107. return current_token.type === TOKEN.START_BLOCK || current_token.type === TOKEN.START_EXPR;
  2108. };
  2109. Tokenizer.prototype._is_closing = function(current_token, open_token) {
  2110. return (current_token.type === TOKEN.END_BLOCK || current_token.type === TOKEN.END_EXPR) &&
  2111. (open_token && (
  2112. (current_token.text === ']' && open_token.text === '[') ||
  2113. (current_token.text === ')' && open_token.text === '(') ||
  2114. (current_token.text === '}' && open_token.text === '{')));
  2115. };
  2116. Tokenizer.prototype._reset = function() {
  2117. in_html_comment = false;
  2118. };
  2119. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  2120. this._readWhitespace();
  2121. var token = null;
  2122. var c = this._input.peek();
  2123. token = token || this._read_singles(c);
  2124. token = token || this._read_word(previous_token);
  2125. token = token || this._read_comment(c);
  2126. token = token || this._read_string(c);
  2127. token = token || this._read_regexp(c, previous_token);
  2128. token = token || this._read_xml(c, previous_token);
  2129. token = token || this._read_non_javascript(c);
  2130. token = token || this._read_punctuation();
  2131. token = token || this._create_token(TOKEN.UNKNOWN, this._input.next());
  2132. return token;
  2133. };
  2134. Tokenizer.prototype._read_word = function(previous_token) {
  2135. var resulting_string;
  2136. resulting_string = this._input.read(acorn.identifier);
  2137. if (resulting_string !== '') {
  2138. if (!(previous_token.type === TOKEN.DOT ||
  2139. (previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
  2140. reserved_word_pattern.test(resulting_string)) {
  2141. if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
  2142. return this._create_token(TOKEN.OPERATOR, resulting_string);
  2143. }
  2144. return this._create_token(TOKEN.RESERVED, resulting_string);
  2145. }
  2146. return this._create_token(TOKEN.WORD, resulting_string);
  2147. }
  2148. resulting_string = this._input.read(number_pattern);
  2149. if (resulting_string !== '') {
  2150. return this._create_token(TOKEN.WORD, resulting_string);
  2151. }
  2152. };
  2153. Tokenizer.prototype._read_singles = function(c) {
  2154. var token = null;
  2155. if (c === null) {
  2156. token = this._create_token(TOKEN.EOF, '');
  2157. } else if (c === '(' || c === '[') {
  2158. token = this._create_token(TOKEN.START_EXPR, c);
  2159. } else if (c === ')' || c === ']') {
  2160. token = this._create_token(TOKEN.END_EXPR, c);
  2161. } else if (c === '{') {
  2162. token = this._create_token(TOKEN.START_BLOCK, c);
  2163. } else if (c === '}') {
  2164. token = this._create_token(TOKEN.END_BLOCK, c);
  2165. } else if (c === ';') {
  2166. token = this._create_token(TOKEN.SEMICOLON, c);
  2167. } else if (c === '.' && dot_pattern.test(this._input.peek(1))) {
  2168. token = this._create_token(TOKEN.DOT, c);
  2169. } else if (c === ',') {
  2170. token = this._create_token(TOKEN.COMMA, c);
  2171. }
  2172. if (token) {
  2173. this._input.next();
  2174. }
  2175. return token;
  2176. };
  2177. Tokenizer.prototype._read_punctuation = function() {
  2178. var resulting_string = this._input.read(punct_pattern);
  2179. if (resulting_string !== '') {
  2180. if (resulting_string === '=') {
  2181. return this._create_token(TOKEN.EQUALS, resulting_string);
  2182. } else {
  2183. return this._create_token(TOKEN.OPERATOR, resulting_string);
  2184. }
  2185. }
  2186. };
  2187. Tokenizer.prototype._read_non_javascript = function(c) {
  2188. var resulting_string = '';
  2189. if (c === '#') {
  2190. if (this._is_first_token()) {
  2191. resulting_string = this._input.read(shebang_pattern);
  2192. if (resulting_string) {
  2193. return this._create_token(TOKEN.UNKNOWN, resulting_string.trim() + '\n');
  2194. }
  2195. }
  2196. // handles extendscript #includes
  2197. resulting_string = this._input.read(include_pattern);
  2198. if (resulting_string) {
  2199. return this._create_token(TOKEN.UNKNOWN, resulting_string.trim() + '\n');
  2200. }
  2201. c = this._input.next();
  2202. // Spidermonkey-specific sharp variables for circular references. Considered obsolete.
  2203. var sharp = '#';
  2204. if (this._input.hasNext() && this._input.testChar(digit)) {
  2205. do {
  2206. c = this._input.next();
  2207. sharp += c;
  2208. } while (this._input.hasNext() && c !== '#' && c !== '=');
  2209. if (c === '#') {
  2210. //
  2211. } else if (this._input.peek() === '[' && this._input.peek(1) === ']') {
  2212. sharp += '[]';
  2213. this._input.next();
  2214. this._input.next();
  2215. } else if (this._input.peek() === '{' && this._input.peek(1) === '}') {
  2216. sharp += '{}';
  2217. this._input.next();
  2218. this._input.next();
  2219. }
  2220. return this._create_token(TOKEN.WORD, sharp);
  2221. }
  2222. this._input.back();
  2223. } else if (c === '<') {
  2224. if (this._input.peek(1) === '?' || this._input.peek(1) === '%') {
  2225. resulting_string = this._input.read(template_pattern);
  2226. if (resulting_string) {
  2227. resulting_string = resulting_string.replace(acorn.allLineBreaks, '\n');
  2228. return this._create_token(TOKEN.STRING, resulting_string);
  2229. }
  2230. } else if (this._input.match(/<\!--/g)) {
  2231. c = '<!--';
  2232. while (this._input.hasNext() && !this._input.testChar(acorn.newline)) {
  2233. c += this._input.next();
  2234. }
  2235. in_html_comment = true;
  2236. return this._create_token(TOKEN.COMMENT, c);
  2237. }
  2238. } else if (c === '-' && in_html_comment && this._input.match(/-->/g)) {
  2239. in_html_comment = false;
  2240. return this._create_token(TOKEN.COMMENT, '-->');
  2241. }
  2242. return null;
  2243. };
  2244. Tokenizer.prototype._read_comment = function(c) {
  2245. var token = null;
  2246. if (c === '/') {
  2247. var comment = '';
  2248. if (this._input.peek(1) === '*') {
  2249. // peek for comment /* ... */
  2250. comment = this._input.read(block_comment_pattern);
  2251. var directives = directives_core.get_directives(comment);
  2252. if (directives && directives.ignore === 'start') {
  2253. comment += directives_core.readIgnored(this._input);
  2254. }
  2255. comment = comment.replace(acorn.allLineBreaks, '\n');
  2256. token = this._create_token(TOKEN.BLOCK_COMMENT, comment);
  2257. token.directives = directives;
  2258. } else if (this._input.peek(1) === '/') {
  2259. // peek for comment // ...
  2260. comment = this._input.read(comment_pattern);
  2261. token = this._create_token(TOKEN.COMMENT, comment);
  2262. }
  2263. }
  2264. return token;
  2265. };
  2266. Tokenizer.prototype._read_string = function(c) {
  2267. if (c === '`' || c === "'" || c === '"') {
  2268. var resulting_string = this._input.next();
  2269. this.has_char_escapes = false;
  2270. if (c === '`') {
  2271. resulting_string += this._read_string_recursive('`', true, '${');
  2272. } else {
  2273. resulting_string += this._read_string_recursive(c);
  2274. }
  2275. if (this.has_char_escapes && this._options.unescape_strings) {
  2276. resulting_string = unescape_string(resulting_string);
  2277. }
  2278. if (this._input.peek() === c) {
  2279. resulting_string += this._input.next();
  2280. }
  2281. return this._create_token(TOKEN.STRING, resulting_string);
  2282. }
  2283. return null;
  2284. };
  2285. Tokenizer.prototype._allow_regexp_or_xml = function(previous_token) {
  2286. // regex and xml can only appear in specific locations during parsing
  2287. return (previous_token.type === TOKEN.RESERVED && in_array(previous_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) ||
  2288. (previous_token.type === TOKEN.END_EXPR && previous_token.text === ')' &&
  2289. previous_token.opened.previous.type === TOKEN.RESERVED && in_array(previous_token.opened.previous.text, ['if', 'while', 'for'])) ||
  2290. (in_array(previous_token.type, [TOKEN.COMMENT, TOKEN.START_EXPR, TOKEN.START_BLOCK, TOKEN.START,
  2291. TOKEN.END_BLOCK, TOKEN.OPERATOR, TOKEN.EQUALS, TOKEN.EOF, TOKEN.SEMICOLON, TOKEN.COMMA
  2292. ]));
  2293. };
  2294. Tokenizer.prototype._read_regexp = function(c, previous_token) {
  2295. if (c === '/' && this._allow_regexp_or_xml(previous_token)) {
  2296. // handle regexp
  2297. //
  2298. var resulting_string = this._input.next();
  2299. var esc = false;
  2300. var in_char_class = false;
  2301. while (this._input.hasNext() &&
  2302. ((esc || in_char_class || this._input.peek() !== c) &&
  2303. !this._input.testChar(acorn.newline))) {
  2304. resulting_string += this._input.peek();
  2305. if (!esc) {
  2306. esc = this._input.peek() === '\\';
  2307. if (this._input.peek() === '[') {
  2308. in_char_class = true;
  2309. } else if (this._input.peek() === ']') {
  2310. in_char_class = false;
  2311. }
  2312. } else {
  2313. esc = false;
  2314. }
  2315. this._input.next();
  2316. }
  2317. if (this._input.peek() === c) {
  2318. resulting_string += this._input.next();
  2319. // regexps may have modifiers /regexp/MOD , so fetch those, too
  2320. // Only [gim] are valid, but if the user puts in garbage, do what we can to take it.
  2321. resulting_string += this._input.read(acorn.identifier);
  2322. }
  2323. return this._create_token(TOKEN.STRING, resulting_string);
  2324. }
  2325. return null;
  2326. };
  2327. var startXmlRegExp = /<()([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;
  2328. var xmlRegExp = /[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;
  2329. Tokenizer.prototype._read_xml = function(c, previous_token) {
  2330. if (this._options.e4x && c === "<" && this._input.test(startXmlRegExp) && this._allow_regexp_or_xml(previous_token)) {
  2331. // handle e4x xml literals
  2332. //
  2333. var xmlStr = '';
  2334. var match = this._input.match(startXmlRegExp);
  2335. if (match) {
  2336. // Trim root tag to attempt to
  2337. var rootTag = match[2].replace(/^{\s+/, '{').replace(/\s+}$/, '}');
  2338. var isCurlyRoot = rootTag.indexOf('{') === 0;
  2339. var depth = 0;
  2340. while (match) {
  2341. var isEndTag = !!match[1];
  2342. var tagName = match[2];
  2343. var isSingletonTag = (!!match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA[");
  2344. if (!isSingletonTag &&
  2345. (tagName === rootTag || (isCurlyRoot && tagName.replace(/^{\s+/, '{').replace(/\s+}$/, '}')))) {
  2346. if (isEndTag) {
  2347. --depth;
  2348. } else {
  2349. ++depth;
  2350. }
  2351. }
  2352. xmlStr += match[0];
  2353. if (depth <= 0) {
  2354. break;
  2355. }
  2356. match = this._input.match(xmlRegExp);
  2357. }
  2358. // if we didn't close correctly, keep unformatted.
  2359. if (!match) {
  2360. xmlStr += this._input.match(/[\s\S]*/g)[0];
  2361. }
  2362. xmlStr = xmlStr.replace(acorn.allLineBreaks, '\n');
  2363. return this._create_token(TOKEN.STRING, xmlStr);
  2364. }
  2365. }
  2366. return null;
  2367. };
  2368. function unescape_string(s) {
  2369. // You think that a regex would work for this
  2370. // return s.replace(/\\x([0-9a-f]{2})/gi, function(match, val) {
  2371. // return String.fromCharCode(parseInt(val, 16));
  2372. // })
  2373. // However, dealing with '\xff', '\\xff', '\\\xff' makes this more fun.
  2374. var out = '',
  2375. escaped = 0;
  2376. var input_scan = new InputScanner(s);
  2377. var matched = null;
  2378. while (input_scan.hasNext()) {
  2379. // Keep any whitespace, non-slash characters
  2380. // also keep slash pairs.
  2381. matched = input_scan.match(/([\s]|[^\\]|\\\\)+/g);
  2382. if (matched) {
  2383. out += matched[0];
  2384. }
  2385. if (input_scan.peek() === '\\') {
  2386. input_scan.next();
  2387. if (input_scan.peek() === 'x') {
  2388. matched = input_scan.match(/x([0-9A-Fa-f]{2})/g);
  2389. } else if (input_scan.peek() === 'u') {
  2390. matched = input_scan.match(/u([0-9A-Fa-f]{4})/g);
  2391. } else {
  2392. out += '\\';
  2393. if (input_scan.hasNext()) {
  2394. out += input_scan.next();
  2395. }
  2396. continue;
  2397. }
  2398. // If there's some error decoding, return the original string
  2399. if (!matched) {
  2400. return s;
  2401. }
  2402. escaped = parseInt(matched[1], 16);
  2403. if (escaped > 0x7e && escaped <= 0xff && matched[0].indexOf('x') === 0) {
  2404. // we bail out on \x7f..\xff,
  2405. // leaving whole string escaped,
  2406. // as it's probably completely binary
  2407. return s;
  2408. } else if (escaped >= 0x00 && escaped < 0x20) {
  2409. // leave 0x00...0x1f escaped
  2410. out += '\\' + matched[0];
  2411. continue;
  2412. } else if (escaped === 0x22 || escaped === 0x27 || escaped === 0x5c) {
  2413. // single-quote, apostrophe, backslash - escape these
  2414. out += '\\' + String.fromCharCode(escaped);
  2415. } else {
  2416. out += String.fromCharCode(escaped);
  2417. }
  2418. }
  2419. }
  2420. return out;
  2421. }
  2422. // handle string
  2423. //
  2424. Tokenizer.prototype._read_string_recursive = function(delimiter, allow_unescaped_newlines, start_sub) {
  2425. // Template strings can travers lines without escape characters.
  2426. // Other strings cannot
  2427. var current_char;
  2428. var resulting_string = '';
  2429. var esc = false;
  2430. while (this._input.hasNext()) {
  2431. current_char = this._input.peek();
  2432. if (!(esc || (current_char !== delimiter &&
  2433. (allow_unescaped_newlines || !acorn.newline.test(current_char))))) {
  2434. break;
  2435. }
  2436. // Handle \r\n linebreaks after escapes or in template strings
  2437. if ((esc || allow_unescaped_newlines) && acorn.newline.test(current_char)) {
  2438. if (current_char === '\r' && this._input.peek(1) === '\n') {
  2439. this._input.next();
  2440. current_char = this._input.peek();
  2441. }
  2442. resulting_string += '\n';
  2443. } else {
  2444. resulting_string += current_char;
  2445. }
  2446. if (esc) {
  2447. if (current_char === 'x' || current_char === 'u') {
  2448. this.has_char_escapes = true;
  2449. }
  2450. esc = false;
  2451. } else {
  2452. esc = current_char === '\\';
  2453. }
  2454. this._input.next();
  2455. if (start_sub && resulting_string.indexOf(start_sub, resulting_string.length - start_sub.length) !== -1) {
  2456. if (delimiter === '`') {
  2457. resulting_string += this._read_string_recursive('}', allow_unescaped_newlines, '`');
  2458. } else {
  2459. resulting_string += this._read_string_recursive('`', allow_unescaped_newlines, '${');
  2460. }
  2461. if (this._input.hasNext()) {
  2462. resulting_string += this._input.next();
  2463. }
  2464. }
  2465. }
  2466. return resulting_string;
  2467. };
  2468. module.exports.Tokenizer = Tokenizer;
  2469. module.exports.TOKEN = TOKEN;
  2470. module.exports.positionable_operators = positionable_operators.slice();
  2471. module.exports.line_starters = line_starters.slice();
  2472. /***/ }),
  2473. /* 8 */
  2474. /***/ (function(module, exports, __webpack_require__) {
  2475. "use strict";
  2476. /*jshint node:true */
  2477. /*
  2478. The MIT License (MIT)
  2479. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2480. Permission is hereby granted, free of charge, to any person
  2481. obtaining a copy of this software and associated documentation files
  2482. (the "Software"), to deal in the Software without restriction,
  2483. including without limitation the rights to use, copy, modify, merge,
  2484. publish, distribute, sublicense, and/or sell copies of the Software,
  2485. and to permit persons to whom the Software is furnished to do so,
  2486. subject to the following conditions:
  2487. The above copyright notice and this permission notice shall be
  2488. included in all copies or substantial portions of the Software.
  2489. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2490. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2491. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2492. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2493. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2494. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2495. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2496. SOFTWARE.
  2497. */
  2498. function InputScanner(input_string) {
  2499. this.__input = input_string || '';
  2500. this.__input_length = this.__input.length;
  2501. this.__position = 0;
  2502. }
  2503. InputScanner.prototype.restart = function() {
  2504. this.__position = 0;
  2505. };
  2506. InputScanner.prototype.back = function() {
  2507. if (this.__position > 0) {
  2508. this.__position -= 1;
  2509. }
  2510. };
  2511. InputScanner.prototype.hasNext = function() {
  2512. return this.__position < this.__input_length;
  2513. };
  2514. InputScanner.prototype.next = function() {
  2515. var val = null;
  2516. if (this.hasNext()) {
  2517. val = this.__input.charAt(this.__position);
  2518. this.__position += 1;
  2519. }
  2520. return val;
  2521. };
  2522. InputScanner.prototype.peek = function(index) {
  2523. var val = null;
  2524. index = index || 0;
  2525. index += this.__position;
  2526. if (index >= 0 && index < this.__input_length) {
  2527. val = this.__input.charAt(index);
  2528. }
  2529. return val;
  2530. };
  2531. InputScanner.prototype.test = function(pattern, index) {
  2532. index = index || 0;
  2533. index += this.__position;
  2534. pattern.lastIndex = index;
  2535. if (index >= 0 && index < this.__input_length) {
  2536. var pattern_match = pattern.exec(this.__input);
  2537. return pattern_match && pattern_match.index === index;
  2538. } else {
  2539. return false;
  2540. }
  2541. };
  2542. InputScanner.prototype.testChar = function(pattern, index) {
  2543. // test one character regex match
  2544. var val = this.peek(index);
  2545. return val !== null && pattern.test(val);
  2546. };
  2547. InputScanner.prototype.match = function(pattern) {
  2548. pattern.lastIndex = this.__position;
  2549. var pattern_match = pattern.exec(this.__input);
  2550. if (pattern_match && pattern_match.index === this.__position) {
  2551. this.__position += pattern_match[0].length;
  2552. } else {
  2553. pattern_match = null;
  2554. }
  2555. return pattern_match;
  2556. };
  2557. InputScanner.prototype.read = function(pattern) {
  2558. var val = '';
  2559. var match = this.match(pattern);
  2560. if (match) {
  2561. val = match[0];
  2562. }
  2563. return val;
  2564. };
  2565. InputScanner.prototype.readUntil = function(pattern, include_match) {
  2566. var val = '';
  2567. var match_index = this.__position;
  2568. pattern.lastIndex = this.__position;
  2569. var pattern_match = pattern.exec(this.__input);
  2570. if (pattern_match) {
  2571. if (include_match) {
  2572. match_index = pattern_match.index + pattern_match[0].length;
  2573. } else {
  2574. match_index = pattern_match.index;
  2575. }
  2576. } else {
  2577. match_index = this.__input_length;
  2578. }
  2579. val = this.__input.substring(this.__position, match_index);
  2580. this.__position = match_index;
  2581. return val;
  2582. };
  2583. InputScanner.prototype.readUntilAfter = function(pattern) {
  2584. return this.readUntil(pattern, true);
  2585. };
  2586. /* css beautifier legacy helpers */
  2587. InputScanner.prototype.peekUntilAfter = function(pattern) {
  2588. var start = this.__position;
  2589. var val = this.readUntilAfter(pattern);
  2590. this.__position = start;
  2591. return val;
  2592. };
  2593. InputScanner.prototype.lookBack = function(testVal) {
  2594. var start = this.__position - 1;
  2595. return start >= testVal.length && this.__input.substring(start - testVal.length, start)
  2596. .toLowerCase() === testVal;
  2597. };
  2598. module.exports.InputScanner = InputScanner;
  2599. /***/ }),
  2600. /* 9 */
  2601. /***/ (function(module, exports, __webpack_require__) {
  2602. "use strict";
  2603. /*jshint node:true */
  2604. /*
  2605. The MIT License (MIT)
  2606. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2607. Permission is hereby granted, free of charge, to any person
  2608. obtaining a copy of this software and associated documentation files
  2609. (the "Software"), to deal in the Software without restriction,
  2610. including without limitation the rights to use, copy, modify, merge,
  2611. publish, distribute, sublicense, and/or sell copies of the Software,
  2612. and to permit persons to whom the Software is furnished to do so,
  2613. subject to the following conditions:
  2614. The above copyright notice and this permission notice shall be
  2615. included in all copies or substantial portions of the Software.
  2616. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2617. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2618. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2619. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2620. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2621. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2622. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2623. SOFTWARE.
  2624. */
  2625. var InputScanner = __webpack_require__(8).InputScanner;
  2626. var Token = __webpack_require__(3).Token;
  2627. var TokenStream = __webpack_require__(10).TokenStream;
  2628. var TOKEN = {
  2629. START: 'TK_START',
  2630. RAW: 'TK_RAW',
  2631. EOF: 'TK_EOF'
  2632. };
  2633. var Tokenizer = function(input_string, options) {
  2634. this._input = new InputScanner(input_string);
  2635. this._options = options || {};
  2636. this.__tokens = null;
  2637. this.__newline_count = 0;
  2638. this.__whitespace_before_token = '';
  2639. this._whitespace_pattern = /[\n\r\t ]+/g;
  2640. this._newline_pattern = /([^\n\r]*)(\r\n|[\n\r])?/g;
  2641. };
  2642. Tokenizer.prototype.tokenize = function() {
  2643. this._input.restart();
  2644. this.__tokens = new TokenStream();
  2645. this._reset();
  2646. var current;
  2647. var previous = new Token(TOKEN.START, '');
  2648. var open_token = null;
  2649. var open_stack = [];
  2650. var comments = new TokenStream();
  2651. while (previous.type !== TOKEN.EOF) {
  2652. current = this._get_next_token(previous, open_token);
  2653. while (this._is_comment(current)) {
  2654. comments.add(current);
  2655. current = this._get_next_token(previous, open_token);
  2656. }
  2657. if (!comments.isEmpty()) {
  2658. current.comments_before = comments;
  2659. comments = new TokenStream();
  2660. }
  2661. current.parent = open_token;
  2662. if (this._is_opening(current)) {
  2663. open_stack.push(open_token);
  2664. open_token = current;
  2665. } else if (open_token && this._is_closing(current, open_token)) {
  2666. current.opened = open_token;
  2667. open_token.closed = current;
  2668. open_token = open_stack.pop();
  2669. current.parent = open_token;
  2670. }
  2671. current.previous = previous;
  2672. previous.next = current;
  2673. this.__tokens.add(current);
  2674. previous = current;
  2675. }
  2676. return this.__tokens;
  2677. };
  2678. Tokenizer.prototype._is_first_token = function() {
  2679. return this.__tokens.isEmpty();
  2680. };
  2681. Tokenizer.prototype._reset = function() {};
  2682. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  2683. this._readWhitespace();
  2684. var resulting_string = this._input.read(/.+/g);
  2685. if (resulting_string) {
  2686. return this._create_token(TOKEN.RAW, resulting_string);
  2687. } else {
  2688. return this._create_token(TOKEN.EOF, '');
  2689. }
  2690. };
  2691. Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:false
  2692. return false;
  2693. };
  2694. Tokenizer.prototype._is_opening = function(current_token) { // jshint unused:false
  2695. return false;
  2696. };
  2697. Tokenizer.prototype._is_closing = function(current_token, open_token) { // jshint unused:false
  2698. return false;
  2699. };
  2700. Tokenizer.prototype._create_token = function(type, text) {
  2701. var token = new Token(type, text, this.__newline_count, this.__whitespace_before_token);
  2702. this.__newline_count = 0;
  2703. this.__whitespace_before_token = '';
  2704. return token;
  2705. };
  2706. Tokenizer.prototype._readWhitespace = function() {
  2707. var resulting_string = this._input.read(this._whitespace_pattern);
  2708. if (resulting_string === ' ') {
  2709. this.__whitespace_before_token = resulting_string;
  2710. } else if (resulting_string !== '') {
  2711. this._newline_pattern.lastIndex = 0;
  2712. var nextMatch = this._newline_pattern.exec(resulting_string);
  2713. while (nextMatch[2]) {
  2714. this.__newline_count += 1;
  2715. nextMatch = this._newline_pattern.exec(resulting_string);
  2716. }
  2717. this.__whitespace_before_token = nextMatch[1];
  2718. }
  2719. };
  2720. module.exports.Tokenizer = Tokenizer;
  2721. module.exports.TOKEN = TOKEN;
  2722. /***/ }),
  2723. /* 10 */
  2724. /***/ (function(module, exports, __webpack_require__) {
  2725. "use strict";
  2726. /*jshint node:true */
  2727. /*
  2728. The MIT License (MIT)
  2729. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2730. Permission is hereby granted, free of charge, to any person
  2731. obtaining a copy of this software and associated documentation files
  2732. (the "Software"), to deal in the Software without restriction,
  2733. including without limitation the rights to use, copy, modify, merge,
  2734. publish, distribute, sublicense, and/or sell copies of the Software,
  2735. and to permit persons to whom the Software is furnished to do so,
  2736. subject to the following conditions:
  2737. The above copyright notice and this permission notice shall be
  2738. included in all copies or substantial portions of the Software.
  2739. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2740. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2741. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2742. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2743. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2744. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2745. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2746. SOFTWARE.
  2747. */
  2748. function TokenStream(parent_token) {
  2749. // private
  2750. this.__tokens = [];
  2751. this.__tokens_length = this.__tokens.length;
  2752. this.__position = 0;
  2753. this.__parent_token = parent_token;
  2754. }
  2755. TokenStream.prototype.restart = function() {
  2756. this.__position = 0;
  2757. };
  2758. TokenStream.prototype.isEmpty = function() {
  2759. return this.__tokens_length === 0;
  2760. };
  2761. TokenStream.prototype.hasNext = function() {
  2762. return this.__position < this.__tokens_length;
  2763. };
  2764. TokenStream.prototype.next = function() {
  2765. var val = null;
  2766. if (this.hasNext()) {
  2767. val = this.__tokens[this.__position];
  2768. this.__position += 1;
  2769. }
  2770. return val;
  2771. };
  2772. TokenStream.prototype.peek = function(index) {
  2773. var val = null;
  2774. index = index || 0;
  2775. index += this.__position;
  2776. if (index >= 0 && index < this.__tokens_length) {
  2777. val = this.__tokens[index];
  2778. }
  2779. return val;
  2780. };
  2781. TokenStream.prototype.add = function(token) {
  2782. if (this.__parent_token) {
  2783. token.parent = this.__parent_token;
  2784. }
  2785. this.__tokens.push(token);
  2786. this.__tokens_length += 1;
  2787. };
  2788. module.exports.TokenStream = TokenStream;
  2789. /***/ }),
  2790. /* 11 */
  2791. /***/ (function(module, exports, __webpack_require__) {
  2792. "use strict";
  2793. /*jshint node:true */
  2794. /*
  2795. The MIT License (MIT)
  2796. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  2797. Permission is hereby granted, free of charge, to any person
  2798. obtaining a copy of this software and associated documentation files
  2799. (the "Software"), to deal in the Software without restriction,
  2800. including without limitation the rights to use, copy, modify, merge,
  2801. publish, distribute, sublicense, and/or sell copies of the Software,
  2802. and to permit persons to whom the Software is furnished to do so,
  2803. subject to the following conditions:
  2804. The above copyright notice and this permission notice shall be
  2805. included in all copies or substantial portions of the Software.
  2806. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2807. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2808. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2809. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  2810. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  2811. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  2812. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  2813. SOFTWARE.
  2814. */
  2815. function Directives(start_block_pattern, end_block_pattern) {
  2816. start_block_pattern = typeof start_block_pattern === 'string' ? start_block_pattern : start_block_pattern.source;
  2817. end_block_pattern = typeof end_block_pattern === 'string' ? end_block_pattern : end_block_pattern.source;
  2818. this.__directives_block_pattern = new RegExp(start_block_pattern + / beautify( \w+[:]\w+)+ /.source + end_block_pattern, 'g');
  2819. this.__directive_pattern = / (\w+)[:](\w+)/g;
  2820. this.__directives_end_ignore_pattern = new RegExp('(?:[\\s\\S]*?)((?:' + start_block_pattern + /\sbeautify\signore:end\s/.source + end_block_pattern + ')|$)', 'g');
  2821. }
  2822. Directives.prototype.get_directives = function(text) {
  2823. if (!text.match(this.__directives_block_pattern)) {
  2824. return null;
  2825. }
  2826. var directives = {};
  2827. this.__directive_pattern.lastIndex = 0;
  2828. var directive_match = this.__directive_pattern.exec(text);
  2829. while (directive_match) {
  2830. directives[directive_match[1]] = directive_match[2];
  2831. directive_match = this.__directive_pattern.exec(text);
  2832. }
  2833. return directives;
  2834. };
  2835. Directives.prototype.readIgnored = function(input) {
  2836. return input.read(this.__directives_end_ignore_pattern);
  2837. };
  2838. module.exports.Directives = Directives;
  2839. /***/ })
  2840. /******/ ]);
  2841. var js_beautify = legacy_beautify_js;
  2842. /* Footer */
  2843. if (typeof define === "function" && define.amd) {
  2844. // Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
  2845. define([], function() {
  2846. return { js_beautify: js_beautify };
  2847. });
  2848. } else if (typeof exports !== "undefined") {
  2849. // Add support for CommonJS. Just put this file somewhere on your require.paths
  2850. // and you will be able to `var js_beautify = require("beautify").js_beautify`.
  2851. exports.js_beautify = js_beautify;
  2852. } else if (typeof window !== "undefined") {
  2853. // If we're running a web page and don't have either of the above, add our one global
  2854. window.js_beautify = js_beautify;
  2855. } else if (typeof global !== "undefined") {
  2856. // If we don't even have window, try global.
  2857. global.js_beautify = js_beautify;
  2858. }
  2859. }());