2017-05-03 15:35:00 +02:00
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
A JavaScript tokenizer / parser / beautifier / compressor.
|
|
|
|
https://github.com/mishoo/UglifyJS2
|
|
|
|
|
|
|
|
-------------------------------- (C) ---------------------------------
|
|
|
|
|
|
|
|
Author: Mihai Bazon
|
|
|
|
<mihai.bazon@gmail.com>
|
|
|
|
http://mihai.bazon.net/blog
|
|
|
|
|
|
|
|
Distributed under the BSD license:
|
|
|
|
|
|
|
|
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.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 HOLDER “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 THE 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.
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
|
|
|
|
|
|
|
|
function is_some_comments(comment) {
|
|
|
|
// multiline comment
|
|
|
|
return comment.type == "comment2" && /@preserve|@license|@cc_on/i.test(comment.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function OutputStream(options) {
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var readonly = !options;
|
2017-05-03 15:35:00 +02:00
|
|
|
options = defaults(options, {
|
|
|
|
ascii_only : false,
|
|
|
|
beautify : false,
|
2018-09-20 02:56:13 +02:00
|
|
|
braces : false,
|
2017-05-03 15:35:00 +02:00
|
|
|
comments : false,
|
2017-08-14 05:01:11 +02:00
|
|
|
ie8 : false,
|
2017-05-03 15:35:00 +02:00
|
|
|
indent_level : 4,
|
|
|
|
indent_start : 0,
|
|
|
|
inline_script : true,
|
|
|
|
keep_quoted_props: false,
|
|
|
|
max_line_len : false,
|
|
|
|
preamble : null,
|
|
|
|
preserve_line : false,
|
|
|
|
quote_keys : false,
|
|
|
|
quote_style : 0,
|
|
|
|
semicolons : true,
|
|
|
|
shebang : true,
|
|
|
|
source_map : null,
|
2017-08-14 05:01:11 +02:00
|
|
|
webkit : false,
|
2017-05-03 15:35:00 +02:00
|
|
|
width : 80,
|
|
|
|
wrap_iife : false,
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Convert comment option to RegExp if neccessary and set up comments filter
|
|
|
|
var comment_filter = return_false; // Default case, throw all comments away
|
|
|
|
if (options.comments) {
|
|
|
|
var comments = options.comments;
|
|
|
|
if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
|
|
|
|
var regex_pos = options.comments.lastIndexOf("/");
|
|
|
|
comments = new RegExp(
|
|
|
|
options.comments.substr(1, regex_pos - 1),
|
|
|
|
options.comments.substr(regex_pos + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (comments instanceof RegExp) {
|
|
|
|
comment_filter = function(comment) {
|
|
|
|
return comment.type != "comment5" && comments.test(comment.value);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (typeof comments === "function") {
|
|
|
|
comment_filter = function(comment) {
|
|
|
|
return comment.type != "comment5" && comments(this, comment);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (comments === "some") {
|
|
|
|
comment_filter = is_some_comments;
|
|
|
|
} else { // NOTE includes "all" option
|
|
|
|
comment_filter = return_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var indentation = 0;
|
|
|
|
var current_col = 0;
|
|
|
|
var current_line = 1;
|
|
|
|
var current_pos = 0;
|
|
|
|
var OUTPUT = "";
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
var to_utf8 = options.ascii_only ? function(str, identifier) {
|
2017-05-03 15:35:00 +02:00
|
|
|
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
|
|
|
|
var code = ch.charCodeAt(0).toString(16);
|
|
|
|
if (code.length <= 2 && !identifier) {
|
|
|
|
while (code.length < 2) code = "0" + code;
|
|
|
|
return "\\x" + code;
|
|
|
|
} else {
|
|
|
|
while (code.length < 4) code = "0" + code;
|
|
|
|
return "\\u" + code;
|
|
|
|
}
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
} : function(str) {
|
2018-09-20 02:56:13 +02:00
|
|
|
var s = "";
|
|
|
|
for (var i = 0, len = str.length; i < len; i++) {
|
|
|
|
if (is_surrogate_pair_head(str[i]) && !is_surrogate_pair_tail(str[i + 1])
|
|
|
|
|| is_surrogate_pair_tail(str[i]) && !is_surrogate_pair_head(str[i - 1])) {
|
|
|
|
s += "\\u" + str.charCodeAt(i).toString(16);
|
|
|
|
} else {
|
|
|
|
s += str[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
2017-05-03 15:35:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function make_string(str, quote) {
|
|
|
|
var dq = 0, sq = 0;
|
|
|
|
str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
|
2018-09-20 02:56:13 +02:00
|
|
|
function(s, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
switch (s) {
|
|
|
|
case '"': ++dq; return '"';
|
|
|
|
case "'": ++sq; return "'";
|
|
|
|
case "\\": return "\\\\";
|
|
|
|
case "\n": return "\\n";
|
|
|
|
case "\r": return "\\r";
|
|
|
|
case "\t": return "\\t";
|
|
|
|
case "\b": return "\\b";
|
|
|
|
case "\f": return "\\f";
|
2017-08-14 05:01:11 +02:00
|
|
|
case "\x0B": return options.ie8 ? "\\x0B" : "\\v";
|
2017-05-03 15:35:00 +02:00
|
|
|
case "\u2028": return "\\u2028";
|
|
|
|
case "\u2029": return "\\u2029";
|
|
|
|
case "\ufeff": return "\\ufeff";
|
|
|
|
case "\0":
|
2017-10-14 18:40:54 +02:00
|
|
|
return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
function quote_single() {
|
|
|
|
return "'" + str.replace(/\x27/g, "\\'") + "'";
|
|
|
|
}
|
|
|
|
function quote_double() {
|
|
|
|
return '"' + str.replace(/\x22/g, '\\"') + '"';
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
str = to_utf8(str);
|
2017-05-03 15:35:00 +02:00
|
|
|
switch (options.quote_style) {
|
|
|
|
case 1:
|
|
|
|
return quote_single();
|
|
|
|
case 2:
|
|
|
|
return quote_double();
|
|
|
|
case 3:
|
|
|
|
return quote == "'" ? quote_single() : quote_double();
|
|
|
|
default:
|
|
|
|
return dq > sq ? quote_single() : quote_double();
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function encode_string(str, quote) {
|
|
|
|
var ret = make_string(str, quote);
|
|
|
|
if (options.inline_script) {
|
2018-09-20 02:56:13 +02:00
|
|
|
ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2");
|
2017-05-03 15:35:00 +02:00
|
|
|
ret = ret.replace(/\x3c!--/g, "\\x3c!--");
|
|
|
|
ret = ret.replace(/--\x3e/g, "--\\x3e");
|
|
|
|
}
|
|
|
|
return ret;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function make_name(name) {
|
|
|
|
name = name.toString();
|
2017-08-14 05:01:11 +02:00
|
|
|
name = to_utf8(name, true);
|
2017-05-03 15:35:00 +02:00
|
|
|
return name;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function make_indent(back) {
|
|
|
|
return repeat_string(" ", options.indent_start + indentation - back * options.indent_level);
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
/* -----[ beautification/minification ]----- */
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var has_parens = false;
|
|
|
|
var line_end = 0;
|
|
|
|
var line_fixed = true;
|
2017-05-03 15:35:00 +02:00
|
|
|
var might_need_space = false;
|
|
|
|
var might_need_semicolon = false;
|
2018-09-20 02:56:13 +02:00
|
|
|
var need_newline_indented = false;
|
|
|
|
var need_space = false;
|
|
|
|
var newline_insert = -1;
|
2017-05-24 15:10:37 +02:00
|
|
|
var last = "";
|
2017-08-14 05:01:11 +02:00
|
|
|
var mapping_token, mapping_name, mappings = options.source_map && [];
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var adjust_mappings = mappings ? function(line, col) {
|
|
|
|
mappings.forEach(function(mapping) {
|
|
|
|
mapping.line += line;
|
|
|
|
mapping.col += col;
|
|
|
|
});
|
|
|
|
} : noop;
|
|
|
|
|
|
|
|
var flush_mappings = mappings ? function() {
|
2017-08-14 05:01:11 +02:00
|
|
|
mappings.forEach(function(mapping) {
|
|
|
|
try {
|
|
|
|
options.source_map.add(
|
|
|
|
mapping.token.file,
|
|
|
|
mapping.line, mapping.col,
|
|
|
|
mapping.token.line, mapping.token.col,
|
|
|
|
!mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name
|
|
|
|
);
|
|
|
|
} catch(ex) {
|
|
|
|
AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", {
|
|
|
|
file: mapping.token.file,
|
|
|
|
line: mapping.token.line,
|
|
|
|
col: mapping.token.col,
|
|
|
|
cline: mapping.line,
|
|
|
|
ccol: mapping.col,
|
|
|
|
name: mapping.name || ""
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mappings = [];
|
|
|
|
} : noop;
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function insert_newlines(count) {
|
|
|
|
var index = OUTPUT.lastIndexOf("\n");
|
|
|
|
if (line_end < index) line_end = index;
|
|
|
|
var left = OUTPUT.slice(0, line_end);
|
|
|
|
var right = OUTPUT.slice(line_end);
|
|
|
|
adjust_mappings(count, right.length - current_col);
|
|
|
|
current_line += count;
|
|
|
|
current_pos += count;
|
|
|
|
current_col = right.length;
|
|
|
|
OUTPUT = left;
|
|
|
|
while (count--) OUTPUT += "\n";
|
|
|
|
OUTPUT += right;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fix_line = options.max_line_len ? function() {
|
|
|
|
if (line_fixed) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (current_col > options.max_line_len) {
|
|
|
|
AST_Node.warn("Output exceeds {max_line_len} characters", options);
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
return;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
if (current_col > options.max_line_len) insert_newlines(1);
|
|
|
|
line_fixed = true;
|
|
|
|
flush_mappings();
|
2017-05-03 15:35:00 +02:00
|
|
|
} : noop;
|
|
|
|
|
|
|
|
var requireSemicolonChars = makePredicate("( [ + * / - , .");
|
|
|
|
|
|
|
|
function print(str) {
|
|
|
|
str = String(str);
|
|
|
|
var ch = str.charAt(0);
|
2018-09-20 02:56:13 +02:00
|
|
|
if (need_newline_indented && ch) {
|
|
|
|
need_newline_indented = false;
|
|
|
|
if (ch != "\n") {
|
|
|
|
print("\n");
|
|
|
|
indent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_space && ch) {
|
|
|
|
need_space = false;
|
|
|
|
if (!/[\s;})]/.test(ch)) {
|
|
|
|
space();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newline_insert = -1;
|
2017-05-24 15:10:37 +02:00
|
|
|
var prev = last.charAt(last.length - 1);
|
2017-05-03 15:35:00 +02:00
|
|
|
if (might_need_semicolon) {
|
|
|
|
might_need_semicolon = false;
|
|
|
|
|
2017-05-24 15:10:37 +02:00
|
|
|
if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
|
2018-09-20 02:56:13 +02:00
|
|
|
if (options.semicolons || requireSemicolonChars[ch]) {
|
2017-05-03 15:35:00 +02:00
|
|
|
OUTPUT += ";";
|
|
|
|
current_col++;
|
|
|
|
current_pos++;
|
|
|
|
} else {
|
2018-09-20 02:56:13 +02:00
|
|
|
fix_line();
|
2017-05-03 15:35:00 +02:00
|
|
|
OUTPUT += "\n";
|
|
|
|
current_pos++;
|
|
|
|
current_line++;
|
|
|
|
current_col = 0;
|
|
|
|
|
|
|
|
if (/^\s+$/.test(str)) {
|
|
|
|
// reset the semicolon flag, since we didn't print one
|
|
|
|
// now and might still have to later
|
|
|
|
might_need_semicolon = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.beautify)
|
|
|
|
might_need_space = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (might_need_space) {
|
|
|
|
if ((is_identifier_char(prev)
|
|
|
|
&& (is_identifier_char(ch) || ch == "\\"))
|
|
|
|
|| (ch == "/" && ch == prev)
|
|
|
|
|| ((ch == "+" || ch == "-") && ch == last))
|
|
|
|
{
|
|
|
|
OUTPUT += " ";
|
|
|
|
current_col++;
|
|
|
|
current_pos++;
|
|
|
|
}
|
|
|
|
might_need_space = false;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
if (mapping_token) {
|
|
|
|
mappings.push({
|
|
|
|
token: mapping_token,
|
|
|
|
name: mapping_name,
|
|
|
|
line: current_line,
|
|
|
|
col: current_col
|
|
|
|
});
|
|
|
|
mapping_token = false;
|
2018-09-20 02:56:13 +02:00
|
|
|
if (line_fixed) flush_mappings();
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
OUTPUT += str;
|
2018-09-20 02:56:13 +02:00
|
|
|
has_parens = str[str.length - 1] == "(";
|
2017-05-03 15:35:00 +02:00
|
|
|
current_pos += str.length;
|
|
|
|
var a = str.split(/\r?\n/), n = a.length - 1;
|
|
|
|
current_line += n;
|
|
|
|
current_col += a[0].length;
|
|
|
|
if (n > 0) {
|
2018-09-20 02:56:13 +02:00
|
|
|
fix_line();
|
2017-05-03 15:35:00 +02:00
|
|
|
current_col = a[n].length;
|
|
|
|
}
|
|
|
|
last = str;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
var space = options.beautify ? function() {
|
|
|
|
print(" ");
|
|
|
|
} : function() {
|
|
|
|
might_need_space = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var indent = options.beautify ? function(half) {
|
|
|
|
if (options.beautify) {
|
|
|
|
print(make_indent(half ? 0.5 : 0));
|
|
|
|
}
|
|
|
|
} : noop;
|
|
|
|
|
|
|
|
var with_indent = options.beautify ? function(col, cont) {
|
|
|
|
if (col === true) col = next_indent();
|
|
|
|
var save_indentation = indentation;
|
|
|
|
indentation = col;
|
|
|
|
var ret = cont();
|
|
|
|
indentation = save_indentation;
|
|
|
|
return ret;
|
|
|
|
} : function(col, cont) { return cont() };
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var may_add_newline = options.max_line_len || options.preserve_line ? function() {
|
|
|
|
fix_line();
|
|
|
|
line_end = OUTPUT.length;
|
|
|
|
line_fixed = false;
|
2017-05-03 15:35:00 +02:00
|
|
|
} : noop;
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var newline = options.beautify ? function() {
|
|
|
|
if (newline_insert < 0) return print("\n");
|
|
|
|
if (OUTPUT[newline_insert] != "\n") {
|
|
|
|
OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
|
|
|
|
current_pos++;
|
|
|
|
current_line++;
|
|
|
|
}
|
|
|
|
newline_insert++;
|
|
|
|
} : may_add_newline;
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
var semicolon = options.beautify ? function() {
|
|
|
|
print(";");
|
|
|
|
} : function() {
|
|
|
|
might_need_semicolon = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
function force_semicolon() {
|
|
|
|
might_need_semicolon = false;
|
|
|
|
print(";");
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function next_indent() {
|
|
|
|
return indentation + options.indent_level;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function with_block(cont) {
|
|
|
|
var ret;
|
|
|
|
print("{");
|
|
|
|
newline();
|
2018-09-20 02:56:13 +02:00
|
|
|
with_indent(next_indent(), function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
ret = cont();
|
|
|
|
});
|
|
|
|
indent();
|
|
|
|
print("}");
|
|
|
|
return ret;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function with_parens(cont) {
|
|
|
|
print("(");
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
//XXX: still nice to have that for argument lists
|
|
|
|
//var ret = with_indent(current_col, cont);
|
|
|
|
var ret = cont();
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
print(")");
|
|
|
|
return ret;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function with_square(cont) {
|
|
|
|
print("[");
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
//var ret = with_indent(current_col, cont);
|
|
|
|
var ret = cont();
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
print("]");
|
|
|
|
return ret;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function comma() {
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
print(",");
|
2018-09-20 02:56:13 +02:00
|
|
|
may_add_newline();
|
2017-05-03 15:35:00 +02:00
|
|
|
space();
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function colon() {
|
|
|
|
print(":");
|
2017-08-14 05:01:11 +02:00
|
|
|
space();
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
var add_mapping = mappings ? function(token, name) {
|
|
|
|
mapping_token = token;
|
|
|
|
mapping_name = name;
|
2017-05-03 15:35:00 +02:00
|
|
|
} : noop;
|
|
|
|
|
|
|
|
function get() {
|
2018-09-20 02:56:13 +02:00
|
|
|
if (!line_fixed) fix_line();
|
2017-05-03 15:35:00 +02:00
|
|
|
return OUTPUT;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function has_nlb() {
|
|
|
|
var index = OUTPUT.lastIndexOf("\n");
|
|
|
|
return /^ *$/.test(OUTPUT.slice(index + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepend_comments(node) {
|
|
|
|
var self = this;
|
|
|
|
var start = node.start;
|
|
|
|
if (!start) return;
|
|
|
|
if (start.comments_before && start.comments_before._dumped === self) return;
|
|
|
|
var comments = start.comments_before;
|
|
|
|
if (!comments) {
|
|
|
|
comments = start.comments_before = [];
|
|
|
|
}
|
|
|
|
comments._dumped = self;
|
|
|
|
|
|
|
|
if (node instanceof AST_Exit && node.value) {
|
|
|
|
var tw = new TreeWalker(function(node) {
|
|
|
|
var parent = tw.parent();
|
|
|
|
if (parent instanceof AST_Exit
|
|
|
|
|| parent instanceof AST_Binary && parent.left === node
|
|
|
|
|| parent.TYPE == "Call" && parent.expression === node
|
|
|
|
|| parent instanceof AST_Conditional && parent.condition === node
|
|
|
|
|| parent instanceof AST_Dot && parent.expression === node
|
|
|
|
|| parent instanceof AST_Sequence && parent.expressions[0] === node
|
|
|
|
|| parent instanceof AST_Sub && parent.expression === node
|
|
|
|
|| parent instanceof AST_UnaryPostfix) {
|
|
|
|
var text = node.start.comments_before;
|
|
|
|
if (text && text._dumped !== self) {
|
|
|
|
text._dumped = self;
|
|
|
|
comments = comments.concat(text);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tw.push(node);
|
|
|
|
node.value.walk(tw);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_pos == 0) {
|
|
|
|
if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
|
|
|
|
print("#!" + comments.shift().value + "\n");
|
|
|
|
indent();
|
|
|
|
}
|
|
|
|
var preamble = options.preamble;
|
|
|
|
if (preamble) {
|
|
|
|
print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comments = comments.filter(comment_filter, node);
|
|
|
|
if (comments.length == 0) return;
|
|
|
|
var last_nlb = has_nlb();
|
|
|
|
comments.forEach(function(c, i) {
|
|
|
|
if (!last_nlb) {
|
|
|
|
if (c.nlb) {
|
|
|
|
print("\n");
|
|
|
|
indent();
|
|
|
|
last_nlb = true;
|
|
|
|
} else if (i > 0) {
|
|
|
|
space();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (/comment[134]/.test(c.type)) {
|
|
|
|
print("//" + c.value.replace(/[@#]__PURE__/g, ' ') + "\n");
|
|
|
|
indent();
|
|
|
|
last_nlb = true;
|
|
|
|
} else if (c.type == "comment2") {
|
|
|
|
print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
|
|
|
|
last_nlb = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!last_nlb) {
|
|
|
|
if (start.nlb) {
|
|
|
|
print("\n");
|
|
|
|
indent();
|
|
|
|
} else {
|
|
|
|
space();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function append_comments(node, tail) {
|
|
|
|
var self = this;
|
|
|
|
var token = node.end;
|
|
|
|
if (!token) return;
|
|
|
|
var comments = token[tail ? "comments_before" : "comments_after"];
|
|
|
|
if (!comments || comments._dumped === self) return;
|
|
|
|
if (!(node instanceof AST_Statement || all(comments, function(c) {
|
|
|
|
return !/comment[134]/.test(c.type);
|
|
|
|
}))) return;
|
|
|
|
comments._dumped = self;
|
|
|
|
var insert = OUTPUT.length;
|
|
|
|
comments.filter(comment_filter, node).forEach(function(c, i) {
|
|
|
|
need_space = false;
|
|
|
|
if (need_newline_indented) {
|
|
|
|
print("\n");
|
|
|
|
indent();
|
|
|
|
need_newline_indented = false;
|
|
|
|
} else if (c.nlb && (i > 0 || !has_nlb())) {
|
|
|
|
print("\n");
|
|
|
|
indent();
|
|
|
|
} else if (i > 0 || !tail) {
|
|
|
|
space();
|
|
|
|
}
|
|
|
|
if (/comment[134]/.test(c.type)) {
|
|
|
|
print("//" + c.value.replace(/[@#]__PURE__/g, ' '));
|
|
|
|
need_newline_indented = true;
|
|
|
|
} else if (c.type == "comment2") {
|
|
|
|
print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
|
|
|
|
need_space = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (OUTPUT.length > insert) newline_insert = insert;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
var stack = [];
|
|
|
|
return {
|
|
|
|
get : get,
|
|
|
|
toString : get,
|
|
|
|
indent : indent,
|
|
|
|
indentation : function() { return indentation },
|
|
|
|
current_width : function() { return current_col - indentation },
|
|
|
|
should_break : function() { return options.width && this.current_width() >= options.width },
|
2018-09-20 02:56:13 +02:00
|
|
|
has_parens : function() { return has_parens },
|
2017-05-03 15:35:00 +02:00
|
|
|
newline : newline,
|
|
|
|
print : print,
|
|
|
|
space : space,
|
|
|
|
comma : comma,
|
|
|
|
colon : colon,
|
|
|
|
last : function() { return last },
|
|
|
|
semicolon : semicolon,
|
|
|
|
force_semicolon : force_semicolon,
|
2017-08-14 05:01:11 +02:00
|
|
|
to_utf8 : to_utf8,
|
2017-05-03 15:35:00 +02:00
|
|
|
print_name : function(name) { print(make_name(name)) },
|
|
|
|
print_string : function(str, quote, escape_directive) {
|
|
|
|
var encoded = encode_string(str, quote);
|
|
|
|
if (escape_directive === true && encoded.indexOf("\\") === -1) {
|
|
|
|
// Insert semicolons to break directive prologue
|
|
|
|
if (!EXPECT_DIRECTIVE.test(OUTPUT)) {
|
|
|
|
force_semicolon();
|
|
|
|
}
|
|
|
|
force_semicolon();
|
|
|
|
}
|
|
|
|
print(encoded);
|
|
|
|
},
|
|
|
|
encode_string : encode_string,
|
|
|
|
next_indent : next_indent,
|
|
|
|
with_indent : with_indent,
|
|
|
|
with_block : with_block,
|
|
|
|
with_parens : with_parens,
|
|
|
|
with_square : with_square,
|
|
|
|
add_mapping : add_mapping,
|
|
|
|
option : function(opt) { return options[opt] },
|
2018-09-20 02:56:13 +02:00
|
|
|
prepend_comments: readonly ? noop : prepend_comments,
|
|
|
|
append_comments : readonly || comment_filter === return_false ? noop : append_comments,
|
2017-05-03 15:35:00 +02:00
|
|
|
line : function() { return current_line },
|
|
|
|
col : function() { return current_col },
|
|
|
|
pos : function() { return current_pos },
|
|
|
|
push_node : function(node) { stack.push(node) },
|
2018-09-20 02:56:13 +02:00
|
|
|
pop_node : options.preserve_line ? function() {
|
|
|
|
var node = stack.pop();
|
|
|
|
if (node.start && node.start.line > current_line) {
|
|
|
|
insert_newlines(node.start.line - current_line);
|
|
|
|
}
|
|
|
|
} : function() {
|
|
|
|
stack.pop();
|
|
|
|
},
|
2017-05-03 15:35:00 +02:00
|
|
|
parent : function(n) {
|
|
|
|
return stack[stack.length - 2 - (n || 0)];
|
|
|
|
}
|
|
|
|
};
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
/* -----[ code generators ]----- */
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
/* -----[ utils ]----- */
|
|
|
|
|
|
|
|
function DEFPRINT(nodetype, generator) {
|
|
|
|
nodetype.DEFMETHOD("_codegen", generator);
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
var in_directive = false;
|
2017-10-14 18:40:54 +02:00
|
|
|
var active_scope = null;
|
|
|
|
var use_asm = null;
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Node.DEFMETHOD("print", function(stream, force_parens) {
|
2017-10-14 18:40:54 +02:00
|
|
|
var self = this, generator = self._codegen;
|
|
|
|
if (self instanceof AST_Scope) {
|
|
|
|
active_scope = self;
|
|
|
|
}
|
|
|
|
else if (!use_asm && self instanceof AST_Directive && self.value == "use asm") {
|
|
|
|
use_asm = active_scope;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
function doit() {
|
2018-09-20 02:56:13 +02:00
|
|
|
stream.prepend_comments(self);
|
2017-05-03 15:35:00 +02:00
|
|
|
self.add_source_map(stream);
|
|
|
|
generator(self, stream);
|
2018-09-20 02:56:13 +02:00
|
|
|
stream.append_comments(self);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
stream.push_node(self);
|
|
|
|
if (force_parens || self.needs_parens(stream)) {
|
|
|
|
stream.with_parens(doit);
|
|
|
|
} else {
|
|
|
|
doit();
|
|
|
|
}
|
|
|
|
stream.pop_node();
|
2017-10-14 18:40:54 +02:00
|
|
|
if (self === use_asm) {
|
|
|
|
use_asm = null;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Node.DEFMETHOD("print_to_string", function(options) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var s = OutputStream(options);
|
|
|
|
this.print(s);
|
|
|
|
return s.get();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ PARENTHESES ]----- */
|
|
|
|
|
|
|
|
function PARENS(nodetype, func) {
|
|
|
|
if (Array.isArray(nodetype)) {
|
2018-09-20 02:56:13 +02:00
|
|
|
nodetype.forEach(function(nodetype) {
|
2017-05-03 15:35:00 +02:00
|
|
|
PARENS(nodetype, func);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
nodetype.DEFMETHOD("needs_parens", func);
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Node, return_false);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// a function expression needs parens around it when it's provably
|
|
|
|
// the first token to appear in a statement.
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Function, function(output) {
|
|
|
|
if (!output.has_parens() && first_in_statement(output)) return true;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (output.option('webkit')) {
|
|
|
|
var p = output.parent();
|
2018-09-20 02:56:13 +02:00
|
|
|
if (p instanceof AST_PropAccess && p.expression === this) return true;
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
if (output.option('wrap_iife')) {
|
|
|
|
var p = output.parent();
|
2018-09-20 02:56:13 +02:00
|
|
|
if (p instanceof AST_Call && p.expression === this) return true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// same goes for an object literal, because otherwise it would be
|
|
|
|
// interpreted as a block of code.
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Object, function(output) {
|
|
|
|
return !output.has_parens() && first_in_statement(output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Unary, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
return p instanceof AST_PropAccess && p.expression === this
|
|
|
|
|| p instanceof AST_Call && p.expression === this;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Sequence, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
|
|
|
|
|| p instanceof AST_Unary // !(foo, bar, baz)
|
|
|
|
|| p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 8
|
|
|
|
|| p instanceof AST_VarDef // var a = (1, 2), b = a + a; ==> b == 4
|
|
|
|
|| p instanceof AST_PropAccess // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
|
|
|
|
|| p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
|
|
|
|
|| p instanceof AST_ObjectProperty // { foo: (1, 2) }.foo ==> 2
|
|
|
|
|| p instanceof AST_Conditional /* (false, true) ? (a = 10, b = 20) : (c = 30)
|
|
|
|
* ==> 20 (side effect, set a := 10 and b := 20) */
|
|
|
|
;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Binary, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
// (foo && bar)()
|
|
|
|
if (p instanceof AST_Call && p.expression === this)
|
|
|
|
return true;
|
|
|
|
// typeof (foo && bar)
|
|
|
|
if (p instanceof AST_Unary)
|
|
|
|
return true;
|
|
|
|
// (foo && bar)["prop"], (foo && bar).prop
|
|
|
|
if (p instanceof AST_PropAccess && p.expression === this)
|
|
|
|
return true;
|
|
|
|
// this deals with precedence: 3 * (2 + 1)
|
|
|
|
if (p instanceof AST_Binary) {
|
|
|
|
var po = p.operator, pp = PRECEDENCE[po];
|
|
|
|
var so = this.operator, sp = PRECEDENCE[so];
|
|
|
|
if (pp > sp
|
|
|
|
|| (pp == sp
|
|
|
|
&& this === p.right)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_PropAccess, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
if (p instanceof AST_New && p.expression === this) {
|
|
|
|
// i.e. new (foo.bar().baz)
|
|
|
|
//
|
|
|
|
// if there's one call into this subtree, then we need
|
|
|
|
// parens around it too, otherwise the call will be
|
|
|
|
// interpreted as passing the arguments to the upper New
|
|
|
|
// expression.
|
2017-08-14 05:01:11 +02:00
|
|
|
var parens = false;
|
|
|
|
this.walk(new TreeWalker(function(node) {
|
|
|
|
if (parens || node instanceof AST_Scope) return true;
|
|
|
|
if (node instanceof AST_Call) {
|
|
|
|
parens = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return parens;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Call, function(output) {
|
|
|
|
var p = output.parent();
|
|
|
|
if (p instanceof AST_New && p.expression === this) return true;
|
2017-05-03 15:35:00 +02:00
|
|
|
// https://bugs.webkit.org/show_bug.cgi?id=123506
|
2018-09-20 02:56:13 +02:00
|
|
|
if (output.option('webkit')) {
|
|
|
|
var g = output.parent(1);
|
|
|
|
return this.expression instanceof AST_Function
|
|
|
|
&& p instanceof AST_PropAccess
|
|
|
|
&& p.expression === this
|
|
|
|
&& g instanceof AST_Assign
|
|
|
|
&& g.left === p;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_New, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
if (!need_constructor_parens(this, output)
|
|
|
|
&& (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
|
|
|
|
|| p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS(AST_Number, function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
if (p instanceof AST_PropAccess && p.expression === this) {
|
|
|
|
var value = this.getValue();
|
|
|
|
if (value < 0 || /^0/.test(make_num(value))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
PARENS([ AST_Assign, AST_Conditional ], function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var p = output.parent();
|
|
|
|
// !(a = false) → true
|
|
|
|
if (p instanceof AST_Unary)
|
|
|
|
return true;
|
|
|
|
// 1 + (a = 2) + 3 → 6, side effect setting a = 2
|
|
|
|
if (p instanceof AST_Binary && !(p instanceof AST_Assign))
|
|
|
|
return true;
|
|
|
|
// (a = func)() —or— new (a = Object)()
|
|
|
|
if (p instanceof AST_Call && p.expression === this)
|
|
|
|
return true;
|
|
|
|
// (a = foo) ? bar : baz
|
|
|
|
if (p instanceof AST_Conditional && p.condition === this)
|
|
|
|
return true;
|
|
|
|
// (a = foo)["prop"] —or— (a = foo).prop
|
|
|
|
if (p instanceof AST_PropAccess && p.expression === this)
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ PRINTERS ]----- */
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Directive, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print_string(self.value, self.quote);
|
|
|
|
output.semicolon();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Debugger, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("debugger");
|
|
|
|
output.semicolon();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ statements ]----- */
|
|
|
|
|
|
|
|
function display_body(body, is_toplevel, output, allow_directives) {
|
|
|
|
var last = body.length - 1;
|
|
|
|
in_directive = allow_directives;
|
2018-09-20 02:56:13 +02:00
|
|
|
body.forEach(function(stmt, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (in_directive === true && !(stmt instanceof AST_Directive ||
|
|
|
|
stmt instanceof AST_EmptyStatement ||
|
|
|
|
(stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
|
|
|
|
)) {
|
|
|
|
in_directive = false;
|
|
|
|
}
|
|
|
|
if (!(stmt instanceof AST_EmptyStatement)) {
|
|
|
|
output.indent();
|
|
|
|
stmt.print(output);
|
|
|
|
if (!(i == last && is_toplevel)) {
|
|
|
|
output.newline();
|
|
|
|
if (is_toplevel) output.newline();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_directive === true &&
|
|
|
|
stmt instanceof AST_SimpleStatement &&
|
|
|
|
stmt.body instanceof AST_String
|
|
|
|
) {
|
|
|
|
in_directive = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
in_directive = false;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
force_statement(this.body, output);
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Statement, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.body.print(output);
|
|
|
|
output.semicolon();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Toplevel, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
display_body(self.body, true, output, true);
|
|
|
|
output.print("");
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_LabeledStatement, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.label.print(output);
|
|
|
|
output.colon();
|
|
|
|
self.body.print(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_SimpleStatement, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.body.print(output);
|
|
|
|
output.semicolon();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
function print_braced_empty(self, output) {
|
|
|
|
output.print("{");
|
|
|
|
output.with_indent(output.next_indent(), function() {
|
|
|
|
output.append_comments(self, true);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
output.print("}");
|
|
|
|
}
|
|
|
|
function print_braced(self, output, allow_directives) {
|
|
|
|
if (self.body.length > 0) {
|
|
|
|
output.with_block(function() {
|
|
|
|
display_body(self.body, false, output, allow_directives);
|
|
|
|
});
|
|
|
|
} else print_braced_empty(self, output);
|
|
|
|
}
|
|
|
|
DEFPRINT(AST_BlockStatement, function(self, output) {
|
|
|
|
print_braced(self, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_EmptyStatement, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.semicolon();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Do, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("do");
|
|
|
|
output.space();
|
|
|
|
make_block(self.body, output);
|
|
|
|
output.space();
|
|
|
|
output.print("while");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.condition.print(output);
|
|
|
|
});
|
|
|
|
output.semicolon();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_While, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("while");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.condition.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_For, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("for");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (self.init) {
|
|
|
|
if (self.init instanceof AST_Definitions) {
|
|
|
|
self.init.print(output);
|
|
|
|
} else {
|
|
|
|
parenthesize_for_noin(self.init, output, true);
|
|
|
|
}
|
|
|
|
output.print(";");
|
|
|
|
output.space();
|
|
|
|
} else {
|
|
|
|
output.print(";");
|
|
|
|
}
|
|
|
|
if (self.condition) {
|
|
|
|
self.condition.print(output);
|
|
|
|
output.print(";");
|
|
|
|
output.space();
|
|
|
|
} else {
|
|
|
|
output.print(";");
|
|
|
|
}
|
|
|
|
if (self.step) {
|
|
|
|
self.step.print(output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_ForIn, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("for");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.init.print(output);
|
|
|
|
output.space();
|
|
|
|
output.print("in");
|
|
|
|
output.space();
|
|
|
|
self.object.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_With, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("with");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.expression.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ functions ]----- */
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var self = this;
|
|
|
|
if (!nokeyword) {
|
|
|
|
output.print("function");
|
|
|
|
}
|
|
|
|
if (self.name) {
|
|
|
|
output.space();
|
|
|
|
self.name.print(output);
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
|
|
|
self.argnames.forEach(function(arg, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (i) output.comma();
|
|
|
|
arg.print(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
print_braced(self, output, true);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Lambda, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self._do_print(output);
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
/* -----[ jumps ]----- */
|
|
|
|
function print_jump(output, kind, target) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print(kind);
|
2018-09-20 02:56:13 +02:00
|
|
|
if (target) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
target.print(output);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
output.semicolon();
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Return, function(self, output) {
|
|
|
|
print_jump(output, "return", self.value);
|
|
|
|
});
|
|
|
|
DEFPRINT(AST_Throw, function(self, output) {
|
|
|
|
print_jump(output, "throw", self.value);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Break, function(self, output) {
|
|
|
|
print_jump(output, "break", self.label);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Continue, function(self, output) {
|
|
|
|
print_jump(output, "continue", self.label);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ if ]----- */
|
|
|
|
function make_then(self, output) {
|
|
|
|
var b = self.body;
|
2018-09-20 02:56:13 +02:00
|
|
|
if (output.option("braces")
|
2017-08-14 05:01:11 +02:00
|
|
|
|| output.option("ie8") && b instanceof AST_Do)
|
2017-05-03 15:35:00 +02:00
|
|
|
return make_block(b, output);
|
|
|
|
// The squeezer replaces "block"-s that contain only a single
|
|
|
|
// statement with the statement itself; technically, the AST
|
|
|
|
// is correct, but this can create problems when we output an
|
|
|
|
// IF having an ELSE clause where the THEN clause ends in an
|
|
|
|
// IF *without* an ELSE block (then the outer ELSE would refer
|
|
|
|
// to the inner IF). This function checks for this case and
|
2018-09-20 02:56:13 +02:00
|
|
|
// adds the block braces if needed.
|
2017-05-03 15:35:00 +02:00
|
|
|
if (!b) return output.force_semicolon();
|
|
|
|
while (true) {
|
|
|
|
if (b instanceof AST_If) {
|
|
|
|
if (!b.alternative) {
|
|
|
|
make_block(self.body, output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
b = b.alternative;
|
|
|
|
}
|
|
|
|
else if (b instanceof AST_StatementWithBody) {
|
|
|
|
b = b.body;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
force_statement(self.body, output);
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
|
|
|
DEFPRINT(AST_If, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("if");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.condition.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
if (self.alternative) {
|
|
|
|
make_then(self, output);
|
|
|
|
output.space();
|
|
|
|
output.print("else");
|
|
|
|
output.space();
|
|
|
|
if (self.alternative instanceof AST_If)
|
|
|
|
self.alternative.print(output);
|
|
|
|
else
|
|
|
|
force_statement(self.alternative, output);
|
|
|
|
} else {
|
|
|
|
self._do_print_body(output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ switch ]----- */
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Switch, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("switch");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.expression.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
|
|
|
var last = self.body.length - 1;
|
2018-09-20 02:56:13 +02:00
|
|
|
if (last < 0) print_braced_empty(self, output);
|
|
|
|
else output.with_block(function() {
|
|
|
|
self.body.forEach(function(branch, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.indent(true);
|
|
|
|
branch.print(output);
|
|
|
|
if (i < last && branch.body.length > 0)
|
|
|
|
output.newline();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.newline();
|
2018-09-20 02:56:13 +02:00
|
|
|
this.body.forEach(function(stmt) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.indent();
|
|
|
|
stmt.print(output);
|
|
|
|
output.newline();
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Default, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("default:");
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Case, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("case");
|
|
|
|
output.space();
|
|
|
|
self.expression.print(output);
|
|
|
|
output.print(":");
|
|
|
|
self._do_print_body(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ exceptions ]----- */
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Try, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("try");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
print_braced(self, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
if (self.bcatch) {
|
|
|
|
output.space();
|
|
|
|
self.bcatch.print(output);
|
|
|
|
}
|
|
|
|
if (self.bfinally) {
|
|
|
|
output.space();
|
|
|
|
self.bfinally.print(output);
|
|
|
|
}
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Catch, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("catch");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.argname.print(output);
|
|
|
|
});
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
print_braced(self, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Finally, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("finally");
|
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
print_braced(self, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Var, function(self, output) {
|
|
|
|
output.print("var");
|
2017-05-03 15:35:00 +02:00
|
|
|
output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
self.definitions.forEach(function(def, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (i) output.comma();
|
|
|
|
def.print(output);
|
|
|
|
});
|
|
|
|
var p = output.parent();
|
2018-09-20 02:56:13 +02:00
|
|
|
if (p && p.init !== self || !(p instanceof AST_For || p instanceof AST_ForIn)) output.semicolon();
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function parenthesize_for_noin(node, output, noin) {
|
2017-08-14 05:01:11 +02:00
|
|
|
var parens = false;
|
|
|
|
// need to take some precautions here:
|
|
|
|
// https://github.com/mishoo/UglifyJS2/issues/60
|
|
|
|
if (noin) node.walk(new TreeWalker(function(node) {
|
|
|
|
if (parens || node instanceof AST_Scope) return true;
|
|
|
|
if (node instanceof AST_Binary && node.operator == "in") {
|
|
|
|
parens = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
node.print(output, parens);
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_VarDef, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.name.print(output);
|
|
|
|
if (self.value) {
|
|
|
|
output.space();
|
|
|
|
output.print("=");
|
|
|
|
output.space();
|
|
|
|
var p = output.parent(1);
|
|
|
|
var noin = p instanceof AST_For || p instanceof AST_ForIn;
|
|
|
|
parenthesize_for_noin(self.value, output, noin);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ other expressions ]----- */
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Call, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.expression.print(output);
|
|
|
|
if (self instanceof AST_New && !need_constructor_parens(self, output))
|
|
|
|
return;
|
2017-10-14 18:40:54 +02:00
|
|
|
if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
|
2017-08-14 05:01:11 +02:00
|
|
|
output.add_mapping(self.start);
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
output.with_parens(function() {
|
|
|
|
self.args.forEach(function(expr, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (i) output.comma();
|
|
|
|
expr.print(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_New, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("new");
|
|
|
|
output.space();
|
|
|
|
AST_Call.prototype._codegen(self, output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Sequence, function(self, output) {
|
|
|
|
self.expressions.forEach(function(node, index) {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (index > 0) {
|
|
|
|
output.comma();
|
|
|
|
if (output.should_break()) {
|
|
|
|
output.newline();
|
|
|
|
output.indent();
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
node.print(output);
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Dot, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var expr = self.expression;
|
|
|
|
expr.print(output);
|
2017-08-14 05:01:11 +02:00
|
|
|
var prop = self.property;
|
2018-09-20 02:56:13 +02:00
|
|
|
if (output.option("ie8") && RESERVED_WORDS[prop]) {
|
2017-08-14 05:01:11 +02:00
|
|
|
output.print("[");
|
|
|
|
output.add_mapping(self.end);
|
|
|
|
output.print_string(prop);
|
|
|
|
output.print("]");
|
|
|
|
} else {
|
|
|
|
if (expr instanceof AST_Number && expr.getValue() >= 0) {
|
|
|
|
if (!/[xa-f.)]/i.test(output.last())) {
|
|
|
|
output.print(".");
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
output.print(".");
|
|
|
|
// the name after dot would be mapped about here.
|
|
|
|
output.add_mapping(self.end);
|
|
|
|
output.print_name(prop);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Sub, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.expression.print(output);
|
|
|
|
output.print("[");
|
|
|
|
self.property.print(output);
|
|
|
|
output.print("]");
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_UnaryPrefix, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var op = self.operator;
|
|
|
|
output.print(op);
|
|
|
|
if (/^[a-z]/i.test(op)
|
|
|
|
|| (/[+-]$/.test(op)
|
|
|
|
&& self.expression instanceof AST_UnaryPrefix
|
|
|
|
&& /^[+-]/.test(self.expression.operator))) {
|
|
|
|
output.space();
|
|
|
|
}
|
|
|
|
self.expression.print(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_UnaryPostfix, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.expression.print(output);
|
|
|
|
output.print(self.operator);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Binary, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var op = self.operator;
|
|
|
|
self.left.print(output);
|
|
|
|
if (op[0] == ">" /* ">>" ">>>" ">" ">=" */
|
|
|
|
&& self.left instanceof AST_UnaryPostfix
|
|
|
|
&& self.left.operator == "--") {
|
|
|
|
// space is mandatory to avoid outputting -->
|
|
|
|
output.print(" ");
|
|
|
|
} else {
|
|
|
|
// the space is optional depending on "beautify"
|
|
|
|
output.space();
|
|
|
|
}
|
|
|
|
output.print(op);
|
|
|
|
if ((op == "<" || op == "<<")
|
|
|
|
&& self.right instanceof AST_UnaryPrefix
|
|
|
|
&& self.right.operator == "!"
|
|
|
|
&& self.right.expression instanceof AST_UnaryPrefix
|
|
|
|
&& self.right.expression.operator == "--") {
|
|
|
|
// space is mandatory to avoid outputting <!--
|
|
|
|
output.print(" ");
|
|
|
|
} else {
|
|
|
|
// the space is optional depending on "beautify"
|
|
|
|
output.space();
|
|
|
|
}
|
|
|
|
self.right.print(output);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Conditional, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
self.condition.print(output);
|
|
|
|
output.space();
|
|
|
|
output.print("?");
|
|
|
|
output.space();
|
|
|
|
self.consequent.print(output);
|
|
|
|
output.space();
|
|
|
|
output.colon();
|
|
|
|
self.alternative.print(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* -----[ literals ]----- */
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Array, function(self, output) {
|
|
|
|
output.with_square(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
var a = self.elements, len = a.length;
|
|
|
|
if (len > 0) output.space();
|
2018-09-20 02:56:13 +02:00
|
|
|
a.forEach(function(exp, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (i) output.comma();
|
|
|
|
exp.print(output);
|
|
|
|
// If the final element is a hole, we need to make sure it
|
|
|
|
// doesn't look like a trailing comma, by inserting an actual
|
|
|
|
// trailing comma.
|
|
|
|
if (i === len - 1 && exp instanceof AST_Hole)
|
|
|
|
output.comma();
|
|
|
|
});
|
|
|
|
if (len > 0) output.space();
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Object, function(self, output) {
|
|
|
|
if (self.properties.length > 0) output.with_block(function() {
|
|
|
|
self.properties.forEach(function(prop, i) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (i) {
|
|
|
|
output.print(",");
|
|
|
|
output.newline();
|
|
|
|
}
|
|
|
|
output.indent();
|
|
|
|
prop.print(output);
|
|
|
|
});
|
|
|
|
output.newline();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
else print_braced_empty(self, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
function print_property_name(key, quote, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (output.option("quote_keys")) {
|
2018-09-20 02:56:13 +02:00
|
|
|
output.print_string(key);
|
|
|
|
} else if ("" + +key == key && key >= 0) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print(make_num(key));
|
2018-09-20 02:56:13 +02:00
|
|
|
} else if (RESERVED_WORDS[key] ? !output.option("ie8") : is_identifier_string(key)) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (quote && output.option("keep_quoted_props")) {
|
|
|
|
output.print_string(key, quote);
|
|
|
|
} else {
|
|
|
|
output.print_name(key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output.print_string(key, quote);
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_ObjectKeyVal, function(self, output) {
|
2017-08-14 05:01:11 +02:00
|
|
|
print_property_name(self.key, self.quote, output);
|
2017-05-03 15:35:00 +02:00
|
|
|
output.colon();
|
|
|
|
self.value.print(output);
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
|
|
|
|
output.print(type);
|
2017-05-03 15:35:00 +02:00
|
|
|
output.space();
|
2017-08-14 05:01:11 +02:00
|
|
|
print_property_name(this.key.name, this.quote, output);
|
|
|
|
this.value._do_print(output, true);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_ObjectSetter, function(self, output) {
|
2017-08-14 05:01:11 +02:00
|
|
|
self._print_getter_setter("set", output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_ObjectGetter, function(self, output) {
|
2017-08-14 05:01:11 +02:00
|
|
|
self._print_getter_setter("get", output);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Symbol, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var def = self.definition();
|
|
|
|
output.print_name(def ? def.mangled_name || def.name : self.name);
|
|
|
|
});
|
|
|
|
DEFPRINT(AST_Hole, noop);
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_This, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print("this");
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Constant, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print(self.getValue());
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_String, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print_string(self.getValue(), self.quote, in_directive);
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_Number, function(self, output) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (use_asm && self.start && self.start.raw != null) {
|
|
|
|
output.print(self.start.raw);
|
|
|
|
} else {
|
|
|
|
output.print(make_num(self.getValue()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFPRINT(AST_RegExp, function(self, output) {
|
2017-08-14 05:01:11 +02:00
|
|
|
var regexp = self.getValue();
|
|
|
|
var str = regexp.toString();
|
|
|
|
if (regexp.raw_source) {
|
|
|
|
str = "/" + regexp.raw_source + str.slice(str.lastIndexOf("/"));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
str = output.to_utf8(str);
|
2017-05-03 15:35:00 +02:00
|
|
|
output.print(str);
|
|
|
|
var p = output.parent();
|
|
|
|
if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === self)
|
|
|
|
output.print(" ");
|
|
|
|
});
|
|
|
|
|
|
|
|
function force_statement(stat, output) {
|
2018-09-20 02:56:13 +02:00
|
|
|
if (output.option("braces")) {
|
2017-05-03 15:35:00 +02:00
|
|
|
make_block(stat, output);
|
|
|
|
} else {
|
|
|
|
if (!stat || stat instanceof AST_EmptyStatement)
|
|
|
|
output.force_semicolon();
|
|
|
|
else
|
|
|
|
stat.print(output);
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// self should be AST_New. decide if we want to show parens or not.
|
|
|
|
function need_constructor_parens(self, output) {
|
|
|
|
// Always print parentheses with arguments
|
|
|
|
if (self.args.length > 0) return true;
|
|
|
|
|
|
|
|
return output.option("beautify");
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function best_of(a) {
|
|
|
|
var best = a[0], len = best.length;
|
|
|
|
for (var i = 1; i < a.length; ++i) {
|
|
|
|
if (a[i].length < len) {
|
|
|
|
best = a[i];
|
|
|
|
len = best.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function make_num(num) {
|
2018-09-20 02:56:13 +02:00
|
|
|
var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
|
|
|
|
var candidates = [ str ];
|
2017-05-03 15:35:00 +02:00
|
|
|
if (Math.floor(num) === num) {
|
2018-09-20 02:56:13 +02:00
|
|
|
if (num < 0) {
|
|
|
|
candidates.push("-0x" + (-num).toString(16).toLowerCase());
|
2017-05-03 15:35:00 +02:00
|
|
|
} else {
|
2018-09-20 02:56:13 +02:00
|
|
|
candidates.push("0x" + num.toString(16).toLowerCase());
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
var match, len, digits;
|
|
|
|
if (match = /^\.0+/.exec(str)) {
|
|
|
|
len = match[0].length;
|
|
|
|
digits = str.slice(len);
|
|
|
|
candidates.push(digits + "e-" + (digits.length + len - 1));
|
|
|
|
} else if (match = /0+$/.exec(str)) {
|
|
|
|
len = match[0].length;
|
|
|
|
candidates.push(str.slice(0, -len) + "e" + len);
|
|
|
|
} else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
|
|
|
|
candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
|
|
|
|
}
|
|
|
|
return best_of(candidates);
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function make_block(stmt, output) {
|
|
|
|
if (!stmt || stmt instanceof AST_EmptyStatement)
|
|
|
|
output.print("{}");
|
|
|
|
else if (stmt instanceof AST_BlockStatement)
|
|
|
|
stmt.print(output);
|
2018-09-20 02:56:13 +02:00
|
|
|
else output.with_block(function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
output.indent();
|
|
|
|
stmt.print(output);
|
|
|
|
output.newline();
|
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
/* -----[ source map generators ]----- */
|
|
|
|
|
|
|
|
function DEFMAP(nodetype, generator) {
|
2018-09-20 02:56:13 +02:00
|
|
|
nodetype.forEach(function(nodetype) {
|
|
|
|
nodetype.DEFMETHOD("add_source_map", generator);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFMAP([
|
|
|
|
// We could easily add info for ALL nodes, but it seems to me that
|
|
|
|
// would be quite wasteful, hence this noop in the base class.
|
|
|
|
AST_Node,
|
|
|
|
// since the label symbol will mark it
|
|
|
|
AST_LabeledStatement,
|
|
|
|
AST_Toplevel,
|
|
|
|
], noop);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// XXX: I'm not exactly sure if we need it for all of these nodes,
|
|
|
|
// or if we should add even more.
|
2018-09-20 02:56:13 +02:00
|
|
|
DEFMAP([
|
|
|
|
AST_Array,
|
|
|
|
AST_BlockStatement,
|
|
|
|
AST_Catch,
|
|
|
|
AST_Constant,
|
|
|
|
AST_Debugger,
|
|
|
|
AST_Definitions,
|
|
|
|
AST_Directive,
|
|
|
|
AST_Finally,
|
|
|
|
AST_Jump,
|
|
|
|
AST_Lambda,
|
|
|
|
AST_New,
|
|
|
|
AST_Object,
|
|
|
|
AST_StatementWithBody,
|
|
|
|
AST_Symbol,
|
|
|
|
AST_Switch,
|
|
|
|
AST_SwitchBranch,
|
|
|
|
AST_Try,
|
|
|
|
], function(output) {
|
|
|
|
output.add_mapping(this.start);
|
|
|
|
});
|
|
|
|
|
|
|
|
DEFMAP([
|
|
|
|
AST_ObjectGetter,
|
|
|
|
AST_ObjectSetter,
|
|
|
|
], function(output) {
|
|
|
|
output.add_mapping(this.start, this.key.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
DEFMAP([ AST_ObjectProperty ], function(output) {
|
|
|
|
output.add_mapping(this.start, this.key);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
})();
|