minor tweaks (#3502)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 20 Oct 2019 07:19:19 +0000 (15:19 +0800)
committerGitHub <noreply@github.com>
Sun, 20 Oct 2019 07:19:19 +0000 (15:19 +0800)
lib/output.js
lib/parse.js
lib/utils.js
test/compress/reduce_vars.js

index b9d8a13..d59176c 100644 (file)
@@ -91,13 +91,11 @@ function OutputStream(options) {
             comment_filter = function(comment) {
                 return comment.type != "comment5" && comments.test(comment.value);
             };
-        }
-        else if (typeof comments === "function") {
+        } else if (typeof comments === "function") {
             comment_filter = function(comment) {
                 return comment.type != "comment5" && comments(this, comment);
             };
-        }
-        else if (comments === "some") {
+        } else if (comments === "some") {
             comment_filter = is_some_comments;
         } else { // NOTE includes "all" option
             comment_filter = return_true;
@@ -643,8 +641,7 @@ function OutputStream(options) {
         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") {
+        } else if (!use_asm && self instanceof AST_Directive && self.value == "use asm") {
             use_asm = active_scope;
         }
         function doit() {
@@ -1043,11 +1040,9 @@ function OutputStream(options) {
                     return;
                 }
                 b = b.alternative;
-            }
-            else if (b instanceof AST_StatementWithBody) {
+            } else if (b instanceof AST_StatementWithBody) {
                 b = b.body;
-            }
-            else break;
+            } else break;
         }
         force_statement(self.body, output);
     }
index b7639df..7321878 100644 (file)
@@ -234,6 +234,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
         directives      : {},
         directive_stack : []
     };
+    var prev_was_dot = false;
 
     function peek() {
         return S.text.charAt(S.pos);
@@ -286,16 +287,12 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
         S.tokpos = S.pos;
     }
 
-    var prev_was_dot = false;
     function token(type, value, is_comment) {
-        S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX[value]) ||
-                           (type == "keyword" && KEYWORDS_BEFORE_EXPRESSION[value]) ||
-                           (type == "punc" && PUNC_BEFORE_EXPRESSION[value]));
-        if (type == "punc" && value == ".") {
-            prev_was_dot = true;
-        } else if (!is_comment) {
-            prev_was_dot = false;
-        }
+        S.regex_allowed = type == "operator" && !UNARY_POSTFIX[value]
+            || type == "keyword" && KEYWORDS_BEFORE_EXPRESSION[value]
+            || type == "punc" && PUNC_BEFORE_EXPRESSION[value];
+        if (type == "punc" && value == ".") prev_was_dot = true;
+        else if (!is_comment) prev_was_dot = false;
         var ret = {
             type    : type,
             value   : value,
@@ -358,11 +355,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
             parse_error("Legacy octal literals are not allowed in strict mode");
         }
         var valid = parse_js_number(num);
-        if (!isNaN(valid)) {
-            return token("num", valid);
-        } else {
-            parse_error("Invalid syntax: " + num);
-        }
+        if (!isNaN(valid)) return token("num", valid);
+        parse_error("Invalid syntax: " + num);
     }
 
     function read_escaped_char(in_string) {
@@ -463,8 +457,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
                 if (ch == "\\") escaped = backslash = true, next();
                 else if (is_identifier_char(ch)) name += next();
                 else break;
-            }
-            else {
+            } else {
                 if (ch != "u") parse_error("Expecting UnicodeEscapeSequence -- uXXXX");
                 ch = read_escaped_char();
                 if (!is_identifier_char(ch)) parse_error("Unicode char: " + ch.charCodeAt(0) + " is not valid in identifier");
@@ -538,9 +531,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
 
     function handle_dot() {
         next();
-        return is_digit(peek().charCodeAt(0))
-            ? read_num(".")
-            : token("punc", ".");
+        return is_digit(peek().charCodeAt(0)) ? read_num(".") : token("punc", ".");
     }
 
     function read_word() {
@@ -592,11 +583,10 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
             switch (code) {
               case 34: case 39: return read_string(ch);
               case 46: return handle_dot();
-              case 47: {
-                  var tok = handle_slash();
-                  if (tok === next_token) continue;
-                  return tok;
-              }
+              case 47:
+                var tok = handle_slash();
+                if (tok === next_token) continue;
+                return tok;
             }
             if (is_digit(code)) return read_num();
             if (PUNC_CHARS[ch]) return token("punc", next());
@@ -614,12 +604,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
 
     next_token.add_directive = function(directive) {
         S.directive_stack[S.directive_stack.length - 1].push(directive);
-
-        if (S.directives[directive] === undefined) {
-            S.directives[directive] = 1;
-        } else {
-            S.directives[directive]++;
-        }
+        if (S.directives[directive]) S.directives[directive]++;
+        else S.directives[directive] = 1;
     }
 
     next_token.push_directives_stack = function() {
@@ -627,13 +613,10 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
     }
 
     next_token.pop_directives_stack = function() {
-        var directives = S.directive_stack[S.directive_stack.length - 1];
-
-        for (var i = 0; i < directives.length; i++) {
+        var directives = S.directive_stack.pop();
+        for (var i = directives.length; --i >= 0;) {
             S.directives[directives[i]]--;
         }
-
-        S.directive_stack.pop();
     }
 
     next_token.has_directive = function(directive) {
@@ -645,27 +628,17 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
 
 /* -----[ Parser (constants) ]----- */
 
-var UNARY_PREFIX = makePredicate([
-    "typeof",
-    "void",
-    "delete",
-    "--",
-    "++",
-    "!",
-    "~",
-    "-",
-    "+"
-]);
+var UNARY_PREFIX = makePredicate("typeof void delete -- ++ ! ~ - +");
 
-var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
+var UNARY_POSTFIX = makePredicate("-- ++");
 
-var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
+var ASSIGNMENT = makePredicate("= += -= /= *= %= >>= <<= >>>= |= ^= &=");
 
 var PRECEDENCE = function(a, ret) {
-    for (var i = 0; i < a.length; ++i) {
-        var b = a[i];
-        for (var j = 0; j < b.length; ++j) {
-            ret[b[j]] = i + 1;
+    for (var i = 0; i < a.length;) {
+        var b = a[i++];
+        for (var j = 0; j < b.length; j++) {
+            ret[b[j]] = i;
         }
     }
     return ret;
@@ -682,7 +655,7 @@ var PRECEDENCE = function(a, ret) {
     ["*", "/", "%"]
 ], {});
 
-var ATOMIC_START_TOKEN = makePredicate([ "atom", "num", "string", "regexp", "name" ]);
+var ATOMIC_START_TOKEN = makePredicate("atom num string regexp name");
 
 /* -----[ Parser ]----- */
 
@@ -698,10 +671,9 @@ function parse($TEXT, options) {
     }, true);
 
     var S = {
-        input         : (typeof $TEXT == "string"
-                         ? tokenizer($TEXT, options.filename,
-                                     options.html5_comments, options.shebang)
-                         : $TEXT),
+        input         : typeof $TEXT == "string"
+                        ? tokenizer($TEXT, options.filename, options.html5_comments, options.shebang)
+                        : $TEXT,
         token         : null,
         prev          : null,
         peeked        : null,
@@ -757,15 +729,12 @@ function parse($TEXT, options) {
     }
 
     function unexpected(token) {
-        if (token == null)
-            token = S.token;
+        if (token == null) token = S.token;
         token_error(token, "Unexpected token: " + token_to_string(token.type, token.value));
     }
 
     function expect_token(type, val) {
-        if (is(type, val)) {
-            return next();
-        }
+        if (is(type, val)) return next();
         token_error(S.token, "Unexpected token: " + token_to_string(S.token.type, S.token.value) + ", expected: " + token_to_string(type, val));
     }
 
index 6dcb631..6ac68bb 100644 (file)
@@ -127,8 +127,7 @@ var MAP = (function() {
                 } else {
                     top.push(val);
                 }
-            }
-            else if (val !== skip) {
+            } else if (val !== skip) {
                 if (val instanceof Splice) {
                     ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
                 } else {
@@ -145,8 +144,7 @@ var MAP = (function() {
             } else {
                 for (i = 0; i < a.length; ++i) if (doit()) break;
             }
-        }
-        else {
+        } else {
             for (i in a) if (HOP(a, i)) if (doit()) break;
         }
         return top.concat(ret);
index 1796ecb..7bf7508 100644 (file)
@@ -4947,7 +4947,7 @@ defun_single_use_loop: {
         unused: true,
     }
     input: {
-        for (var x, i = 2; --i >= 0; ) {
+        for (var x, i = 2; --i >= 0;) {
             var y = x;
             x = f;
             console.log(x === y);
@@ -4955,7 +4955,7 @@ defun_single_use_loop: {
         function f() {};
     }
     expect: {
-        for (var x, i = 2; --i >= 0; ) {
+        for (var x, i = 2; --i >= 0;) {
             var y = x;
             x = f;
             console.log(x === y);