@putout/printer
Advanced tools
@@ -11,9 +11,7 @@ 'use strict'; | ||
const { | ||
isMultiLine, | ||
isIncreaseIndent, | ||
isCurrentNewLine, | ||
isMultiLine, | ||
} = require('./newline'); | ||
const {types} = require('@putout/babel'); | ||
const { | ||
@@ -25,5 +23,11 @@ isInsideArray, | ||
const {isObjectExpression} = types; | ||
const {types} = require('@putout/babel'); | ||
const { | ||
isObjectExpression, | ||
isSpreadElement, | ||
} = types; | ||
const isNextObject = (a) => a.getNextSibling().isObjectExpression(); | ||
const isPrevObject = (a) => a.getPrevSibling().isObjectExpression(); | ||
const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a); | ||
@@ -58,3 +62,3 @@ const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1; | ||
const elements = path.get('elements'); | ||
const shouldIncreaseIndent = isIncreaseIndent(path); | ||
const shouldIncreaseIndent = !isIncreaseIndent(path); | ||
@@ -86,3 +90,3 @@ print('['); | ||
maybe.print.newline(is && !isNextObject(element)); | ||
maybe.print.space(element.isSpreadElement() && isNextObject(element)); | ||
maybe.print.space(isObjectAfterSpread(element)); | ||
@@ -89,0 +93,0 @@ if (!is && index < n) { |
'use strict'; | ||
const {isSimple} = require('@putout/operate'); | ||
const isTwoStringsTupleAndParentIsArrayWithFirstArrayElement = (path) => { | ||
const {elements} = path.node; | ||
if (!isStringAndString(elements)) | ||
return false; | ||
return isParentIsArrayWithFirstArrayElement(path); | ||
}; | ||
const isLotsOfElementsFirstNotObject = (path) => { | ||
const {elements} = path.node; | ||
const [first] = elements; | ||
return elements.length > 3 && !isObjectExpression(first); | ||
}; | ||
const isTwoSimplesInsideObjectProperty = (path) => { | ||
const {node, parentPath} = path; | ||
const {elements} = node; | ||
const {length} = elements; | ||
const [a, b] = elements; | ||
if (length > 2) | ||
return false; | ||
if (!parentPath.isObjectProperty()) | ||
return false; | ||
if (!isStringLiteral(a) || !isStringLiteral(b)) | ||
return false; | ||
return !isCoupleLines(path); | ||
}; | ||
const { | ||
@@ -49,3 +15,2 @@ isObjectExpression, | ||
isSpreadElement, | ||
isNumericLiteral, | ||
} = require('@putout/babel').types; | ||
@@ -60,3 +25,2 @@ | ||
isIdentifierAndIdentifier, | ||
satisfy, | ||
} = require('../../is'); | ||
@@ -88,66 +52,64 @@ | ||
const isOneLineElements = satisfy([ | ||
isOneSimple, | ||
isOneSpread, | ||
isIdentifierAndIdentifier, | ||
isBooleanAndSimple, | ||
isNullAndSimple, | ||
isSimpleAndCall, | ||
isTwoStringsDifferentLength, | ||
isStringAndArray, | ||
isStringAndMember, | ||
isStringAndIdentifier, | ||
isIdentifierAndString, | ||
isSimpleAndObject, | ||
]); | ||
const isOneLinePath = satisfy([ | ||
isCallInsideArrow, | ||
notIncreaseIndent, | ||
isInsideLoop, | ||
isTwoSimplesInsideObjectProperty, | ||
isTwoStringsTupleAndParentIsArrayWithFirstArrayElement, | ||
]); | ||
const isMultiLinePath = satisfy([ | ||
tooLong, | ||
isCoupleLines, | ||
notNumbersInsideForOf, | ||
]); | ||
module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => { | ||
if (isLotsOfElementsFirstNotObject(path)) | ||
if (elements.length > 3 && !isObjectExpression(elements[0])) | ||
return MULTI_LINE; | ||
if (isOneLineElements(elements)) | ||
if (isOneSimple(path)) | ||
return ONE_LINE; | ||
if (isOneLinePath(path)) | ||
if (isOneSpread(elements)) | ||
return ONE_LINE; | ||
if (elements.length === 2 && isIdentifierAndIdentifier(elements)) | ||
return ONE_LINE; | ||
if (isCallInsideArrow(path)) | ||
return ONE_LINE; | ||
if (isIncreaseIndent(path)) | ||
return ONE_LINE; | ||
if (isInsideLoop(path)) | ||
return ONE_LINE; | ||
if (isBooleanAndSimple(elements)) | ||
return ONE_LINE; | ||
if (isNullAndSimple(elements)) | ||
return ONE_LINE; | ||
if (isSimpleAndCall(elements)) | ||
return ONE_LINE; | ||
if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine)) | ||
return ONE_LINE; | ||
if (isMultiLinePath(path)) | ||
return MULTI_LINE; | ||
if (isTwoStringsDifferentLength(elements)) | ||
return ONE_LINE; | ||
return ONE_LINE; | ||
}; | ||
function notNumbersInsideForOf(path) { | ||
const {elements} = path.node; | ||
if (isTwoSimplesInsideObjectProperty(path)) | ||
return ONE_LINE; | ||
if (isNumbers(elements)) | ||
return false; | ||
if (isStringAndArray(elements)) | ||
return ONE_LINE; | ||
return !isForOf(path) && isLastArg(path) && !isParentProperty(path); | ||
} | ||
const isParentIsArrayWithFirstArrayElement = ({parentPath}) => { | ||
if (!isArrayExpression(parentPath)) | ||
return false; | ||
if (isStringAndMember(elements)) | ||
return ONE_LINE; | ||
const [first] = parentPath.node.elements; | ||
if (isStringAndIdentifier(elements)) | ||
return ONE_LINE; | ||
return isArrayExpression(first); | ||
if (isIdentifierAndString(elements)) | ||
return ONE_LINE; | ||
if (isSimpleAndObject(elements)) | ||
return ONE_LINE; | ||
if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0])) | ||
return ONE_LINE; | ||
if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path)) | ||
return MULTI_LINE; | ||
return ONE_LINE; | ||
}; | ||
@@ -175,3 +137,24 @@ | ||
function isOneSimple(elements) { | ||
const isTwoSimplesInsideObjectProperty = (path) => { | ||
const {node, parentPath} = path; | ||
const {elements} = node; | ||
const {length} = elements; | ||
const [a, b] = elements; | ||
if (length > 2) | ||
return false; | ||
if (!parentPath.isObjectProperty()) | ||
return false; | ||
if (!isStringLiteral(a) || !isStringLiteral(b)) | ||
return false; | ||
return !isCoupleLines(path); | ||
}; | ||
function isOneSimple(path) { | ||
const elements = path.get('elements'); | ||
if (elements.length !== 1) | ||
@@ -244,3 +227,3 @@ return false; | ||
for (const element of elements) { | ||
if (isNumericLiteral(element)) | ||
if (element.isNumericLiteral()) | ||
return true; | ||
@@ -260,27 +243,19 @@ } | ||
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); | ||
module.exports.isIncreaseIndent = isIncreaseIndent; | ||
function isIncreaseIndent(path) { | ||
const elements = path.get('elements'); | ||
return isStringLiteral(first) && isObjectExpression(last); | ||
}; | ||
module.exports.isIncreaseIndent = not(notIncreaseIndent); | ||
function notIncreaseIndent(path) { | ||
if (isInsideCallLoop(path)) | ||
if (!elements.length) | ||
return false; | ||
if (isSecondSpread(path)) | ||
if (isInsideCallLoop(path)) | ||
return false; | ||
if (isStringAndObject(path)) | ||
if (elements[0].isObjectExpression()) | ||
return true; | ||
return isFirstObject(path); | ||
if (isSpreadElement(elements[1])) | ||
return false; | ||
return isStringAndObject(elements); | ||
} | ||
@@ -295,2 +270,9 @@ | ||
const isStringAndObject = (elements) => { | ||
const first = elements.at(0); | ||
const last = elements.at(-1); | ||
return isStringLiteral(first) && isObjectExpression(last); | ||
}; | ||
module.exports.isCurrentNewLine = (path) => { | ||
@@ -297,0 +279,0 @@ if (path.isSpreadElement()) |
@@ -63,8 +63,3 @@ 'use strict'; | ||
module.exports.isIdentifierAndIdentifier = (elements) => { | ||
if (elements.length !== 2) | ||
return false; | ||
const [a, b] = elements; | ||
module.exports.isIdentifierAndIdentifier = ([a, b]) => { | ||
return isIdentifier(a) && isIdentifier(b); | ||
@@ -71,0 +66,0 @@ }; |
{ | ||
"name": "@putout/printer", | ||
"version": "6.12.0", | ||
"version": "6.13.0", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", |
Sorry, the diff of this file is not supported yet
201385
-0.07%4551
-0.42%