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

Improve RegEx parser, reduce possibilities as the key for arbitrary properties #12121

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update types to work with `Node16` module resolution ([#12097](https://github.com/tailwindlabs/tailwindcss/pull/12097))
- Don’t crash when important and parent selectors are equal in `@apply` ([#12112](https://github.com/tailwindlabs/tailwindcss/pull/12112))
- Eliminate irrelevant rules when applying variants ([#12113](https://github.com/tailwindlabs/tailwindcss/pull/12113))
- Improve RegEx parser, reduce possibilities as the key for arbitrary properties ([#12121](https://github.com/tailwindlabs/tailwindcss/pull/12121))

### Added

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/lib/defaultExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export function defaultExtractor(context) {
let results = []

for (let pattern of patterns) {
results = [...results, ...(content.match(pattern) ?? [])]
for (let result of content.match(pattern) ?? []) {
results.push(clipAtBalancedParens(result))
}
}

return results.filter((v) => v !== undefined).map(clipAtBalancedParens)
return results
}
}

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

// Utilities
regex.pattern([
Expand Down
21 changes: 21 additions & 0 deletions tests/parse-candidate-strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,25 @@ describe.each([
expect(extractions).not.toContain(`bold`)
})
})

describe('real world', () => {
it.each([
[
'const myCVAComponent = cva([],{defaultVariants:{size:"md"},variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
],
[
'const myCVAComponent = cva([],{defaultVariants:{size:"md"}, variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
],
[
'const myCVAComponent = cva("",{defaultVariants:{size:"md"},variants:{size:{sm:["p-1"],md:["p-1.5"],lg:["p-2"],xl:["p-2.5"]}}});',
],
])('should work for issue #12109 (%#)', async (content) => {
let extractions = parse(content)

expect(extractions).toContain('p-1')
expect(extractions).toContain('p-1.5')
expect(extractions).toContain('p-2')
expect(extractions).toContain('p-2.5')
})
})
})