Skip to content

Commit ab17c6c

Browse files
authored
Handle unknown typehints (#5588)
If you use a typehint like `w-[length:12px]`, then currently that wouldn't generate anything because we don't have a matchUtilities for `w` with a `length` type. To fix this, we can detect if this is unnecessary, if it is we still generate the expected outcome. (In this case `width: 12px`) but we also warn to the user that we detected this. Currently we detect this by checking if there is only a single plugin registered for handling the prefix (e.g.: `w-`). We can probably improve this by also checking all the types that all plugins are handling for the resolved plugins.
1 parent 79b37a8 commit ab17c6c

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/lib/generateRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ function* resolveMatches(candidate, context) {
226226
for (let matchedPlugins of resolveMatchedPlugins(classCandidate, context)) {
227227
let matches = []
228228
let [plugins, modifier] = matchedPlugins
229+
let isOnlyPlugin = plugins.length === 1
229230

230231
for (let [sort, plugin] of plugins) {
231232
if (typeof plugin === 'function') {
232-
for (let ruleSet of [].concat(plugin(modifier))) {
233+
for (let ruleSet of [].concat(plugin(modifier, { isOnlyPlugin }))) {
233234
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
234235
for (let rule of rules) {
235236
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])

src/lib/setupContextUtils.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,29 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
300300

301301
classList.add([prefixedIdentifier, options])
302302

303-
function wrapped(modifier) {
303+
function wrapped(modifier, { isOnlyPlugin }) {
304304
let { type = 'any' } = options
305305
type = [].concat(type)
306306
let [value, coercedType] = coerceValue(type, modifier, options.values, tailwindConfig)
307307

308-
if (!type.includes(coercedType) || value === undefined) {
308+
if (value === undefined) {
309309
return []
310310
}
311311

312+
if (!type.includes(coercedType)) {
313+
if (isOnlyPlugin) {
314+
log.warn([
315+
`Unnecessary typehint \`${coercedType}\` in \`${identifier}-${modifier}\`.`,
316+
`You can safely update it to \`${identifier}-${modifier.replace(
317+
coercedType + ':',
318+
''
319+
)}\`.`,
320+
])
321+
} else {
322+
return []
323+
}
324+
}
325+
312326
if (!isValidArbitraryValue(value)) {
313327
return []
314328
}

tests/arbitrary-values.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ it('should not generate any css if an unknown typehint is used', () => {
3030
})
3131
})
3232

33+
it('should handle unknown typehints', () => {
34+
let config = { content: [{ raw: html`<div class="w-[length:12px]"></div>` }] }
35+
36+
return run('@tailwind utilities', config).then((result) => {
37+
return expect(result.css).toMatchFormattedCss(`
38+
.w-\\[length\\:12px\\] {
39+
width: 12px;
40+
}
41+
`)
42+
})
43+
})
44+
3345
it('should convert _ to spaces', () => {
3446
let config = {
3547
content: [

0 commit comments

Comments
 (0)