Skip to content

Commit ff8fd8c

Browse files
Don’t replace _ in suggested theme keys (#16433)
Fixes tailwindlabs/tailwindcss-intellisense#1184 The alpha and beta releases used `_` in theme keys to represent a `.`. This meant we used `--leading-1_5` instead of `--leading-1\.5` to add utilities like `leading-1.5`. We prefer the use of the escaped dot now but still want to make sure suggestions for the legacy key format still works as expected when surrounded by numbers.
1 parent aad440e commit ff8fd8c

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Export `tailwindcss/lib/util/flattenColorPalette.js` for backward compatibility ([#16411](https://github.com/tailwindlabs/tailwindcss/pull/16411))
1313
- Fix sorting numeric utilities when they have different magnitudes ([#16414](https://github.com/tailwindlabs/tailwindcss/pull/16414))
1414
- Show suggestions for fractions in IntelliSense ([#16353](https://github.com/tailwindlabs/tailwindcss/pull/16353))
15+
- Don’t replace `_` in suggested theme keys ([#16433](https://github.com/tailwindlabs/tailwindcss/pull/16433))
1516

1617
## [4.0.6] - 2025-02-10
1718

packages/tailwindcss/src/intellisense.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,40 @@ test('Custom functional @utility', async () => {
526526
expect(classNames).toContain('example-xs')
527527
expect(classMap.get('example-xs')?.modifiers).toEqual(['foo', 'bar'])
528528
})
529+
530+
test('Theme keys with underscores are suggested with underscores', async () => {
531+
let input = css`
532+
@import 'tailwindcss/utilities';
533+
534+
@theme {
535+
/* Disable the spacing scale */
536+
--spacing: initial;
537+
538+
/* This will get suggeted with a dot because its surrounded by numbers */
539+
--spacing-1_5: 1.5rem;
540+
541+
/* This will get suggeted with a dot */
542+
--spacing-2\.5: 1.5rem;
543+
544+
/* This will get suggeted with an underscore */
545+
--spacing-logo_margin: 0.875rem;
546+
}
547+
`
548+
549+
let design = await __unstable__loadDesignSystem(input, {
550+
loadStylesheet: async (_, base) => ({
551+
base,
552+
content: '@tailwind utilities;',
553+
}),
554+
})
555+
556+
let entries = design.getClassList().filter(([name]) => name.startsWith('p-'))
557+
558+
expect(entries).toContainEqual(['p-1.5', { modifiers: [] }])
559+
expect(entries).toContainEqual(['p-2.5', { modifiers: [] }])
560+
expect(entries).toContainEqual(['p-logo_margin', { modifiers: [] }])
561+
562+
expect(entries).not.toContainEqual(['p-1_5', { modifiers: [] }])
563+
expect(entries).not.toContainEqual(['p-2_5', { modifiers: [] }])
564+
expect(entries).not.toContainEqual(['p-logo.margin', { modifiers: [] }])
565+
})

packages/tailwindcss/src/utilities.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,20 @@ export function createUtilities(theme: Theme) {
220220
* Register list of suggestions for a class
221221
*/
222222
function suggest(classRoot: string, defns: () => SuggestionDefinition[]) {
223+
/**
224+
* The alpha and beta releases used `_` in theme keys to represent a `.`. This meant we used
225+
* `--leading-1_5` instead of `--leading-1\.5` to add utilities like `leading-1.5`.
226+
*
227+
* We prefer the use of the escaped dot now but still want to make sure suggestions for the
228+
* legacy key format still works as expected when surrounded by numbers.
229+
*/
230+
const LEGACY_NUMERIC_KEY = /(\d+)_(\d+)/g
231+
223232
function* resolve(themeKeys: ThemeKey[]) {
224233
for (let value of theme.keysInNamespaces(themeKeys)) {
225-
yield value.replaceAll('_', '.')
234+
yield value.replace(LEGACY_NUMERIC_KEY, (_, a, b) => {
235+
return `${a}.${b}`
236+
})
226237
}
227238
}
228239

0 commit comments

Comments
 (0)