Skip to content

Commit

Permalink
Eliminate recursion from candidatePermutations
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Feb 4, 2022
1 parent 9f23bb3 commit 66d6313
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/lib/generateRules.js
Expand Up @@ -25,33 +25,33 @@ function getClassNameFromSelector(selector) {
// Example with dynamic classes:
// ['grid-cols', '[[linename],1fr,auto]']
// ['grid', 'cols-[[linename],1fr,auto]']
function* candidatePermutations(candidate, lastIndex = Infinity) {
if (lastIndex < 0) {
return
}
function* candidatePermutations(candidate) {
let lastIndex = Infinity

let dashIdx
while (lastIndex >= 0) {
let dashIdx

if (lastIndex === Infinity && candidate.endsWith(']')) {
let bracketIdx = candidate.indexOf('[')
if (lastIndex === Infinity && candidate.endsWith(']')) {
let bracketIdx = candidate.indexOf('[')

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

if (dashIdx < 0) {
return
}
if (dashIdx < 0) {
break
}

let prefix = candidate.slice(0, dashIdx)
let modifier = candidate.slice(dashIdx + 1)
let prefix = candidate.slice(0, dashIdx)
let modifier = candidate.slice(dashIdx + 1)

yield [prefix, modifier]
yield [prefix, modifier]

yield* candidatePermutations(candidate, dashIdx - 1)
lastIndex = dashIdx - 1
}
}

function applyPrefix(matches, context) {
Expand Down
16 changes: 16 additions & 0 deletions tests/basic-usage.test.js
Expand Up @@ -172,3 +172,19 @@ it('shadows support values without a leading zero', () => {
`)
})
})

it('can scan extremely long classes without crashing', () => {
let val = 'cols-' + "-a".repeat(4000)
let config = {
content: [{ raw: html`<div class="${val}"></div>` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css``)
})
})

0 comments on commit 66d6313

Please sign in to comment.