UNPKG

orionsoft-react-scripts

Version:

Orionsoft Configuration and scripts for Create React App.

1,433 lines (1,244 loc) 94.7 kB
/* 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> 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, isArray, 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, Await: 1, Assignment: 1, Conditional: 2, ArrowFunction: 2, LogicalOR: 3, LogicalAND: 4, BitwiseOR: 5, BitwiseXOR: 6, BitwiseAND: 7, Equality: 8, Relational: 9, BitwiseSHIFT: 10, Additive: 11, Multiplicative: 12, Unary: 13, Postfix: 14, Call: 15, New: 16, TaggedTemplate: 17, Member: 18, Primary: 19 }; BinaryPrecedence = { '||': 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 }; //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; //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; } isArray = Array.isArray; if (!isArray) { isArray = function isArray(array) { return Object.prototype.toString.call(array) === '[object Array]'; }; } 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 += 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 (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; if (func.async) { return generateAsyncPrefix(func, !prop.computed); } else { // avoid space before method name return generateStarSuffix(func) ? '*' : ''; } } 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' + 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.Sequence, E_TTT) ), ')']; }); result.push(this.maybeBlock(stmt.body, flags)); return result; }; CodeGenerator.prototype.generatePropertyKey = function (expr, computed, value) { var result = []; if (computed) { result.push('['); } if (value.type === 'AssignmentPattern') { result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT)); } else { result.push(this.generateExpression(expr, Precedence.Sequence, 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.Assignment, 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; 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); } }); 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(