Skip to content

Commit 0af88b1

Browse files
authored
Improve RegEx parser, reduce possibilities as the key for arbitrary properties (#12121)
* optimize handling of RegEx parser results Previous: - Copy `results`, for every subsequent result of other `patterns` - Loop over results to filter out `undefined` values - Loop over results to map to `clipAtBalancedParens` Current: - For each candidate, push the `clipAtBalancedParens(candidate)` into the `results` This way we are not copying existing results, and we are also avoiding additional loops over the entire array to filter out `undefined` values and map to `clipAtBalancedParens`. * do not allow `]` in the first part of arbitrary properties ``` [foo:bar] ─┬─ └── This part cannot contain `]` ``` This is also a very targeted fix for when the arbitrary properties seem to match a large piece of text, but shouldn't * add real world tests for parsing candidate strings * sync package-lock.json * update changelog
1 parent 5542340 commit 0af88b1

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Update types to work with `Node16` module resolution ([#12097](https://github.com/tailwindlabs/tailwindcss/pull/12097))
2424
- Don’t crash when important and parent selectors are equal in `@apply` ([#12112](https://github.com/tailwindlabs/tailwindcss/pull/12112))
2525
- Eliminate irrelevant rules when applying variants ([#12113](https://github.com/tailwindlabs/tailwindcss/pull/12113))
26+
- Improve RegEx parser, reduce possibilities as the key for arbitrary properties ([#12121](https://github.com/tailwindlabs/tailwindcss/pull/12121))
2627

2728
### Added
2829

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/defaultExtractor.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ export function defaultExtractor(context) {
1111
let results = []
1212

1313
for (let pattern of patterns) {
14-
results = [...results, ...(content.match(pattern) ?? [])]
14+
for (let result of content.match(pattern) ?? []) {
15+
results.push(clipAtBalancedParens(result))
16+
}
1517
}
1618

17-
return results.filter((v) => v !== undefined).map(clipAtBalancedParens)
19+
return results
1820
}
1921
}
2022

@@ -33,7 +35,7 @@ function* buildRegExps(context) {
3335
// This is a targeted fix to continue to allow theme()
3436
// with square brackets to work in arbitrary properties
3537
// while fixing a problem with the regex matching too much
36-
/\[[^\s:'"`]+:[^\s]+?\[[^\s]+\][^\s]+?\]/,
38+
/\[[^\s:'"`\]]+:[^\s]+?\[[^\s]+\][^\s]+?\]/,
3739

3840
// Utilities
3941
regex.pattern([

tests/parse-candidate-strings.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,25 @@ describe.each([
438438
expect(extractions).not.toContain(`bold`)
439439
})
440440
})
441+
442+
describe('real world', () => {
443+
it.each([
444+
[
445+
'const myCVAComponent = cva([],{defaultVariants:{size:"md"},variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
446+
],
447+
[
448+
'const myCVAComponent = cva([],{defaultVariants:{size:"md"}, variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
449+
],
450+
[
451+
'const myCVAComponent = cva("",{defaultVariants:{size:"md"},variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
452+
],
453+
])('should work for issue #12109 (%#)', async (content) => {
454+
let extractions = parse(content)
455+
456+
expect(extractions).toContain('p-1')
457+
expect(extractions).toContain('p-1.5')
458+
expect(extractions).toContain('p-2')
459+
expect(extractions).toContain('p-2.5')
460+
})
461+
})
441462
})

0 commit comments

Comments
 (0)