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

Commit 3abb7ed

Browse files
committed
Error if variants are used in wrong order
1 parent dafb3d3 commit 3abb7ed

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

src/index.test.css

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@
119119
--tw-bg-opacity: 1;
120120
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
121121
}
122-
.group:hover .group-hover\:dark .apply-dark-group-example-b {
123-
--tw-bg-opacity: 1;
124-
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
125-
}
126122
@media (min-width: 640px) {
127123
@media (prefers-reduced-motion: no-preference) {
128124
.group:active .crazy-example:focus {
@@ -357,7 +353,7 @@ div {
357353
--tw-ring-opacity: 1;
358354
--tw-ring-color: rgba(59, 130, 246, var(--tw-ring-opacity));
359355
}
360-
.hover\:focus\:font-light:hover:focus {
356+
.focus\:hover\:font-light:focus:hover {
361357
font-weight: 300;
362358
}
363359
.disabled\:font-bold:disabled {
@@ -512,14 +508,12 @@ div {
512508
}
513509
}
514510
@media (prefers-reduced-motion: no-preference) {
515-
.dark .md\:dark\:motion-safe\:foo\:active\:custom-util:active {
511+
.foo\:dark .md\:dark\:motion-safe\:active\:custom-util:active {
516512
background: #abcdef !important;
517513
}
518514
}
519-
}
520-
@media (min-width: 640px) {
521-
@media (min-width: 768px) {
522-
.sm\:md\:text-center {
515+
@media (min-width: 640px) {
516+
.md\:sm\:text-center {
523517
text-align: center;
524518
}
525519
}

src/index.test.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<div class="sm:custom-util"></div>
2323
<div class="dark:custom-util"></div>
2424
<div class="motion-safe:custom-util"></div>
25-
<div class="md:dark:motion-safe:foo:active:custom-util"></div>
25+
<div class="md:foo:dark:motion-safe:active:custom-util"></div>
2626
<div class="aspect-w-1 aspect-h-2"></div>
2727
<div class="aspect-w-3 aspect-h-4"></div>
2828
<div class="filter-none filter-grayscale"></div>
@@ -33,8 +33,7 @@
3333
<div class="focus:ring-2 focus:ring-blue-500"></div>
3434
<div class="hover:font-bold"></div>
3535
<div class="disabled:font-bold"></div>
36-
<div class="hover:focus:font-light"></div>
37-
<div class="sm:font-normal:bg-black"></div>
36+
<div class="focus:hover:font-light"></div>
3837
<div class="first:pt-0"></div>
3938
<div class="container"></div>
4039
<div class="group-hover:opacity-100"></div>
@@ -43,8 +42,8 @@
4342
<div class="motion-safe:transition"></div>
4443
<div class="motion-reduce:transition"></div>
4544
<div class="md:motion-safe:hover:transition"></div>
46-
<div class="sm:md:text-center shadow-sm md:shadow-sm"></div>
47-
<div class="sm:md:text-center shadow-sm md:shadow-sm"></div>
45+
<div class="md:sm:text-center shadow-sm md:shadow-sm"></div>
46+
<div class="md:sm:text-center shadow-sm md:shadow-sm"></div>
4847
<div class="bg-green-500 md:opacity-50 md:hover:opacity-20 sm:tabular-nums"></div>
4948
<div class="text-center shadow-md hover:shadow-lg transform scale-50 hover:scale-75"></div>
5049
</body>

src/index.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ test('it works', () => {
139139
.apply-dark-group-example-a {
140140
@apply dark:group-hover:bg-green-500;
141141
}
142-
.apply-dark-group-example-b {
143-
@apply group-hover:dark:bg-green-500;
144-
}
145142
.crazy-example {
146143
@apply sm:motion-safe:group-active:focus:opacity-10;
147144
}

src/lib/generateRules.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const postcss = require('postcss')
22
const { default: parseObjectStyles } = require('tailwindcss/lib/util/parseObjectStyles')
33
const { transformAllSelectors } = require('../pluginUtils')
4-
const { toPostCssNode, isPlainObject } = require('./utils')
4+
const { toPostCssNode, isPlainObject, bigSign } = require('./utils')
55
const selectorParser = require('postcss-selector-parser')
66

77
let classNameParser = selectorParser((selectors) => {
@@ -131,10 +131,22 @@ function resolveMatchedPlugins(classCandidate, context) {
131131
return null
132132
}
133133

134+
function sortAgainst(toSort, against) {
135+
return toSort.slice().sort((a, z) => {
136+
return bigSign(against.get(a)[0] - against.get(z)[0])
137+
})
138+
}
139+
134140
function resolveMatches(candidate, context) {
135141
let [classCandidate, ...variants] = candidate.split(':').reverse()
136142
let matchedPlugins = resolveMatchedPlugins(classCandidate, context)
137143

144+
let sorted = sortAgainst(variants, context.variantMap)
145+
if (sorted.toString() !== variants.toString()) {
146+
let corrected = sorted.reverse().concat(classCandidate).join(':')
147+
throw new Error(`Class ${candidate} should be written as ${corrected}`)
148+
}
149+
138150
if (matchedPlugins === null) {
139151
return []
140152
}

0 commit comments

Comments
 (0)