Skip to content

Commit 9d58143

Browse files
committed
Refactor
1 parent 0ae5696 commit 9d58143

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

src/index.ts

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getCustomizations } from './options.js'
1515
import { loadPlugins } from './plugins.js'
1616
import { sortClasses, sortClassList } from './sorting.js'
1717
import type { Customizations, StringChange, TransformerContext, TransformerEnv, TransformerMetadata } from './types'
18-
import { spliceChangesIntoString, visit } from './utils.js'
18+
import { spliceChangesIntoString, visit, type Path } from './utils.js'
1919

2020
let base = await loadPlugins()
2121

@@ -152,8 +152,8 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
152152
ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.raw),
153153

154154
collapseWhitespace: {
155-
start: concat?.key !== 'right' && i === 0,
156-
end: concat?.key !== 'left' && i >= node.expressions.length,
155+
start: collapseWhitespace.start && i === 0,
156+
end: collapseWhitespace.end && i >= node.expressions.length,
157157
},
158158
}),
159159
})
@@ -612,6 +612,49 @@ function isSortableExpression(
612612
return false
613613
}
614614

615+
function canCollapseWhitespaceIn(path: Path<import('@babel/types').Node, any>) {
616+
let start = true
617+
let end = true
618+
619+
for (let entry of path) {
620+
if (!entry.parent) continue
621+
622+
// Nodes inside concat expressions shouldn't collapse whitespace
623+
// depending on which side they're part of.
624+
if (entry.parent.type === 'BinaryExpression' && entry.parent.operator === '+') {
625+
start &&= entry.key !== 'right'
626+
end &&= entry.key !== 'left'
627+
}
628+
629+
// This is probably expression *inside* of a template literal. To collapse whitespace
630+
// `Expression`s adjacent-before a quasi must start with whitespace
631+
// `Expression`s adjacent-after a quasi must end with whitespace
632+
//
633+
// Note this check will bail out on more than it really should as it
634+
// could be reset somewhere along the way by having whitespace around a
635+
// string further up but not at the "root" but that complicates things
636+
if (entry.parent.type === 'TemplateLiteral') {
637+
let nodeStart = entry.node.start ?? null
638+
let nodeEnd = entry.node.end ?? null
639+
640+
for (let quasi of entry.parent.quasis) {
641+
let quasiStart = quasi.end ?? null
642+
let quasiEnd = quasi.end ?? null
643+
644+
if (nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2) {
645+
start &&= /^\s/.test(quasi.value.raw)
646+
}
647+
648+
if (nodeEnd !== null && quasiStart !== null && nodeEnd - quasiStart <= 2) {
649+
end &&= /\s$/.test(quasi.value.raw)
650+
}
651+
}
652+
}
653+
}
654+
655+
return { start, end }
656+
}
657+
615658
// TODO: The `ast` types here aren't strictly correct.
616659
//
617660
// We cross several parsers that share roughly the same shape so things are
@@ -621,61 +664,15 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
621664

622665
function sortInside(ast: import('@babel/types').Node) {
623666
visit(ast, (node, path) => {
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-
637-
// This is probably expression *inside* of a template literal. To collapse whitespace
638-
// `Expression`s adjacent-before a quasi must start with whitespace
639-
// `Expression`s adjacent-after a quasi must end with whitespace
640-
//
641-
// Note this check will bail out on more than it really should as it
642-
// could be reset somewhere along the way by having whitespace around a
643-
// string further up but not at the "root" but that complicates things
644-
if (entry.parent.type === 'TemplateLiteral') {
645-
let nodeStart = entry.node.start ?? null
646-
let nodeEnd = entry.node.end ?? null
647-
648-
for (let quasi of entry.parent.quasis) {
649-
let quasiStart = quasi.end ?? null
650-
let quasiEnd = quasi.end ?? null
651-
652-
if (nodeStart !== null && quasiEnd !== null && nodeStart - quasiEnd <= 2) {
653-
start &&= /^\s/.test(quasi.value.raw)
654-
}
655-
656-
if (nodeEnd !== null && quasiStart !== null && nodeEnd - quasiStart <= 2) {
657-
end &&= /\s$/.test(quasi.value.raw)
658-
}
659-
}
660-
}
661-
}
667+
let collapseWhitespace = canCollapseWhitespaceIn(path)
662668

663669
if (isStringLiteral(node)) {
664-
sortStringLiteral(node, {
665-
env,
666-
collapseWhitespace: { start, end },
667-
})
670+
sortStringLiteral(node, { env, collapseWhitespace })
668671
} else if (node.type === 'TemplateLiteral') {
669-
sortTemplateLiteral(node, {
670-
env,
671-
collapseWhitespace: { start, end },
672-
})
672+
sortTemplateLiteral(node, { env, collapseWhitespace })
673673
} else if (node.type === 'TaggedTemplateExpression') {
674674
if (isSortableTemplateExpression(node, functions)) {
675-
sortTemplateLiteral(node.quasi, {
676-
env,
677-
collapseWhitespace: { start, end },
678-
})
675+
sortTemplateLiteral(node.quasi, { env, collapseWhitespace })
679676
}
680677
}
681678
})

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface PathEntry<T, Meta> {
1919
meta: Meta
2020
}
2121

22-
type Path<T, Meta> = PathEntry<T, Meta>[]
22+
export type Path<T, Meta> = PathEntry<T, Meta>[]
2323

2424
type Visitor<T, Meta extends Record<string, unknown>> = (
2525
node: T,

0 commit comments

Comments
 (0)