Do not overwrite options.comments + cleanup

This commit is contained in:
Anthony Van de Gejuchte
2016-10-26 17:34:30 +02:00
committed by Richard van Velzen
parent 057de570e6
commit 79b98a9fe8
4 changed files with 63 additions and 52 deletions

View File

@@ -916,7 +916,7 @@ merge(Compressor.prototype, {
(function (def){ (function (def){
var unary_bool = [ "!", "delete" ]; var unary_bool = [ "!", "delete" ];
var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ]; var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];
def(AST_Node, function(){ return false }); def(AST_Node, return_false);
def(AST_UnaryPrefix, function(){ def(AST_UnaryPrefix, function(){
return member(this.operator, unary_bool); return member(this.operator, unary_bool);
}); });
@@ -934,16 +934,16 @@ merge(Compressor.prototype, {
def(AST_Seq, function(){ def(AST_Seq, function(){
return this.cdr.is_boolean(); return this.cdr.is_boolean();
}); });
def(AST_True, function(){ return true }); def(AST_True, return_true);
def(AST_False, function(){ return true }); def(AST_False, return_true);
})(function(node, func){ })(function(node, func){
node.DEFMETHOD("is_boolean", func); node.DEFMETHOD("is_boolean", func);
}); });
// methods to determine if an expression has a string result type // methods to determine if an expression has a string result type
(function (def){ (function (def){
def(AST_Node, function(){ return false }); def(AST_Node, return_false);
def(AST_String, function(){ return true }); def(AST_String, return_true);
def(AST_UnaryPrefix, function(){ def(AST_UnaryPrefix, function(){
return this.operator == "typeof"; return this.operator == "typeof";
}); });
@@ -1197,11 +1197,11 @@ merge(Compressor.prototype, {
// determine if expression has side effects // determine if expression has side effects
(function(def){ (function(def){
def(AST_Node, function(compressor){ return true }); def(AST_Node, return_true);
def(AST_EmptyStatement, function(compressor){ return false }); def(AST_EmptyStatement, return_false);
def(AST_Constant, function(compressor){ return false }); def(AST_Constant, return_false);
def(AST_This, function(compressor){ return false }); def(AST_This, return_false);
def(AST_Call, function(compressor){ def(AST_Call, function(compressor){
var pure = compressor.option("pure_funcs"); var pure = compressor.option("pure_funcs");
@@ -1221,13 +1221,13 @@ merge(Compressor.prototype, {
def(AST_SimpleStatement, function(compressor){ def(AST_SimpleStatement, function(compressor){
return this.body.has_side_effects(compressor); return this.body.has_side_effects(compressor);
}); });
def(AST_Defun, function(compressor){ return true }); def(AST_Defun, return_true);
def(AST_Function, function(compressor){ return false }); def(AST_Function, return_false);
def(AST_Binary, function(compressor){ def(AST_Binary, function(compressor){
return this.left.has_side_effects(compressor) return this.left.has_side_effects(compressor)
|| this.right.has_side_effects(compressor); || this.right.has_side_effects(compressor);
}); });
def(AST_Assign, function(compressor){ return true }); def(AST_Assign, return_true);
def(AST_Conditional, function(compressor){ def(AST_Conditional, function(compressor){
return this.condition.has_side_effects(compressor) return this.condition.has_side_effects(compressor)
|| this.consequent.has_side_effects(compressor) || this.consequent.has_side_effects(compressor)

View File

@@ -45,6 +45,20 @@
var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/; var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
function is_some_comments(comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
return type == "comment5";
}
function is_comment5(comment) {
return comment.type == "comment5";
}
function OutputStream(options) { function OutputStream(options) {
options = defaults(options, { options = defaults(options, {
@@ -72,46 +86,30 @@ function OutputStream(options) {
}, true); }, true);
// Convert comment option to RegExp if neccessary and set up comments filter // Convert comment option to RegExp if neccessary and set up comments filter
if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) { var comment_filter = options.shebang ? is_comment5 : return_false; // Default case, throw all comments away except shebangs
var regex_pos = options.comments.lastIndexOf("/"); if (options.comments) {
options.comments = new RegExp( var comments = options.comments;
options.comments.substr(1, regex_pos - 1), if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
options.comments.substr(regex_pos + 1) var regex_pos = options.comments.lastIndexOf("/");
); comments = new RegExp(
} options.comments.substr(1, regex_pos - 1),
if (options.comments instanceof RegExp) { options.comments.substr(regex_pos + 1)
options.comments = (function(f) { );
return function(comment) {
return comment.type == "comment5" || f.test(comment.value);
}
})(options.comments);
}
else if (typeof options.comments === "function") {
options.comments = (function(f) {
return function(comment) {
return comment.type == "comment5" || f(this, comment);
}
})(options.comments);
}
else if (options.comments === "some") {
options.comments = function(comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
return type == "comment5";
} }
} if (comments instanceof RegExp) {
else if (options.comments){ // NOTE includes "all" option comment_filter = function(comment) {
options.comments = function() { return comment.type == "comment5" || comments.test(comment.value);
return true; };
} }
} else { else if (typeof comments === "function") {
// Falsy case, so reject all comments, except shebangs comment_filter = function(comment) {
options.comments = function(comment) { return comment.type == "comment5" || comments(this, comment);
return comment.type == "comment5"; };
}
else if (comments === "some") {
comment_filter = is_some_comments;
} else { // NOTE includes "all" option
comment_filter = return_true;
} }
} }
@@ -421,6 +419,7 @@ function OutputStream(options) {
with_square : with_square, with_square : with_square,
add_mapping : add_mapping, add_mapping : add_mapping,
option : function(opt) { return options[opt] }, option : function(opt) { return options[opt] },
comment_filter : comment_filter,
line : function() { return current_line }, line : function() { return current_line },
col : function() { return current_col }, col : function() { return current_col },
pos : function() { return current_pos }, pos : function() { return current_pos },
@@ -503,7 +502,7 @@ function OutputStream(options) {
})); }));
} }
comments = comments.filter(output.option("comments"), self); comments = comments.filter(output.comment_filter, self);
// Keep single line comments after nlb, after nlb // Keep single line comments after nlb, after nlb
if (!output.option("beautify") && comments.length > 0 && if (!output.option("beautify") && comments.length > 0 &&

View File

@@ -112,6 +112,8 @@ function merge(obj, ext) {
}; };
function noop() {}; function noop() {};
function return_false() { return false; }
function return_true() { return true; }
var MAP = (function(){ var MAP = (function(){
function MAP(a, f, backwards) { function MAP(a, f, backwards) {

View File

@@ -2,7 +2,7 @@ var UglifyJS = require('../../');
var assert = require("assert"); var assert = require("assert");
describe("comment filters", function() { describe("comment filters", function() {
it("Should be able to filter comments by passing regex", function() { it("Should be able to filter comments by passing regexp", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8"); var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n"); assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
}); });
@@ -62,4 +62,14 @@ describe("comment filters", function() {
var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */"); var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */");
assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/\n"); assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/\n");
}); });
it("Should have no problem on multiple calls", function() {
const options = {
comments: /ok/
};
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
});
}); });