MCPcopy
hub / github.com/tailwindlabs/tailwindcss / matchUtilities

Function matchUtilities

packages/tailwindcss/src/compat/plugin-api.ts:350–510  ·  packages/tailwindcss/src/compat/plugin-api.ts::matchUtilities
(utilities, options)

Source from the content-addressed store, hash-verified

348 },
349
350 matchUtilities(utilities, options) {
351 let types = options?.type
352 ? Array.isArray(options?.type)
353 ? options.type
354 : [options.type]
355 : [class="st">'any']
356
357 for (let [name, fn] of Object.entries(utilities)) {
358 if (!IS_VALID_UTILITY_NAME.test(name)) {
359 throw new Error(
360 `\`matchUtilities({ class="st">'${name}' : … })\` defines an invalid utility name. Utilities should be alphanumeric and start with a lowercase letter, eg. \`scrollbar\`.`,
361 )
362 }
363
364 function compileFn({ negative }: { negative: boolean }) {
365 return (candidate: Extract<Candidate, { kind: class="st">'functional' }>) => {
366 class="cm">// Throw out any candidate whose value is not a supported type
367 if (
368 candidate.value?.kind === class="st">'arbitrary' &&
369 types.length > 0 &&
370 !types.includes(class="st">'any')
371 ) {
372 class="cm">// The candidate has an explicit data type but it's not in the list
373 class="cm">// of supported types by this utility. For example, a `scrollbar`
374 class="cm">// utility that is only used to change the scrollbar color but is
375 class="cm">// used with a `length` value: `scrollbar-[length:var(--whatever)]`
376 if (candidate.value.dataType && !types.includes(candidate.value.dataType)) {
377 return
378 }
379
380 class="cm">// The candidate does not have an explicit data type and the value
381 class="cm">// cannot be inferred as one of the supported types. For example, a
382 class="cm">// `scrollbar` utility that is only used to change the scrollbar
383 class="cm">// color but is used with a `length` value: `scrollbar-[33px]`
384 if (
385 !candidate.value.dataType &&
386 !inferDataType(candidate.value.value, types as any[])
387 ) {
388 return
389 }
390 }
391
392 let isColor = types.includes(class="st">'color')
393
394 class="cm">// Resolve the candidate value
395 let value: string | null = null
396 let ignoreModifier = false
397
398 {
399 let values = options?.values ?? {}
400
401 if (isColor) {
402 class="cm">// Color utilities implicitly support `inherit`, `transparent`, and `currentcolor`
403 class="cm">// for backwards compatibility but still allow them to be overridden
404 values = Object.assign(
405 {
406 inherit: class="st">'inherit',
407 transparent: class="st">'transparent',

Callers 6

index.test.tsFile · 0.85
plugin-api.test.tsFile · 0.85
utilityFunction · 0.85
loadModuleFunction · 0.85
config.test.tsFile · 0.85

Calls 7

compileFnFunction · 0.85
entriesMethod · 0.45
functionalMethod · 0.45
suggestMethod · 0.45
keysMethod · 0.45
hasMethod · 0.45
addMethod · 0.45

Tested by 2

utilityFunction · 0.68
loadModuleFunction · 0.68