Skip to content

Commit

Permalink
support custom indentation (#5318)
Browse files Browse the repository at this point in the history
closes #50
  • Loading branch information
alexlamsl committed Jan 28, 2022
1 parent 3693bde commit e4a91a8
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,11 @@ can pass additional arguments that control the code output:

- `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
occurrences of `</script>` in strings
Expand Down
20 changes: 14 additions & 6 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_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 line_end = 0;
var line_fixed = true;
Expand Down Expand Up @@ -368,14 +376,14 @@ function OutputStream(options) {

var indent = options.beautify ? function(half) {
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;

var with_indent = options.beautify ? function(cont) {
var save_indentation = indentation;
indentation += options.indent_level;
var save_indentation = current_indent;
current_indent += full_indent;
cont();
indentation = save_indentation;
current_indent = save_indentation;
} : function(cont) { cont() };

var may_add_newline = options.max_line_len || options.preserve_line ? function() {
Expand Down Expand Up @@ -559,7 +567,7 @@ function OutputStream(options) {
reset : reset,
indent : indent,
should_break : options.beautify && options.width ? function() {
return current_col - indentation >= options.width;
return current_col >= options.width;
} : return_false,
has_parens : function() { return last.slice(-1) == "(" },
newline : newline,
Expand Down
8 changes: 0 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ function find_if(func, array) {
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) {
Object.defineProperty(fn.prototype, "stack", {
get: function() {
Expand Down
100 changes: 100 additions & 0 deletions test/compress/indentation.js
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit e4a91a8

Please sign in to comment.