aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnthony Van de Gejuchte <anthonyvdgent@gmail.com>2016-10-26 17:34:30 +0200
committerRichard van Velzen <rvanvelzen1@gmail.com>2016-11-29 20:24:08 +0100
commit79b98a9fe87f950607c601a45a3566a46c32f425 (patch)
tree43bd4aa1d97e2ee3d6fb3246a8d84fa23b8e192d /lib
parent057de570e69b300ca472719cb234a32e593d34ec (diff)
downloadtracifyjs-79b98a9fe87f950607c601a45a3566a46c32f425.tar.gz
tracifyjs-79b98a9fe87f950607c601a45a3566a46c32f425.zip
Do not overwrite options.comments + cleanup
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js24
-rw-r--r--lib/output.js77
-rw-r--r--lib/utils.js2
3 files changed, 52 insertions, 51 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 1845717c..5879b93b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -916,7 +916,7 @@ merge(Compressor.prototype, {
(function (def){
var unary_bool = [ "!", "delete" ];
var binary_bool = [ "in", "instanceof", "==", "!=", "===", "!==", "<", "<=", ">=", ">" ];
- def(AST_Node, function(){ return false });
+ def(AST_Node, return_false);
def(AST_UnaryPrefix, function(){
return member(this.operator, unary_bool);
});
@@ -934,16 +934,16 @@ merge(Compressor.prototype, {
def(AST_Seq, function(){
return this.cdr.is_boolean();
});
- def(AST_True, function(){ return true });
- def(AST_False, function(){ return true });
+ def(AST_True, return_true);
+ def(AST_False, return_true);
})(function(node, func){
node.DEFMETHOD("is_boolean", func);
});
// methods to determine if an expression has a string result type
(function (def){
- def(AST_Node, function(){ return false });
- def(AST_String, function(){ return true });
+ def(AST_Node, return_false);
+ def(AST_String, return_true);
def(AST_UnaryPrefix, function(){
return this.operator == "typeof";
});
@@ -1197,11 +1197,11 @@ merge(Compressor.prototype, {
// determine if expression has side effects
(function(def){
- def(AST_Node, function(compressor){ return true });
+ def(AST_Node, return_true);
- def(AST_EmptyStatement, function(compressor){ return false });
- def(AST_Constant, function(compressor){ return false });
- def(AST_This, function(compressor){ return false });
+ def(AST_EmptyStatement, return_false);
+ def(AST_Constant, return_false);
+ def(AST_This, return_false);
def(AST_Call, function(compressor){
var pure = compressor.option("pure_funcs");
@@ -1221,13 +1221,13 @@ merge(Compressor.prototype, {
def(AST_SimpleStatement, function(compressor){
return this.body.has_side_effects(compressor);
});
- def(AST_Defun, function(compressor){ return true });
- def(AST_Function, function(compressor){ return false });
+ def(AST_Defun, return_true);
+ def(AST_Function, return_false);
def(AST_Binary, function(compressor){
return this.left.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){
return this.condition.has_side_effects(compressor)
|| this.consequent.has_side_effects(compressor)
diff --git a/lib/output.js b/lib/output.js
index 63a78c60..50e5aa43 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -45,6 +45,20 @@
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) {
options = defaults(options, {
@@ -72,46 +86,30 @@ function OutputStream(options) {
}, true);
// Convert comment option to RegExp if neccessary and set up comments filter
- if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
- var regex_pos = options.comments.lastIndexOf("/");
- options.comments = new RegExp(
- options.comments.substr(1, regex_pos - 1),
- options.comments.substr(regex_pos + 1)
- );
- }
- if (options.comments instanceof RegExp) {
- 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";
+ var comment_filter = options.shebang ? is_comment5 : return_false; // Default case, throw all comments away except shebangs
+ 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)
+ );
}
- }
- else if (options.comments){ // NOTE includes "all" option
- options.comments = function() {
- return true;
+ 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 {
- // Falsy case, so reject all comments, except shebangs
- options.comments = function(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,
add_mapping : add_mapping,
option : function(opt) { return options[opt] },
+ comment_filter : comment_filter,
line : function() { return current_line },
col : function() { return current_col },
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
if (!output.option("beautify") && comments.length > 0 &&
diff --git a/lib/utils.js b/lib/utils.js
index 8ef61936..d0a21ac9 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -112,6 +112,8 @@ function merge(obj, ext) {
};
function noop() {};
+function return_false() { return false; }
+function return_true() { return true; }
var MAP = (function(){
function MAP(a, f, backwards) {