Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid over-extracting utilities from candidates with decimal values #13959

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid over-extracting utilities from candidates with decimal values
Prevent candidates like `px-1.5` from generating both the `px-1.5` class and the `px-1` class.
  • Loading branch information
adamwathan committed Jul 5, 2024
commit db92f78b455a4241df66c6bc6ed6efc599e2348f
28 changes: 25 additions & 3 deletions src/lib/defaultExtractor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as regex from './regex'
import { splitAtTopLevelOnly } from '../util/splitAtTopLevelOnly'

export function defaultExtractor(context) {
let patterns = Array.from(buildRegExps(context))
Expand All @@ -16,6 +17,30 @@ export function defaultExtractor(context) {
}
}

// Extract any subclasses from languages like Slim and Pug, eg:
// div.flex.px-5.underline
for (let result of results.slice()) {
let segments = splitAtTopLevelOnly(result, '.')

for (let idx = 0; idx < segments.length; idx++) {
let segment = segments[idx]
if (idx >= segments.length - 1) {
results.push(segment)
continue
}

// If the next segment is a number, discard both, for example seeing
// `px-1` and `5` means the real candidate was `px-1.5` which is already
// captured.
let next = parseInt(segments[idx + 1])
if (isNaN(next)) {
results.push(segment)
} else {
idx++
}
}
}

return results
}
}
Expand Down Expand Up @@ -127,9 +152,6 @@ function* buildRegExps(context) {
utility,
])
}

// 5. Inner matches
yield /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g
}

// We want to capture any "special" characters
Expand Down
13 changes: 13 additions & 0 deletions tests/default-extractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,19 @@ test('classes in slim templates', async () => {
expect(extractions).toContain('text-gray-500')
})

test("classes with fractional numeric values don't also generate the whole number utility", async () => {
const extractions = defaultExtractor(`
<div class="px-1.5 py-2.75">Hello world!</div>
`)

expect(extractions).toContain('px-1.5')
expect(extractions).toContain('py-2.75')
expect(extractions).not.toContain('px-1')
expect(extractions).not.toContain('5')
expect(extractions).not.toContain('py-2')
expect(extractions).not.toContain('75')
})

test('multi-word + arbitrary values + quotes', async () => {
const extractions = defaultExtractor(`
grid-cols-['repeat(2)']
Expand Down
Loading