@@ -917,9 +917,11 @@ can pass additional arguments that control the code output:
|
|||||||
|
|
||||||
- `galio` (default: `false`) — enable workarounds for ANT Galio bugs
|
- `galio` (default: `false`) — enable workarounds for ANT Galio bugs
|
||||||
|
|
||||||
- `indent_level` (default: `4`)
|
- `indent_level` (default: `4`) — indent by specified number of spaces or the
|
||||||
|
exact whitespace sequence supplied, e.g. `"\t"`.
|
||||||
|
|
||||||
- `indent_start` (default: `0`) — prefix all lines by that many spaces
|
- `indent_start` (default: `0`) — prefix all lines by whitespace sequence
|
||||||
|
specified in the same format as `indent_level`.
|
||||||
|
|
||||||
- `inline_script` (default: `true`) — escape HTML comments and the slash in
|
- `inline_script` (default: `true`) — escape HTML comments and the slash in
|
||||||
occurrences of `</script>` in strings
|
occurrences of `</script>` in strings
|
||||||
|
|||||||
@@ -101,9 +101,17 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function make_indent(value) {
|
||||||
|
if (typeof value == "number") return new Array(value + 1).join(" ");
|
||||||
|
if (!/^\s*$/.test(value)) throw new Error("unsupported indentation: " + JSON.stringify("" + value));
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
var current_col = 0;
|
var current_col = 0;
|
||||||
var current_line = 1;
|
var current_line = 1;
|
||||||
var indentation = options.indent_start;
|
var current_indent = make_indent(options.indent_start);
|
||||||
|
var full_indent = make_indent(options.indent_level);
|
||||||
|
var half_indent = full_indent.length + 1 >> 1;
|
||||||
var last;
|
var last;
|
||||||
var line_end = 0;
|
var line_end = 0;
|
||||||
var line_fixed = true;
|
var line_fixed = true;
|
||||||
@@ -368,14 +376,14 @@ function OutputStream(options) {
|
|||||||
|
|
||||||
var indent = options.beautify ? function(half) {
|
var indent = options.beautify ? function(half) {
|
||||||
if (need_newline_indented) print("\n");
|
if (need_newline_indented) print("\n");
|
||||||
print(repeat_string(" ", half ? indentation - (options.indent_level >> 1) : indentation));
|
print(half ? current_indent.slice(0, -half_indent) : current_indent);
|
||||||
} : noop;
|
} : noop;
|
||||||
|
|
||||||
var with_indent = options.beautify ? function(cont) {
|
var with_indent = options.beautify ? function(cont) {
|
||||||
var save_indentation = indentation;
|
var save_indentation = current_indent;
|
||||||
indentation += options.indent_level;
|
current_indent += full_indent;
|
||||||
cont();
|
cont();
|
||||||
indentation = save_indentation;
|
current_indent = save_indentation;
|
||||||
} : function(cont) { cont() };
|
} : function(cont) { cont() };
|
||||||
|
|
||||||
var may_add_newline = options.max_line_len || options.preserve_line ? function() {
|
var may_add_newline = options.max_line_len || options.preserve_line ? function() {
|
||||||
@@ -559,7 +567,7 @@ function OutputStream(options) {
|
|||||||
reset : reset,
|
reset : reset,
|
||||||
indent : indent,
|
indent : indent,
|
||||||
should_break : options.beautify && options.width ? function() {
|
should_break : options.beautify && options.width ? function() {
|
||||||
return current_col - indentation >= options.width;
|
return current_col >= options.width;
|
||||||
} : return_false,
|
} : return_false,
|
||||||
has_parens : function() { return last.slice(-1) == "(" },
|
has_parens : function() { return last.slice(-1) == "(" },
|
||||||
newline : newline,
|
newline : newline,
|
||||||
|
|||||||
@@ -55,14 +55,6 @@ function find_if(func, array) {
|
|||||||
for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
|
for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
function repeat_string(str, i) {
|
|
||||||
if (i <= 0) return "";
|
|
||||||
if (i == 1) return str;
|
|
||||||
var d = repeat_string(str, i >> 1);
|
|
||||||
d += d;
|
|
||||||
return i & 1 ? d + str : d;
|
|
||||||
}
|
|
||||||
|
|
||||||
function configure_error_stack(fn) {
|
function configure_error_stack(fn) {
|
||||||
Object.defineProperty(fn.prototype, "stack", {
|
Object.defineProperty(fn.prototype, "stack", {
|
||||||
get: function() {
|
get: function() {
|
||||||
|
|||||||
100
test/compress/indentation.js
Normal file
100
test/compress/indentation.js
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
numeric: {
|
||||||
|
beautify = {
|
||||||
|
beautify: true,
|
||||||
|
indent_start: 1,
|
||||||
|
indent_level: 3,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (42) {
|
||||||
|
case null:
|
||||||
|
console.log("FAIL");
|
||||||
|
}
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
expect_exact: [
|
||||||
|
" switch (42) {",
|
||||||
|
" case null:",
|
||||||
|
' console.log("FAIL");',
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
' console.log("PASS");',
|
||||||
|
]
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|
||||||
|
spaces: {
|
||||||
|
beautify = {
|
||||||
|
beautify: true,
|
||||||
|
indent_start: " ",
|
||||||
|
indent_level: " ",
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (42) {
|
||||||
|
case null:
|
||||||
|
console.log("FAIL");
|
||||||
|
}
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
expect_exact: [
|
||||||
|
" switch (42) {",
|
||||||
|
" case null:",
|
||||||
|
' console.log("FAIL");',
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
' console.log("PASS");',
|
||||||
|
]
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|
||||||
|
tabs: {
|
||||||
|
beautify = {
|
||||||
|
beautify: true,
|
||||||
|
indent_start: "\t",
|
||||||
|
indent_level: "\t",
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (42) {
|
||||||
|
case null:
|
||||||
|
console.log("FAIL");
|
||||||
|
}
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
expect_exact: [
|
||||||
|
"\tswitch (42) {",
|
||||||
|
"\tcase null:",
|
||||||
|
'\t\tconsole.log("FAIL");',
|
||||||
|
"\t}",
|
||||||
|
"",
|
||||||
|
'\tconsole.log("PASS");',
|
||||||
|
]
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|
||||||
|
mixed: {
|
||||||
|
beautify = {
|
||||||
|
beautify: true,
|
||||||
|
indent_start: "\n",
|
||||||
|
indent_level: " \t",
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (42) {
|
||||||
|
case null:
|
||||||
|
console.log("FAIL");
|
||||||
|
}
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
expect_exact: [
|
||||||
|
"",
|
||||||
|
"switch (42) {",
|
||||||
|
"",
|
||||||
|
" case null:",
|
||||||
|
"",
|
||||||
|
' \tconsole.log("FAIL");',
|
||||||
|
"",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
'console.log("PASS");',
|
||||||
|
]
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user