Skip to content

Commit

Permalink
feature: ArrayExpression: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 1, 2023
1 parent b38c30a commit 39e3a0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
8 changes: 4 additions & 4 deletions lib/tokenize/expressions/array-expression/array-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const {
} = require('../../is');

const {
isNewlineBetweenElements,
isMultiLine,
isIncreaseIndent,
isCurrentNewLine,
} = require('./new-line');
} = require('./newline');

const {types} = require('@putout/babel');
const {
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports.ArrayExpression = {
} = semantics;

const elements = path.get('elements');
const shouldIncreaseIndent = !isIncreaseIndent(path);
const shouldIncreaseIndent = isIncreaseIndent(path);

print('[');

Expand All @@ -76,7 +76,7 @@ module.exports.ArrayExpression = {
if (indented)
maybe.indent.inc(shouldIncreaseIndent);

const isNewLine = isNewlineBetweenElements(path, {
const isNewLine = isMultiLine(path, {
elements,
maxElementsInOneLine,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
const ONE_LINE = false;
const MULTI_LINE = true;

module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine}) => {
module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
if (elements.length > 3 && !isObjectExpression(elements[0]))
return MULTI_LINE;

Expand All @@ -63,7 +63,7 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
if (isCallInsideArrow(path))
return ONE_LINE;

if (isIncreaseIndent(path))
if (notIncreaseIndent(path))
return ONE_LINE;

if (isInsideLoop(path))
Expand Down Expand Up @@ -102,7 +102,7 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
if (isSimpleAndObject(elements))
return ONE_LINE;

if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
if (isStringAndString(elements) && isParentIsArrayWithFirstArrayElement(path))
return ONE_LINE;

if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
Expand All @@ -111,6 +111,15 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
return ONE_LINE;
};

const isParentIsArrayWithFirstArrayElement = ({parentPath}) => {
if (!isArrayExpression(parentPath))
return false;

const [first] = parentPath.node.elements;

return isArrayExpression(first);
};

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

const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
Expand Down Expand Up @@ -235,23 +244,31 @@ function isParentProperty(path) {
return path.find(isObjectProperty);
}

module.exports.isIncreaseIndent = isIncreaseIndent;
function isIncreaseIndent(path) {
const elements = path.get('elements');
const not = (fn) => (...a) => !fn(...a);

const isFirstObject = (path) => isObjectExpression(path.node.elements[0]);
const isSecondSpread = (path) => isSpreadElement(path.node.elements[1]);

const isStringAndObject = (path) => {
const {elements} = path.node;
const first = elements.at(0);
const last = elements.at(-1);

if (!elements.length)
return isStringLiteral(first) && isObjectExpression(last);
};

module.exports.isIncreaseIndent = not(notIncreaseIndent);
function notIncreaseIndent(path) {
if (isInsideCallLoop(path))
return false;

if (isInsideCallLoop(path))
if (isSecondSpread(path))
return false;

if (elements[0].isObjectExpression())
if (isStringAndObject(path))
return true;

if (isSpreadElement(elements[1]))
return false;

return isStringAndObject(elements);
return isFirstObject(path);
}

function isInsideCallLoop(path) {
Expand All @@ -261,13 +278,6 @@ function isInsideCallLoop(path) {
return path.parentPath.parentPath.isForOfStatement();
}

const isStringAndObject = (elements) => {
const first = elements.at(0);
const last = elements.at(-1);

return isStringLiteral(first) && isObjectExpression(last);
};

module.exports.isCurrentNewLine = (path) => {
if (path.isSpreadElement())
return true;
Expand Down

0 comments on commit 39e3a0c

Please sign in to comment.