Skip to content

Commit

Permalink
make array generation configurable (fixes estools#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rk committed Mar 16, 2021
1 parent 7a48a21 commit f0871b5
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 13 deletions.
97 changes: 84 additions & 13 deletions escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
parentheses,
semicolons,
safeConcatenation,
multilineArrayStartsInline,
maxArrayElementsPerLine,
arrayMaxLineWidth,
directive,
extra,
parse,
Expand Down Expand Up @@ -196,7 +199,12 @@
parentheses: true,
semicolons: true,
safeConcatenation: false,
preserveBlankLines: false
preserveBlankLines: false,
arrays: {
multilineArrayStartsInline: false,
maxElementsPerLine: 1,
maxLineWidth: 80
}
},
moz: {
comprehensionExpressionStartsWithAssignment: false,
Expand Down Expand Up @@ -2087,35 +2095,95 @@
},

ArrayExpression: function (expr, precedence, flags, isPattern) {
var result, multiline, that = this;
var result, multiline, currentLine, elementsInLine, that = this;
if (!expr.elements.length) {
return '[]';
}
multiline = isPattern ? false : expr.elements.length > 1;
result = ['[', multiline ? newline : ''];

var i, iz;

result = ['['];

for (i = 0, iz = expr.elements.length; i < iz; ++i) {
if (!expr.elements[i]) {
if (i + 1 === iz) {
result.push(',');
}
} else {
result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
}
if (i + 1 < iz) {
result.push(',' + space);
}
}

result.push(']');

var arrayLength = (base + toSourceNodeWhenNeeded(result).toString()).length;
multiline = !isPattern && (arrayLength > arrayMaxLineWidth || expr.elements.length > maxArrayElementsPerLine);

if (!multiline) {
return result;
}

withIndent(function (indent) {
var i, iz;

if (multilineArrayStartsInline) {
result = ['['];
currentLine = base + '[';
} else {
result = ['[', newline, indent];
currentLine = indent;
}

elementsInLine = 0;

var i, iz, content, contentPrefix, contentStr, exceedsMaxLength;

for (i = 0, iz = expr.elements.length; i < iz; ++i) {

content = [];

if (!expr.elements[i]) {
if (multiline) {
result.push(indent);
}
if (i + 1 === iz) {
result.push(',');
}
} else {
result.push(multiline ? indent : '');
result.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
content.push(that.generateExpression(expr.elements[i], Precedence.Assignment, E_TTT));
}
if (i + 1 < iz) {
result.push(',' + (multiline ? newline : space));
content.push(',');
}

contentPrefix = (i > 0 ? space : '');
contentStr = contentPrefix + toSourceNodeWhenNeeded(content).toString();
exceedsMaxLength = (currentLine + contentStr.split(newline)[0]).length > arrayMaxLineWidth;

if (exceedsMaxLength && currentLine === indent) {
// it will never fit
exceedsMaxLength = false;
}

if (exceedsMaxLength || elementsInLine >= maxArrayElementsPerLine) {
result.push(newline + indent);
currentLine = indent;
elementsInLine = 1;
} else {
result.push(contentPrefix);
currentLine += contentPrefix + contentStr;
var lines = currentLine.split(newline)
currentLine = lines[lines.length-1];
elementsInLine++;
}

result.push(content);
}
});
if (multiline && !endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {

if (!endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString())) {
result.push(newline);
}
result.push(multiline ? base : '');
result.push(base);
result.push(']');
return result;
},
Expand Down Expand Up @@ -2598,6 +2666,9 @@
parentheses = options.format.parentheses;
semicolons = options.format.semicolons;
safeConcatenation = options.format.safeConcatenation;
multilineArrayStartsInline = options.format.arrays.multilineArrayStartsInline;
maxArrayElementsPerLine = options.format.arrays.maxElementsPerLine;
arrayMaxLineWidth = options.format.arrays.maxLineWidth;
directive = options.directive;
parse = json ? null : options.parse;
sourceMap = options.sourceMap;
Expand Down
72 changes: 72 additions & 0 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,78 @@ data = [{
'!function(){42}': '!function\t()\t{\n 42\n}',
'42;foo': '42;\nfoo'
}
}, {
options: {
format: {
arrays: {
multilineArrayStartsInline: true,
maxElementsPerLine: 1,
maxLineWidth: 40
}
}
},
items: {
'[,,3]': '[,\n ,\n 3\n];',
'[1, 2, 3]': '[1,\n 2,\n 3\n];',
'[1, 2, 3, 4]': '[1,\n 2,\n 3,\n 4\n];',
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[1,\n 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
'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];'
}
}, {
options: {
format: {
arrays: {
multilineArrayStartsInline: false,
maxElementsPerLine: 1,
maxLineWidth: 40
}
}
},
items: {
'[,,3]': '[\n ,\n ,\n 3\n];',
'[1, 2, 3]': '[\n 1,\n 2,\n 3\n];',
'[1, 2, 3, 4]': '[\n 1,\n 2,\n 3,\n 4\n];',
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[\n 1,\n 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
'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];'
}
}, {
options: {
format: {
arrays: {
multilineArrayStartsInline: false,
maxElementsPerLine: 3,
maxLineWidth: 40
}
}
},
items: {
'[,,3]': '[, , 3];',
'[1, 2, 3]': '[1, 2, 3];',
'[1, 2, 3, 4]': '[\n 1, 2, 3,\n 4\n];',
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[\n 1, 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
'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];'
}
}, {
options: {
format: {
arrays: {
multilineArrayStartsInline: true,
maxElementsPerLine: 3,
maxLineWidth: 40
}
}
},
items: {
'[,,3]': '[, , 3];',
'[1, 2, 3]': '[1, 2, 3];',
'[1, 2, 3, 4]': '[1, 2, 3,\n 4\n];',
'[1, 2, "three-is-a-longer-string-in-this-test"]': '[1, 2,\n \'three-is-a-longer-string-in-this-test\'\n];',
'foo = ["long-text-that-only-fits-in-singl-row"]': 'foo = [\n \'long-text-that-only-fits-in-singl-row\'\n];',
'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];'
}
}];

function adjustRegexLiteral(key, value) {
Expand Down

0 comments on commit f0871b5

Please sign in to comment.