Skip to content

Commit 0eb4461

Browse files
committed
Refactor
1 parent fa61eae commit 0eb4461

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/index.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -621,34 +621,37 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
621621

622622
function sortInside(ast: import('@babel/types').Node) {
623623
visit(ast, (node, path) => {
624-
let concat = path.find((entry) => {
625-
return entry.parent && entry.parent.type === 'BinaryExpression' && entry.parent.operator === '+'
626-
})
624+
let start = true
625+
let end = true
626+
627+
for (let entry of path) {
628+
if (!entry.parent) continue
629+
630+
// Nodes inside concat expressions shouldn't collapse whitespace
631+
// depending on which side they're part of.
632+
if (entry.parent.type === 'BinaryExpression' && entry.parent.operator === '+') {
633+
start = entry.key !== 'right'
634+
end = entry.key !== 'left'
635+
636+
break
637+
}
638+
}
627639

628640
if (isStringLiteral(node)) {
629641
sortStringLiteral(node, {
630642
env,
631-
collapseWhitespace: {
632-
start: concat?.key !== 'right',
633-
end: concat?.key !== 'left',
634-
},
643+
collapseWhitespace: { start, end },
635644
})
636645
} else if (node.type === 'TemplateLiteral') {
637646
sortTemplateLiteral(node, {
638647
env,
639-
collapseWhitespace: {
640-
start: concat?.key !== 'right',
641-
end: concat?.key !== 'left',
642-
},
648+
collapseWhitespace: { start, end },
643649
})
644650
} else if (node.type === 'TaggedTemplateExpression') {
645651
if (isSortableTemplateExpression(node, functions)) {
646652
sortTemplateLiteral(node.quasi, {
647653
env,
648-
collapseWhitespace: {
649-
start: concat?.key !== 'right',
650-
end: concat?.key !== 'left',
651-
},
654+
collapseWhitespace: { start, end },
652655
})
653656
}
654657
}

0 commit comments

Comments
 (0)