Skip to content

Commit

Permalink
Ignore unset values (like null or undefined) when resolving the c…
Browse files Browse the repository at this point in the history
…lassList for intellisense (#9385)

* ignored `undefined` and `null` value values for intellisense

We are not completely ignoring "all" falsey values, because then we
would get rid of `0` values (e.g.: `p-0`) which is not what we want.

* update changelog
  • Loading branch information
RobinMalfait committed Sep 21, 2022
1 parent 063ca64 commit e625252
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improve data type analyses for arbitrary values ([#9320](https://github.com/tailwindlabs/tailwindcss/pull/9320))
- Don't emit generated utilities with invalid uses of theme functions ([#9319](https://github.com/tailwindlabs/tailwindcss/pull/9319))
- Revert change that only listened for stdin close on TTYs ([#9331](https://github.com/tailwindlabs/tailwindcss/pull/9331))
- Ignore unset values (like `null` or `undefined`) when resolving the classList for intellisense ([#9385](https://github.com/tailwindlabs/tailwindcss/pull/9385))

## [3.1.8] - 2022-08-05

Expand Down
5 changes: 5 additions & 0 deletions src/lib/setupContextUtils.js
Expand Up @@ -838,6 +838,11 @@ function registerPlugins(plugins, context) {
let negativeClasses = []

for (let [key, value] of Object.entries(options?.values ?? {})) {
// Ignore undefined and null values
if (value == null) {
continue
}

output.push(formatClass(utilName, key))
if (options?.supportsNegativeValues && negateValue(value)) {
negativeClasses.push(formatClass(utilName, `-${key}`))
Expand Down
35 changes: 35 additions & 0 deletions tests/getClassList.test.js
Expand Up @@ -82,3 +82,38 @@ it('should not generate utilities with opacity even if safe-listed', () => {

expect(classes).not.toContain('bg-red-500/50')
})

it('should not generate utilities that are set to undefined or null to so that they are removed', () => {
let config = {
theme: {
extend: {
colors: {
red: null,
green: undefined,
blue: {
100: null,
200: undefined,
},
},
},
},
safelist: [
{
pattern: /^bg-(red|green|blue)-.*$/,
},
],
}

let context = createContext(resolveConfig(config))
let classes = context.getClassList()

expect(classes).not.toContain('bg-red-100') // Red is `null`

expect(classes).not.toContain('bg-green-100') // Green is `undefined`

expect(classes).not.toContain('bg-blue-100') // Blue.100 is `null`
expect(classes).not.toContain('bg-blue-200') // Blue.200 is `undefined`

expect(classes).toContain('bg-blue-50')
expect(classes).toContain('bg-blue-300')
})

0 comments on commit e625252

Please sign in to comment.