Skip to content

Commit 4b84870

Browse files
committed
Eliminate recursion from candidatePermutations
1 parent 9f23bb3 commit 4b84870

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/lib/generateRules.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ function getClassNameFromSelector(selector) {
2525
// Example with dynamic classes:
2626
// ['grid-cols', '[[linename],1fr,auto]']
2727
// ['grid', 'cols-[[linename],1fr,auto]']
28-
function* candidatePermutations(candidate, lastIndex = Infinity) {
29-
if (lastIndex < 0) {
30-
return
31-
}
28+
function* candidatePermutations(candidate) {
29+
let lastIndex = Infinity
3230

33-
let dashIdx
31+
while (lastIndex >= 0) {
32+
let dashIdx
3433

35-
if (lastIndex === Infinity && candidate.endsWith(']')) {
36-
let bracketIdx = candidate.indexOf('[')
34+
if (lastIndex === Infinity && candidate.endsWith(']')) {
35+
let bracketIdx = candidate.indexOf('[')
3736

38-
// If character before `[` isn't a dash or a slash, this isn't a dynamic class
39-
// eg. string[]
40-
dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
41-
} else {
42-
dashIdx = candidate.lastIndexOf('-', lastIndex)
43-
}
37+
// If character before `[` isn't a dash or a slash, this isn't a dynamic class
38+
// eg. string[]
39+
dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
40+
} else {
41+
dashIdx = candidate.lastIndexOf('-', lastIndex)
42+
}
4443

45-
if (dashIdx < 0) {
46-
return
47-
}
44+
if (dashIdx < 0) {
45+
break
46+
}
4847

49-
let prefix = candidate.slice(0, dashIdx)
50-
let modifier = candidate.slice(dashIdx + 1)
48+
let prefix = candidate.slice(0, dashIdx)
49+
let modifier = candidate.slice(dashIdx + 1)
5150

52-
yield [prefix, modifier]
51+
yield [prefix, modifier]
5352

54-
yield* candidatePermutations(candidate, dashIdx - 1)
53+
lastIndex = dashIdx - 1
54+
}
5555
}
5656

5757
function applyPrefix(matches, context) {

tests/basic-usage.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,19 @@ it('shadows support values without a leading zero', () => {
172172
`)
173173
})
174174
})
175+
176+
it('can scan extremely long classes without crashing', () => {
177+
let val = 'cols-' + '-a'.repeat(4000)
178+
let config = {
179+
content: [{ raw: html`<div class="${val}"></div>` }],
180+
corePlugins: { preflight: false },
181+
}
182+
183+
let input = css`
184+
@tailwind utilities;
185+
`
186+
187+
return run(input, config).then((result) => {
188+
expect(result.css).toMatchFormattedCss(css``)
189+
})
190+
})

0 commit comments

Comments
 (0)