beautify.js 144 KB

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