| 347 | } |
| 348 | |
| 349 | function getPropertySort(nodes: AstNode[]) { |
| 350 | // Determine sort order based on properties used |
| 351 | let order = new Set<number>() |
| 352 | let count = 0 |
| 353 | let q: AstNode[] = nodes.slice() |
| 354 | |
| 355 | let seenTwSort = false |
| 356 | |
| 357 | while (q.length > 0) { |
| 358 | // SAFETY: At this point it is safe to use TypeScript's non-null assertion |
| 359 | // operator because we guarded against `q.length > 0` above. |
| 360 | let node = q.shift()! |
| 361 | if (node.kind === 'declaration') { |
| 362 | // Empty strings should still be counted, e.g.: `--tw-foo:;` is valid |
| 363 | if (node.value === undefined) continue |
| 364 | |
| 365 | count++ |
| 366 | |
| 367 | if (seenTwSort) continue |
| 368 | |
| 369 | if (node.property === '--tw-sort') { |
| 370 | let idx = GLOBAL_PROPERTY_ORDER.indexOf(node.value ?? '') |
| 371 | if (idx !== -1) { |
| 372 | order.add(idx) |
| 373 | seenTwSort = true |
| 374 | continue |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | let idx = GLOBAL_PROPERTY_ORDER.indexOf(node.property) |
| 379 | if (idx !== -1) order.add(idx) |
| 380 | } else if (node.kind === 'rule' || node.kind === 'at-rule') { |
| 381 | for (let child of node.nodes) { |
| 382 | q.push(child) |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return { |
| 388 | order: Array.from(order).sort((a, z) => a - z), |
| 389 | count, |
| 390 | } |
| 391 | } |