Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65dc530

Browse files
Do not allow variants to end with - or _ (#18872)
This PR is a followup of #18867, but this time we won't allow `@custom-variant` to end with `-` or `_`. The same reasoning applies here where Oxide doesn't pick this up but Intellisense and Tailwind CSS' core does. --------- Co-authored-by: Jordan Pittman <[email protected]>
1 parent 54c3f30 commit 65dc530

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Hide internal fields from completions in `matchUtilities` ([#18820](https://github.com/tailwindlabs/tailwindcss/pull/18820))
2323
- Ignore `.vercel` folders by default (can be overridden by `@source …` rules) ([#18855](https://github.com/tailwindlabs/tailwindcss/pull/18855))
2424
- Consider variants starting with `@-` to be invalid (e.g. `@-2xl:flex`) ([#18869](https://github.com/tailwindlabs/tailwindcss/pull/18869))
25-
- Do not allow custom variants to start with a `-` ([#18867](https://github.com/tailwindlabs/tailwindcss/pull/18867))
25+
- Do not allow custom variants to start or end with a `-` or `_` ([#18867](https://github.com/tailwindlabs/tailwindcss/pull/18867), [#18872](https://github.com/tailwindlabs/tailwindcss/pull/18872))
2626
- Upgrade: Migrate `aria` theme keys to `@custom-variant` ([#18815](https://github.com/tailwindlabs/tailwindcss/pull/18815))
2727
- Upgrade: Migrate `data` theme keys to `@custom-variant` ([#18816](https://github.com/tailwindlabs/tailwindcss/pull/18816))
2828
- Upgrade: Migrate `supports` theme keys to `@custom-variant` ([#18817](https://github.com/tailwindlabs/tailwindcss/pull/18817))

packages/tailwindcss/src/index.test.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,24 +3772,31 @@ describe('@custom-variant', () => {
37723772
)
37733773
})
37743774

3775-
test('@custom-variant cannot contain dashes on its own', () => {
3776-
return expect(
3777-
compileCss(css`
3778-
@custom-variant - (&.dash);
3779-
`),
3780-
).rejects.toThrowErrorMatchingInlineSnapshot(
3781-
`[Error: \`@custom-variant -\` defines an invalid variant name. Variants should only contain alphanumeric, dashes, or underscore characters and start with a lowercase letter or number.]`,
3782-
)
3783-
})
3784-
3785-
test('@custom-variant cannot contain multiple dashes on their own', () => {
3786-
return expect(
3787-
compileCss(css`
3788-
@custom-variant --- (&.dashed);
3789-
`),
3790-
).rejects.toThrowErrorMatchingInlineSnapshot(
3791-
`[Error: \`@custom-variant ---\` defines an invalid variant name. Variants should only contain alphanumeric, dashes, or underscore characters and start with a lowercase letter or number.]`,
3792-
)
3775+
test.each([
3776+
// Cannot be a dash on its own
3777+
[`@custom-variant - (&);`],
3778+
// Cannot be multiple dashes on their own
3779+
[`@custom-variant --- (&);`],
3780+
// Cannot be an underscore on its own
3781+
[`@custom-variant _ (&);`],
3782+
// Cannot be multiple underscores on their own
3783+
[`@custom-variant ___ (&);`],
3784+
3785+
// Cannot start with a dash
3786+
[`@custom-variant -foo (&);`],
3787+
[`@custom-variant --foo (&);`],
3788+
// Cannot start with an underscore
3789+
[`@custom-variant _foo (&);`],
3790+
[`@custom-variant __foo (&);`],
3791+
3792+
// Cannot end with a dash
3793+
[`@custom-variant foo- (&);`],
3794+
[`@custom-variant foo-- (&);`],
3795+
// Cannot end with an underscore
3796+
[`@custom-variant foo_ (&);`],
3797+
[`@custom-variant foo__ (&);`],
3798+
])('@custom-variant must have a valid name', (input) => {
3799+
return expect(compileCss(input)).rejects.toThrowError()
37933800
})
37943801

37953802
test('@custom-variant must not container special characters', () => {

packages/tailwindcss/src/variants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { DefaultMap } from './utils/default-map'
1818
import { isPositiveInteger } from './utils/infer-data-type'
1919
import { segment } from './utils/segment'
2020

21-
export const IS_VALID_VARIANT_NAME = /^@?[a-z0-9][a-zA-Z0-9_-]*$/
21+
export const IS_VALID_VARIANT_NAME = /^@?[a-z0-9][a-zA-Z0-9_-]*(?<![_-])$/
2222

2323
type VariantFn<T extends Variant['kind']> = (
2424
rule: Rule,

0 commit comments

Comments
 (0)