Skip to content

Commit 5c47128

Browse files
committed
Don’t replace _ in suggested theme keys
1 parent 711b50d commit 5c47128

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/tailwindcss/src/intellisense.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,32 @@ 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 an underscore */
542+
--spacing-logo_margin: 0.875rem;
543+
}
544+
`
545+
546+
let design = await __unstable__loadDesignSystem(input, {
547+
loadStylesheet: async (_, base) => ({
548+
base,
549+
content: '@tailwind utilities;',
550+
}),
551+
})
552+
553+
let entries = design.getClassList().filter(([name]) => name.startsWith('p-'))
554+
555+
expect(entries).toContainEqual(['p-1.5', { modifiers: [] }])
556+
expect(entries).toContainEqual(['p-logo_margin', { modifiers: [] }])
557+
})

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)