123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667 |
- /*
- Copyright (C) 2012-2014 Yusuke Suzuki <utatane.tea@gmail.com>
- Copyright (C) 2015 Ingvar Stepanyan <me@rreverser.com>
- Copyright (C) 2014 Ivan Nikulin <ifaaan@gmail.com>
- Copyright (C) 2012-2013 Michael Ficarra <escodegen.copyright@michael.ficarra.me>
- Copyright (C) 2012-2013 Mathias Bynens <mathias@qiwi.be>
- Copyright (C) 2013 Irakli Gozalishvili <rfobic@gmail.com>
- Copyright (C) 2012 Robert Gust-Bardon <donate@robert.gust-bardon.org>
- Copyright (C) 2012 John Freeman <jfreeman08@gmail.com>
- Copyright (C) 2011-2012 Ariya Hidayat <ariya.hidayat@gmail.com>
- Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
- Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
- Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
- Copyright (C) 2020 Apple Inc. All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- /*global exports:true, require:true, global:true*/
- (function () {
- 'use strict';
- var Syntax,
- Precedence,
- BinaryPrecedence,
- SourceNode,
- estraverse,
- esutils,
- base,
- indent,
- json,
- renumber,
- hexadecimal,
- quotes,
- escapeless,
- newline,
- space,
- parentheses,
- semicolons,
- safeConcatenation,
- directive,
- extra,
- parse,
- sourceMap,
- sourceCode,
- preserveBlankLines,
- FORMAT_MINIFY,
- FORMAT_DEFAULTS;
- estraverse = require('estraverse');
- esutils = require('esutils');
- Syntax = estraverse.Syntax;
- // Generation is done by generateExpression.
- function isExpression(node) {
- return CodeGenerator.Expression.hasOwnProperty(node.type);
- }
- // Generation is done by generateStatement.
- function isStatement(node) {
- return CodeGenerator.Statement.hasOwnProperty(node.type);
- }
- Precedence = {
- Sequence: 0,
- Yield: 1,
- Assignment: 1,
- Conditional: 2,
- ArrowFunction: 2,
- Coalesce: 3,
- LogicalOR: 4,
- LogicalAND: 5,
- BitwiseOR: 6,
- BitwiseXOR: 7,
- BitwiseAND: 8,
- Equality: 9,
- Relational: 10,
- BitwiseSHIFT: 11,
- Additive: 12,
- Multiplicative: 13,
- Exponentiation: 14,
- Await: 15,
- Unary: 15,
- Postfix: 16,
- OptionalChaining: 17,
- Call: 18,
- New: 19,
- TaggedTemplate: 20,
- Member: 21,
- Primary: 22
- };
- BinaryPrecedence = {
- '??': Precedence.Coalesce,
- '||': Precedence.LogicalOR,
- '&&': Precedence.LogicalAND,
- '|': Precedence.BitwiseOR,
- '^': Precedence.BitwiseXOR,
- '&': Precedence.BitwiseAND,
- '==': Precedence.Equality,
- '!=': Precedence.Equality,
- '===': Precedence.Equality,
- '!==': Precedence.Equality,
- 'is': Precedence.Equality,
- 'isnt': Precedence.Equality,
- '<': Precedence.Relational,
- '>': Precedence.Relational,
- '<=': Precedence.Relational,
- '>=': Precedence.Relational,
- 'in': Precedence.Relational,
- 'instanceof': Precedence.Relational,
- '<<': Precedence.BitwiseSHIFT,
- '>>': Precedence.BitwiseSHIFT,
- '>>>': Precedence.BitwiseSHIFT,
- '+': Precedence.Additive,
- '-': Precedence.Additive,
- '*': Precedence.Multiplicative,
- '%': Precedence.Multiplicative,
- '/': Precedence.Multiplicative,
- '**': Precedence.Exponentiation
- };
- //Flags
- var F_ALLOW_IN = 1,
- F_ALLOW_CALL = 1 << 1,
- F_ALLOW_UNPARATH_NEW = 1 << 2,
- F_FUNC_BODY = 1 << 3,
- F_DIRECTIVE_CTX = 1 << 4,
- F_SEMICOLON_OPT = 1 << 5,
- F_FOUND_COALESCE = 1 << 6;
- //Expression flag sets
- //NOTE: Flag order:
- // F_ALLOW_IN
- // F_ALLOW_CALL
- // F_ALLOW_UNPARATH_NEW
- var E_FTT = F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW,
- E_TTF = F_ALLOW_IN | F_ALLOW_CALL,
- E_TTT = F_ALLOW_IN | F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW,
- E_TFF = F_ALLOW_IN,
- E_FFT = F_ALLOW_UNPARATH_NEW,
- E_TFT = F_ALLOW_IN | F_ALLOW_UNPARATH_NEW;
- //Statement flag sets
- //NOTE: Flag order:
- // F_ALLOW_IN
- // F_FUNC_BODY
- // F_DIRECTIVE_CTX
- // F_SEMICOLON_OPT
- var S_TFFF = F_ALLOW_IN,
- S_TFFT = F_ALLOW_IN | F_SEMICOLON_OPT,
- S_FFFF = 0x00,
- S_TFTF = F_ALLOW_IN | F_DIRECTIVE_CTX,
- S_TTFF = F_ALLOW_IN | F_FUNC_BODY;
- function getDefaultOptions() {
- // default options
- return {
- indent: null,
- base: null,
- parse: null,
- comment: false,
- format: {
- indent: {
- style: ' ',
- base: 0,
- adjustMultilineComment: false
- },
- newline: '\n',
- space: ' ',
- json: false,
- renumber: false,
- hexadecimal: false,
- quotes: 'single',
- escapeless: false,
- compact: false,
- parentheses: true,
- semicolons: true,
- safeConcatenation: false,
- preserveBlankLines: false
- },
- moz: {
- comprehensionExpressionStartsWithAssignment: false,
- starlessGenerator: false
- },
- sourceMap: null,
- sourceMapRoot: null,
- sourceMapWithCode: false,
- directive: false,
- raw: true,
- verbatim: null,
- sourceCode: null
- };
- }
- function stringRepeat(str, num) {
- var result = '';
- for (num |= 0; num > 0; num >>>= 1, str += str) {
- if (num & 1) {
- result += str;
- }
- }
- return result;
- }
- function hasLineTerminator(str) {
- return (/[\r\n]/g).test(str);
- }
- function endsWithLineTerminator(str) {
- var len = str.length;
- return len && esutils.code.isLineTerminator(str.charCodeAt(len - 1));
- }
- function merge(target, override) {
- var key;
- for (key in override) {
- if (override.hasOwnProperty(key)) {
- target[key] = override[key];
- }
- }
- return target;
- }
- function updateDeeply(target, override) {
- var key, val;
- function isHashObject(target) {
- return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp);
- }
- for (key in override) {
- if (override.hasOwnProperty(key)) {
- val = override[key];
- if (isHashObject(val)) {
- if (isHashObject(target[key])) {
- updateDeeply(target[key], val);
- } else {
- target[key] = updateDeeply({}, val);
- }
- } else {
- target[key] = val;
- }
- }
- }
- return target;
- }
- function generateNumber(value) {
- var result, point, temp, exponent, pos;
- if (value !== value) {
- throw new Error('Numeric literal whose value is NaN');
- }
- if (value < 0 || (value === 0 && 1 / value < 0)) {
- throw new Error('Numeric literal whose value is negative');
- }
- if (value === 1 / 0) {
- return json ? 'null' : renumber ? '1e400' : '1e+400';
- }
- result = '' + value;
- if (!renumber || result.length < 3) {
- return result;
- }
- point = result.indexOf('.');
- if (!json && result.charCodeAt(0) === 0x30 /* 0 */ && point === 1) {
- point = 0;
- result = result.slice(1);
- }
- temp = result;
- result = result.replace('e+', 'e');
- exponent = 0;
- if ((pos = temp.indexOf('e')) > 0) {
- exponent = +temp.slice(pos + 1);
- temp = temp.slice(0, pos);
- }
- if (point >= 0) {
- exponent -= temp.length - point - 1;
- temp = +(temp.slice(0, point) + temp.slice(point + 1)) + '';
- }
- pos = 0;
- while (temp.charCodeAt(temp.length + pos - 1) === 0x30 /* 0 */) {
- --pos;
- }
- if (pos !== 0) {
- exponent -= pos;
- temp = temp.slice(0, pos);
- }
- if (exponent !== 0) {
- temp += 'e' + exponent;
- }
- if ((temp.length < result.length ||
- (hexadecimal && value > 1e12 && Math.floor(value) === value && (temp = '0x' + value.toString(16)).length < result.length)) &&
- +temp === value) {
- result = temp;
- }
- return result;
- }
- // Generate valid RegExp expression.
- // This function is based on https://github.com/Constellation/iv Engine
- function escapeRegExpCharacter(ch, previousIsBackslash) {
- // not handling '\' and handling \u2028 or \u2029 to unicode escape sequence
- if ((ch & ~1) === 0x2028) {
- return (previousIsBackslash ? 'u' : '\\u') + ((ch === 0x2028) ? '2028' : '2029');
- } else if (ch === 10 || ch === 13) { // \n, \r
- return (previousIsBackslash ? '' : '\\') + ((ch === 10) ? 'n' : 'r');
- }
- return String.fromCharCode(ch);
- }
- function generateRegExp(reg) {
- var match, result, flags, i, iz, ch, characterInBrack, previousIsBackslash;
- result = reg.toString();
- if (reg.source) {
- // extract flag from toString result
- match = result.match(/\/([^/]*)$/);
- if (!match) {
- return result;
- }
- flags = match[1];
- result = '';
- characterInBrack = false;
- previousIsBackslash = false;
- for (i = 0, iz = reg.source.length; i < iz; ++i) {
- ch = reg.source.charCodeAt(i);
- if (!previousIsBackslash) {
- if (characterInBrack) {
- if (ch === 93) { // ]
- characterInBrack = false;
- }
- } else {
- if (ch === 47) { // /
- result += '\\';
- } else if (ch === 91) { // [
- characterInBrack = true;
- }
- }
- result += escapeRegExpCharacter(ch, previousIsBackslash);
- previousIsBackslash = ch === 92; // \
- } else {
- // if new RegExp("\\\n') is provided, create /\n/
- result += escapeRegExpCharacter(ch, previousIsBackslash);
- // prevent like /\\[/]/
- previousIsBackslash = false;
- }
- }
- return '/' + result + '/' + flags;
- }
- return result;
- }
- function escapeAllowedCharacter(code, next) {
- var hex;
- if (code === 0x08 /* \b */) {
- return '\\b';
- }
- if (code === 0x0C /* \f */) {
- return '\\f';
- }
- if (code === 0x09 /* \t */) {
- return '\\t';
- }
- hex = code.toString(16).toUpperCase();
- if (json || code > 0xFF) {
- return '\\u' + '0000'.slice(hex.length) + hex;
- } else if (code === 0x0000 && !esutils.code.isDecimalDigit(next)) {
- return '\\0';
- } else if (code === 0x000B /* \v */) { // '\v'
- return '\\x0B';
- } else {
- return '\\x' + '00'.slice(hex.length) + hex;
- }
- }
- function escapeDisallowedCharacter(code) {
- if (code === 0x5C /* \ */) {
- return '\\\\';
- }
- if (code === 0x0A /* \n */) {
- return '\\n';
- }
- if (code === 0x0D /* \r */) {
- return '\\r';
- }
- if (code === 0x2028) {
- return '\\u2028';
- }
- if (code === 0x2029) {
- return '\\u2029';
- }
- throw new Error('Incorrectly classified character');
- }
- function escapeDirective(str) {
- var i, iz, code, quote;
- quote = quotes === 'double' ? '"' : '\'';
- for (i = 0, iz = str.length; i < iz; ++i) {
- code = str.charCodeAt(i);
- if (code === 0x27 /* ' */) {
- quote = '"';
- break;
- } else if (code === 0x22 /* " */) {
- quote = '\'';
- break;
- } else if (code === 0x5C /* \ */) {
- ++i;
- }
- }
- return quote + str + quote;
- }
- function escapeString(str) {
- var result = '', i, len, code, singleQuotes = 0, doubleQuotes = 0, single, quote;
- for (i = 0, len = str.length; i < len; ++i) {
- code = str.charCodeAt(i);
- if (code === 0x27 /* ' */) {
- ++singleQuotes;
- } else if (code === 0x22 /* " */) {
- ++doubleQuotes;
- } else if (code === 0x2F /* / */ && json) {
- result += '\\';
- } else if (esutils.code.isLineTerminator(code) || code === 0x5C /* \ */) {
- result += escapeDisallowedCharacter(code);
- continue;
- } else if (!esutils.code.isIdentifierPartES5(code) && (json && code < 0x20 /* SP */ || !json && !escapeless && (code < 0x20 /* SP */ || code > 0x7E /* ~ */))) {
- result += escapeAllowedCharacter(code, str.charCodeAt(i + 1));
- continue;
- }
- result += String.fromCharCode(code);
- }
- single = !(quotes === 'double' || (quotes === 'auto' && doubleQuotes < singleQuotes));
- quote = single ? '\'' : '"';
- if (!(single ? singleQuotes : doubleQuotes)) {
- return quote + result + quote;
- }
- str = result;
- result = quote;
- for (i = 0, len = str.length; i < len; ++i) {
- code = str.charCodeAt(i);
- if ((code === 0x27 /* ' */ && single) || (code === 0x22 /* " */ && !single)) {
- result += '\\';
- }
- result += String.fromCharCode(code);
- }
- return result + quote;
- }
- /**
- * flatten an array to a string, where the array can contain
- * either strings or nested arrays
- */
- function flattenToString(arr) {
- var i, iz, elem, result = '';
- for (i = 0, iz = arr.length; i < iz; ++i) {
- elem = arr[i];
- result += Array.isArray(elem) ? flattenToString(elem) : elem;
- }
- return result;
- }
- /**
- * convert generated to a SourceNode when source maps are enabled.
- */
- function toSourceNodeWhenNeeded(generated, node) {
- if (!sourceMap) {
- // with no source maps, generated is either an
- // array or a string. if an array, flatten it.
- // if a string, just return it
- if (Array.isArray(generated)) {
- return flattenToString(generated);
- } else {
- return generated;
- }
- }
- if (node == null) {
- if (generated instanceof SourceNode) {
- return generated;
- } else {
- node = {};
- }
- }
- if (node.loc == null) {
- return new SourceNode(null, null, sourceMap, generated, node.name || null);
- }
- return new SourceNode(node.loc.start.line, node.loc.start.column, (sourceMap === true ? node.loc.source || null : sourceMap), generated, node.name || null);
- }
- function noEmptySpace() {
- return (space) ? space : ' ';
- }
- function join(left, right) {
- var leftSource,
- rightSource,
- leftCharCode,
- rightCharCode;
- leftSource = toSourceNodeWhenNeeded(left).toString();
- if (leftSource.length === 0) {
- return [right];
- }
- rightSource = toSourceNodeWhenNeeded(right).toString();
- if (rightSource.length === 0) {
- return [left];
- }
- leftCharCode = leftSource.charCodeAt(leftSource.length - 1);
- rightCharCode = rightSource.charCodeAt(0);
- if ((leftCharCode === 0x2B /* + */ || leftCharCode === 0x2D /* - */) && leftCharCode === rightCharCode ||
- esutils.code.isIdentifierPartES5(leftCharCode) && esutils.code.isIdentifierPartES5(rightCharCode) ||
- leftCharCode === 0x2F /* / */ && rightCharCode === 0x69 /* i */) { // infix word operators all start with `i`
- return [left, noEmptySpace(), right];
- } else if (esutils.code.isWhiteSpace(leftCharCode) || esutils.code.isLineTerminator(leftCharCode) ||
- esutils.code.isWhiteSpace(rightCharCode) || esutils.code.isLineTerminator(rightCharCode)) {
- return [left, right];
- }
- return [left, space, right];
- }
- function addIndent(stmt) {
- return [base, stmt];
- }
- function withIndent(fn) {
- var previousBase;
- previousBase = base;
- base += indent;
- fn(base);
- base = previousBase;
- }
- function calculateSpaces(str) {
- var i;
- for (i = str.length - 1; i >= 0; --i) {
- if (esutils.code.isLineTerminator(str.charCodeAt(i))) {
- break;
- }
- }
- return (str.length - 1) - i;
- }
- function adjustMultilineComment(value, specialBase) {
- var array, i, len, line, j, spaces, previousBase, sn;
- array = value.split(/\r\n|[\r\n]/);
- spaces = Number.MAX_VALUE;
- // first line doesn't have indentation
- for (i = 1, len = array.length; i < len; ++i) {
- line = array[i];
- j = 0;
- while (j < line.length && esutils.code.isWhiteSpace(line.charCodeAt(j))) {
- ++j;
- }
- if (spaces > j) {
- spaces = j;
- }
- }
- if (typeof specialBase !== 'undefined') {
- // pattern like
- // {
- // var t = 20; /*
- // * this is comment
- // */
- // }
- previousBase = base;
- if (array[1][spaces] === '*') {
- specialBase += ' ';
- }
- base = specialBase;
- } else {
- if (spaces & 1) {
- // /*
- // *
- // */
- // If spaces are odd number, above pattern is considered.
- // We waste 1 space.
- --spaces;
- }
- previousBase = base;
- }
- for (i = 1, len = array.length; i < len; ++i) {
- sn = toSourceNodeWhenNeeded(addIndent(array[i].slice(spaces)));
- array[i] = sourceMap ? sn.join('') : sn;
- }
- base = previousBase;
- return array.join('\n');
- }
- function generateComment(comment, specialBase) {
- if (comment.type === 'Line') {
- if (endsWithLineTerminator(comment.value)) {
- return '//' + comment.value;
- } else {
- // Always use LineTerminator
- var result = '//' + comment.value;
- if (!preserveBlankLines) {
- result += '\n';
- }
- return result;
- }
- }
- if (extra.format.indent.adjustMultilineComment && /[\n\r]/.test(comment.value)) {
- return adjustMultilineComment('/*' + comment.value + '*/', specialBase);
- }
- return '/*' + comment.value + '*/';
- }
- function addComments(stmt, result) {
- var i, len, comment, save, tailingToStatement, specialBase, fragment,
- extRange, range, prevRange, prefix, infix, suffix, count;
- if (stmt.leadingComments && stmt.leadingComments.length > 0) {
- save = result;
- if (preserveBlankLines) {
- comment = stmt.leadingComments[0];
- result = [];
- extRange = comment.extendedRange;
- range = comment.range;
- prefix = sourceCode.substring(extRange[0], range[0]);
- count = (prefix.match(/\n/g) || []).length;
- if (count > 0) {
- result.push(stringRepeat('\n', count));
- result.push(addIndent(generateComment(comment)));
- } else {
- result.push(prefix);
- result.push(generateComment(comment));
- }
- prevRange = range;
- for (i = 1, len = stmt.leadingComments.length; i < len; i++) {
- comment = stmt.leadingComments[i];
- range = comment.range;
- infix = sourceCode.substring(prevRange[1], range[0]);
- count = (infix.match(/\n/g) || []).length;
- result.push(stringRepeat('\n', count));
- result.push(addIndent(generateComment(comment)));
- prevRange = range;
- }
- suffix = sourceCode.substring(range[1], extRange[1]);
- count = (suffix.match(/\n/g) || []).length;
- result.push(stringRepeat('\n', count));
- } else {
- comment = stmt.leadingComments[0];
- result = [];
- if (safeConcatenation && stmt.type === Syntax.Program && stmt.body.length === 0) {
- result.push('\n');
- }
- result.push(generateComment(comment));
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push('\n');
- }
- for (i = 1, len = stmt.leadingComments.length; i < len; ++i) {
- comment = stmt.leadingComments[i];
- fragment = [generateComment(comment)];
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- fragment.push('\n');
- }
- result.push(addIndent(fragment));
- }
- }
- result.push(addIndent(save));
- }
- if (stmt.trailingComments) {
- if (preserveBlankLines) {
- comment = stmt.trailingComments[0];
- extRange = comment.extendedRange;
- range = comment.range;
- prefix = sourceCode.substring(extRange[0], range[0]);
- count = (prefix.match(/\n/g) || []).length;
- if (count > 0) {
- result.push(stringRepeat('\n', count));
- result.push(addIndent(generateComment(comment)));
- } else {
- result.push(prefix);
- result.push(generateComment(comment));
- }
- } else {
- tailingToStatement = !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString());
- specialBase = stringRepeat(' ', calculateSpaces(toSourceNodeWhenNeeded([base, result, indent]).toString()));
- for (i = 0, len = stmt.trailingComments.length; i < len; ++i) {
- comment = stmt.trailingComments[i];
- if (tailingToStatement) {
- // We assume target like following script
- //
- // var t = 20; /**
- // * This is comment of t
- // */
- if (i === 0) {
- // first case
- result = [result, indent];
- } else {
- result = [result, specialBase];
- }
- result.push(generateComment(comment, specialBase));
- } else {
- result = [result, addIndent(generateComment(comment))];
- }
- if (i !== len - 1 && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result = [result, '\n'];
- }
- }
- }
- }
- return result;
- }
- function generateBlankLines(start, end, result) {
- var j, newlineCount = 0;
- for (j = start; j < end; j++) {
- if (sourceCode[j] === '\n') {
- newlineCount++;
- }
- }
- for (j = 1; j < newlineCount; j++) {
- result.push(newline);
- }
- }
- function parenthesize(text, current, should) {
- if (current < should) {
- return ['(', text, ')'];
- }
- return text;
- }
- function generateVerbatimString(string) {
- var i, iz, result;
- result = string.split(/\r\n|\n/);
- for (i = 1, iz = result.length; i < iz; i++) {
- result[i] = newline + base + result[i];
- }
- return result;
- }
- function generateVerbatim(expr, precedence) {
- var verbatim, result, prec;
- verbatim = expr[extra.verbatim];
- if (typeof verbatim === 'string') {
- result = parenthesize(generateVerbatimString(verbatim), Precedence.Sequence, precedence);
- } else {
- // verbatim is object
- result = generateVerbatimString(verbatim.content);
- prec = (verbatim.precedence != null) ? verbatim.precedence : Precedence.Sequence;
- result = parenthesize(result, prec, precedence);
- }
- return toSourceNodeWhenNeeded(result, expr);
- }
- function CodeGenerator() {
- }
- // Helpers.
- CodeGenerator.prototype.maybeBlock = function(stmt, flags) {
- var result, noLeadingComment, that = this;
- noLeadingComment = !extra.comment || !stmt.leadingComments;
- if (stmt.type === Syntax.BlockStatement && noLeadingComment) {
- return [space, this.generateStatement(stmt, flags)];
- }
- if (stmt.type === Syntax.EmptyStatement && noLeadingComment) {
- return ';';
- }
- withIndent(function () {
- result = [
- newline,
- addIndent(that.generateStatement(stmt, flags))
- ];
- });
- return result;
- };
- CodeGenerator.prototype.maybeBlockSuffix = function (stmt, result) {
- var ends = endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString());
- if (stmt.type === Syntax.BlockStatement && (!extra.comment || !stmt.leadingComments) && !ends) {
- return [result, space];
- }
- if (ends) {
- return [result, base];
- }
- return [result, newline, base];
- };
- function generateIdentifier(node) {
- return toSourceNodeWhenNeeded(node.name, node);
- }
- function generateAsyncPrefix(node, spaceRequired) {
- return node.async ? 'async' + (spaceRequired ? noEmptySpace() : space) : '';
- }
- function generateStarSuffix(node) {
- var isGenerator = node.generator && !extra.moz.starlessGenerator;
- return isGenerator ? '*' + space : '';
- }
- function generateMethodPrefix(prop) {
- var func = prop.value, prefix = '';
- if (func.async) {
- prefix += generateAsyncPrefix(func, !prop.computed);
- }
- if (func.generator) {
- // avoid space before method name
- prefix += generateStarSuffix(func) ? '*' : '';
- }
- return prefix;
- }
- CodeGenerator.prototype.generatePattern = function (node, precedence, flags) {
- if (node.type === Syntax.Identifier) {
- return generateIdentifier(node);
- }
- return this.generateExpression(node, precedence, flags);
- };
- CodeGenerator.prototype.generateFunctionParams = function (node) {
- var i, iz, result, hasDefault;
- hasDefault = false;
- if (node.type === Syntax.ArrowFunctionExpression &&
- !node.rest && (!node.defaults || node.defaults.length === 0) &&
- node.params.length === 1 && node.params[0].type === Syntax.Identifier) {
- // arg => { } case
- result = [generateAsyncPrefix(node, true), generateIdentifier(node.params[0])];
- } else {
- result = node.type === Syntax.ArrowFunctionExpression ? [generateAsyncPrefix(node, false)] : [];
- result.push('(');
- if (node.defaults) {
- hasDefault = true;
- }
- for (i = 0, iz = node.params.length; i < iz; ++i) {
- if (hasDefault && node.defaults[i]) {
- // Handle default values.
- result.push(this.generateAssignment(node.params[i], node.defaults[i], '=', Precedence.Assignment, E_TTT));
- } else {
- result.push(this.generatePattern(node.params[i], Precedence.Assignment, E_TTT));
- }
- if (i + 1 < iz) {
- result.push(',' + space);
- }
- }
- if (node.rest) {
- if (node.params.length) {
- result.push(',' + space);
- }
- result.push('...');
- result.push(generateIdentifier(node.rest));
- }
- result.push(')');
- }
- return result;
- };
- CodeGenerator.prototype.generateFunctionBody = function (node) {
- var result, expr;
- result = this.generateFunctionParams(node);
- if (node.type === Syntax.ArrowFunctionExpression) {
- result.push(space);
- result.push('=>');
- }
- if (node.expression) {
- result.push(space);
- expr = this.generateExpression(node.body, Precedence.Assignment, E_TTT);
- if (expr.toString().charAt(0) === '{') {
- expr = ['(', expr, ')'];
- }
- result.push(expr);
- } else {
- result.push(this.maybeBlock(node.body, S_TTFF));
- }
- return result;
- };
- CodeGenerator.prototype.generateIterationForStatement = function (operator, stmt, flags) {
- var result = ['for' + (stmt.await ? noEmptySpace() + 'await' : '') + space + '('], that = this;
- withIndent(function () {
- if (stmt.left.type === Syntax.VariableDeclaration) {
- withIndent(function () {
- result.push(stmt.left.kind + noEmptySpace());
- result.push(that.generateStatement(stmt.left.declarations[0], S_FFFF));
- });
- } else {
- result.push(that.generateExpression(stmt.left, Precedence.Call, E_TTT));
- }
- result = join(result, operator);
- result = [join(
- result,
- that.generateExpression(stmt.right, Precedence.Assignment, E_TTT)
- ), ')'];
- });
- result.push(this.maybeBlock(stmt.body, flags));
- return result;
- };
- CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
- var result = [];
- if (computed) {
- result.push('[');
- }
- result.push(this.generateExpression(expr, Precedence.Assignment, E_TTT));
- if (computed) {
- result.push(']');
- }
- return result;
- };
- CodeGenerator.prototype.generateAssignment = function (left, right, operator, precedence, flags) {
- if (Precedence.Assignment < precedence) {
- flags |= F_ALLOW_IN;
- }
- return parenthesize(
- [
- this.generateExpression(left, Precedence.Call, flags),
- space + operator + space,
- this.generateExpression(right, Precedence.Assignment, flags)
- ],
- Precedence.Assignment,
- precedence
- );
- };
- CodeGenerator.prototype.semicolon = function (flags) {
- if (!semicolons && flags & F_SEMICOLON_OPT) {
- return '';
- }
- return ';';
- };
- // Statements.
- CodeGenerator.Statement = {
- BlockStatement: function (stmt, flags) {
- var range, content, result = ['{', newline], that = this;
- withIndent(function () {
- // handle functions without any code
- if (stmt.body.length === 0 && preserveBlankLines) {
- range = stmt.range;
- if (range[1] - range[0] > 2) {
- content = sourceCode.substring(range[0] + 1, range[1] - 1);
- if (content[0] === '\n') {
- result = ['{'];
- }
- result.push(content);
- }
- }
- var i, iz, fragment, bodyFlags;
- bodyFlags = S_TFFF;
- if (flags & F_FUNC_BODY) {
- bodyFlags |= F_DIRECTIVE_CTX;
- }
- for (i = 0, iz = stmt.body.length; i < iz; ++i) {
- if (preserveBlankLines) {
- // handle spaces before the first line
- if (i === 0) {
- if (stmt.body[0].leadingComments) {
- range = stmt.body[0].leadingComments[0].extendedRange;
- content = sourceCode.substring(range[0], range[1]);
- if (content[0] === '\n') {
- result = ['{'];
- }
- }
- if (!stmt.body[0].leadingComments) {
- generateBlankLines(stmt.range[0], stmt.body[0].range[0], result);
- }
- }
- // handle spaces between lines
- if (i > 0) {
- if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) {
- generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result);
- }
- }
- }
- if (i === iz - 1) {
- bodyFlags |= F_SEMICOLON_OPT;
- }
- if (stmt.body[i].leadingComments && preserveBlankLines) {
- fragment = that.generateStatement(stmt.body[i], bodyFlags);
- } else {
- fragment = addIndent(that.generateStatement(stmt.body[i], bodyFlags));
- }
- result.push(fragment);
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- if (preserveBlankLines && i < iz - 1) {
- // don't add a new line if there are leading coments
- // in the next statement
- if (!stmt.body[i + 1].leadingComments) {
- result.push(newline);
- }
- } else {
- result.push(newline);
- }
- }
- if (preserveBlankLines) {
- // handle spaces after the last line
- if (i === iz - 1) {
- if (!stmt.body[i].trailingComments) {
- generateBlankLines(stmt.body[i].range[1], stmt.range[1], result);
- }
- }
- }
- }
- });
- result.push(addIndent('}'));
- return result;
- },
- BreakStatement: function (stmt, flags) {
- if (stmt.label) {
- return 'break ' + stmt.label.name + this.semicolon(flags);
- }
- return 'break' + this.semicolon(flags);
- },
- ContinueStatement: function (stmt, flags) {
- if (stmt.label) {
- return 'continue ' + stmt.label.name + this.semicolon(flags);
- }
- return 'continue' + this.semicolon(flags);
- },
- ClassBody: function (stmt, flags) {
- var result = [ '{', newline], that = this;
- withIndent(function (indent) {
- var i, iz;
- for (i = 0, iz = stmt.body.length; i < iz; ++i) {
- result.push(indent);
- result.push(that.generateExpression(stmt.body[i], Precedence.Sequence, E_TTT));
- if (i + 1 < iz) {
- result.push(newline);
- }
- }
- });
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(base);
- result.push('}');
- return result;
- },
- ClassDeclaration: function (stmt, flags) {
- var result, fragment;
- result = ['class'];
- if (stmt.id) {
- result = join(result, this.generateExpression(stmt.id, Precedence.Sequence, E_TTT));
- }
- if (stmt.superClass) {
- fragment = join('extends', this.generateExpression(stmt.superClass, Precedence.Unary, E_TTT));
- result = join(result, fragment);
- }
- result.push(space);
- result.push(this.generateStatement(stmt.body, S_TFFT));
- return result;
- },
- DirectiveStatement: function (stmt, flags) {
- if (extra.raw && stmt.raw) {
- return stmt.raw + this.semicolon(flags);
- }
- return escapeDirective(stmt.directive) + this.semicolon(flags);
- },
- DoWhileStatement: function (stmt, flags) {
- // Because `do 42 while (cond)` is Syntax Error. We need semicolon.
- var result = join('do', this.maybeBlock(stmt.body, S_TFFF));
- result = this.maybeBlockSuffix(stmt.body, result);
- return join(result, [
- 'while' + space + '(',
- this.generateExpression(stmt.test, Precedence.Sequence, E_TTT),
- ')' + this.semicolon(flags)
- ]);
- },
- CatchClause: function (stmt, flags) {
- var result, that = this;
- withIndent(function () {
- var guard;
- if (stmt.param) {
- result = [
- 'catch' + space + '(',
- that.generateExpression(stmt.param, Precedence.Sequence, E_TTT),
- ')'
- ];
- if (stmt.guard) {
- guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT);
- result.splice(2, 0, ' if ', guard);
- }
- } else {
- result = ['catch'];
- }
- });
- result.push(this.maybeBlock(stmt.body, S_TFFF));
- return result;
- },
- DebuggerStatement: function (stmt, flags) {
- return 'debugger' + this.semicolon(flags);
- },
- EmptyStatement: function (stmt, flags) {
- return ';';
- },
- ExportDefaultDeclaration: function (stmt, flags) {
- var result = [ 'export' ], bodyFlags;
- bodyFlags = (flags & F_SEMICOLON_OPT) ? S_TFFT : S_TFFF;
- // export default HoistableDeclaration[Default]
- // export default AssignmentExpression[In] ;
- result = join(result, 'default');
- if (isStatement(stmt.declaration)) {
- result = join(result, this.generateStatement(stmt.declaration, bodyFlags));
- } else {
- result = join(result, this.generateExpression(stmt.declaration, Precedence.Assignment, E_TTT) + this.semicolon(flags));
- }
- return result;
- },
- ExportNamedDeclaration: function (stmt, flags) {
- var result = [ 'export' ], bodyFlags, that = this;
- bodyFlags = (flags & F_SEMICOLON_OPT) ? S_TFFT : S_TFFF;
- // export VariableStatement
- // export Declaration[Default]
- if (stmt.declaration) {
- return join(result, this.generateStatement(stmt.declaration, bodyFlags));
- }
- // export ExportClause[NoReference] FromClause ;
- // export ExportClause ;
- if (stmt.specifiers) {
- if (stmt.specifiers.length === 0) {
- result = join(result, '{' + space + '}');
- } else if (stmt.specifiers[0].type === Syntax.ExportBatchSpecifier) {
- result = join(result, this.generateExpression(stmt.specifiers[0], Precedence.Sequence, E_TTT));
- } else {
- result = join(result, '{');
- withIndent(function (indent) {
- var i, iz;
- result.push(newline);
- for (i = 0, iz = stmt.specifiers.length; i < iz; ++i) {
- result.push(indent);
- result.push(that.generateExpression(stmt.specifiers[i], Precedence.Sequence, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + newline);
- }
- }
- });
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(base + '}');
- }
- if (stmt.source) {
- result = join(result, [
- 'from' + space,
- // ModuleSpecifier
- this.generateExpression(stmt.source, Precedence.Sequence, E_TTT),
- this.semicolon(flags)
- ]);
- } else {
- result.push(this.semicolon(flags));
- }
- }
- return result;
- },
- ExportAllDeclaration: function (stmt, flags) {
- // export * FromClause ;
- return [
- 'export' + space,
- '*' + space,
- 'from' + space,
- // ModuleSpecifier
- this.generateExpression(stmt.source, Precedence.Sequence, E_TTT),
- this.semicolon(flags)
- ];
- },
- ExpressionStatement: function (stmt, flags) {
- var result, fragment;
- function isClassPrefixed(fragment) {
- var code;
- if (fragment.slice(0, 5) !== 'class') {
- return false;
- }
- code = fragment.charCodeAt(5);
- return code === 0x7B /* '{' */ || esutils.code.isWhiteSpace(code) || esutils.code.isLineTerminator(code);
- }
- function isFunctionPrefixed(fragment) {
- var code;
- if (fragment.slice(0, 8) !== 'function') {
- return false;
- }
- code = fragment.charCodeAt(8);
- return code === 0x28 /* '(' */ || esutils.code.isWhiteSpace(code) || code === 0x2A /* '*' */ || esutils.code.isLineTerminator(code);
- }
- function isAsyncPrefixed(fragment) {
- var code, i, iz;
- if (fragment.slice(0, 5) !== 'async') {
- return false;
- }
- if (!esutils.code.isWhiteSpace(fragment.charCodeAt(5))) {
- return false;
- }
- for (i = 6, iz = fragment.length; i < iz; ++i) {
- if (!esutils.code.isWhiteSpace(fragment.charCodeAt(i))) {
- break;
- }
- }
- if (i === iz) {
- return false;
- }
- if (fragment.slice(i, i + 8) !== 'function') {
- return false;
- }
- code = fragment.charCodeAt(i + 8);
- return code === 0x28 /* '(' */ || esutils.code.isWhiteSpace(code) || code === 0x2A /* '*' */ || esutils.code.isLineTerminator(code);
- }
- result = [this.generateExpression(stmt.expression, Precedence.Sequence, E_TTT)];
- // 12.4 '{', 'function', 'class' is not allowed in this position.
- // wrap expression with parentheses
- fragment = toSourceNodeWhenNeeded(result).toString();
- if (fragment.charCodeAt(0) === 0x7B /* '{' */ || // ObjectExpression
- isClassPrefixed(fragment) ||
- isFunctionPrefixed(fragment) ||
- isAsyncPrefixed(fragment) ||
- (directive && (flags & F_DIRECTIVE_CTX) && stmt.expression.type === Syntax.Literal && typeof stmt.expression.value === 'string')) {
- result = ['(', result, ')' + this.semicolon(flags)];
- } else {
- result.push(this.semicolon(flags));
- }
- return result;
- },
- ImportDeclaration: function (stmt, flags) {
- // ES6: 15.2.1 valid import declarations:
- // - import ImportClause FromClause ;
- // - import ModuleSpecifier ;
- var result, cursor, that = this;
- // If no ImportClause is present,
- // this should be `import ModuleSpecifier` so skip `from`
- // ModuleSpecifier is StringLiteral.
- if (stmt.specifiers.length === 0) {
- // import ModuleSpecifier ;
- return [
- 'import',
- space,
- // ModuleSpecifier
- this.generateExpression(stmt.source, Precedence.Sequence, E_TTT),
- this.semicolon(flags)
- ];
- }
- // import ImportClause FromClause ;
- result = [
- 'import'
- ];
- cursor = 0;
- // ImportedBinding
- if (stmt.specifiers[cursor].type === Syntax.ImportDefaultSpecifier) {
- result = join(result, [
- this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT)
- ]);
- ++cursor;
- }
- if (stmt.specifiers[cursor]) {
- if (cursor !== 0) {
- result.push(',');
- }
- if (stmt.specifiers[cursor].type === Syntax.ImportNamespaceSpecifier) {
- // NameSpaceImport
- result = join(result, [
- space,
- this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT)
- ]);
- } else {
- // NamedImports
- result.push(space + '{');
- if ((stmt.specifiers.length - cursor) === 1) {
- // import { ... } from "...";
- result.push(space);
- result.push(this.generateExpression(stmt.specifiers[cursor], Precedence.Sequence, E_TTT));
- result.push(space + '}' + space);
- } else {
- // import {
- // ...,
- // ...,
- // } from "...";
- withIndent(function (indent) {
- var i, iz;
- result.push(newline);
- for (i = cursor, iz = stmt.specifiers.length; i < iz; ++i) {
- result.push(indent);
- result.push(that.generateExpression(stmt.specifiers[i], Precedence.Sequence, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + newline);
- }
- }
- });
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(base + '}' + space);
- }
- }
- }
- result = join(result, [
- 'from' + space,
- // ModuleSpecifier
- this.generateExpression(stmt.source, Precedence.Sequence, E_TTT),
- this.semicolon(flags)
- ]);
- return result;
- },
- VariableDeclarator: function (stmt, flags) {
- var itemFlags = (flags & F_ALLOW_IN) ? E_TTT : E_FTT;
- if (stmt.init) {
- return [
- this.generateExpression(stmt.id, Precedence.Assignment, itemFlags),
- space,
- '=',
- space,
- this.generateExpression(stmt.init, Precedence.Assignment, itemFlags)
- ];
- }
- return this.generatePattern(stmt.id, Precedence.Assignment, itemFlags);
- },
- VariableDeclaration: function (stmt, flags) {
- // VariableDeclarator is typed as Statement,
- // but joined with comma (not LineTerminator).
- // So if comment is attached to target node, we should specialize.
- var result, i, iz, node, bodyFlags, that = this;
- result = [ stmt.kind ];
- bodyFlags = (flags & F_ALLOW_IN) ? S_TFFF : S_FFFF;
- function block() {
- node = stmt.declarations[0];
- if (extra.comment && node.leadingComments) {
- result.push('\n');
- result.push(addIndent(that.generateStatement(node, bodyFlags)));
- } else {
- result.push(noEmptySpace());
- result.push(that.generateStatement(node, bodyFlags));
- }
- for (i = 1, iz = stmt.declarations.length; i < iz; ++i) {
- node = stmt.declarations[i];
- if (extra.comment && node.leadingComments) {
- result.push(',' + newline);
- result.push(addIndent(that.generateStatement(node, bodyFlags)));
- } else {
- result.push(',' + space);
- result.push(that.generateStatement(node, bodyFlags));
- }
- }
- }
- if (stmt.declarations.length > 1) {
- withIndent(block);
- } else {
- block();
- }
- result.push(this.semicolon(flags));
- return result;
- },
- ThrowStatement: function (stmt, flags) {
- return [join(
- 'throw',
- this.generateExpression(stmt.argument, Precedence.Sequence, E_TTT)
- ), this.semicolon(flags)];
- },
- TryStatement: function (stmt, flags) {
- var result, i, iz, guardedHandlers;
- result = ['try', this.maybeBlock(stmt.block, S_TFFF)];
- result = this.maybeBlockSuffix(stmt.block, result);
- if (stmt.handlers) {
- // old interface
- for (i = 0, iz = stmt.handlers.length; i < iz; ++i) {
- result = join(result, this.generateStatement(stmt.handlers[i], S_TFFF));
- if (stmt.finalizer || i + 1 !== iz) {
- result = this.maybeBlockSuffix(stmt.handlers[i].body, result);
- }
- }
- } else {
- guardedHandlers = stmt.guardedHandlers || [];
- for (i = 0, iz = guardedHandlers.length; i < iz; ++i) {
- result = join(result, this.generateStatement(guardedHandlers[i], S_TFFF));
- if (stmt.finalizer || i + 1 !== iz) {
- result = this.maybeBlockSuffix(guardedHandlers[i].body, result);
- }
- }
- // new interface
- if (stmt.handler) {
- if (Array.isArray(stmt.handler)) {
- for (i = 0, iz = stmt.handler.length; i < iz; ++i) {
- result = join(result, this.generateStatement(stmt.handler[i], S_TFFF));
- if (stmt.finalizer || i + 1 !== iz) {
- result = this.maybeBlockSuffix(stmt.handler[i].body, result);
- }
- }
- } else {
- result = join(result, this.generateStatement(stmt.handler, S_TFFF));
- if (stmt.finalizer) {
- result = this.maybeBlockSuffix(stmt.handler.body, result);
- }
- }
- }
- }
- if (stmt.finalizer) {
- result = join(result, ['finally', this.maybeBlock(stmt.finalizer, S_TFFF)]);
- }
- return result;
- },
- SwitchStatement: function (stmt, flags) {
- var result, fragment, i, iz, bodyFlags, that = this;
- withIndent(function () {
- result = [
- 'switch' + space + '(',
- that.generateExpression(stmt.discriminant, Precedence.Sequence, E_TTT),
- ')' + space + '{' + newline
- ];
- });
- if (stmt.cases) {
- bodyFlags = S_TFFF;
- for (i = 0, iz = stmt.cases.length; i < iz; ++i) {
- if (i === iz - 1) {
- bodyFlags |= F_SEMICOLON_OPT;
- }
- fragment = addIndent(this.generateStatement(stmt.cases[i], bodyFlags));
- result.push(fragment);
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- result.push(newline);
- }
- }
- }
- result.push(addIndent('}'));
- return result;
- },
- SwitchCase: function (stmt, flags) {
- var result, fragment, i, iz, bodyFlags, that = this;
- withIndent(function () {
- if (stmt.test) {
- result = [
- join('case', that.generateExpression(stmt.test, Precedence.Sequence, E_TTT)),
- ':'
- ];
- } else {
- result = ['default:'];
- }
- i = 0;
- iz = stmt.consequent.length;
- if (iz && stmt.consequent[0].type === Syntax.BlockStatement) {
- fragment = that.maybeBlock(stmt.consequent[0], S_TFFF);
- result.push(fragment);
- i = 1;
- }
- if (i !== iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- bodyFlags = S_TFFF;
- for (; i < iz; ++i) {
- if (i === iz - 1 && flags & F_SEMICOLON_OPT) {
- bodyFlags |= F_SEMICOLON_OPT;
- }
- fragment = addIndent(that.generateStatement(stmt.consequent[i], bodyFlags));
- result.push(fragment);
- if (i + 1 !== iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- result.push(newline);
- }
- }
- });
- return result;
- },
- IfStatement: function (stmt, flags) {
- var result, bodyFlags, semicolonOptional, that = this;
- withIndent(function () {
- result = [
- 'if' + space + '(',
- that.generateExpression(stmt.test, Precedence.Sequence, E_TTT),
- ')'
- ];
- });
- semicolonOptional = flags & F_SEMICOLON_OPT;
- bodyFlags = S_TFFF;
- if (semicolonOptional) {
- bodyFlags |= F_SEMICOLON_OPT;
- }
- if (stmt.alternate) {
- result.push(this.maybeBlock(stmt.consequent, S_TFFF));
- result = this.maybeBlockSuffix(stmt.consequent, result);
- if (stmt.alternate.type === Syntax.IfStatement) {
- result = join(result, ['else ', this.generateStatement(stmt.alternate, bodyFlags)]);
- } else {
- result = join(result, join('else', this.maybeBlock(stmt.alternate, bodyFlags)));
- }
- } else {
- result.push(this.maybeBlock(stmt.consequent, bodyFlags));
- }
- return result;
- },
- ForStatement: function (stmt, flags) {
- var result, that = this;
- withIndent(function () {
- result = ['for' + space + '('];
- if (stmt.init) {
- if (stmt.init.type === Syntax.VariableDeclaration) {
- result.push(that.generateStatement(stmt.init, S_FFFF));
- } else {
- // F_ALLOW_IN becomes false.
- result.push(that.generateExpression(stmt.init, Precedence.Sequence, E_FTT));
- result.push(';');
- }
- } else {
- result.push(';');
- }
- if (stmt.test) {
- result.push(space);
- result.push(that.generateExpression(stmt.test, Precedence.Sequence, E_TTT));
- result.push(';');
- } else {
- result.push(';');
- }
- if (stmt.update) {
- result.push(space);
- result.push(that.generateExpression(stmt.update, Precedence.Sequence, E_TTT));
- result.push(')');
- } else {
- result.push(')');
- }
- });
- result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF));
- return result;
- },
- ForInStatement: function (stmt, flags) {
- return this.generateIterationForStatement('in', stmt, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF);
- },
- ForOfStatement: function (stmt, flags) {
- return this.generateIterationForStatement('of', stmt, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF);
- },
- LabeledStatement: function (stmt, flags) {
- return [stmt.label.name + ':', this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF)];
- },
- Program: function (stmt, flags) {
- var result, fragment, i, iz, bodyFlags;
- iz = stmt.body.length;
- result = [safeConcatenation && iz > 0 ? '\n' : ''];
- bodyFlags = S_TFTF;
- for (i = 0; i < iz; ++i) {
- if (!safeConcatenation && i === iz - 1) {
- bodyFlags |= F_SEMICOLON_OPT;
- }
- if (preserveBlankLines) {
- // handle spaces before the first line
- if (i === 0) {
- if (!stmt.body[0].leadingComments) {
- generateBlankLines(stmt.range[0], stmt.body[i].range[0], result);
- }
- }
- // handle spaces between lines
- if (i > 0) {
- if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) {
- generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result);
- }
- }
- }
- fragment = addIndent(this.generateStatement(stmt.body[i], bodyFlags));
- result.push(fragment);
- if (i + 1 < iz && !endsWithLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- if (preserveBlankLines) {
- if (!stmt.body[i + 1].leadingComments) {
- result.push(newline);
- }
- } else {
- result.push(newline);
- }
- }
- if (preserveBlankLines) {
- // handle spaces after the last line
- if (i === iz - 1) {
- if (!stmt.body[i].trailingComments) {
- generateBlankLines(stmt.body[i].range[1], stmt.range[1], result);
- }
- }
- }
- }
- return result;
- },
- FunctionDeclaration: function (stmt, flags) {
- return [
- generateAsyncPrefix(stmt, true),
- 'function',
- generateStarSuffix(stmt) || noEmptySpace(),
- stmt.id ? generateIdentifier(stmt.id) : '',
- this.generateFunctionBody(stmt)
- ];
- },
- ReturnStatement: function (stmt, flags) {
- if (stmt.argument) {
- return [join(
- 'return',
- this.generateExpression(stmt.argument, Precedence.Sequence, E_TTT)
- ), this.semicolon(flags)];
- }
- return ['return' + this.semicolon(flags)];
- },
- WhileStatement: function (stmt, flags) {
- var result, that = this;
- withIndent(function () {
- result = [
- 'while' + space + '(',
- that.generateExpression(stmt.test, Precedence.Sequence, E_TTT),
- ')'
- ];
- });
- result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF));
- return result;
- },
- WithStatement: function (stmt, flags) {
- var result, that = this;
- withIndent(function () {
- result = [
- 'with' + space + '(',
- that.generateExpression(stmt.object, Precedence.Sequence, E_TTT),
- ')'
- ];
- });
- result.push(this.maybeBlock(stmt.body, flags & F_SEMICOLON_OPT ? S_TFFT : S_TFFF));
- return result;
- }
- };
- merge(CodeGenerator.prototype, CodeGenerator.Statement);
- // Expressions.
- CodeGenerator.Expression = {
- SequenceExpression: function (expr, precedence, flags) {
- var result, i, iz;
- if (Precedence.Sequence < precedence) {
- flags |= F_ALLOW_IN;
- }
- result = [];
- for (i = 0, iz = expr.expressions.length; i < iz; ++i) {
- result.push(this.generateExpression(expr.expressions[i], Precedence.Assignment, flags));
- if (i + 1 < iz) {
- result.push(',' + space);
- }
- }
- return parenthesize(result, Precedence.Sequence, precedence);
- },
- AssignmentExpression: function (expr, precedence, flags) {
- return this.generateAssignment(expr.left, expr.right, expr.operator, precedence, flags);
- },
- ArrowFunctionExpression: function (expr, precedence, flags) {
- return parenthesize(this.generateFunctionBody(expr), Precedence.ArrowFunction, precedence);
- },
- ConditionalExpression: function (expr, precedence, flags) {
- if (Precedence.Conditional < precedence) {
- flags |= F_ALLOW_IN;
- }
- return parenthesize(
- [
- this.generateExpression(expr.test, Precedence.Coalesce, flags),
- space + '?' + space,
- this.generateExpression(expr.consequent, Precedence.Assignment, flags),
- space + ':' + space,
- this.generateExpression(expr.alternate, Precedence.Assignment, flags)
- ],
- Precedence.Conditional,
- precedence
- );
- },
- LogicalExpression: function (expr, precedence, flags) {
- if (expr.operator === '??') {
- flags |= F_FOUND_COALESCE;
- }
- return this.BinaryExpression(expr, precedence, flags);
- },
- BinaryExpression: function (expr, precedence, flags) {
- var result, leftPrecedence, rightPrecedence, currentPrecedence, fragment, leftSource;
- currentPrecedence = BinaryPrecedence[expr.operator];
- leftPrecedence = expr.operator === '**' ? Precedence.Postfix : currentPrecedence;
- rightPrecedence = expr.operator === '**' ? currentPrecedence : currentPrecedence + 1;
- if (currentPrecedence < precedence) {
- flags |= F_ALLOW_IN;
- }
- fragment = this.generateExpression(expr.left, leftPrecedence, flags);
- leftSource = fragment.toString();
- if (leftSource.charCodeAt(leftSource.length - 1) === 0x2F /* / */ && esutils.code.isIdentifierPartES5(expr.operator.charCodeAt(0))) {
- result = [fragment, noEmptySpace(), expr.operator];
- } else {
- result = join(fragment, expr.operator);
- }
- fragment = this.generateExpression(expr.right, rightPrecedence, flags);
- if (expr.operator === '/' && fragment.toString().charAt(0) === '/' ||
- expr.operator.slice(-1) === '<' && fragment.toString().slice(0, 3) === '!--') {
- // If '/' concats with '/' or `<` concats with `!--`, it is interpreted as comment start
- result.push(noEmptySpace());
- result.push(fragment);
- } else {
- result = join(result, fragment);
- }
- if (expr.operator === 'in' && !(flags & F_ALLOW_IN)) {
- return ['(', result, ')'];
- }
- if ((expr.operator === '||' || expr.operator === '&&') && (flags & F_FOUND_COALESCE)) {
- return ['(', result, ')'];
- }
- return parenthesize(result, currentPrecedence, precedence);
- },
- CallExpression: function (expr, precedence, flags) {
- var result, i, iz;
- // F_ALLOW_UNPARATH_NEW becomes false.
- result = [this.generateExpression(expr.callee, Precedence.Call, E_TTF)];
- if (expr.optional) {
- result.push('?.');
- }
- result.push('(');
- for (i = 0, iz = expr['arguments'].length; i < iz; ++i) {
- result.push(this.generateExpression(expr['arguments'][i], Precedence.Assignment, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + space);
- }
- }
- result.push(')');
- if (!(flags & F_ALLOW_CALL)) {
- return ['(', result, ')'];
- }
- return parenthesize(result, Precedence.Call, precedence);
- },
- ChainExpression: function (expr, precedence, flags) {
- if (Precedence.OptionalChaining < precedence) {
- flags |= F_ALLOW_CALL;
- }
- var result = this.generateExpression(expr.expression, Precedence.OptionalChaining, flags);
- return parenthesize(result, Precedence.OptionalChaining, precedence);
- },
- NewExpression: function (expr, precedence, flags) {
- var result, length, i, iz, itemFlags;
- length = expr['arguments'].length;
- // F_ALLOW_CALL becomes false.
- // F_ALLOW_UNPARATH_NEW may become false.
- itemFlags = (flags & F_ALLOW_UNPARATH_NEW && !parentheses && length === 0) ? E_TFT : E_TFF;
- result = join(
- 'new',
- this.generateExpression(expr.callee, Precedence.New, itemFlags)
- );
- if (!(flags & F_ALLOW_UNPARATH_NEW) || parentheses || length > 0) {
- result.push('(');
- for (i = 0, iz = length; i < iz; ++i) {
- result.push(this.generateExpression(expr['arguments'][i], Precedence.Assignment, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + space);
- }
- }
- result.push(')');
- }
- return parenthesize(result, Precedence.New, precedence);
- },
- MemberExpression: function (expr, precedence, flags) {
- var result, fragment;
- // F_ALLOW_UNPARATH_NEW becomes false.
- result = [this.generateExpression(expr.object, Precedence.Call, (flags & F_ALLOW_CALL) ? E_TTF : E_TFF)];
- if (expr.computed) {
- if (expr.optional) {
- result.push('?.');
- }
- result.push('[');
- result.push(this.generateExpression(expr.property, Precedence.Sequence, flags & F_ALLOW_CALL ? E_TTT : E_TFT));
- result.push(']');
- } else {
- if (!expr.optional && expr.object.type === Syntax.Literal && typeof expr.object.value === 'number') {
- fragment = toSourceNodeWhenNeeded(result).toString();
- // When the following conditions are all true,
- // 1. No floating point
- // 2. Don't have exponents
- // 3. The last character is a decimal digit
- // 4. Not hexadecimal OR octal number literal
- // we should add a floating point.
- if (
- fragment.indexOf('.') < 0 &&
- !/[eExX]/.test(fragment) &&
- esutils.code.isDecimalDigit(fragment.charCodeAt(fragment.length - 1)) &&
- !(fragment.length >= 2 && fragment.charCodeAt(0) === 48) // '0'
- ) {
- result.push(' ');
- }
- }
- result.push(expr.optional ? '?.' : '.');
- result.push(generateIdentifier(expr.property));
- }
- return parenthesize(result, Precedence.Member, precedence);
- },
- MetaProperty: function (expr, precedence, flags) {
- var result;
- result = [];
- result.push(typeof expr.meta === "string" ? expr.meta : generateIdentifier(expr.meta));
- result.push('.');
- result.push(typeof expr.property === "string" ? expr.property : generateIdentifier(expr.property));
- return parenthesize(result, Precedence.Member, precedence);
- },
- UnaryExpression: function (expr, precedence, flags) {
- var result, fragment, rightCharCode, leftSource, leftCharCode;
- fragment = this.generateExpression(expr.argument, Precedence.Unary, E_TTT);
- if (space === '') {
- result = join(expr.operator, fragment);
- } else {
- result = [expr.operator];
- if (expr.operator.length > 2) {
- // delete, void, typeof
- // get `typeof []`, not `typeof[]`
- result = join(result, fragment);
- } else {
- // Prevent inserting spaces between operator and argument if it is unnecessary
- // like, `!cond`
- leftSource = toSourceNodeWhenNeeded(result).toString();
- leftCharCode = leftSource.charCodeAt(leftSource.length - 1);
- rightCharCode = fragment.toString().charCodeAt(0);
- if (((leftCharCode === 0x2B /* + */ || leftCharCode === 0x2D /* - */) && leftCharCode === rightCharCode) ||
- (esutils.code.isIdentifierPartES5(leftCharCode) && esutils.code.isIdentifierPartES5(rightCharCode))) {
- result.push(noEmptySpace());
- result.push(fragment);
- } else {
- result.push(fragment);
- }
- }
- }
- return parenthesize(result, Precedence.Unary, precedence);
- },
- YieldExpression: function (expr, precedence, flags) {
- var result;
- if (expr.delegate) {
- result = 'yield*';
- } else {
- result = 'yield';
- }
- if (expr.argument) {
- result = join(
- result,
- this.generateExpression(expr.argument, Precedence.Yield, E_TTT)
- );
- }
- return parenthesize(result, Precedence.Yield, precedence);
- },
- AwaitExpression: function (expr, precedence, flags) {
- var result = join(
- expr.all ? 'await*' : 'await',
- this.generateExpression(expr.argument, Precedence.Await, E_TTT)
- );
- return parenthesize(result, Precedence.Await, precedence);
- },
- UpdateExpression: function (expr, precedence, flags) {
- if (expr.prefix) {
- return parenthesize(
- [
- expr.operator,
- this.generateExpression(expr.argument, Precedence.Unary, E_TTT)
- ],
- Precedence.Unary,
- precedence
- );
- }
- return parenthesize(
- [
- this.generateExpression(expr.argument, Precedence.Postfix, E_TTT),
- expr.operator
- ],
- Precedence.Postfix,
- precedence
- );
- },
- FunctionExpression: function (expr, precedence, flags) {
- var result = [
- generateAsyncPrefix(expr, true),
- 'function'
- ];
- if (expr.id) {
- result.push(generateStarSuffix(expr) || noEmptySpace());
- result.push(generateIdentifier(expr.id));
- } else {
- result.push(generateStarSuffix(expr) || space);
- }
- result.push(this.generateFunctionBody(expr));
- return result;
- },
- ArrayPattern: function (expr, precedence, flags) {
- return this.ArrayExpression(expr, precedence, flags, true);
- },
- ArrayExpression: function (expr, precedence, flags, isPattern) {
- var result, multiline, that = this;
- if (!expr.elements.length) {
- return '[]';
- }
- multiline = isPattern ? false : expr.elements.length > 1;
- result = ['[', multiline ? newline : ''];
- withIndent(function (indent) {
- var i, iz;
- for (i = 0, iz = expr.elements.length; i < iz; ++i) {
- if (!expr.elements[i]) {
- if (multiline) {
- result.push(indent);
- }
- if (i + 1 === iz) {
- result.push(',');
- }
- } else {
- result.push(multiline ? indent : '');
- result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
- }
- if (i + 1 < iz) {
- result.push(',' + (multiline ? newline : space));
- }
- }
- });
- if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(multiline ? base : '');
- result.push(']');
- return result;
- },
- RestElement: function(expr, precedence, flags) {
- return '...' + this.generatePattern(expr.argument);
- },
- ClassExpression: function (expr, precedence, flags) {
- var result, fragment;
- result = ['class'];
- if (expr.id) {
- result = join(result, this.generateExpression(expr.id, Precedence.Sequence, E_TTT));
- }
- if (expr.superClass) {
- fragment = join('extends', this.generateExpression(expr.superClass, Precedence.Unary, E_TTT));
- result = join(result, fragment);
- }
- result.push(space);
- result.push(this.generateStatement(expr.body, S_TFFT));
- return result;
- },
- MethodDefinition: function (expr, precedence, flags) {
- var result, fragment;
- if (expr['static']) {
- result = ['static' + space];
- } else {
- result = [];
- }
- if (expr.kind === 'get' || expr.kind === 'set') {
- fragment = [
- join(expr.kind, this.generatePropertyKey(expr.key, expr.computed)),
- this.generateFunctionBody(expr.value)
- ];
- } else {
- fragment = [
- generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed),
- this.generateFunctionBody(expr.value)
- ];
- }
- return join(result, fragment);
- },
- Property: function (expr, precedence, flags) {
- if (expr.kind === 'get' || expr.kind === 'set') {
- return [
- expr.kind, noEmptySpace(),
- this.generatePropertyKey(expr.key, expr.computed),
- this.generateFunctionBody(expr.value)
- ];
- }
- if (expr.shorthand) {
- if (expr.value.type === "AssignmentPattern") {
- return this.AssignmentPattern(expr.value, Precedence.Sequence, E_TTT);
- }
- return this.generatePropertyKey(expr.key, expr.computed);
- }
- if (expr.method) {
- return [
- generateMethodPrefix(expr),
- this.generatePropertyKey(expr.key, expr.computed),
- this.generateFunctionBody(expr.value)
- ];
- }
- return [
- this.generatePropertyKey(expr.key, expr.computed),
- ':' + space,
- this.generateExpression(expr.value, Precedence.Assignment, E_TTT)
- ];
- },
- ObjectExpression: function (expr, precedence, flags) {
- var multiline, result, fragment, that = this;
- if (!expr.properties.length) {
- return '{}';
- }
- multiline = expr.properties.length > 1;
- withIndent(function () {
- fragment = that.generateExpression(expr.properties[0], Precedence.Sequence, E_TTT);
- });
- if (!multiline) {
- // issues 4
- // Do not transform from
- // dejavu.Class.declare({
- // method2: function () {}
- // });
- // to
- // dejavu.Class.declare({method2: function () {
- // }});
- if (!hasLineTerminator(toSourceNodeWhenNeeded(fragment).toString())) {
- return [ '{', space, fragment, space, '}' ];
- }
- }
- withIndent(function (indent) {
- var i, iz;
- result = [ '{', newline, indent, fragment ];
- if (multiline) {
- result.push(',' + newline);
- for (i = 1, iz = expr.properties.length; i < iz; ++i) {
- result.push(indent);
- result.push(that.generateExpression(expr.properties[i], Precedence.Sequence, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + newline);
- }
- }
- }
- });
- if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(base);
- result.push('}');
- return result;
- },
- AssignmentPattern: function(expr, precedence, flags) {
- return this.generateAssignment(expr.left, expr.right, '=', precedence, flags);
- },
- ObjectPattern: function (expr, precedence, flags) {
- var result, i, iz, multiline, property, that = this;
- if (!expr.properties.length) {
- return '{}';
- }
- multiline = false;
- if (expr.properties.length === 1) {
- property = expr.properties[0];
- if (
- property.type === Syntax.Property
- && property.value.type !== Syntax.Identifier
- ) {
- multiline = true;
- }
- } else {
- for (i = 0, iz = expr.properties.length; i < iz; ++i) {
- property = expr.properties[i];
- if (
- property.type === Syntax.Property
- && !property.shorthand
- ) {
- multiline = true;
- break;
- }
- }
- }
- result = ['{', multiline ? newline : '' ];
- withIndent(function (indent) {
- var i, iz;
- for (i = 0, iz = expr.properties.length; i < iz; ++i) {
- result.push(multiline ? indent : '');
- result.push(that.generateExpression(expr.properties[i], Precedence.Sequence, E_TTT));
- if (i + 1 < iz) {
- result.push(',' + (multiline ? newline : space));
- }
- }
- });
- if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
- result.push(newline);
- }
- result.push(multiline ? base : '');
- result.push('}');
- return result;
- },
- ThisExpression: function (expr, precedence, flags) {
- return 'this';
- },
- Super: function (expr, precedence, flags) {
- return 'super';
- },
- Identifier: function (expr, precedence, flags) {
- return generateIdentifier(expr);
- },
- ImportDefaultSpecifier: function (expr, precedence, flags) {
- return generateIdentifier(expr.id || expr.local);
- },
- ImportNamespaceSpecifier: function (expr, precedence, flags) {
- var result = ['*'];
- var id = expr.id || expr.local;
- if (id) {
- result.push(space + 'as' + noEmptySpace() + generateIdentifier(id));
- }
- return result;
- },
- ImportSpecifier: function (expr, precedence, flags) {
- var imported = expr.imported;
- var result = [ imported.name ];
- var local = expr.local;
- if (local && local.name !== imported.name) {
- result.push(noEmptySpace() + 'as' + noEmptySpace() + generateIdentifier(local));
- }
- return result;
- },
- ExportSpecifier: function (expr, precedence, flags) {
- var local = expr.local;
- var result = [ local.name ];
- var exported = expr.exported;
- if (exported && exported.name !== local.name) {
- result.push(noEmptySpace() + 'as' + noEmptySpace() + generateIdentifier(exported));
- }
- return result;
- },
- Literal: function (expr, precedence, flags) {
- var raw;
- if (expr.hasOwnProperty('raw') && parse && extra.raw) {
- try {
- raw = parse(expr.raw).body[0].expression;
- if (raw.type === Syntax.Literal) {
- if (raw.value === expr.value) {
- return expr.raw;
- }
- }
- } catch (e) {
- // not use raw property
- }
- }
- if (expr.regex) {
- return '/' + expr.regex.pattern + '/' + expr.regex.flags;
- }
- if (typeof expr.value === 'bigint') {
- return expr.value.toString() + 'n';
- }
- // `expr.value` can be null if `expr.bigint` exists. We need to check
- // `expr.bigint` first.
- if (expr.bigint) {
- return expr.bigint + 'n';
- }
- if (expr.value === null) {
- return 'null';
- }
- if (typeof expr.value === 'string') {
- return escapeString(expr.value);
- }
- if (typeof expr.value === 'number') {
- return generateNumber(expr.value);
- }
- if (typeof expr.value === 'boolean') {
- return expr.value ? 'true' : 'false';
- }
- return generateRegExp(expr.value);
- },
- GeneratorExpression: function (expr, precedence, flags) {
- return this.ComprehensionExpression(expr, precedence, flags);
- },
- ComprehensionExpression: function (expr, precedence, flags) {
- // GeneratorExpression should be parenthesized with (...), ComprehensionExpression with [...]
- // Due to https://bugzilla.mozilla.org/show_bug.cgi?id=883468 position of expr.body can differ in Spidermonkey and ES6
- var result, i, iz, fragment, that = this;
- result = (expr.type === Syntax.GeneratorExpression) ? ['('] : ['['];
- if (extra.moz.comprehensionExpressionStartsWithAssignment) {
- fragment = this.generateExpression(expr.body, Precedence.Assignment, E_TTT);
- result.push(fragment);
- }
- if (expr.blocks) {
- withIndent(function () {
- for (i = 0, iz = expr.blocks.length; i < iz; ++i) {
- fragment = that.generateExpression(expr.blocks[i], Precedence.Sequence, E_TTT);
- if (i > 0 || extra.moz.comprehensionExpressionStartsWithAssignment) {
- result = join(result, fragment);
- } else {
- result.push(fragment);
- }
- }
- });
- }
- if (expr.filter) {
- result = join(result, 'if' + space);
- fragment = this.generateExpression(expr.filter, Precedence.Sequence, E_TTT);
- result = join(result, [ '(', fragment, ')' ]);
- }
- if (!extra.moz.comprehensionExpressionStartsWithAssignment) {
- fragment = this.generateExpression(expr.body, Precedence.Assignment, E_TTT);
- result = join(result, fragment);
- }
- result.push((expr.type === Syntax.GeneratorExpression) ? ')' : ']');
- return result;
- },
- ComprehensionBlock: function (expr, precedence, flags) {
- var fragment;
- if (expr.left.type === Syntax.VariableDeclaration) {
- fragment = [
- expr.left.kind, noEmptySpace(),
- this.generateStatement(expr.left.declarations[0], S_FFFF)
- ];
- } else {
- fragment = this.generateExpression(expr.left, Precedence.Call, E_TTT);
- }
- fragment = join(fragment, expr.of ? 'of' : 'in');
- fragment = join(fragment, this.generateExpression(expr.right, Precedence.Sequence, E_TTT));
- return [ 'for' + space + '(', fragment, ')' ];
- },
- SpreadElement: function (expr, precedence, flags) {
- return [
- '...',
- this.generateExpression(expr.argument, Precedence.Assignment, E_TTT)
- ];
- },
- TaggedTemplateExpression: function (expr, precedence, flags) {
- var itemFlags = E_TTF;
- if (!(flags & F_ALLOW_CALL)) {
- itemFlags = E_TFF;
- }
- var result = [
- this.generateExpression(expr.tag, Precedence.Call, itemFlags),
- this.generateExpression(expr.quasi, Precedence.Primary, E_FFT)
- ];
- return parenthesize(result, Precedence.TaggedTemplate, precedence);
- },
- TemplateElement: function (expr, precedence, flags) {
- // Don't use "cooked". Since tagged template can use raw template
- // representation. So if we do so, it breaks the script semantics.
- return expr.value.raw;
- },
- TemplateLiteral: function (expr, precedence, flags) {
- var result, i, iz;
- result = [ '`' ];
- for (i = 0, iz = expr.quasis.length; i < iz; ++i) {
- result.push(this.generateExpression(expr.quasis[i], Precedence.Primary, E_TTT));
- if (i + 1 < iz) {
- result.push('${' + space);
- result.push(this.generateExpression(expr.expressions[i], Precedence.Sequence, E_TTT));
- result.push(space + '}');
- }
- }
- result.push('`');
- return result;
- },
- ModuleSpecifier: function (expr, precedence, flags) {
- return this.Literal(expr, precedence, flags);
- },
- ImportExpression: function(expr, precedence, flag) {
- return parenthesize([
- 'import(',
- this.generateExpression(expr.source, Precedence.Assignment, E_TTT),
- ')'
- ], Precedence.Call, precedence);
- }
- };
- merge(CodeGenerator.prototype, CodeGenerator.Expression);
- CodeGenerator.prototype.generateExpression = function (expr, precedence, flags) {
- var result, type;
- type = expr.type || Syntax.Property;
- if (extra.verbatim && expr.hasOwnProperty(extra.verbatim)) {
- return generateVerbatim(expr, precedence);
- }
- result = this[type](expr, precedence, flags);
- if (extra.comment) {
- result = addComments(expr, result);
- }
- return toSourceNodeWhenNeeded(result, expr);
- };
- CodeGenerator.prototype.generateStatement = function (stmt, flags) {
- var result,
- fragment;
- result = this[stmt.type](stmt, flags);
- // Attach comments
- if (extra.comment) {
- result = addComments(stmt, result);
- }
- fragment = toSourceNodeWhenNeeded(result).toString();
- if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') {
- result = sourceMap ? toSourceNodeWhenNeeded(result).replaceRight(/\s+$/, '') : fragment.replace(/\s+$/, '');
- }
- return toSourceNodeWhenNeeded(result, stmt);
- };
- function generateInternal(node) {
- var codegen;
- codegen = new CodeGenerator();
- if (isStatement(node)) {
- return codegen.generateStatement(node, S_TFFF);
- }
- if (isExpression(node)) {
- return codegen.generateExpression(node, Precedence.Sequence, E_TTT);
- }
- throw new Error('Unknown node type: ' + node.type);
- }
- function generate(node, options) {
- var defaultOptions = getDefaultOptions(), result, pair;
- if (options != null) {
- // Obsolete options
- //
- // `options.indent`
- // `options.base`
- //
- // Instead of them, we can use `option.format.indent`.
- if (typeof options.indent === 'string') {
- defaultOptions.format.indent.style = options.indent;
- }
- if (typeof options.base === 'number') {
- defaultOptions.format.indent.base = options.base;
- }
- options = updateDeeply(defaultOptions, options);
- indent = options.format.indent.style;
- if (typeof options.base === 'string') {
- base = options.base;
- } else {
- base = stringRepeat(indent, options.format.indent.base);
- }
- } else {
- options = defaultOptions;
- indent = options.format.indent.style;
- base = stringRepeat(indent, options.format.indent.base);
- }
- json = options.format.json;
- renumber = options.format.renumber;
- hexadecimal = json ? false : options.format.hexadecimal;
- quotes = json ? 'double' : options.format.quotes;
- escapeless = options.format.escapeless;
- newline = options.format.newline;
- space = options.format.space;
- if (options.format.compact) {
- newline = space = indent = base = '';
- }
- parentheses = options.format.parentheses;
- semicolons = options.format.semicolons;
- safeConcatenation = options.format.safeConcatenation;
- directive = options.directive;
- parse = json ? null : options.parse;
- sourceMap = options.sourceMap;
- sourceCode = options.sourceCode;
- preserveBlankLines = options.format.preserveBlankLines && sourceCode !== null;
- extra = options;
- if (sourceMap) {
- if (!exports.browser) {
- // We assume environment is node.js
- // And prevent from including source-map by browserify
- SourceNode = require('source-map').SourceNode;
- } else {
- SourceNode = global.sourceMap.SourceNode;
- }
- }
- result = generateInternal(node);
- if (!sourceMap) {
- pair = {code: result.toString(), map: null};
- return options.sourceMapWithCode ? pair : pair.code;
- }
- pair = result.toStringWithSourceMap({
- file: options.file,
- sourceRoot: options.sourceMapRoot
- });
- if (options.sourceContent) {
- pair.map.setSourceContent(options.sourceMap,
- options.sourceContent);
- }
- if (options.sourceMapWithCode) {
- return pair;
- }
- return pair.map.toString();
- }
- FORMAT_MINIFY = {
- indent: {
- style: '',
- base: 0
- },
- renumber: true,
- hexadecimal: true,
- quotes: 'auto',
- escapeless: true,
- compact: true,
- parentheses: false,
- semicolons: false
- };
- FORMAT_DEFAULTS = getDefaultOptions().format;
- exports.version = require('./package.json').version;
- exports.generate = generate;
- exports.attachComments = estraverse.attachComments;
- exports.Precedence = updateDeeply({}, Precedence);
- exports.browser = false;
- exports.FORMAT_MINIFY = FORMAT_MINIFY;
- exports.FORMAT_DEFAULTS = FORMAT_DEFAULTS;
- }());
- /* vim: set sw=4 ts=4 et tw=80 : */
|