Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit fac7cfe

Browse files
committed
fix future sort order bug
When sorting based on the BigInt values, we are doing `Number(a - z)`, but if the result of `a - z` is bigger then the max number, then overflows would happen. This will result in a hard-to-debug bug. This is a prevention for the future, because currently all our `a - z` values will not reach Number.MAX_SAFE_INTEGER mark (9007199254740991).
1 parent 57fd486 commit fac7cfe

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ let env = {
2626
DEBUG: process.env.DEBUG !== undefined,
2727
}
2828

29+
function sign(bigIntValue) {
30+
if (bigIntValue === 0n) {
31+
return 0
32+
}
33+
34+
if (bigIntValue > 0n) {
35+
return 1
36+
}
37+
38+
return -1
39+
}
40+
2941
// ---
3042

3143
// This is used to trigger rebuilds. Just updating the timestamp
@@ -262,7 +274,7 @@ function generateRules(tailwindConfig, candidates, context) {
262274

263275
function buildStylesheet(rules, context) {
264276
let sortedRules = rules.sort(([a], [z]) => {
265-
return Math.sign(Number(a - z))
277+
return sign(a - z)
266278
})
267279

268280
let returnValue = {
@@ -1132,7 +1144,7 @@ module.exports = (pluginOptions = {}) => {
11321144
}
11331145

11341146
// Inject the rules, sorted, correctly
1135-
for (let [sort, sibling] of siblings.sort(([a], [z]) => Math.sign(Number(z - a)))) {
1147+
for (let [sort, sibling] of siblings.sort(([a], [z]) => sign(z - a))) {
11361148
// `apply.parent` is refering to the node at `.abc` in: .abc { @apply mt-2 }
11371149
apply.parent.after(sibling)
11381150
}

0 commit comments

Comments
 (0)