Skip to content

Commit

Permalink
Don’t crash when scanning candidate matching the prefix (#14588)
Browse files Browse the repository at this point in the history
When a prefix is set in a stylesheet and we found a candidate that is
equal to the prefix we would crash:
```css
@import "tailwindcss" prefix(tomato);
```

```js
console.log("tomato")
```

This PR fixes this case by ensuring that we have something that looks
like a variant before considering a prefix.
  • Loading branch information
thecrypticace authored Oct 4, 2024
1 parent 0f37845 commit 60b0e9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Don’t crash when scanning a candidate equal to the configured prefix ([#14588](https://github.com/tailwindlabs/tailwindcss/pull/14588))

## [4.0.0-alpha.26] - 2024-10-03

Expand Down
1 change: 1 addition & 0 deletions packages/tailwindcss/src/candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter
// all utilities must start with that variant which we will then remove from
// the variant list so no other part of the codebase has to know about it.
if (designSystem.theme.prefix) {
if (rawVariants.length === 1) return null
if (rawVariants[0] !== designSystem.theme.prefix) return null

rawVariants.shift()
Expand Down
16 changes: 16 additions & 0 deletions packages/tailwindcss/src/prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,19 @@ test('a prefix must be letters only', async () => {
`[Error: The prefix "__" is invalid. Prefixes must be lowercase ASCII letters (a-z) only.]`,
)
})

test('a candidate matching the prefix does not crash', async () => {
let input = css`
@theme reference prefix(tomato);
@tailwind utilities;
`

let compiler = await compile(input)

expect(compiler.build(['tomato', 'tomato:flex'])).toMatchInlineSnapshot(`
".tomato\\:flex {
display: flex;
}
"
`)
})

0 comments on commit 60b0e9c

Please sign in to comment.