beautify.js 144 KB

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