From 6472f9410ec93c4f934643d7fd7ff3edeef6b191 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 17 Oct 2012 14:51:27 +0300 Subject: [PATCH] add `semicolons` option in the code generator (default: `true`) pass `false` to separate statements with newlines instead of semicolons --- README.md | 4 ++++ lib/output.js | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0fe6ed35..793306d8 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,10 @@ can pass additional arguments that control the code output: - `bracketize` (default `false`) -- always insert brackets in `if`, `for`, `do`, `while` or `with` statements, even if their body is a single statement. +- `semicolons` (default `true`) -- separate statements with semicolons. If + you pass `false` then whenever possible we will use a newline instead of a + semicolon, leading to more readable output of uglified code (size before + gzip could be smaller; size after gzip insignificantly larger). ### Keeping copyright notices or other comments diff --git a/lib/output.js b/lib/output.js index dd40972e..8d2c4d39 100644 --- a/lib/output.js +++ b/lib/output.js @@ -58,6 +58,7 @@ function OutputStream(options) { beautify : false, source_map : null, bracketize : false, + semicolons : true, comments : false }, true); @@ -130,14 +131,23 @@ function OutputStream(options) { print("\n"); }; + var requireSemicolonChars = makePredicate("( [ + * / - , ."); + function print(str) { str = String(str); var ch = str.charAt(0); if (might_need_semicolon) { if (";}".indexOf(ch) < 0 && !/[;]$/.test(last)) { - OUTPUT += ";"; - current_col++; - current_pos++; + if (options.semicolons || requireSemicolonChars(ch)) { + OUTPUT += ";"; + current_col++; + current_pos++; + } else { + OUTPUT += "\n"; + current_pos++; + current_line++; + current_col = 0; + } if (!options.beautify) might_need_space = false; }