beautify.js 125 KB

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