Skip to content

Commit f0871b5

Browse files
committed
make array generation configurable (fixes estools#260)
1 parent 7a48a21 commit f0871b5

File tree

2 files changed

+156
-13
lines changed

2 files changed

+156
-13
lines changed

escodegen.js

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
parentheses,
5757
semicolons,
5858
safeConcatenation,
59+
multilineArrayStartsInline,
60+
maxArrayElementsPerLine,
61+
arrayMaxLineWidth,
5962
directive,
6063
extra,
6164
parse,
@@ -196,7 +199,12 @@
196199
parentheses: true,
197200
semicolons: true,
198201
safeConcatenation: false,
199-
preserveBlankLines: false
202+
preserveBlankLines: false,
203+
arrays: {
204+
multilineArrayStartsInline: false,
205+
maxElementsPerLine: 1,
206+
maxLineWidth: 80
207+
}
200208
},
201209
moz: {
202210
comprehensionExpressionStartsWithAssignment: false,
@@ -2087,35 +2095,95 @@
20872095
},
20882096

20892097
ArrayExpression: function (expr, precedence, flags, isPattern) {
2090-
var result, multiline, that = this;
2098+
var result, multiline, currentLine, elementsInLine, that = this;
20912099
if (!expr.elements.length) {
20922100
return '[]';
20932101
}
2094-
multiline = isPattern ? false : expr.elements.length > 1;
2095-
result = ['[', multiline ? newline : ''];
2102+
2103+
var i, iz;
2104+
2105+
result = ['['];
2106+
2107+
for (i = 0, iz = expr.elements.length; i < iz; ++i) {
2108+
if (!expr.elements[i]) {
2109+
if (i + 1 === iz) {
2110+
result.push(',');
2111+
}
2112+
} else {
2113+
result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
2114+
}
2115+
if (i + 1 < iz) {
2116+
result.push(',' + space);
2117+
}
2118+
}
2119+
2120+
result.push(']');
2121+
2122+
var arrayLength = (base + toSourceNodeWhenNeeded(result).toString()).length;
2123+
multiline = !isPattern && (arrayLength > arrayMaxLineWidth || expr.elements.length > maxArrayElementsPerLine);
2124+
2125+
if (!multiline) {
2126+
return result;
2127+
}
2128+
20962129
withIndent(function (indent) {
2097-
var i, iz;
2130+
2131+
if (multilineArrayStartsInline) {
2132+
result = ['['];
2133+
currentLine = base + '[';
2134+
} else {
2135+
result = ['[', newline, indent];
2136+
currentLine = indent;
2137+
}
2138+
2139+
elementsInLine = 0;
2140+
2141+
var i, iz, content, contentPrefix, contentStr, exceedsMaxLength;
2142+
20982143
for (i = 0, iz = expr.elements.length; i < iz; ++i) {
2144+
2145+
content = [];
2146+
20992147
if (!expr.elements[i]) {
2100-
if (multiline) {
2101-
result.push(indent);
2102-
}
21032148
if (i + 1 === iz) {
21042149
result.push(',');
21052150
}
21062151
} else {
2107-
result.push(multiline ? indent : '');
2108-
result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
2152+
content.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
21092153
}
21102154
if (i + 1 < iz) {
2111-
result.push(',' + (multiline ? newline : space));
2155+
content.push(',');
21122156
}
2157+
2158+
contentPrefix = (i > 0 ? space : '');
2159+
contentStr = contentPrefix + toSourceNodeWhenNeeded(content).toString();
2160+
exceedsMaxLength = (currentLine + contentStr.split(newline)[0]).length > arrayMaxLineWidth;
2161+
2162+
if (exceedsMaxLength && currentLine === indent) {
2163+
// it will never fit
2164+
exceedsMaxLength = false;
2165+
}
2166+
2167+
if (exceedsMaxLength || elementsInLine >= maxArrayElementsPerLine) {
2168+
result.push(newline + indent);
2169+
currentLine = indent;
2170+
elementsInLine = 1;
2171+
} else {
2172+
result.push(contentPrefix);
2173+
currentLine += contentPrefix + contentStr;
2174+
var lines = currentLine.split(newline)
2175+
currentLine = lines[lines.length-1];
2176+
elementsInLine++;
2177+
}
2178+
2179+
result.push(content);
21132180
}
21142181
});
2115-
if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
2182+
2183+
if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
21162184
result.push(newline);
21172185
}
2118-
result.push(multiline ? base : '');
2186+
result.push(base);
21192187
result.push(']');
21202188
return result;
21212189
},
@@ -2598,6 +2666,9 @@
25982666
parentheses = options.format.parentheses;
25992667
semicolons = options.format.semicolons;
26002668
safeConcatenation = options.format.safeConcatenation;
2669+
multilineArrayStartsInline = options.format.arrays.multilineArrayStartsInline;
2670+
maxArrayElementsPerLine = options.format.arrays.maxElementsPerLine;
2671+
arrayMaxLineWidth = options.format.arrays.maxLineWidth;
26012672
directive = options.directive;
26022673
parse = json ? null : options.parse;
26032674
sourceMap = options.sourceMap;

test/options.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,78 @@ data = [{
958958
'!function(){42}': '!function\t()\t{\n 42\n}',
959959
'42;foo': '42;\nfoo'
960960
}
961+
}, {
962+
options: {
963+
format: {
964+
arrays: {
965+
multilineArrayStartsInline: true,
966+
maxElementsPerLine: 1,
967+
maxLineWidth: 40
968+
}
969+
}
970+
},
971+
items: {
972+
'[,,3]': '[,\n ,\n 3\n];',
973+
'[1, 2, 3]': '[1,\n 2,\n 3\n];',
974+
'[1, 2, 3, 4]': '[1,\n 2,\n 3,\n 4\n];',
975+
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[1,\n 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
976+
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
977+
'foo = ["very-long-text-that-will-never-fit-according-to-the-configured-max-width"]': 'foo = [\n \'very-long-text-that-will-never-fit-according-to-the-configured-max-width\'\n];'
978+
}
979+
}, {
980+
options: {
981+
format: {
982+
arrays: {
983+
multilineArrayStartsInline: false,
984+
maxElementsPerLine: 1,
985+
maxLineWidth: 40
986+
}
987+
}
988+
},
989+
items: {
990+
'[,,3]': '[\n ,\n ,\n 3\n];',
991+
'[1, 2, 3]': '[\n 1,\n 2,\n 3\n];',
992+
'[1, 2, 3, 4]': '[\n 1,\n 2,\n 3,\n 4\n];',
993+
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[\n 1,\n 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
994+
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
995+
'foo = ["very-long-text-that-will-never-fit-according-to-the-configured-max-width"]': 'foo = [\n \'very-long-text-that-will-never-fit-according-to-the-configured-max-width\'\n];'
996+
}
997+
}, {
998+
options: {
999+
format: {
1000+
arrays: {
1001+
multilineArrayStartsInline: false,
1002+
maxElementsPerLine: 3,
1003+
maxLineWidth: 40
1004+
}
1005+
}
1006+
},
1007+
items: {
1008+
'[,,3]': '[, , 3];',
1009+
'[1, 2, 3]': '[1, 2, 3];',
1010+
'[1, 2, 3, 4]': '[\n 1, 2, 3,\n 4\n];',
1011+
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[\n 1, 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
1012+
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
1013+
'foo = ["very-long-text-that-will-never-fit-according-to-the-configured-max-width"]': 'foo = [\n \'very-long-text-that-will-never-fit-according-to-the-configured-max-width\'\n];'
1014+
}
1015+
}, {
1016+
options: {
1017+
format: {
1018+
arrays: {
1019+
multilineArrayStartsInline: true,
1020+
maxElementsPerLine: 3,
1021+
maxLineWidth: 40
1022+
}
1023+
}
1024+
},
1025+
items: {
1026+
'[,,3]': '[, , 3];',
1027+
'[1, 2, 3]': '[1, 2, 3];',
1028+
'[1, 2, 3, 4]': '[1, 2, 3,\n 4\n];',
1029+
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[1, 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
1030+
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
1031+
'foo = ["very-long-text-that-will-never-fit-according-to-the-configured-max-width"]': 'foo = [\n \'very-long-text-that-will-never-fit-according-to-the-configured-max-width\'\n];'
1032+
}
9611033
}];
9621034

9631035
function adjustRegexLiteral(key, value) {

0 commit comments

Comments
 (0)