Skip to content

Commit 001e16b

Browse files
committed
Update variant handling, use getVariants API
1 parent d073bb9 commit 001e16b

File tree

6 files changed

+318
-128
lines changed

6 files changed

+318
-128
lines changed

packages/tailwindcss-language-server/src/server.ts

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
FeatureFlags,
6161
Settings,
6262
ClassNames,
63+
Variant,
6364
} from 'tailwindcss-language-service/src/util/state'
6465
import {
6566
provideDiagnostics,
@@ -1146,105 +1147,116 @@ function isAtRule(node: Node): node is AtRule {
11461147
return node.type === 'atrule'
11471148
}
11481149

1149-
function getVariants(state: State): Record<string, string> {
1150-
if (state.jit) {
1151-
function escape(className: string): string {
1152-
let node = state.modules.postcssSelectorParser.module.className()
1153-
node.value = className
1154-
return dlv(node, 'raws.value', node.value)
1155-
}
1150+
function getVariants(state: State): Array<Variant> {
1151+
if (state.jitContext?.getVariants) {
1152+
return state.jitContext.getVariants()
1153+
}
11561154

1157-
let result = {}
1155+
if (state.jit) {
1156+
let result: Array<Variant> = []
11581157
// [name, [sort, fn]]
11591158
// [name, [[sort, fn]]]
11601159
Array.from(state.jitContext.variantMap as Map<string, [any, any]>).forEach(
11611160
([variantName, variantFnOrFns]) => {
1162-
let fns = (Array.isArray(variantFnOrFns[0]) ? variantFnOrFns : [variantFnOrFns]).map(
1163-
([_sort, fn]) => fn
1164-
)
1161+
result.push({
1162+
name: variantName,
1163+
values: [],
1164+
isArbitrary: false,
1165+
selectors: () => {
1166+
function escape(className: string): string {
1167+
let node = state.modules.postcssSelectorParser.module.className()
1168+
node.value = className
1169+
return dlv(node, 'raws.value', node.value)
1170+
}
11651171

1166-
let placeholder = '__variant_placeholder__'
1172+
let fns = (Array.isArray(variantFnOrFns[0]) ? variantFnOrFns : [variantFnOrFns]).map(
1173+
([_sort, fn]) => fn
1174+
)
11671175

1168-
let root = state.modules.postcss.module.root({
1169-
nodes: [
1170-
state.modules.postcss.module.rule({
1171-
selector: `.${escape(placeholder)}`,
1172-
nodes: [],
1173-
}),
1174-
],
1175-
})
1176+
let placeholder = '__variant_placeholder__'
11761177

1177-
let classNameParser = state.modules.postcssSelectorParser.module((selectors) => {
1178-
return selectors.first.filter(({ type }) => type === 'class').pop().value
1179-
})
1178+
let root = state.modules.postcss.module.root({
1179+
nodes: [
1180+
state.modules.postcss.module.rule({
1181+
selector: `.${escape(placeholder)}`,
1182+
nodes: [],
1183+
}),
1184+
],
1185+
})
11801186

1181-
function getClassNameFromSelector(selector) {
1182-
return classNameParser.transformSync(selector)
1183-
}
1187+
let classNameParser = state.modules.postcssSelectorParser.module((selectors) => {
1188+
return selectors.first.filter(({ type }) => type === 'class').pop().value
1189+
})
11841190

1185-
function modifySelectors(modifierFunction) {
1186-
root.each((rule) => {
1187-
if (rule.type !== 'rule') {
1188-
return
1191+
function getClassNameFromSelector(selector) {
1192+
return classNameParser.transformSync(selector)
11891193
}
11901194

1191-
rule.selectors = rule.selectors.map((selector) => {
1192-
return modifierFunction({
1193-
get className() {
1194-
return getClassNameFromSelector(selector)
1195+
function modifySelectors(modifierFunction) {
1196+
root.each((rule) => {
1197+
if (rule.type !== 'rule') {
1198+
return
1199+
}
1200+
1201+
rule.selectors = rule.selectors.map((selector) => {
1202+
return modifierFunction({
1203+
get className() {
1204+
return getClassNameFromSelector(selector)
1205+
},
1206+
selector,
1207+
})
1208+
})
1209+
})
1210+
return root
1211+
}
1212+
1213+
let definitions = []
1214+
1215+
for (let fn of fns) {
1216+
let definition: string
1217+
let container = root.clone()
1218+
let returnValue = fn({
1219+
container,
1220+
separator: state.separator,
1221+
modifySelectors,
1222+
format: (def: string) => {
1223+
definition = def.replace(/:merge\(([^)]+)\)/g, '$1')
1224+
},
1225+
wrap: (rule: Container) => {
1226+
if (isAtRule(rule)) {
1227+
definition = `@${rule.name} ${rule.params}`
1228+
}
11951229
},
1196-
selector,
11971230
})
1198-
})
1199-
})
1200-
return root
1201-
}
12021231

1203-
let definitions = []
1204-
1205-
for (let fn of fns) {
1206-
let definition: string
1207-
let container = root.clone()
1208-
let returnValue = fn({
1209-
container,
1210-
separator: state.separator,
1211-
modifySelectors,
1212-
format: (def: string) => {
1213-
definition = def.replace(/:merge\(([^)]+)\)/g, '$1')
1214-
},
1215-
wrap: (rule: Container) => {
1216-
if (isAtRule(rule)) {
1217-
definition = `@${rule.name} ${rule.params}`
1232+
if (!definition) {
1233+
definition = returnValue
12181234
}
1219-
},
1220-
})
1221-
1222-
if (!definition) {
1223-
definition = returnValue
1224-
}
12251235

1226-
if (definition) {
1227-
definitions.push(definition)
1228-
continue
1229-
}
1236+
if (definition) {
1237+
definitions.push(definition)
1238+
continue
1239+
}
12301240

1231-
container.walkDecls((decl) => {
1232-
decl.remove()
1233-
})
1241+
container.walkDecls((decl) => {
1242+
decl.remove()
1243+
})
12341244

1235-
definition = container
1236-
.toString()
1237-
.replace(`.${escape(`${variantName}:${placeholder}`)}`, '&')
1238-
.replace(/(?<!\\)[{}]/g, '')
1239-
.replace(/\s*\n\s*/g, ' ')
1240-
.trim()
1245+
definition = container
1246+
.toString()
1247+
.replace(`.${escape(`${variantName}:${placeholder}`)}`, '&')
1248+
.replace(/(?<!\\)[{}]/g, '')
1249+
.replace(/\s*\n\s*/g, ' ')
1250+
.trim()
12411251

1242-
if (!definition.includes(placeholder)) {
1243-
definitions.push(definition)
1244-
}
1245-
}
1252+
if (!definition.includes(placeholder)) {
1253+
definitions.push(definition)
1254+
}
1255+
}
12461256

1247-
result[variantName] = definitions.join(', ') || null
1257+
return definitions
1258+
},
1259+
})
12481260
}
12491261
)
12501262

@@ -1276,7 +1288,12 @@ function getVariants(state: State): Record<string, string> {
12761288
})
12771289
})
12781290

1279-
return variants.reduce((obj, variant) => ({ ...obj, [variant]: null }), {})
1291+
return variants.map((variant) => ({
1292+
name: variant,
1293+
values: [],
1294+
isArbitrary: false,
1295+
selectors: () => [],
1296+
}))
12801297
}
12811298

12821299
async function getPlugins(config: any) {

0 commit comments

Comments
 (0)