Skip to content

Prevent duplicate suggestions when using @theme and @utility together #17675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/tailwindcss/src/intellisense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,25 @@ test('Theme keys with underscores are suggested with underscores', async () => {
expect(entries).not.toContainEqual(['p-2_5', { modifiers: [] }])
expect(entries).not.toContainEqual(['p-logo.margin', { modifiers: [] }])
})

test('Custom @utility and existing utility with names matching theme keys dont give duplicate results', async () => {
let input = css`
@theme reference {
--leading-sm: 0.25rem;
--text-header: 1.5rem;
}

@utility text-header {
text-transform: uppercase;
}
`

let design = await __unstable__loadDesignSystem(input)

let classList = design.getClassList()
let classMap = new Map(classList)
let matches = classList.filter(([className]) => className === 'text-header')

expect(matches).toHaveLength(1)
expect(classMap.get('text-header')?.modifiers).toEqual(['sm'])
})
39 changes: 19 additions & 20 deletions packages/tailwindcss/src/intellisense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ export type ClassEntry = [string, ClassMetadata]
const IS_FRACTION = /^\d+\/\d+$/

export function getClassList(design: DesignSystem): ClassEntry[] {
let list: ClassItem[] = []
let items = new DefaultMap<string, ClassItem>((utility) => ({
name: utility,
utility,
fraction: false,
modifiers: [],
}))

// Static utilities only work as-is
for (let utility of design.utilities.keys('static')) {
list.push({
name: utility,
utility,
fraction: false,
modifiers: [],
})
let item = items.get(utility)
item.fraction = false
item.modifiers = []
}

// Functional utilities have their own list of completions
Expand All @@ -42,28 +44,25 @@ export function getClassList(design: DesignSystem): ClassEntry[] {

let name = value === null ? utility : `${utility}-${value}`

list.push({
name,
utility,
fraction,
modifiers: group.modifiers,
})
let item = items.get(name)
item.utility = utility
item.fraction ||= fraction
item.modifiers.push(...group.modifiers)

if (group.supportsNegative) {
list.push({
name: `-${name}`,
utility: `-${utility}`,
fraction,
modifiers: group.modifiers,
})
let item = items.get(`-${name}`)
item.utility = `-${utility}`
item.fraction ||= fraction
item.modifiers.push(...group.modifiers)
}
}
}
}

if (list.length === 0) return []
if (items.size === 0) return []

// Sort utilities by their class name
let list = Array.from(items.values())
list.sort((a, b) => compare(a.name, b.name))

let entries = sortFractionsLast(list)
Expand Down