__init__.py 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  1. from __future__ import print_function
  2. import sys
  3. import os
  4. import io
  5. import getopt
  6. import re
  7. import string
  8. import errno
  9. import copy
  10. from jsbeautifier.__version__ import __version__
  11. #
  12. # The MIT License (MIT)
  13. # Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
  14. # Permission is hereby granted, free of charge, to any person
  15. # obtaining a copy of this software and associated documentation files
  16. # (the "Software"), to deal in the Software without restriction,
  17. # including without limitation the rights to use, copy, modify, merge,
  18. # publish, distribute, sublicense, and/or sell copies of the Software,
  19. # and to permit persons to whom the Software is furnished to do so,
  20. # subject to the following conditions:
  21. # The above copyright notice and this permission notice shall be
  22. # included in all copies or substantial portions of the Software.
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. # SOFTWARE.
  31. #
  32. # Originally written by Einar Lielmanis et al.,
  33. # Conversion to python by Einar Lielmanis, einar@jsbeautifier.org,
  34. # Parsing improvement for brace-less and semicolon-less statements
  35. # by Liam Newman <bitwiseman@gmail.com>
  36. # Python is not my native language, feel free to push things around.
  37. #
  38. # Use either from command line (script displays its usage when run
  39. # without any parameters),
  40. #
  41. #
  42. # or, alternatively, use it as a module:
  43. #
  44. # import jsbeautifier
  45. # res = jsbeautifier.beautify('your javascript string')
  46. # res = jsbeautifier.beautify_file('some_file.js')
  47. #
  48. # you may specify some options:
  49. #
  50. # opts = jsbeautifier.default_options()
  51. # opts.indent_size = 2
  52. # res = jsbeautifier.beautify('some javascript', opts)
  53. #
  54. #
  55. # Here are the available options: (read source)
  56. class BeautifierOptions:
  57. def __init__(self):
  58. self.indent_size = 4
  59. self.indent_char = ' '
  60. self.indent_with_tabs = False
  61. self.eol = 'auto'
  62. self.preserve_newlines = True
  63. self.max_preserve_newlines = 10
  64. self.space_in_paren = False
  65. self.space_in_empty_paren = False
  66. self.e4x = False
  67. self.jslint_happy = False
  68. self.space_after_anon_function = False
  69. self.brace_style = 'collapse'
  70. self.keep_array_indentation = False
  71. self.keep_function_indentation = False
  72. self.eval_code = False
  73. self.unescape_strings = False
  74. self.wrap_line_length = 0
  75. self.break_chained_methods = False
  76. self.end_with_newline = False
  77. self.comma_first = False
  78. self.operator_position = 'before-newline'
  79. self.css = None
  80. self.js = None
  81. self.html = None
  82. # For testing of beautify ignore:start directive
  83. self.test_output_raw = False
  84. self.editorconfig = False
  85. def mergeOpts(self, targetType):
  86. finalOpts = copy.copy(self)
  87. local = getattr(finalOpts, targetType)
  88. if (local):
  89. delattr(finalOpts, targetType)
  90. for key in local:
  91. setattr(finalOpts, key, local[key])
  92. return finalOpts
  93. def __repr__(self):
  94. return \
  95. """indent_size = %d
  96. indent_char = [%s]
  97. preserve_newlines = %s
  98. max_preserve_newlines = %d
  99. space_in_paren = %s
  100. jslint_happy = %s
  101. space_after_anon_function = %s
  102. indent_with_tabs = %s
  103. brace_style = %s
  104. keep_array_indentation = %s
  105. eval_code = %s
  106. wrap_line_length = %s
  107. unescape_strings = %s
  108. """ % ( self.indent_size,
  109. self.indent_char,
  110. self.preserve_newlines,
  111. self.max_preserve_newlines,
  112. self.space_in_paren,
  113. self.jslint_happy,
  114. self.space_after_anon_function,
  115. self.indent_with_tabs,
  116. self.brace_style,
  117. self.keep_array_indentation,
  118. self.eval_code,
  119. self.wrap_line_length,
  120. self.unescape_strings,
  121. )
  122. class BeautifierFlags:
  123. def __init__(self, mode):
  124. self.mode = mode
  125. self.parent = None
  126. self.last_text = ''
  127. self.last_word = ''
  128. self.declaration_statement = False
  129. self.declaration_assignment = False
  130. self.multiline_frame = False
  131. self.inline_frame = False
  132. self.if_block = False
  133. self.else_block = False
  134. self.do_block = False
  135. self.do_while = False
  136. self.import_block = False
  137. self.in_case = False
  138. self.in_case_statement = False
  139. self.case_body = False
  140. self.indentation_level = 0
  141. self.line_indent_level = 0
  142. self.start_line_index = 0
  143. self.ternary_depth = 0
  144. def apply_base(self, flags_base, added_newline):
  145. next_indent_level = flags_base.indentation_level
  146. if not added_newline and \
  147. flags_base.line_indent_level > next_indent_level:
  148. next_indent_level = flags_base.line_indent_level
  149. self.parent = flags_base
  150. self.last_text = flags_base.last_text
  151. self.last_word = flags_base.last_word
  152. self.indentation_level = next_indent_level
  153. class Acorn:
  154. def __init__(self):
  155. # This is not pretty, but given how we did the version import
  156. # it is the only way to do this without having setup.py fail on a missing six dependency.
  157. self.six = __import__("six")
  158. # This section of code was translated to python from acorn (javascript).
  159. #
  160. # Acorn was written by Marijn Haverbeke and released under an MIT
  161. # license. The Unicode regexps (for identifiers and whitespace) were
  162. # taken from [Esprima](http://esprima.org) by Ariya Hidayat.
  163. #
  164. # Git repositories for Acorn are available at
  165. #
  166. # http://marijnhaverbeke.nl/git/acorn
  167. # https://github.com/marijnh/acorn.git
  168. # ## Character categories
  169. # Big ugly regular expressions that match characters in the
  170. # whitespace, identifier, and identifier-start categories. These
  171. # are only applied when a character is found to actually have a
  172. # code point above 128.
  173. self.nonASCIIwhitespace = re.compile(self.six.u("[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]"))
  174. self.nonASCIIidentifierStartChars = self.six.u("\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")
  175. self.nonASCIIidentifierChars = self.six.u("\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")
  176. self.nonASCIIidentifierStart = re.compile("[" + self.nonASCIIidentifierStartChars + "]")
  177. self.nonASCIIidentifier = re.compile("[" + self.nonASCIIidentifierStartChars + self.nonASCIIidentifierChars + "]")
  178. # Whether a single character denotes a newline.
  179. self.newline = re.compile(self.six.u("[\n\r\u2028\u2029]"))
  180. # Matches a whole line break (where CRLF is considered a single
  181. # line break). Used to count lines.
  182. # in javascript, these two differ
  183. # in python they are the same, different methods are called on them
  184. self.lineBreak = re.compile(self.six.u("\r\n|[\n\r\u2028\u2029]"))
  185. self.allLineBreaks = self.lineBreak
  186. # Test whether a given character code starts an identifier.
  187. def isIdentifierStart(self, code):
  188. if code < 65:
  189. return code in [36, 64] # permit $ (36) and @ (64). @ is used in ES7 decorators.
  190. if code < 91:
  191. return True # 65 through 91 are uppercase letters
  192. if code < 97:
  193. return code == 95 # permit _ (95)
  194. if code < 123:
  195. return True # 97 through 123 are lowercase letters
  196. return code >= 0xaa and self.nonASCIIidentifierStart.match(self.six.unichr(code)) != None
  197. # Test whether a given character is part of an identifier.
  198. def isIdentifierChar(self, code):
  199. if code < 48:
  200. return code == 36
  201. if code < 58:
  202. return True
  203. if code < 65:
  204. return False
  205. if code < 91:
  206. return True
  207. if code < 97:
  208. return code == 95
  209. if code < 123:
  210. return True
  211. return code >= 0xaa and self.nonASCIIidentifier.match(self.six.unichr(code)) != None
  212. class Token:
  213. def __init__(self, type, text, newlines = 0, whitespace_before = '', mode = None, parent = None):
  214. self.type = type
  215. self.text = text
  216. self.comments_before = []
  217. self.newlines = newlines
  218. self.wanted_newline = newlines > 0
  219. self.whitespace_before = whitespace_before
  220. self.parent = None
  221. self.opened = None
  222. self.directives = None
  223. def default_options():
  224. return BeautifierOptions()
  225. def beautify(string, opts = default_options() ):
  226. b = Beautifier()
  227. return b.beautify(string, opts)
  228. def set_file_editorconfig_opts(filename, js_options):
  229. from editorconfig import get_properties, EditorConfigError
  230. try:
  231. _ecoptions = get_properties(os.path.abspath(filename))
  232. if _ecoptions.get("indent_style") == "tab":
  233. js_options.indent_with_tabs = True
  234. elif _ecoptions.get("indent_style") == "space":
  235. js_options.indent_with_tabs = False
  236. if _ecoptions.get("indent_size"):
  237. js_options.indent_size = int(_ecoptions["indent_size"])
  238. if _ecoptions.get("max_line_length"):
  239. if _ecoptions.get("max_line_length") == "off":
  240. js_options.wrap_line_length = 0
  241. else:
  242. js_options.wrap_line_length = int(_ecoptions["max_line_length"])
  243. if _ecoptions.get("insert_final_newline") == 'true':
  244. js_options.end_with_newline = True
  245. elif _ecoptions.get("insert_final_newline") == 'false':
  246. js_options.end_with_newline = False
  247. if _ecoptions.get("end_of_line"):
  248. if _ecoptions["end_of_line"] == "cr":
  249. js_options.eol = '\r'
  250. elif _ecoptions["end_of_line"] == "lf":
  251. js_options.eol = '\n'
  252. elif _ecoptions["end_of_line"] == "crlf":
  253. js_options.eol = '\r\n'
  254. except EditorConfigError as ex:
  255. # do not error on bad editor config
  256. print("Error loading EditorConfig. Ignoring.", file=sys.stderr)
  257. def beautify_file(file_name, opts = default_options() ):
  258. input_string = ''
  259. if file_name == '-': # stdin
  260. try:
  261. if sys.stdin.isatty():
  262. raise Exception()
  263. stream = sys.stdin
  264. input_string = ''.join(stream.readlines())
  265. except Exception as ex:
  266. print("Must pipe input or define at least one file.", file=sys.stderr)
  267. usage(sys.stderr)
  268. raise Exception()
  269. else:
  270. stream = io.open(file_name, 'rt', newline='')
  271. input_string = ''.join(stream.readlines())
  272. return beautify(input_string, opts)
  273. def usage(stream=sys.stdout):
  274. print("jsbeautifier.py@" + __version__ + """
  275. Javascript beautifier (http://jsbeautifier.org/)
  276. Usage: jsbeautifier.py [options] <infile>
  277. <infile> can be "-", which means stdin.
  278. <outfile> defaults to stdout
  279. Input options:
  280. -i, --stdin Read input from stdin
  281. Output options:
  282. -s, --indent-size=NUMBER Indentation size. (default 4).
  283. -c, --indent-char=CHAR Character to indent with. (default space).
  284. -e, --eol=STRING Character(s) to use as line terminators.
  285. (default first newline in file, otherwise "\\n")
  286. -t, --indent-with-tabs Indent with tabs, overrides -s and -c
  287. -d, --disable-preserve-newlines Do not preserve existing line breaks.
  288. -P, --space-in-paren Add padding spaces within paren, ie. f( a, b )
  289. -E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
  290. -j, --jslint-happy More jslint-compatible output
  291. -a, --space_after_anon_function Add a space before an anonymous function's parens, ie. function ()
  292. -b, --brace-style=collapse Brace style (collapse, expand, end-expand, none)(,preserve-inline)
  293. -k, --keep-array-indentation Keep array indentation.
  294. -r, --replace Write output in-place, replacing input
  295. -o, --outfile=FILE Specify a file to output to (default stdout)
  296. -f, --keep-function-indentation Do not re-indent function bodies defined in var lines.
  297. -x, --unescape-strings Decode printable chars encoded in \\xNN notation.
  298. -X, --e4x Pass E4X xml literals through untouched
  299. -w, --wrap-line-length Attempt to wrap line when it exceeds this length.
  300. NOTE: Line continues until next wrap point is found.
  301. -n, --end_with_newline End output with newline
  302. --editorconfig Enable setting configuration from EditorConfig
  303. Rarely needed options:
  304. --eval-code evaluate code if a JS interpreter is
  305. installed. May be useful with some obfuscated
  306. script but poses a potential security issue.
  307. -l, --indent-level=NUMBER Initial indentation level. (default 0).
  308. -h, --help, --usage Prints this help statement.
  309. -v, --version Show the version
  310. """, file=stream)
  311. if stream == sys.stderr:
  312. return 1
  313. else:
  314. return 0
  315. OPERATOR_POSITION = {
  316. 'before_newline': 'before-newline',
  317. 'after_newline': 'after-newline',
  318. 'preserve_newline': 'preserve-newline'
  319. }
  320. OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION['before_newline'], OPERATOR_POSITION['preserve_newline']];
  321. def sanitizeOperatorPosition(opPosition):
  322. if not opPosition:
  323. return OPERATOR_POSITION['before_newline']
  324. elif opPosition not in OPERATOR_POSITION.values():
  325. raise ValueError("Invalid Option Value: The option 'operator_position' must be one of the following values\n" +
  326. str(OPERATOR_POSITION.values()) +
  327. "\nYou passed in: '" + opPosition + "'")
  328. return opPosition
  329. class MODE:
  330. BlockStatement, Statement, ObjectLiteral, ArrayLiteral, \
  331. ForInitializer, Conditional, Expression = range(7)
  332. class Beautifier:
  333. def __init__(self, opts = default_options() ):
  334. self.opts = copy.copy(opts)
  335. self.acorn = Acorn()
  336. self.blank_state()
  337. def blank_state(self, js_source_text = None):
  338. # internal flags
  339. self.flags = None
  340. self.previous_flags = None
  341. self.flag_store = []
  342. self.tokens = []
  343. self.token_pos = 0
  344. # force opts.space_after_anon_function to true if opts.jslint_happy
  345. if self.opts.jslint_happy:
  346. self.opts.space_after_anon_function = True
  347. if self.opts.indent_with_tabs:
  348. self.opts.indent_char = "\t"
  349. self.opts.indent_size = 1
  350. if self.opts.eol == 'auto':
  351. self.opts.eol = '\n'
  352. if self.acorn.lineBreak.search(js_source_text or ''):
  353. self.opts.eol = self.acorn.lineBreak.search(js_source_text).group()
  354. self.opts.eol = self.opts.eol.replace('\\r', '\r').replace('\\n', '\n')
  355. self.indent_string = self.opts.indent_char * self.opts.indent_size
  356. self.baseIndentString = ''
  357. self.last_type = 'TK_START_BLOCK' # last token type
  358. self.last_last_text = '' # pre-last token text
  359. preindent_index = 0;
  360. if not js_source_text == None and len(js_source_text) > 0:
  361. while preindent_index < len(js_source_text) and \
  362. js_source_text[preindent_index] in [' ', '\t'] :
  363. self.baseIndentString += js_source_text[preindent_index]
  364. preindent_index += 1
  365. js_source_text = js_source_text[preindent_index:]
  366. self.output = Output(self.indent_string, self.baseIndentString)
  367. # If testing the ignore directive, start with output disable set to true
  368. self.output.raw = self.opts.test_output_raw;
  369. self.set_mode(MODE.BlockStatement)
  370. return js_source_text
  371. def beautify(self, s, opts = None ):
  372. if opts != None:
  373. opts = opts.mergeOpts('js')
  374. self.opts = copy.copy(opts)
  375. #Compat with old form
  376. if self.opts.brace_style == 'collapse-preserve-inline':
  377. self.opts.brace_style = 'collapse,preserve-inline'
  378. split = re.compile("[^a-zA-Z0-9_\-]+").split(self.opts.brace_style)
  379. self.opts.brace_style = split[0]
  380. self.opts.brace_preserve_inline = (True if bool(split[1] == 'preserve-inline') else None) if len(split) > 1 else False
  381. if self.opts.brace_style not in ['expand', 'collapse', 'end-expand', 'none']:
  382. raise(Exception('opts.brace_style must be "expand", "collapse", "end-expand", or "none".'))
  383. if self.opts.brace_preserve_inline == None:
  384. raise(Exception('opts.brace_style second item must be "preserve-inline"'))
  385. s = self.blank_state(s)
  386. input = self.unpack(s, self.opts.eval_code)
  387. self.handlers = {
  388. 'TK_START_EXPR': self.handle_start_expr,
  389. 'TK_END_EXPR': self.handle_end_expr,
  390. 'TK_START_BLOCK': self.handle_start_block,
  391. 'TK_END_BLOCK': self.handle_end_block,
  392. 'TK_WORD': self.handle_word,
  393. 'TK_RESERVED': self.handle_word,
  394. 'TK_SEMICOLON': self.handle_semicolon,
  395. 'TK_STRING': self.handle_string,
  396. 'TK_EQUALS': self.handle_equals,
  397. 'TK_OPERATOR': self.handle_operator,
  398. 'TK_COMMA': self.handle_comma,
  399. 'TK_BLOCK_COMMENT': self.handle_block_comment,
  400. 'TK_COMMENT': self.handle_comment,
  401. 'TK_DOT': self.handle_dot,
  402. 'TK_UNKNOWN': self.handle_unknown,
  403. 'TK_EOF': self.handle_eof
  404. }
  405. self.tokens = Tokenizer(input, self.opts, self.indent_string).tokenize()
  406. self.token_pos = 0
  407. current_token = self.get_token()
  408. while current_token != None:
  409. self.handlers[current_token.type](current_token)
  410. self.last_last_text = self.flags.last_text
  411. self.last_type = current_token.type
  412. self.flags.last_text = current_token.text
  413. self.token_pos += 1
  414. current_token = self.get_token()
  415. sweet_code = self.output.get_code()
  416. if self.opts.end_with_newline:
  417. sweet_code += '\n'
  418. if not self.opts.eol == '\n':
  419. sweet_code = sweet_code.replace('\n', self.opts.eol)
  420. return sweet_code
  421. def handle_whitespace_and_comments(self, local_token, preserve_statement_flags = False):
  422. newlines = local_token.newlines
  423. keep_whitespace = self.opts.keep_array_indentation and self.is_array(self.flags.mode)
  424. for comment_token in local_token.comments_before:
  425. # The cleanest handling of inline comments is to treat them as though they aren't there.
  426. # Just continue formatting and the behavior should be logical.
  427. # Also ignore unknown tokens. Again, this should result in better behavior.
  428. self.handle_whitespace_and_comments(comment_token, preserve_statement_flags)
  429. self.handlers[comment_token.type](comment_token, preserve_statement_flags)
  430. if keep_whitespace:
  431. for i in range(newlines):
  432. self.print_newline(i > 0, preserve_statement_flags)
  433. else: # not keep_whitespace
  434. if self.opts.max_preserve_newlines != 0 and newlines > self.opts.max_preserve_newlines:
  435. newlines = self.opts.max_preserve_newlines
  436. if self.opts.preserve_newlines and newlines > 1:
  437. self.print_newline(False, preserve_statement_flags)
  438. for i in range(1, newlines):
  439. self.print_newline(True, preserve_statement_flags)
  440. def unpack(self, source, evalcode=False):
  441. import jsbeautifier.unpackers as unpackers
  442. try:
  443. return unpackers.run(source, evalcode)
  444. except unpackers.UnpackingError as error:
  445. return source
  446. def is_special_word(self, s):
  447. return s in ['case', 'return', 'do', 'if', 'throw', 'else']
  448. def is_array(self, mode):
  449. return mode == MODE.ArrayLiteral
  450. def is_expression(self, mode):
  451. return mode in [MODE.Expression, MODE.ForInitializer, MODE.Conditional]
  452. _newline_restricted_tokens = ['break','continue','return', 'throw']
  453. def allow_wrap_or_preserved_newline(self, current_token, force_linewrap = False):
  454. # never wrap the first token of a line.
  455. if self.output.just_added_newline():
  456. return
  457. shouldPreserveOrForce = (self.opts.preserve_newlines and current_token.wanted_newline) or force_linewrap
  458. operatorLogicApplies = self.flags.last_text in Tokenizer.positionable_operators or current_token.text in Tokenizer.positionable_operators
  459. if operatorLogicApplies:
  460. shouldPrintOperatorNewline = (self.flags.last_text in Tokenizer.positionable_operators and self.opts.operator_position in OPERATOR_POSITION_BEFORE_OR_PRESERVE) \
  461. or current_token.text in Tokenizer.positionable_operators
  462. shouldPreserveOrForce = shouldPreserveOrForce and shouldPrintOperatorNewline
  463. if shouldPreserveOrForce:
  464. self.print_newline(preserve_statement_flags = True)
  465. elif self.opts.wrap_line_length > 0:
  466. if self.last_type == 'TK_RESERVED' and self.flags.last_text in self._newline_restricted_tokens:
  467. # These tokens should never have a newline inserted between
  468. # them and the following expression.
  469. return
  470. proposed_line_length = self.output.current_line.get_character_count() + len(current_token.text)
  471. if self.output.space_before_token:
  472. proposed_line_length += 1
  473. if proposed_line_length >= self.opts.wrap_line_length:
  474. self.print_newline(preserve_statement_flags = True)
  475. def print_newline(self, force_newline = False, preserve_statement_flags = False):
  476. if not preserve_statement_flags:
  477. if self.flags.last_text != ';' and self.flags.last_text != ',' and self.flags.last_text != '=' and self.last_type != 'TK_OPERATOR':
  478. next_token = self.get_token(1)
  479. while (self.flags.mode == MODE.Statement and
  480. not (self.flags.if_block and next_token and next_token.type == 'TK_RESERVED' and next_token.text == 'else') and
  481. not self.flags.do_block):
  482. self.restore_mode()
  483. if self.output.add_new_line(force_newline):
  484. self.flags.multiline_frame = True
  485. def print_token_line_indentation(self, current_token):
  486. if self.output.just_added_newline():
  487. line = self.output.current_line
  488. if self.opts.keep_array_indentation and self.is_array(self.flags.mode) and current_token.wanted_newline:
  489. line.push(current_token.whitespace_before)
  490. self.output.space_before_token = False
  491. elif self.output.set_indent(self.flags.indentation_level):
  492. self.flags.line_indent_level = self.flags.indentation_level
  493. def print_token(self, current_token, s=None):
  494. if self.output.raw:
  495. self.output.add_raw_token(current_token)
  496. return
  497. if self.opts.comma_first and self.last_type == 'TK_COMMA' and self.output.just_added_newline():
  498. if self.output.previous_line.last() == ',':
  499. # if the comma was already at the start of the line,
  500. # pull back onto that line and reprint the indentation
  501. popped = self.output.previous_line.pop()
  502. if self.output.previous_line.is_empty():
  503. self.output.previous_line.push(popped)
  504. self.output.trim(True)
  505. self.output.current_line.pop()
  506. self.output.trim()
  507. # add the comma in front of the next token
  508. self.print_token_line_indentation(current_token)
  509. self.output.add_token(',')
  510. self.output.space_before_token = True
  511. if s == None:
  512. s = current_token.text
  513. self.print_token_line_indentation(current_token)
  514. self.output.add_token(s);
  515. def indent(self):
  516. self.flags.indentation_level += 1
  517. def deindent(self):
  518. allow_deindent = self.flags.indentation_level > 0 and ((self.flags.parent == None) or self.flags.indentation_level > self.flags.parent.indentation_level)
  519. if allow_deindent:
  520. self.flags.indentation_level -= 1
  521. def set_mode(self, mode):
  522. if self.flags:
  523. self.flag_store.append(self.flags)
  524. self.previous_flags = self.flags
  525. else:
  526. self.previous_flags = BeautifierFlags(mode)
  527. self.flags = BeautifierFlags(mode)
  528. self.flags.apply_base(self.previous_flags, self.output.just_added_newline())
  529. self.flags.start_line_index = self.output.get_line_number();
  530. def restore_mode(self):
  531. if len(self.flag_store) > 0:
  532. self.previous_flags = self.flags
  533. self.flags = self.flag_store.pop()
  534. if self.previous_flags.mode == MODE.Statement:
  535. self.output.remove_redundant_indentation(self.previous_flags)
  536. def start_of_object_property(self):
  537. return self.flags.parent.mode == MODE.ObjectLiteral and self.flags.mode == MODE.Statement and \
  538. ((self.flags.last_text == ':' and self.flags.ternary_depth == 0) or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set']))
  539. def start_of_statement(self, current_token):
  540. if (
  541. (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['var', 'let', 'const'] and current_token.type == 'TK_WORD') \
  542. or (self.last_type == 'TK_RESERVED' and self.flags.last_text== 'do') \
  543. or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['return', 'throw'] and not current_token.wanted_newline) \
  544. or (self.last_type == 'TK_RESERVED' and self.flags.last_text == 'else' \
  545. and not (current_token.type == 'TK_RESERVED' and current_token.text == 'if' and not len(current_token.comments_before))) \
  546. or (self.last_type == 'TK_END_EXPR' and (self.previous_flags.mode == MODE.ForInitializer or self.previous_flags.mode == MODE.Conditional)) \
  547. or (self.last_type == 'TK_WORD' and self.flags.mode == MODE.BlockStatement \
  548. and not self.flags.in_case
  549. and not (current_token.text == '--' or current_token.text == '++')
  550. and self.last_last_text != 'function'
  551. and current_token.type != 'TK_WORD' and current_token.type != 'TK_RESERVED') \
  552. or (self.flags.mode == MODE.ObjectLiteral and \
  553. ((self.flags.last_text == ':' and self.flags.ternary_depth == 0) or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set'])))
  554. ):
  555. self.set_mode(MODE.Statement)
  556. self.indent()
  557. self.handle_whitespace_and_comments(current_token, True);
  558. # Issue #276:
  559. # If starting a new statement with [if, for, while, do], push to a new line.
  560. # if (a) if (b) if(c) d(); else e(); else f();
  561. if not self.start_of_object_property():
  562. self.allow_wrap_or_preserved_newline(current_token, current_token.type == 'TK_RESERVED' and current_token.text in ['do', 'for', 'if', 'while'])
  563. return True
  564. else:
  565. return False
  566. def get_token(self, offset = 0):
  567. index = self.token_pos + offset
  568. if index < 0 or index >= len(self.tokens):
  569. return None
  570. else:
  571. return self.tokens[index]
  572. def handle_start_expr(self, current_token):
  573. if self.start_of_statement(current_token):
  574. # The conditional starts the statement if appropriate.
  575. pass
  576. else:
  577. self.handle_whitespace_and_comments(current_token)
  578. next_mode = MODE.Expression
  579. if current_token.text == '[':
  580. if self.last_type == 'TK_WORD' or self.flags.last_text == ')':
  581. if self.last_type == 'TK_RESERVED' and self.flags.last_text in Tokenizer.line_starters:
  582. self.output.space_before_token = True
  583. self.set_mode(next_mode)
  584. self.print_token(current_token)
  585. self.indent()
  586. if self.opts.space_in_paren:
  587. self.output.space_before_token = True
  588. return
  589. next_mode = MODE.ArrayLiteral
  590. if self.is_array(self.flags.mode):
  591. if self.flags.last_text == '[' or (
  592. self.flags.last_text == ',' and (self.last_last_text == ']' or self.last_last_text == '}')):
  593. # ], [ goes to a new line
  594. # }, [ goes to a new line
  595. if not self.opts.keep_array_indentation:
  596. self.print_newline()
  597. else:
  598. if self.last_type == 'TK_RESERVED' and self.flags.last_text == 'for':
  599. next_mode = MODE.ForInitializer
  600. elif self.last_type == 'TK_RESERVED' and self.flags.last_text in ['if', 'while']:
  601. next_mode = MODE.Conditional
  602. else:
  603. next_mode = MODE.Expression
  604. if self.flags.last_text == ';' or self.last_type == 'TK_START_BLOCK':
  605. self.print_newline()
  606. elif self.last_type in ['TK_END_EXPR', 'TK_START_EXPR', 'TK_END_BLOCK'] or self.flags.last_text == '.':
  607. # do nothing on (( and )( and ][ and ]( and .(
  608. # TODO: Consider whether forcing this is required. Review failing tests when removed.
  609. self.allow_wrap_or_preserved_newline(current_token, current_token.wanted_newline)
  610. elif not (self.last_type == 'TK_RESERVED' and current_token.text == '(') and self.last_type not in ['TK_WORD', 'TK_OPERATOR']:
  611. self.output.space_before_token = True
  612. elif (self.last_type == 'TK_RESERVED' and (self.flags.last_word == 'function' or self.flags.last_word == 'typeof')) or \
  613. (self.flags.last_text == '*' and (
  614. self.last_last_text in ['function', 'yield'] or
  615. (self.flags.mode == MODE.ObjectLiteral and self.last_last_text in ['{', ',']))):
  616. # function() vs function (), typeof() vs typeof ()
  617. # function*() vs function* (), yield*() vs yield* ()
  618. if self.opts.space_after_anon_function:
  619. self.output.space_before_token = True
  620. elif self.last_type == 'TK_RESERVED' and (self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == 'catch'):
  621. # TODO: option space_before_conditional
  622. self.output.space_before_token = True
  623. elif current_token.text == '(' and self.last_type == 'TK_RESERVED' and self.flags.last_word == 'await':
  624. self.output.space_before_token = True
  625. # Support of this kind of newline preservation:
  626. # a = (b &&
  627. # (c || d));
  628. if self.last_type in ['TK_EQUALS', 'TK_OPERATOR']:
  629. if not self.start_of_object_property():
  630. self.allow_wrap_or_preserved_newline(current_token)
  631. # Support preserving wrapped arrow function expressions
  632. # a.b('c',
  633. # () => d.e
  634. # )
  635. if current_token.text == '(' and self.last_type not in ['TK_WORD', 'TK_RESERVED']:
  636. self.allow_wrap_or_preserved_newline(current_token)
  637. self.set_mode(next_mode)
  638. self.print_token(current_token)
  639. if self.opts.space_in_paren:
  640. self.output.space_before_token = True
  641. # In all cases, if we newline while inside an expression it should be indented.
  642. self.indent()
  643. def handle_end_expr(self, current_token):
  644. # statements inside expressions are not valid syntax, but...
  645. # statements must all be closed when their container closes
  646. while self.flags.mode == MODE.Statement:
  647. self.restore_mode()
  648. self.handle_whitespace_and_comments(current_token)
  649. if self.flags.multiline_frame:
  650. self.allow_wrap_or_preserved_newline(current_token, current_token.text == ']' and self.is_array(self.flags.mode) and not self.opts.keep_array_indentation)
  651. if self.opts.space_in_paren:
  652. if self.last_type == 'TK_START_EXPR' and not self.opts.space_in_empty_paren:
  653. # empty parens are always "()" and "[]", not "( )" or "[ ]"
  654. self.output.space_before_token = False
  655. self.output.trim()
  656. else:
  657. self.output.space_before_token = True
  658. if current_token.text == ']' and self.opts.keep_array_indentation:
  659. self.print_token(current_token)
  660. self.restore_mode()
  661. else:
  662. self.restore_mode()
  663. self.print_token(current_token)
  664. self.output.remove_redundant_indentation(self.previous_flags)
  665. # do {} while () // no statement required after
  666. if self.flags.do_while and self.previous_flags.mode == MODE.Conditional:
  667. self.previous_flags.mode = MODE.Expression
  668. self.flags.do_block = False
  669. self.flags.do_while = False
  670. def handle_start_block(self, current_token):
  671. self.handle_whitespace_and_comments(current_token)
  672. # Check if this is a BlockStatement that should be treated as a ObjectLiteral
  673. next_token = self.get_token(1)
  674. second_token = self.get_token(2)
  675. if second_token != None and \
  676. ((second_token.text in [':', ','] and next_token.type in ['TK_STRING', 'TK_WORD', 'TK_RESERVED']) \
  677. or (next_token.text in ['get', 'set', '...'] and second_token.type in ['TK_WORD', 'TK_RESERVED'])):
  678. # We don't support TypeScript,but we didn't break it for a very long time.
  679. # We'll try to keep not breaking it.
  680. if not self.last_last_text in ['class','interface']:
  681. self.set_mode(MODE.ObjectLiteral)
  682. else:
  683. self.set_mode(MODE.BlockStatement)
  684. elif self.last_type == 'TK_OPERATOR' and self.flags.last_text == '=>':
  685. # arrow function: (param1, paramN) => { statements }
  686. self.set_mode(MODE.BlockStatement)
  687. elif self.last_type in ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA', 'TK_OPERATOR'] or \
  688. (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['return', 'throw', 'import', 'default']):
  689. # Detecting shorthand function syntax is difficult by scanning forward,
  690. # so check the surrounding context.
  691. # If the block is being returned, imported, export default, passed as arg,
  692. # assigned with = or assigned in a nested object, treat as an ObjectLiteral.
  693. self.set_mode(MODE.ObjectLiteral)
  694. else:
  695. self.set_mode(MODE.BlockStatement)
  696. empty_braces = (not next_token == None) and len(next_token.comments_before) == 0 and next_token.text == '}'
  697. empty_anonymous_function = empty_braces and self.flags.last_word == 'function' and \
  698. self.last_type == 'TK_END_EXPR'
  699. if self.opts.brace_preserve_inline: # check for inline, set inline_frame if so
  700. # search forward for newline wanted inside this block
  701. index = 0
  702. check_token = None
  703. self.flags.inline_frame = True
  704. do_loop = True
  705. while (do_loop):
  706. index += 1
  707. check_token = self.get_token(index)
  708. if check_token.wanted_newline:
  709. self.flags.inline_frame = False
  710. do_loop = (check_token.type != 'TK_EOF' and
  711. not (check_token.type == 'TK_END_BLOCK' and check_token.opened == current_token))
  712. if (self.opts.brace_style == 'expand' or \
  713. (self.opts.brace_style == 'none' and current_token.wanted_newline)) and \
  714. not self.flags.inline_frame:
  715. if self.last_type != 'TK_OPERATOR' and \
  716. (empty_anonymous_function or
  717. self.last_type == 'TK_EQUALS' or
  718. (self.last_type == 'TK_RESERVED' and self.is_special_word(self.flags.last_text) and self.flags.last_text != 'else')):
  719. self.output.space_before_token = True
  720. else:
  721. self.print_newline(preserve_statement_flags = True)
  722. else: # collapse || inline_frame
  723. if self.is_array(self.previous_flags.mode) and (self.last_type == 'TK_START_EXPR' or self.last_type == 'TK_COMMA'):
  724. # if we're preserving inline,
  725. # allow newline between comma and next brace.
  726. if self.flags.inline_frame:
  727. self.allow_wrap_or_preserved_newline(current_token)
  728. self.flags.inline_frame = True
  729. self.previous_flags.multiline_frame = self.previous_flags.multiline_frame or self.flags.multiline_frame
  730. self.flags.multiline_frame = False
  731. elif self.last_type == 'TK_COMMA':
  732. self.output.space_before_token = True
  733. elif self.last_type not in ['TK_OPERATOR', 'TK_START_EXPR']:
  734. if self.last_type == 'TK_START_BLOCK' and not self.flags.inline_frame:
  735. self.print_newline()
  736. else:
  737. self.output.space_before_token = True
  738. self.print_token(current_token)
  739. self.indent()
  740. def handle_end_block(self, current_token):
  741. # statements must all be closed when their container closes
  742. self.handle_whitespace_and_comments(current_token)
  743. while self.flags.mode == MODE.Statement:
  744. self.restore_mode()
  745. empty_braces = self.last_type == 'TK_START_BLOCK'
  746. if self.flags.inline_frame and not empty_braces: # try inline_frame (only set if opt.braces-preserve-inline) first
  747. self.output.space_before_token = True;
  748. elif self.opts.brace_style == 'expand':
  749. if not empty_braces:
  750. self.print_newline()
  751. else:
  752. # skip {}
  753. if not empty_braces:
  754. if self.is_array(self.flags.mode) and self.opts.keep_array_indentation:
  755. self.opts.keep_array_indentation = False
  756. self.print_newline()
  757. self.opts.keep_array_indentation = True
  758. else:
  759. self.print_newline()
  760. self.restore_mode()
  761. self.print_token(current_token)
  762. def handle_word(self, current_token):
  763. if current_token.type == 'TK_RESERVED':
  764. if current_token.text in ['set', 'get'] and self.flags.mode != MODE.ObjectLiteral:
  765. current_token.type = 'TK_WORD'
  766. elif current_token.text in ['as', 'from'] and not self.flags.import_block:
  767. current_token.type = 'TK_WORD'
  768. elif self.flags.mode == MODE.ObjectLiteral:
  769. next_token = self.get_token(1)
  770. if next_token.text == ':':
  771. current_token.type = 'TK_WORD'
  772. if self.start_of_statement(current_token):
  773. # The conditional starts the statement if appropriate.
  774. if self.last_type == 'TK_RESERVED' and self.flags.last_text in ['var', 'let', 'const'] and current_token.type == 'TK_WORD':
  775. self.flags.declaration_statement = True
  776. elif current_token.wanted_newline and \
  777. not self.is_expression(self.flags.mode) and \
  778. (self.last_type != 'TK_OPERATOR' or (self.flags.last_text == '--' or self.flags.last_text == '++')) and \
  779. self.last_type != 'TK_EQUALS' and \
  780. (self.opts.preserve_newlines or not (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['var', 'let', 'const', 'set', 'get'])):
  781. self.handle_whitespace_and_comments(current_token)
  782. self.print_newline()
  783. else:
  784. self.handle_whitespace_and_comments(current_token)
  785. if self.flags.do_block and not self.flags.do_while:
  786. if current_token.type == 'TK_RESERVED' and current_token.text == 'while':
  787. # do {} ## while ()
  788. self.output.space_before_token = True
  789. self.print_token(current_token)
  790. self.output.space_before_token = True
  791. self.flags.do_while = True
  792. return
  793. else:
  794. # do {} should always have while as the next word.
  795. # if we don't see the expected while, recover
  796. self.print_newline()
  797. self.flags.do_block = False
  798. # if may be followed by else, or not
  799. # Bare/inline ifs are tricky
  800. # Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
  801. if self.flags.if_block:
  802. if (not self.flags.else_block) and (current_token.type == 'TK_RESERVED' and current_token.text == 'else'):
  803. self.flags.else_block = True
  804. else:
  805. while self.flags.mode == MODE.Statement:
  806. self.restore_mode()
  807. self.flags.if_block = False
  808. if current_token.type == 'TK_RESERVED' and (current_token.text == 'case' or (current_token.text == 'default' and self.flags.in_case_statement)):
  809. self.print_newline()
  810. if self.flags.case_body or self.opts.jslint_happy:
  811. self.flags.case_body = False
  812. self.deindent()
  813. self.print_token(current_token)
  814. self.flags.in_case = True
  815. self.flags.in_case_statement = True
  816. return
  817. if self.last_type in ['TK_COMMA', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']:
  818. if not self.start_of_object_property():
  819. self.allow_wrap_or_preserved_newline(current_token)
  820. if current_token.type == 'TK_RESERVED' and current_token.text == 'function':
  821. if (self.flags.last_text in ['}', ';'] or
  822. (self.output.just_added_newline() and not (self.flags.last_text in ['(', '[', '{', ':', '=', ','] or self.last_type == 'TK_OPERATOR'))):
  823. # make sure there is a nice clean space of at least one blank line
  824. # before a new function definition, except in arrays
  825. if not self.output.just_added_blankline() and len(current_token.comments_before) == 0:
  826. self.print_newline()
  827. self.print_newline(True)
  828. if self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD':
  829. if self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set', 'new', 'return', 'export', 'async']:
  830. self.output.space_before_token = True
  831. elif self.last_type == 'TK_RESERVED' and self.flags.last_text == 'default' and self.last_last_text == 'export':
  832. self.output.space_before_token = True
  833. else:
  834. self.print_newline()
  835. elif self.last_type == 'TK_OPERATOR' or self.flags.last_text == '=':
  836. # foo = function
  837. self.output.space_before_token = True
  838. elif not self.flags.multiline_frame and (self.is_expression(self.flags.mode) or self.is_array(self.flags.mode)):
  839. # (function
  840. pass
  841. else:
  842. self.print_newline()
  843. self.print_token(current_token)
  844. self.flags.last_word = current_token.text
  845. return
  846. prefix = 'NONE'
  847. if self.last_type == 'TK_END_BLOCK':
  848. if self.previous_flags.inline_frame:
  849. prefix = 'SPACE'
  850. elif not (current_token.type == 'TK_RESERVED' and current_token.text in ['else', 'catch', 'finally', 'from']):
  851. prefix = 'NEWLINE'
  852. else:
  853. if self.opts.brace_style in ['expand', 'end-expand'] or \
  854. (self.opts.brace_style == 'none' and current_token.wanted_newline):
  855. prefix = 'NEWLINE'
  856. else:
  857. prefix = 'SPACE'
  858. self.output.space_before_token = True
  859. elif self.last_type == 'TK_SEMICOLON' and self.flags.mode == MODE.BlockStatement:
  860. # TODO: Should this be for STATEMENT as well?
  861. prefix = 'NEWLINE'
  862. elif self.last_type == 'TK_SEMICOLON' and self.is_expression(self.flags.mode):
  863. prefix = 'SPACE'
  864. elif self.last_type == 'TK_STRING':
  865. prefix = 'NEWLINE'
  866. elif self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD' or \
  867. (self.flags.last_text == '*' and (
  868. self.last_last_text in ['function', 'yield'] or
  869. (self.flags.mode == MODE.ObjectLiteral and self.last_last_text in ['{', ',']))):
  870. prefix = 'SPACE'
  871. elif self.last_type == 'TK_START_BLOCK':
  872. if self.flags.inline_frame:
  873. prefix = 'SPACE'
  874. else:
  875. prefix = 'NEWLINE'
  876. elif self.last_type == 'TK_END_EXPR':
  877. self.output.space_before_token = True
  878. prefix = 'NEWLINE'
  879. if current_token.type == 'TK_RESERVED' and current_token.text in Tokenizer.line_starters and self.flags.last_text != ')':
  880. if self.flags.inline_frame or self.flags.last_text == 'else ' or self.flags.last_text == 'export':
  881. prefix = 'SPACE'
  882. else:
  883. prefix = 'NEWLINE'
  884. if current_token.type == 'TK_RESERVED' and current_token.text in ['else', 'catch', 'finally']:
  885. if ((not (self.last_type == 'TK_END_BLOCK' and self.previous_flags.mode == MODE.BlockStatement)) \
  886. or self.opts.brace_style == 'expand' \
  887. or self.opts.brace_style == 'end-expand' \
  888. or (self.opts.brace_style == 'none' and current_token.wanted_newline)) \
  889. and not self.flags.inline_frame:
  890. self.print_newline()
  891. else:
  892. self.output.trim(True)
  893. # If we trimmed and there's something other than a close block before us
  894. # put a newline back in. Handles '} // comment' scenario.
  895. if self.output.current_line.last() != '}':
  896. self.print_newline()
  897. self.output.space_before_token = True
  898. elif prefix == 'NEWLINE':
  899. if self.last_type == 'TK_RESERVED' and self.is_special_word(self.flags.last_text):
  900. # no newline between return nnn
  901. self.output.space_before_token = True
  902. elif self.last_type != 'TK_END_EXPR':
  903. if (self.last_type != 'TK_START_EXPR' or not (current_token.type == 'TK_RESERVED' and current_token.text in ['var', 'let', 'const'])) and self.flags.last_text != ':':
  904. # no need to force newline on VAR -
  905. # for (var x = 0...
  906. if current_token.type == 'TK_RESERVED' and current_token.text == 'if' and self.flags.last_text == 'else':
  907. self.output.space_before_token = True
  908. else:
  909. self.print_newline()
  910. elif current_token.type == 'TK_RESERVED' and current_token.text in Tokenizer.line_starters and self.flags.last_text != ')':
  911. self.print_newline()
  912. elif self.flags.multiline_frame and self.is_array(self.flags.mode) and self.flags.last_text == ',' and self.last_last_text == '}':
  913. self.print_newline() # }, in lists get a newline
  914. elif prefix == 'SPACE':
  915. self.output.space_before_token = True
  916. self.print_token(current_token)
  917. self.flags.last_word = current_token.text
  918. if current_token.type == 'TK_RESERVED':
  919. if current_token.text == 'do':
  920. self.flags.do_block = True
  921. elif current_token.text == 'if':
  922. self.flags.if_block = True
  923. elif current_token.text == 'import':
  924. self.flags.import_block = True
  925. elif current_token.text == 'from' and self.flags.import_block:
  926. self.flags.import_block = False
  927. def handle_semicolon(self, current_token):
  928. if self.start_of_statement(current_token):
  929. # The conditional starts the statement if appropriate.
  930. # Semicolon can be the start (and end) of a statement
  931. self.output.space_before_token = False
  932. else:
  933. self.handle_whitespace_and_comments(current_token)
  934. next_token = self.get_token(1)
  935. while (self.flags.mode == MODE.Statement and
  936. not (self.flags.if_block and next_token and next_token.type == 'TK_RESERVED' and next_token.text == 'else') and
  937. not self.flags.do_block):
  938. self.restore_mode()
  939. if self.flags.import_block:
  940. self.flags.import_block = False
  941. self.print_token(current_token)
  942. def handle_string(self, current_token):
  943. if self.start_of_statement(current_token):
  944. # The conditional starts the statement if appropriate.
  945. # One difference - strings want at least a space before
  946. self.output.space_before_token = True
  947. else:
  948. self.handle_whitespace_and_comments(current_token)
  949. if self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD' or self.flags.inline_frame:
  950. self.output.space_before_token = True
  951. elif self.last_type in ['TK_COMMA', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']:
  952. if not self.start_of_object_property():
  953. self.allow_wrap_or_preserved_newline(current_token)
  954. else:
  955. self.print_newline()
  956. self.print_token(current_token)
  957. def handle_equals(self, current_token):
  958. if self.start_of_statement(current_token):
  959. # The conditional starts the statement if appropriate.
  960. pass
  961. else:
  962. self.handle_whitespace_and_comments(current_token)
  963. if self.flags.declaration_statement:
  964. # just got an '=' in a var-line, different line breaking rules will apply
  965. self.flags.declaration_assignment = True
  966. self.output.space_before_token = True
  967. self.print_token(current_token)
  968. self.output.space_before_token = True
  969. def handle_comma(self, current_token):
  970. self.handle_whitespace_and_comments(current_token, True)
  971. self.print_token(current_token)
  972. self.output.space_before_token = True
  973. if self.flags.declaration_statement:
  974. if self.is_expression(self.flags.parent.mode):
  975. # do not break on comma, for ( var a = 1, b = 2
  976. self.flags.declaration_assignment = False
  977. if self.flags.declaration_assignment:
  978. self.flags.declaration_assignment = False
  979. self.print_newline(preserve_statement_flags = True)
  980. elif self.opts.comma_first:
  981. # for comma-first, we want to allow a newline before the comma
  982. # to turn into a newline after the comma, which we will fixup later
  983. self.allow_wrap_or_preserved_newline(current_token)
  984. elif self.flags.mode == MODE.ObjectLiteral \
  985. or (self.flags.mode == MODE.Statement and self.flags.parent.mode == MODE.ObjectLiteral):
  986. if self.flags.mode == MODE.Statement:
  987. self.restore_mode()
  988. if not self.flags.inline_frame:
  989. self.print_newline()
  990. elif self.opts.comma_first:
  991. # EXPR or DO_BLOCK
  992. # for comma-first, we want to allow a newline before the comma
  993. # to turn into a newline after the comma, which we will fixup later
  994. self.allow_wrap_or_preserved_newline(current_token)
  995. def handle_operator(self, current_token):
  996. isGeneratorAsterisk = current_token.text == '*' and \
  997. ((self.last_type == 'TK_RESERVED' and self.flags.last_text in ['function', 'yield']) or
  998. (self.last_type in ['TK_START_BLOCK', 'TK_COMMA', 'TK_END_BLOCK', 'TK_SEMICOLON']))
  999. isUnary = current_token.text in ['+', '-'] \
  1000. and (self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR'] \
  1001. or self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == ',')
  1002. if self.start_of_statement(current_token):
  1003. # The conditional starts the statement if appropriate.
  1004. pass
  1005. else:
  1006. preserve_statement_flags = not isGeneratorAsterisk
  1007. self.handle_whitespace_and_comments(current_token, preserve_statement_flags)
  1008. if self.last_type == 'TK_RESERVED' and self.is_special_word(self.flags.last_text):
  1009. # return had a special handling in TK_WORD
  1010. self.output.space_before_token = True
  1011. self.print_token(current_token)
  1012. return
  1013. # hack for actionscript's import .*;
  1014. if current_token.text == '*' and self.last_type == 'TK_DOT':
  1015. self.print_token(current_token)
  1016. return
  1017. if current_token.text == '::':
  1018. # no spaces around the exotic namespacing syntax operator
  1019. self.print_token(current_token)
  1020. return
  1021. # Allow line wrapping between operators when operator_position is
  1022. # set to before or preserve
  1023. if self.last_type == 'TK_OPERATOR' and self.opts.operator_position in OPERATOR_POSITION_BEFORE_OR_PRESERVE:
  1024. self.allow_wrap_or_preserved_newline(current_token)
  1025. if current_token.text == ':' and self.flags.in_case:
  1026. self.flags.case_body = True
  1027. self.indent()
  1028. self.print_token(current_token)
  1029. self.print_newline()
  1030. self.flags.in_case = False
  1031. return
  1032. space_before = True
  1033. space_after = True
  1034. in_ternary = False
  1035. if current_token.text == ':':
  1036. if self.flags.ternary_depth == 0:
  1037. # Colon is invalid javascript outside of ternary and object, but do our best to guess what was meant.
  1038. space_before = False
  1039. else:
  1040. self.flags.ternary_depth -= 1
  1041. in_ternary = True
  1042. elif current_token.text == '?':
  1043. self.flags.ternary_depth += 1
  1044. # let's handle the operator_position option prior to any conflicting logic
  1045. if (not isUnary) and (not isGeneratorAsterisk) and \
  1046. self.opts.preserve_newlines and current_token.text in Tokenizer.positionable_operators:
  1047. isColon = current_token.text == ':'
  1048. isTernaryColon = isColon and in_ternary
  1049. isOtherColon = isColon and not in_ternary
  1050. if self.opts.operator_position == OPERATOR_POSITION['before_newline']:
  1051. # if the current token is : and it's not a ternary statement then we set space_before to false
  1052. self.output.space_before_token = not isOtherColon
  1053. self.print_token(current_token)
  1054. if (not isColon) or isTernaryColon:
  1055. self.allow_wrap_or_preserved_newline(current_token)
  1056. self.output.space_before_token = True
  1057. return
  1058. elif self.opts.operator_position == OPERATOR_POSITION['after_newline']:
  1059. # if the current token is anything but colon, or (via deduction) it's a colon and in a ternary statement,
  1060. # then print a newline.
  1061. self.output.space_before_token = True
  1062. if (not isColon) or isTernaryColon:
  1063. if self.get_token(1).wanted_newline:
  1064. self.print_newline(preserve_statement_flags = True)
  1065. else:
  1066. self.allow_wrap_or_preserved_newline(current_token)
  1067. else:
  1068. self.output.space_before_token = False
  1069. self.print_token(current_token)
  1070. self.output.space_before_token = True
  1071. return
  1072. elif self.opts.operator_position == OPERATOR_POSITION['preserve_newline']:
  1073. if not isOtherColon:
  1074. self.allow_wrap_or_preserved_newline(current_token)
  1075. # if we just added a newline, or the current token is : and it's not a ternary statement,
  1076. # then we set space_before to false
  1077. self.output.space_before_token = not (self.output.just_added_newline() or isOtherColon)
  1078. self.print_token(current_token)
  1079. self.output.space_before_token = True
  1080. return
  1081. if isGeneratorAsterisk:
  1082. self.allow_wrap_or_preserved_newline(current_token)
  1083. space_before = False
  1084. next_token = self.get_token(1)
  1085. space_after = next_token and next_token.type in ['TK_WORD','TK_RESERVED']
  1086. elif current_token.text == '...':
  1087. self.allow_wrap_or_preserved_newline(current_token)
  1088. space_before = self.last_type == 'TK_START_BLOCK'
  1089. space_after = False
  1090. elif current_token.text in ['--', '++', '!', '~'] or isUnary:
  1091. space_before = False
  1092. space_after = False
  1093. # http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
  1094. # if there is a newline between -- or ++ and anything else we should preserve it.
  1095. if current_token.wanted_newline and (current_token.text == '--' or current_token.text == '++'):
  1096. self.print_newline(preserve_statement_flags = True)
  1097. if self.flags.last_text == ';' and self.is_expression(self.flags.mode):
  1098. # for (;; ++i)
  1099. # ^^
  1100. space_before = True
  1101. if self.last_type == 'TK_RESERVED':
  1102. space_before = True
  1103. elif self.last_type == 'TK_END_EXPR':
  1104. space_before = not (self.flags.last_text == ']' and current_token.text in ['--', '++'])
  1105. elif self.last_type == 'TK_OPERATOR':
  1106. # a++ + ++b
  1107. # a - -b
  1108. space_before = current_token.text in ['--', '-','++', '+'] and self.flags.last_text in ['--', '-','++', '+']
  1109. # + and - are not unary when preceeded by -- or ++ operator
  1110. # a-- + b
  1111. # a * +b
  1112. # a - -b
  1113. if current_token.text in ['-', '+'] and self.flags.last_text in ['--', '++']:
  1114. space_after = True
  1115. if (((self.flags.mode == MODE.BlockStatement and not self.flags.inline_frame) or self.flags.mode == MODE.Statement)
  1116. and self.flags.last_text in ['{', ';']):
  1117. # { foo: --i }
  1118. # foo(): --bar
  1119. self.print_newline()
  1120. if space_before:
  1121. self.output.space_before_token = True
  1122. self.print_token(current_token)
  1123. if space_after:
  1124. self.output.space_before_token = True
  1125. def handle_block_comment(self, current_token, preserve_statement_flags):
  1126. if self.output.raw:
  1127. self.output.add_raw_token(current_token)
  1128. if current_token.directives and current_token.directives.get('preserve') == 'end':
  1129. # If we're testing the raw output behavior, do not allow a directive to turn it off.
  1130. self.output.raw = self.opts.test_output_raw
  1131. return
  1132. if current_token.directives:
  1133. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1134. self.print_token(current_token)
  1135. if current_token.directives.get('preserve') == 'start':
  1136. self.output.raw = True
  1137. self.print_newline(preserve_statement_flags = True)
  1138. return
  1139. # inline block
  1140. if not self.acorn.newline.search(current_token.text) and not current_token.wanted_newline:
  1141. self.output.space_before_token = True
  1142. self.print_token(current_token)
  1143. self.output.space_before_token = True
  1144. return
  1145. lines = self.acorn.allLineBreaks.split(current_token.text)
  1146. javadoc = False
  1147. starless = False
  1148. last_indent = current_token.whitespace_before
  1149. last_indent_length = len(last_indent)
  1150. # block comment starts with a new line
  1151. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1152. if len(lines) > 1:
  1153. javadoc = not any(l for l in lines[1:] if ( l.strip() == '' or (l.lstrip())[0] != '*'))
  1154. starless = all(l.startswith(last_indent) or l.strip() == '' for l in lines[1:])
  1155. # first line always indented
  1156. self.print_token(current_token, lines[0])
  1157. for line in lines[1:]:
  1158. self.print_newline(preserve_statement_flags = True)
  1159. if javadoc:
  1160. # javadoc: reformat and re-indent
  1161. self.print_token(current_token, ' ' + line.lstrip())
  1162. elif starless and len(line) > last_indent_length:
  1163. # starless: re-indent non-empty content, avoiding trim
  1164. self.print_token(current_token, line[last_indent_length:])
  1165. else:
  1166. # normal comments output raw
  1167. self.output.add_token(line)
  1168. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1169. def handle_comment(self, current_token, preserve_statement_flags):
  1170. if current_token.wanted_newline:
  1171. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1172. if not current_token.wanted_newline:
  1173. self.output.trim(True)
  1174. self.output.space_before_token = True
  1175. self.print_token(current_token)
  1176. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1177. def handle_dot(self, current_token):
  1178. if self.start_of_statement(current_token):
  1179. # The conditional starts the statement if appropriate.
  1180. pass
  1181. else:
  1182. self.handle_whitespace_and_comments(current_token, True)
  1183. if self.last_type == 'TK_RESERVED' and self.is_special_word(self.flags.last_text):
  1184. self.output.space_before_token = True
  1185. else:
  1186. # allow preserved newlines before dots in general
  1187. # force newlines on dots after close paren when break_chained - for bar().baz()
  1188. self.allow_wrap_or_preserved_newline(current_token,
  1189. self.flags.last_text == ')' and self.opts.break_chained_methods)
  1190. self.print_token(current_token)
  1191. def handle_unknown(self, current_token, preserve_statement_flags):
  1192. self.print_token(current_token)
  1193. if current_token.text[-1] == '\n':
  1194. self.print_newline(preserve_statement_flags = preserve_statement_flags)
  1195. def handle_eof(self, current_token):
  1196. # Unwind any open statements
  1197. while self.flags.mode == MODE.Statement:
  1198. self.restore_mode()
  1199. self.handle_whitespace_and_comments(current_token)
  1200. def mkdir_p(path):
  1201. try:
  1202. if path:
  1203. os.makedirs(path)
  1204. except OSError as exc: # Python >2.5
  1205. if exc.errno == errno.EEXIST and os.path.isdir(path):
  1206. pass
  1207. else:
  1208. raise Exception()
  1209. # Using object instead of string to allow for later expansion of info about each line
  1210. class OutputLine:
  1211. def __init__(self, parent):
  1212. self.__parent = parent
  1213. self.__character_count = 0
  1214. self.__indent_count = -1
  1215. self.__items = []
  1216. self.__empty = True
  1217. def get_character_count(self):
  1218. return self.__character_count
  1219. def is_empty(self):
  1220. return self.__empty
  1221. def set_indent(self, level):
  1222. self.__character_count = self.__parent.baseIndentLength + level * self.__parent.indent_length
  1223. self.__indent_count = level;
  1224. def last(self):
  1225. if not self.is_empty():
  1226. return self.__items[-1]
  1227. else:
  1228. return None
  1229. def push(self, input):
  1230. self.__items.append(input)
  1231. self.__character_count += len(input)
  1232. self.__empty = False
  1233. def pop(self):
  1234. item = None
  1235. if not self.is_empty():
  1236. item = self.__items.pop()
  1237. self.__character_count -= len(item)
  1238. self.__empty = len(self.__items) == 0
  1239. return item
  1240. def remove_indent(self):
  1241. if self.__indent_count > 0:
  1242. self.__indent_count -= 1
  1243. self.__character_count -= self.__parent.indent_length
  1244. def trim(self):
  1245. while self.last() == ' ':
  1246. item = self._items.pop()
  1247. self.__character_count -= 1
  1248. self.__empty = len(self.__items) == 0
  1249. def toString(self):
  1250. result = ''
  1251. if not self.is_empty():
  1252. if self.__indent_count >= 0:
  1253. result = self.__parent.indent_cache[self.__indent_count]
  1254. result += ''.join(self.__items)
  1255. return result
  1256. class Output:
  1257. def __init__(self, indent_string, baseIndentString = ''):
  1258. self.indent_string = indent_string
  1259. self.baseIndentString = baseIndentString
  1260. self.indent_cache = [ baseIndentString ]
  1261. self.baseIndentLength = len(baseIndentString)
  1262. self.indent_length = len(indent_string)
  1263. self.raw = False
  1264. self.lines = []
  1265. self.previous_line = None
  1266. self.current_line = None
  1267. self.space_before_token = False
  1268. self.add_outputline()
  1269. def add_outputline(self):
  1270. self.previous_line = self.current_line
  1271. self.current_line = OutputLine(self)
  1272. self.lines.append(self.current_line)
  1273. def get_line_number(self):
  1274. return len(self.lines)
  1275. def add_new_line(self, force_newline):
  1276. if len(self.lines) == 1 and self.just_added_newline():
  1277. # no newline on start of file
  1278. return False
  1279. if force_newline or not self.just_added_newline():
  1280. if not self.raw:
  1281. self.add_outputline()
  1282. return True
  1283. return False
  1284. def get_code(self):
  1285. sweet_code = "\n".join(line.toString() for line in self.lines)
  1286. return re.sub('[\r\n\t ]+$', '', sweet_code)
  1287. def set_indent(self, level):
  1288. # Never indent your first output indent at the start of the file
  1289. if len(self.lines) > 1:
  1290. while level >= len(self.indent_cache):
  1291. self.indent_cache.append(self.indent_cache[-1] + self.indent_string)
  1292. self.current_line.set_indent(level)
  1293. return True
  1294. self.current_line.set_indent(0)
  1295. return False
  1296. def add_raw_token(self, token):
  1297. for _ in range(token.newlines):
  1298. self.add_outputline()
  1299. self.current_line.push(token.whitespace_before)
  1300. self.current_line.push(token.text)
  1301. self.space_before_token = False
  1302. def add_token(self, printable_token):
  1303. self.add_space_before_token()
  1304. self.current_line.push(printable_token)
  1305. def add_space_before_token(self):
  1306. if self.space_before_token and not self.just_added_newline():
  1307. self.current_line.push(' ')
  1308. self.space_before_token = False
  1309. def remove_redundant_indentation(self, frame):
  1310. # This implementation is effective but has some issues:
  1311. # - can cause line wrap to happen too soon due to indent removal
  1312. # after wrap points are calculated
  1313. # These issues are minor compared to ugly indentation.
  1314. if frame.multiline_frame or frame.mode == MODE.ForInitializer or frame.mode == MODE.Conditional:
  1315. return
  1316. # remove one indent from each line inside this section
  1317. index = frame.start_line_index
  1318. while index < len(self.lines):
  1319. self.lines[index].remove_indent()
  1320. index += 1
  1321. def trim(self, eat_newlines = False):
  1322. self.current_line.trim()
  1323. while eat_newlines and len(self.lines) > 1 and self.current_line.is_empty():
  1324. self.lines.pop()
  1325. self.current_line = self.lines[-1]
  1326. self.current_line.trim()
  1327. if len(self.lines) > 1:
  1328. self.previous_line = self.lines[-2]
  1329. else:
  1330. self.previous_line = None
  1331. def just_added_newline(self):
  1332. return self.current_line.is_empty()
  1333. def just_added_blankline(self):
  1334. if self.just_added_newline():
  1335. if len(self.lines) == 1:
  1336. return True
  1337. line = self.lines[-2]
  1338. return line.is_empty()
  1339. return False
  1340. class InputScanner:
  1341. def __init__(self, input):
  1342. self.__input = input
  1343. self.__input_length = len(self.__input)
  1344. self.__position = 0
  1345. def back(self):
  1346. self.__position -= 1
  1347. def hasNext(self):
  1348. return self.__position < self.__input_length
  1349. def next(self):
  1350. val = None
  1351. if self.hasNext():
  1352. val = self.__input[self.__position]
  1353. self.__position += 1
  1354. return val;
  1355. def peek(self, index = 0):
  1356. val = None
  1357. index += self.__position;
  1358. if index >= 0 and index < self.__input_length:
  1359. val = self.__input[index];
  1360. return val;
  1361. def peekCharCode(self, index = 0):
  1362. val = 0
  1363. index += self.__position;
  1364. if index >= 0 and index < self.__input_length:
  1365. val = ord(self.__input[index])
  1366. return val
  1367. def test(self, pattern, index = 0):
  1368. index += self.__position;
  1369. return index >= 0 and index < self.__input_length and pattern.match(self.__input, index)
  1370. def testChar(self, pattern, index = 0):
  1371. val = self.peek(index)
  1372. return val != None and pattern.match(val)
  1373. def match(self, pattern):
  1374. pattern_match = None
  1375. if self.hasNext():
  1376. pattern_match = pattern.match(self.__input, self.__position)
  1377. if pattern_match:
  1378. self.__position += len(pattern_match.group(0));
  1379. return pattern_match
  1380. class Tokenizer:
  1381. whitespace = ["\n", "\r", "\t", " "]
  1382. digit = re.compile('[0-9]')
  1383. digit_bin = re.compile('[01]')
  1384. digit_oct = re.compile('[01234567]')
  1385. digit_hex = re.compile('[0123456789abcdefABCDEF]')
  1386. positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' ')
  1387. punct = (positionable_operators +
  1388. # non-positionable operators - these do not follow operator position settings
  1389. '! %= &= *= **= ++ += , -- -= /= :: <<= = => >>= >>>= ^= |= ~ ...'.split(' '))
  1390. # Words which always should start on a new line
  1391. line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',')
  1392. reserved_words = line_starters + ['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']
  1393. def __init__ (self, input_string, opts, indent_string):
  1394. self.input = InputScanner(input_string)
  1395. self.opts = opts
  1396. self.indent_string = indent_string
  1397. self.acorn = Acorn()
  1398. # /* ... */ comment ends with nearest */ or end of file
  1399. self.block_comment_pattern = re.compile('([\s\S]*?)((?:\*\/)|$)')
  1400. # comment ends just before nearest linefeed or end of file
  1401. self.comment_pattern = re.compile(self.acorn.six.u('([^\n\r\u2028\u2029]*)'))
  1402. self.directives_block_pattern = re.compile('\/\* beautify( \w+[:]\w+)+ \*\/')
  1403. self.directive_pattern = re.compile(' (\w+)[:](\w+)')
  1404. self.directives_end_ignore_pattern = re.compile('([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)')
  1405. self.template_pattern = re.compile('((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)')
  1406. def tokenize(self):
  1407. self.in_html_comment = False
  1408. self.tokens = []
  1409. next = None
  1410. last = None
  1411. open = None
  1412. open_stack = []
  1413. comments = []
  1414. while not (not last == None and last.type == 'TK_EOF'):
  1415. token_values = self.__tokenize_next()
  1416. next = Token(token_values[1], token_values[0], self.n_newlines, self.whitespace_before_token)
  1417. while next.type == 'TK_COMMENT' or next.type == 'TK_BLOCK_COMMENT' or next.type == 'TK_UNKNOWN':
  1418. if next.type == 'TK_BLOCK_COMMENT':
  1419. next.directives = token_values[2]
  1420. comments.append(next)
  1421. token_values = self.__tokenize_next()
  1422. next = Token(token_values[1], token_values[0], self.n_newlines, self.whitespace_before_token)
  1423. if len(comments) > 0:
  1424. next.comments_before = comments
  1425. comments = []
  1426. if next.type == 'TK_START_BLOCK' or next.type == 'TK_START_EXPR':
  1427. next.parent = last
  1428. open_stack.append(open)
  1429. open = next
  1430. elif (next.type == 'TK_END_BLOCK' or next.type == 'TK_END_EXPR') and \
  1431. (not open == None and ( \
  1432. (next.text == ']' and open.text == '[') or \
  1433. (next.text == ')' and open.text == '(') or \
  1434. (next.text == '}' and open.text == '{'))):
  1435. next.parent = open.parent
  1436. next.opened = open
  1437. open = open_stack.pop()
  1438. self.tokens.append(next)
  1439. last = next
  1440. return self.tokens
  1441. def get_directives (self, text):
  1442. if not self.directives_block_pattern.match(text):
  1443. return None
  1444. directives = {}
  1445. directive_match = self.directive_pattern.search(text)
  1446. while directive_match:
  1447. directives[directive_match.group(1)] = directive_match.group(2)
  1448. directive_match = self.directive_pattern.search(text, directive_match.end())
  1449. return directives
  1450. def __tokenize_next(self):
  1451. whitespace_on_this_line = []
  1452. self.n_newlines = 0
  1453. self.whitespace_before_token = ''
  1454. c = self.input.next()
  1455. if c == None:
  1456. return '', 'TK_EOF'
  1457. if len(self.tokens) > 0:
  1458. last_token = self.tokens[-1]
  1459. else:
  1460. # For the sake of tokenizing we can pretend that there was on open brace to start
  1461. last_token = Token('TK_START_BLOCK', '{')
  1462. while c in self.whitespace:
  1463. if self.acorn.newline.match(c):
  1464. # treat \r\n as one newline
  1465. if not (c == '\n' and self.input.peek(-2) == '\r'):
  1466. self.n_newlines += 1
  1467. whitespace_on_this_line = []
  1468. else:
  1469. whitespace_on_this_line.append(c)
  1470. c = self.input.next()
  1471. if c == None:
  1472. return '', 'TK_EOF'
  1473. if len(whitespace_on_this_line) != 0:
  1474. self.whitespace_before_token = ''.join(whitespace_on_this_line)
  1475. if self.digit.match(c) or (c == '.' and self.input.testChar(self.digit)):
  1476. allow_decimal = True
  1477. allow_e = True
  1478. local_digit = self.digit
  1479. if c == '0' and self.input.testChar(re.compile('[XxOoBb]')):
  1480. # switch to hex/oct/bin number, no decimal or e, just hex/oct/bin digits
  1481. allow_decimal = False
  1482. allow_e = False
  1483. if self.input.testChar(re.compile('[Bb]')):
  1484. local_digit = self.digit_bin
  1485. elif self.input.testChar(re.compile('[Oo]')):
  1486. local_digit = self.digit_oct
  1487. else:
  1488. local_digit = self.digit_hex
  1489. c += self.input.next()
  1490. elif c == '.':
  1491. # Already have a decimal for this literal, don't allow another
  1492. allow_decimal = False
  1493. else:
  1494. # we know this first loop will run. It keeps the logic simpler.
  1495. c = ''
  1496. self.input.back()
  1497. # Add the digits
  1498. while self.input.testChar(local_digit):
  1499. c += self.input.next()
  1500. if allow_decimal and self.input.peek() == '.':
  1501. c += self.input.next()
  1502. allow_decimal = False
  1503. # a = 1.e-7 is valid, so we test for . then e in one loop
  1504. if allow_e and self.input.testChar(re.compile('[Ee]')):
  1505. c += self.input.next()
  1506. if self.input.testChar(re.compile('[+-]')):
  1507. c += self.input.next()
  1508. allow_e = False
  1509. allow_decimal = False
  1510. return c, 'TK_WORD'
  1511. if self.acorn.isIdentifierStart(self.input.peekCharCode(-1)):
  1512. if self.input.hasNext():
  1513. while self.acorn.isIdentifierChar(self.input.peekCharCode()):
  1514. c += self.input.next()
  1515. if not self.input.hasNext():
  1516. break
  1517. if not (last_token.type == 'TK_DOT' \
  1518. or (last_token.type == 'TK_RESERVED' and last_token.text in ['set', 'get'])) \
  1519. and c in self.reserved_words:
  1520. if c == 'in' or c == 'of': # in and of are operators, need to hack
  1521. return c, 'TK_OPERATOR'
  1522. return c, 'TK_RESERVED'
  1523. return c, 'TK_WORD'
  1524. if c in '([':
  1525. return c, 'TK_START_EXPR'
  1526. if c in ')]':
  1527. return c, 'TK_END_EXPR'
  1528. if c == '{':
  1529. return c, 'TK_START_BLOCK'
  1530. if c == '}':
  1531. return c, 'TK_END_BLOCK'
  1532. if c == ';':
  1533. return c, 'TK_SEMICOLON'
  1534. if c == '/':
  1535. comment = ''
  1536. inline_comment = True
  1537. if self.input.peek() == '*': # peek /* .. */ comment
  1538. self.input.next()
  1539. comment_match = self.input.match(self.block_comment_pattern)
  1540. comment = '/*' + comment_match.group(0)
  1541. directives = self.get_directives(comment)
  1542. if directives and directives.get('ignore') == 'start':
  1543. comment_match = self.input.match(self.directives_end_ignore_pattern)
  1544. comment += comment_match.group(0)
  1545. comment = re.sub(self.acorn.allLineBreaks, '\n', comment)
  1546. return comment, 'TK_BLOCK_COMMENT', directives
  1547. if self.input.peek() == '/': # peek // comment
  1548. self.input.next()
  1549. comment_match = self.input.match(self.comment_pattern)
  1550. comment = '//' + comment_match.group(0)
  1551. return comment, 'TK_COMMENT'
  1552. startXmlRegExp = re.compile('<()([-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*>')
  1553. self.has_char_escapes = False
  1554. if c == '`' or c == "'" or c == '"' or \
  1555. ( \
  1556. (c == '/') or \
  1557. (self.opts.e4x and c == "<" and self.input.test(startXmlRegExp, -1)) \
  1558. ) and ( \
  1559. (last_token.type == 'TK_RESERVED' and last_token.text in ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield']) or \
  1560. (last_token.type == 'TK_END_EXPR' and last_token.text == ')' and \
  1561. last_token.parent and last_token.parent.type == 'TK_RESERVED' and last_token.parent.text in ['if', 'while', 'for']) or \
  1562. (last_token.type in ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK', 'TK_END_BLOCK', 'TK_OPERATOR', \
  1563. 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA'])):
  1564. sep = c
  1565. esc = False
  1566. esc1 = 0
  1567. esc2 = 0
  1568. resulting_string = c
  1569. in_char_class = False
  1570. if sep == '/':
  1571. # handle regexp
  1572. in_char_class = False
  1573. while self.input.hasNext() and \
  1574. (esc or in_char_class or self.input.peek()!= sep) and \
  1575. not self.input.testChar(self.acorn.newline):
  1576. resulting_string += self.input.peek()
  1577. if not esc:
  1578. esc = self.input.peek() == '\\'
  1579. if self.input.peek() == '[':
  1580. in_char_class = True
  1581. elif self.input.peek() == ']':
  1582. in_char_class = False
  1583. else:
  1584. esc = False
  1585. self.input.next()
  1586. elif self.opts.e4x and sep == '<':
  1587. # handle e4x xml literals
  1588. xmlRegExp = re.compile('[\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*>')
  1589. self.input.back()
  1590. xmlStr = ""
  1591. match = self.input.match(xmlRegExp)
  1592. if match:
  1593. rootTag = match.group(2)
  1594. rootTag = re.sub(r'^{\s+', '{', re.sub(r'\s+}$', '}', rootTag))
  1595. isCurlyRoot = rootTag.startswith('{')
  1596. depth = 0
  1597. while (match):
  1598. isEndTag = match.group(1)
  1599. tagName = match.group(2)
  1600. isSingletonTag = (match.groups()[-1] != "") or (match.group(2)[0:8] == "![CDATA[")
  1601. if not isSingletonTag and (
  1602. tagName == rootTag or (isCurlyRoot and re.sub(r'^{\s+', '{', re.sub(r'\s+}$', '}', tagName)))):
  1603. if isEndTag:
  1604. depth -= 1
  1605. else:
  1606. depth += 1
  1607. xmlStr += match.group(0)
  1608. if depth <= 0:
  1609. break
  1610. match = self.input.match(xmlRegExp)
  1611. # if we didn't close correctly, keep unformatted.
  1612. if not match:
  1613. xmlStr += self.input.match(re.compile('[\s\S]*')).group(0)
  1614. xmlStr = re.sub(self.acorn.allLineBreaks, '\n', xmlStr)
  1615. return xmlStr, 'TK_STRING'
  1616. else:
  1617. # handle string
  1618. def parse_string(self, resulting_string, delimiter, allow_unescaped_newlines = False, start_sub = None):
  1619. esc = False
  1620. while self.input.hasNext():
  1621. current_char = self.input.peek()
  1622. if not (esc or (current_char != delimiter and
  1623. (allow_unescaped_newlines or not self.acorn.newline.match(current_char)))):
  1624. break
  1625. # Handle \r\n linebreaks after escapes or in template strings
  1626. if (esc or allow_unescaped_newlines) and self.acorn.newline.match(current_char):
  1627. if current_char == '\r' and self.input.peek(1) == '\n':
  1628. self.input.next()
  1629. current_char = self.input.peek()
  1630. resulting_string += '\n'
  1631. else:
  1632. resulting_string += current_char
  1633. if esc:
  1634. if current_char == 'x' or current_char == 'u':
  1635. self.has_char_escapes = True
  1636. esc = False
  1637. else:
  1638. esc = current_char == '\\'
  1639. self.input.next()
  1640. if start_sub and resulting_string.endswith(start_sub):
  1641. if delimiter == '`':
  1642. resulting_string = parse_string(self, resulting_string, '}', allow_unescaped_newlines, '`')
  1643. else:
  1644. resulting_string = parse_string(self, resulting_string, '`', allow_unescaped_newlines, '${')
  1645. if self.input.hasNext():
  1646. resulting_string += self.input.next()
  1647. return resulting_string
  1648. if sep == '`':
  1649. resulting_string = parse_string(self, resulting_string, '`', True, '${')
  1650. else:
  1651. resulting_string = parse_string(self, resulting_string, sep)
  1652. if self.has_char_escapes and self.opts.unescape_strings:
  1653. resulting_string = self.unescape_string(resulting_string)
  1654. if self.input.peek() == sep:
  1655. resulting_string += self.input.next()
  1656. if sep == '/':
  1657. # regexps may have modifiers /regexp/MOD, so fetch those too
  1658. # Only [gim] are valid, but if the user puts in garbage, do what we can to take it.
  1659. while self.input.hasNext() and self.acorn.isIdentifierStart(self.input.peekCharCode()):
  1660. resulting_string += self.input.next()
  1661. resulting_string = re.sub(self.acorn.allLineBreaks, '\n', resulting_string)
  1662. return resulting_string, 'TK_STRING'
  1663. if c == '#':
  1664. # she-bang
  1665. if len(self.tokens) == 0 and self.input.peek() == '!':
  1666. resulting_string = c
  1667. while self.input.hasNext() and c != '\n':
  1668. c = self.input.next()
  1669. resulting_string += c
  1670. return resulting_string.strip() + '\n', 'TK_UNKNOWN'
  1671. # Spidermonkey-specific sharp variables for circular references
  1672. # https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
  1673. # http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
  1674. sharp = '#'
  1675. if self.input.hasNext() and self.input.testChar(self.digit):
  1676. while True:
  1677. c = self.input.next()
  1678. sharp += c
  1679. if (not self.input.hasNext()) or c == '#' or c == '=':
  1680. break
  1681. if c == '#':
  1682. pass
  1683. elif self.input.peek() == '[' and self.input.peek(1) == ']':
  1684. sharp += '[]'
  1685. self.input.next()
  1686. self.input.next()
  1687. elif self.input.peek() == '{' and self.input.peek(1) == '}':
  1688. sharp += '{}'
  1689. self.input.next()
  1690. self.input.next()
  1691. return sharp, 'TK_WORD'
  1692. if c == '<' and self.input.peek() in ['?', '%']:
  1693. self.input.back()
  1694. template_match = self.input.match(self.template_pattern)
  1695. if template_match:
  1696. c = template_match.group(0)
  1697. c = re.sub(self.acorn.allLineBreaks, '\n', c)
  1698. return c, 'TK_STRING'
  1699. if c == '<' and self.input.match(re.compile('\!--')):
  1700. c = '<!--'
  1701. while self.input.hasNext() and not self.input.testChar(self.acorn.newline):
  1702. c += self.input.next()
  1703. self.in_html_comment = True
  1704. return c, 'TK_COMMENT'
  1705. if c == '-' and self.in_html_comment and self.input.match(re.compile('->')):
  1706. self.in_html_comment = False
  1707. return '-->', 'TK_COMMENT'
  1708. if c == '.':
  1709. if self.input.peek() == '.' and self.input.peek(1) == '.':
  1710. c += self.input.next() + self.input.next()
  1711. return c, 'TK_OPERATOR'
  1712. return c, 'TK_DOT'
  1713. if c in self.punct:
  1714. while self.input.hasNext() and c + self.input.peek() in self.punct:
  1715. c += self.input.next()
  1716. if not self.input.hasNext():
  1717. break
  1718. if c == ',':
  1719. return c, 'TK_COMMA'
  1720. if c == '=':
  1721. return c, 'TK_EQUALS'
  1722. return c, 'TK_OPERATOR'
  1723. return c, 'TK_UNKNOWN'
  1724. def unescape_string(self, s):
  1725. # You think that a regex would work for this
  1726. # return s.replace(/\\x([0-9a-f]{2})/gi, function(match, val) {
  1727. # return String.fromCharCode(parseInt(val, 16));
  1728. # })
  1729. # However, dealing with '\xff', '\\xff', '\\\xff' makes this more fun.
  1730. out = self.acorn.six.u('')
  1731. escaped = 0
  1732. input_scan = InputScanner(s)
  1733. matched = None
  1734. while input_scan.hasNext():
  1735. # Keep any whitespace, non-slash characters
  1736. # also keep slash pairs.
  1737. matched = input_scan.match(re.compile(r'([\s]|[^\\]|\\\\)+'))
  1738. if matched:
  1739. out += matched.group(0)
  1740. if input_scan.peek() != '\\':
  1741. continue
  1742. input_scan.next()
  1743. if input_scan.peek() == 'x':
  1744. matched = input_scan.match(re.compile('x([0-9A-Fa-f]{2})'))
  1745. elif input_scan.peek() == 'u':
  1746. matched = input_scan.match(re.compile('u([0-9A-Fa-f]{4})'));
  1747. else:
  1748. out += '\\'
  1749. if input_scan.hasNext():
  1750. out += input_scan.next()
  1751. continue
  1752. # If there's some error decoding, return the original string
  1753. if not matched:
  1754. return s
  1755. escaped = int(matched.group(1), 16)
  1756. if escaped > 0x7e and escaped <= 0xff and matched.group(0).startswith('x'):
  1757. # we bail out on \x7f..\xff,
  1758. # leaving whole string escaped,
  1759. # as it's probably completely binary
  1760. return s
  1761. elif escaped >= 0x00 and escaped < 0x20:
  1762. # leave 0x00...0x1f escaped
  1763. out += '\\' + matched.group(0)
  1764. continue
  1765. elif escaped == 0x22 or escaped == 0x27 or escaped == 0x5c:
  1766. # single-quote, apostrophe, backslash - escape these
  1767. out += ('\\' + chr(escaped))
  1768. else:
  1769. out += self.acorn.six.unichr(escaped)
  1770. return out
  1771. def isFileDifferent(filepath, expected):
  1772. try:
  1773. return (''.join(io.open(filepath, 'rt', newline='').readlines()) != expected)
  1774. except:
  1775. return True
  1776. def main():
  1777. argv = sys.argv[1:]
  1778. try:
  1779. opts, args = getopt.getopt(argv, "s:c:e:o:rdEPjabkil:xhtfvXnCO:w:",
  1780. ['indent-size=','indent-char=','eol=''outfile=', 'replace', 'disable-preserve-newlines',
  1781. 'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
  1782. 'brace-style=', 'keep-array-indentation', 'indent-level=', 'unescape-strings',
  1783. 'help', 'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation', 'version',
  1784. 'e4x', 'end-with-newline','comma-first','operator-position=','wrap-line-length','editorconfig'])
  1785. except getopt.GetoptError as ex:
  1786. print(ex, file=sys.stderr)
  1787. return usage(sys.stderr)
  1788. js_options = default_options()
  1789. file = None
  1790. outfile = 'stdout'
  1791. replace = False
  1792. if len(args) == 1:
  1793. file = args[0]
  1794. for opt, arg in opts:
  1795. if opt in ('--keep-array-indentation', '-k'):
  1796. js_options.keep_array_indentation = True
  1797. if opt in ('--keep-function-indentation','-f'):
  1798. js_options.keep_function_indentation = True
  1799. elif opt in ('--outfile', '-o'):
  1800. outfile = arg
  1801. elif opt in ('--replace', '-r'):
  1802. replace = True
  1803. elif opt in ('--indent-size', '-s'):
  1804. js_options.indent_size = int(arg)
  1805. elif opt in ('--indent-char', '-c'):
  1806. js_options.indent_char = arg
  1807. elif opt in ('--eol', '-e'):
  1808. js_options.eol = arg
  1809. elif opt in ('--indent-with-tabs', '-t'):
  1810. js_options.indent_with_tabs = True
  1811. elif opt in ('--disable-preserve-newlines', '-d'):
  1812. js_options.preserve_newlines = False
  1813. elif opt in ('--space-in-paren', '-P'):
  1814. js_options.space_in_paren = True
  1815. elif opt in ('--space-in-empty-paren', '-E'):
  1816. js_options.space_in_empty_paren = True
  1817. elif opt in ('--jslint-happy', '-j'):
  1818. js_options.jslint_happy = True
  1819. elif opt in ('--space_after_anon_function', '-a'):
  1820. js_options.space_after_anon_function = True
  1821. elif opt in ('--eval-code'):
  1822. js_options.eval_code = True
  1823. elif opt in ('--brace-style', '-b'):
  1824. js_options.brace_style = arg
  1825. elif opt in ('--unescape-strings', '-x'):
  1826. js_options.unescape_strings = True
  1827. elif opt in ('--e4x', '-X'):
  1828. js_options.e4x = True
  1829. elif opt in ('--end-with-newline', '-n'):
  1830. js_options.end_with_newline = True
  1831. elif opt in ('--comma-first', '-C'):
  1832. js_options.comma_first = True
  1833. elif opt in ('--operator-position', '-O'):
  1834. js_options.operator_position = sanitizeOperatorPosition(arg)
  1835. elif opt in ('--wrap-line-length ', '-w'):
  1836. js_options.wrap_line_length = int(arg)
  1837. elif opt in ('--stdin', '-i'):
  1838. file = '-'
  1839. elif opt in ('--editorconfig'):
  1840. js_options.editorconfig = True
  1841. elif opt in ('--version', '-v'):
  1842. return print(__version__)
  1843. elif opt in ('--help', '--usage', '-h'):
  1844. return usage()
  1845. if not file:
  1846. file = '-'
  1847. try:
  1848. if outfile == 'stdout' and replace and not file == '-':
  1849. outfile = file
  1850. # Editorconfig used only on files, not stdin
  1851. if getattr(js_options, 'editorconfig'):
  1852. editorconfig_filepath = file
  1853. if editorconfig_filepath == '-':
  1854. if outfile != 'stdout':
  1855. editorconfig_filepath = outfile
  1856. else:
  1857. fileType = 'js'
  1858. editorconfig_filepath = 'stdin.' + fileType
  1859. # debug("EditorConfig is enabled for ", editorconfig_filepath);
  1860. js_options = copy.copy(js_options)
  1861. set_file_editorconfig_opts(editorconfig_filepath, js_options)
  1862. pretty = beautify_file(file, js_options)
  1863. if outfile == 'stdout':
  1864. # python automatically converts newlines in text to "\r\n" when on windows
  1865. # switch to binary to prevent this
  1866. if sys.platform == "win32":
  1867. import msvcrt
  1868. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  1869. sys.stdout.write(pretty)
  1870. else:
  1871. if isFileDifferent(outfile, pretty):
  1872. mkdir_p(os.path.dirname(outfile))
  1873. # python automatically converts newlines in text to "\r\n" when on windows
  1874. # set newline to empty to prevent this
  1875. with io.open(outfile, 'wt', newline='') as f:
  1876. print('writing ' + outfile, file=sys.stderr)
  1877. try:
  1878. f.write(pretty)
  1879. except TypeError:
  1880. # This is not pretty, but given how we did the version import
  1881. # it is the only way to do this without having setup.py fail on a missing six dependency.
  1882. six = __import__("six")
  1883. f.write(six.u(pretty))
  1884. except Exception as ex:
  1885. print(ex, file=sys.stderr)
  1886. return 1
  1887. # Success
  1888. return 0