Skip to content

Commit

Permalink
feature: @putout/printer: ArrayExpression: maxElementsInOneLine
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 12, 2023
1 parent 9dd950b commit 9d730e9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ if(a>3)console.log('ok');else console.log('not ok');

Options used to configure logic of output, similar to ESLint rules:

```js


```
- `maxElementsInOneLine` - count of `ArrayExpression` and `ArrayPattern` elements placed in one line.

### `write`

Expand Down
10 changes: 5 additions & 5 deletions lib/tokenize/expressions/array-expression/array-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const {
isStringAndMember,
} = require('../../is');

const MAX_ARRAY_ELEMENTS_WHEN_USED_AS_ARGUMENT = 4;

const isForOf = ({parentPath}) => parentPath.isForOfStatement();

const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
Expand Down Expand Up @@ -112,7 +110,8 @@ module.exports.ArrayExpression = {
before(path, {print}) {
print.breakline();
},
print(path, {print, maybe, indent}) {
print(path, {print, maybe, indent}, semantics) {
const {maxElementsInOneLine} = semantics;
const elements = path.get('elements');
const shouldIncreaseIndent = !isIncreaseIndent(path);

Expand All @@ -123,6 +122,7 @@ module.exports.ArrayExpression = {

const isNewLine = isNewlineBetweenElements(path, {
elements,
maxElementsInOneLine,
});

const n = elements.length - 1;
Expand Down Expand Up @@ -256,7 +256,7 @@ function isOneSimple(path) {
return first.isMemberExpression();
}

function isNewlineBetweenElements(path, {elements}) {
function isNewlineBetweenElements(path, {elements, maxElementsInOneLine}) {
if (elements.length > 3 && !isObjectExpression(elements[0]))
return true;

Expand Down Expand Up @@ -287,7 +287,7 @@ function isNewlineBetweenElements(path, {elements}) {
if (isSimpleAndCall(elements))
return false;

if (isShortTwoSimplesInsideCall(path, MAX_ARRAY_ELEMENTS_WHEN_USED_AS_ARGUMENT))
if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
return false;

if (isTwoStringsDifferentLength(elements))
Expand Down
13 changes: 13 additions & 0 deletions lib/tokenize/expressions/array-expression/array-expression.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ test('printer: tokenizer: statement: ArrayExpression: space', (t) => {
t.equal(result, fixture.arrayExpressionSpaceFix);
t.end();
});

test('printer: tokenizer: statement: ArrayExpression: maxElementsInOneLine', (t) => {
const ast = parse(fixture.arrayExpressionMaxElementsInOneLine);

const result = print(ast, {
semantics: {
maxElementsInOneLine: 1,
},
});

t.equal(result, fixture.arrayExpressionMaxElementsInOneLineFix);
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn([
'hello',
'world',
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn(['hello', 'world'])

0 comments on commit 9d730e9

Please sign in to comment.