Skip to content

Commit 0aeec86

Browse files
Don't crash when setting JS theme value to null'
1 parent ac202ff commit 0aeec86

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

packages/tailwindcss/src/compat/apply-config-to-theme.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Theme, ThemeOptions } from '../theme'
44
import { applyConfigToTheme, keyPathToCssProperty } from './apply-config-to-theme'
55
import { resolveConfig } from './config/resolve-config'
66

7+
const css = String.raw
8+
79
test('config values can be merged into the theme', () => {
810
let theme = new Theme()
911
let design = buildDesignSystem(theme)
@@ -223,3 +225,39 @@ test('converts opacity modifiers from decimal to percentage values', () => {
223225
expect(theme.resolve('20', ['--opacity'])).toEqual('20%')
224226
expect(theme.resolve('25', ['--opacity'])).toEqual('25%')
225227
})
228+
229+
test('handles setting theme keys to null', async () => {
230+
let theme = new Theme()
231+
let design = buildDesignSystem(theme)
232+
233+
theme.add('--color-blue-400', 'blue', ThemeOptions.DEFAULT)
234+
theme.add('--color-blue-500', '#3b82f6')
235+
theme.add('--color-red-400', 'red', ThemeOptions.DEFAULT)
236+
theme.add('--color-red-500', '#ef4444')
237+
238+
let { resolvedConfig, replacedThemeKeys } = resolveConfig(design, [
239+
{
240+
config: {
241+
theme: {
242+
extend: {
243+
colors: {
244+
blue: null,
245+
},
246+
},
247+
},
248+
},
249+
base: '/root',
250+
reference: false,
251+
},
252+
])
253+
applyConfigToTheme(design, resolvedConfig, replacedThemeKeys)
254+
255+
expect(theme.namespace('--color')).toMatchInlineSnapshot(`
256+
Map {
257+
"blue-400" => "blue",
258+
"blue-500" => "#3b82f6",
259+
"red-400" => "red",
260+
"red-500" => "#ef4444",
261+
}
262+
`)
263+
})

packages/tailwindcss/src/compat/config.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,3 +1628,54 @@ test('old theme values are merged with their renamed counterparts in the CSS the
16281628

16291629
expect(didCallPluginFn).toHaveBeenCalled()
16301630
})
1631+
1632+
test('handles setting theme keys to null', async () => {
1633+
let compiler = await compile(
1634+
css`
1635+
@theme default {
1636+
--color-red-50: oklch(0.971 0.013 17.38);
1637+
--color-red-100: oklch(0.936 0.032 17.717);
1638+
}
1639+
@config "./my-config.js";
1640+
@tailwind utilities;
1641+
@theme {
1642+
--color-red-100: oklch(0.936 0.032 17.717);
1643+
--color-red-200: oklch(0.885 0.062 18.334);
1644+
}
1645+
`,
1646+
{
1647+
loadModule: async () => {
1648+
return {
1649+
module: {
1650+
theme: {
1651+
extend: {
1652+
colors: {
1653+
red: null,
1654+
},
1655+
},
1656+
},
1657+
},
1658+
base: '/root',
1659+
}
1660+
},
1661+
},
1662+
)
1663+
1664+
expect(compiler.build(['bg-red-50', 'bg-red-100', 'bg-red-200'])).toMatchInlineSnapshot(`
1665+
":root, :host {
1666+
--color-red-50: oklch(0.971 0.013 17.38);
1667+
--color-red-100: oklch(0.936 0.032 17.717);
1668+
--color-red-200: oklch(0.885 0.062 18.334);
1669+
}
1670+
.bg-red-50 {
1671+
background-color: var(--color-red-50);
1672+
}
1673+
.bg-red-100 {
1674+
background-color: var(--color-red-100);
1675+
}
1676+
.bg-red-200 {
1677+
background-color: var(--color-red-200);
1678+
}
1679+
"
1680+
`)
1681+
})

packages/tailwindcss/src/compat/config/resolve-config.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,48 @@ test('theme keys can read from the CSS theme', () => {
254254
new Set(['colors', 'accentColor', 'placeholderColor', 'caretColor', 'transitionColor']),
255255
)
256256
})
257+
258+
test('handles null as theme values', () => {
259+
let theme = new Theme()
260+
theme.add('--color-red-50', 'red')
261+
theme.add('--color-red-100', 'red')
262+
263+
let design = buildDesignSystem(theme)
264+
265+
let { resolvedConfig, replacedThemeKeys } = resolveConfig(design, [
266+
{
267+
config: {
268+
theme: {
269+
colors: ({ theme }) => ({
270+
// Reads from the --color-* namespace
271+
...theme('color'),
272+
}),
273+
},
274+
},
275+
base: '/root',
276+
reference: false,
277+
},
278+
{
279+
config: {
280+
theme: {
281+
extend: {
282+
colors: {
283+
red: null,
284+
},
285+
},
286+
},
287+
},
288+
base: '/root',
289+
reference: false,
290+
},
291+
])
292+
293+
expect(resolvedConfig).toMatchObject({
294+
theme: {
295+
colors: {
296+
red: null,
297+
},
298+
},
299+
})
300+
expect(replacedThemeKeys).toEqual(new Set(['colors']))
301+
})

packages/tailwindcss/src/compat/plugin-functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function get(obj: any, path: string[]) {
224224
let key = path[i]
225225

226226
// The key does not exist so concatenate it with the next key
227-
if (obj[key] === undefined) {
227+
if (obj?.[key] === undefined) {
228228
if (path[i + 1] === undefined) {
229229
return undefined
230230
}

0 commit comments

Comments
 (0)