beautify.js 145 KB

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