Skip to content

Commit

Permalink
feature: @putout/printer: ArrayExpression: [boolean, object]
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 1, 2024
1 parent 85147f6 commit 510df22
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
node-version:
- 18.x
- 20.x
- 21.x
- 22.x
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ This is the same as `print('__left')` but more low-level, and supports only obje
About speed, for file `speed.js`:

```js
const {readFileSync} = require('fs');
const {readFileSync} = require('node:fs');

const putout = require('putout');
const parser = require('@babel/parser');
Expand Down
4 changes: 4 additions & 0 deletions lib/tokenize/expressions/array-expression/newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const isSimpleAndCall = ([a, b]) => {
};

const isBooleanAndSimple = ([a, b]) => isBooleanLiteral(a) && isSimple(b);
const isBooleanAndObject = ([a, b]) => isBooleanLiteral(a) && isObjectExpression(b);
const isNullAndSimple = ([a, b]) => isNullLiteral(a) && isSimple(b);
const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
const ONE_LINE = false;
Expand Down Expand Up @@ -258,6 +259,9 @@ function isIncreaseIndent(path) {
if (!elements.length)
return false;

if (isBooleanAndObject(elements))
return true;

if (isInsideCallLoop(path))
return false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const places = putout.findPlaces(ast, fixture.comment, {
rules: {
'find/push': [true, {
ignore: true,
}],
},
});

const bools = [
true,
true,
false,
false,
];
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ module.exports.ObjectExpression = (path, printer, semantics) => {

const n = properties.length - 1;

maybe.indent.inc(isMemberExpressionCallee(path));
const memberCallee = isMemberExpressionCallee(path);
maybe.indent.inc(memberCallee);

for (const [index, property] of properties.entries()) {
if (property.isSpreadElement()) {
Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports.ObjectExpression = (path, printer, semantics) => {
print('}');
maybe.print(parens, ')');

maybe.indent.dec(isMemberExpressionCallee(path));
maybe.indent.dec(memberCallee);
};

const hasNextLeadingComment = (path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ test('printer: tokenizer: object-expression: nested call', (t) => {
t.end();
});

test('printer: tokenizer: object-expression: inside array: with boolean', (t) => {
t.print(fixture.objectInsideArrayWithBoolean);
t.end();
});

test('printer: tokenizer: object-expression: no-trailing-comma', (t) => {
t.print(fixture.objectExpressionNoTrailingComma, {
semantics: {
Expand Down
1 change: 1 addition & 0 deletions lib/tokenize/expressions/object-pattern/object-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function shouldAddNewline(path, semantics) {
const moreLength = moreThenMaxPropertiesLengthInOneLine(path, {
maxPropertiesLengthInOneLine,
});

const moreCount = moreThenMaxPropertiesInOneLine(path, {
maxPropertiesInOneLine,
});
Expand Down
4 changes: 2 additions & 2 deletions test/fixture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const {join} = require('path');
const {readFileSync} = require('fs');
const {join} = require('node:path');
const {readFileSync} = require('node:fs');
const tryCatch = require('try-catch');
const kebabCase = require('just-kebab-case');

Expand Down
1 change: 0 additions & 1 deletion test/print-extension/print-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ module.exports.printExtension = ({fail, equal}) => (fixture, options) => {

const expected = `${fixture}\n`;

debugger;
return equal(source, expected);
};

0 comments on commit 510df22

Please sign in to comment.