Skip to content

Commit bff5638

Browse files
committed
connect dangling comments to correct "bucket"
1 parent 349921e commit bff5638

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

packages/@tailwindcss-upgrade/src/codemods/migrate-at-layer-utilities.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,7 @@ describe('comments', () => {
778778
/* After */
779779
`),
780780
).toMatchInlineSnapshot(`
781-
"/* After */
782-
783-
/* Tailwind Utilities: */
781+
"/* Tailwind Utilities: */
784782
@utility no-scrollbar {
785783
/* Chrome, Safari and Opera */
786784
/* Second comment */
@@ -801,6 +799,7 @@ describe('comments', () => {
801799
.before {
802800
/* Inside */
803801
}
802+
/* After */
804803
805804
/* Above */
806805
.after {

packages/@tailwindcss-upgrade/src/codemods/migrate-missing-layers.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ it('should migrate rules above the `@tailwind base` directive in an `@layer base
120120
"@charset "UTF-8";
121121
@layer foo, bar, baz;
122122
123-
@tailwind base;
124-
@tailwind components;
125-
@tailwind utilities;
126-
127123
/**!
128124
* License header
129125
*/
130126
127+
@tailwind base;
128+
@tailwind components;
129+
@tailwind utilities;
130+
131131
@layer base {
132132
html {
133133
color: red;

packages/@tailwindcss-upgrade/src/codemods/sort-buckets.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,26 @@ export function sortBuckets(): Plugin {
5050
return WalkAction.Skip
5151
}
5252

53-
// Comments belong to the "next" bucket. If a bucket contains comments
54-
// at the end, they belong to the next bucket.
53+
// Comments belong to the bucket of the nearest node, which is typically
54+
// in the "next" bucket.
5555
if (node.type === 'comment') {
56-
comments.push(node)
57-
return
56+
// We already have comments, which means that we already have nodes
57+
// that belong in the next bucket, so we should move the current
58+
// comment into the next bucket as well.
59+
if (comments.length > 0) {
60+
comments.push(node)
61+
return
62+
}
63+
64+
// Figure out the closest node to the comment
65+
let prevDistance = distance(node.prev(), node) ?? Infinity
66+
let nextDistance = distance(node, node.next()) ?? Infinity
67+
68+
if (prevDistance < nextDistance) {
69+
buckets.get(lastLayer).nodes?.push(node)
70+
} else {
71+
comments.push(node)
72+
}
5873
}
5974

6075
// Known at-rules
@@ -140,3 +155,14 @@ export function sortBuckets(): Plugin {
140155
OnceExit: migrate,
141156
}
142157
}
158+
159+
function distance(before?: ChildNode, after?: ChildNode): number | null {
160+
if (!before || !after) return null
161+
if (!before.source || !after.source) return null
162+
if (!before.source.start || !after.source.start) return null
163+
if (!before.source.end || !after.source.end) return null
164+
165+
// Compare end of Before, to start of After
166+
let d = Math.abs(before.source.end.line - after.source.start.line)
167+
return d
168+
}

0 commit comments

Comments
 (0)