beautify.js 165 KB

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