Skip to content

Commit 4a1b382

Browse files
fix: incorrect conflicting classnames (#186) (#248)
1 parent 7fa086b commit 4a1b382

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

lib/config/groups.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ module.exports.groups = [
619619
{
620620
type: 'Backgrounds',
621621
members: [
622+
{
623+
type: 'Background Image URL',
624+
members: 'bg\\-\\[url\\((?<value>${backgroundImageUrl})\\)\\]',
625+
},
622626
{
623627
type: 'Background Attachment',
624628
members: 'bg\\-(fixed|local|scroll)',

lib/util/groupMethods.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ function generateOptions(propName, keys, config, isNegative = false) {
252252
// Forbidden prefixes
253253
escapedKeys.push(`\\[length\:.{1,}\\]`);
254254
return '(' + escapedKeys.join('|') + ')';
255+
case 'backgroundImageUrl':
256+
// Forbidden prefixes
257+
escapedKeys.push(`.{1,}`);
258+
return '(' + escapedKeys.join('|') + ')';
255259
case 'backgroundImage':
256260
// Forbidden prefixes
257261
escapedKeys.push(`\\[url\\(.{1,}\\)\\]`);
@@ -324,6 +328,10 @@ function patchRegex(re, config) {
324328
generateOptions(absoluteProp, [], config, isNegative)
325329
);
326330
return `${patched}(${replaced})`;
331+
} else if (prop === 'backgroundImageUrl') {
332+
// Special case
333+
replaced = replaced.replace(new RegExp('\\$\\{' + prop + '\\}'), generateOptions(prop, [], config, false));
334+
return `${patched}(${replaced})`;
327335
} else if (!config.theme || !config.theme[absoluteProp]) {
328336
// prop not found in config
329337
return;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-tailwindcss",
3-
"version": "3.12.1-beta.0",
3+
"version": "3.12.1",
44
"description": "Rules enforcing best practices while using Tailwind CSS",
55
"keywords": [
66
"eslint",

tests/lib/rules/no-contradicting-classname.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ ruleTester.run("no-contradicting-classname", rule, {
291291
\`)}
292292
/>`,
293293
},
294+
{
295+
code: `
296+
<div class="bg-[url('/image.jpg')] bg-center">Issue #186</div>`,
297+
},
294298
],
295299

296300
invalid: [
@@ -694,6 +698,13 @@ ruleTester.run("no-contradicting-classname", rule, {
694698
</div>`,
695699
errors: generateErrors(["group/name:scale-75 group/name:scale-50"]),
696700
},
701+
{
702+
code: `
703+
<div class="bg-[url('default.jpg')] sm:bg-[url('foo.jpg')] sm:bg-[url('bar.jpg')]">
704+
named group
705+
</div>`,
706+
errors: generateErrors(["sm:bg-[url('foo.jpg')] sm:bg-[url('bar.jpg')]"]),
707+
},
697708
// {
698709
// code: `
699710
// <div class="scale-75 transform-none">

tests/lib/util/groupMethods.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,54 @@ describe("parseClassname", function () {
244244
assert.equal(regex1.exec(str1).groups.negPos, "0");
245245
});
246246
});
247+
248+
describe("getGroupIndex", function () {
249+
const targetProperties = {
250+
Backgrounds: [
251+
"Background Image URL",
252+
"Background Attachment",
253+
"Background Clip",
254+
"Background Color",
255+
"Deprecated Background Opacity",
256+
"Background Origin",
257+
"Background Position",
258+
"Background Repeat",
259+
"Background Size",
260+
"Background Image",
261+
"Gradient Color Stops",
262+
],
263+
};
264+
const targetGroups = defaultGroups.filter((g) => Object.keys(targetProperties).includes(g.type));
265+
266+
it("should have filtered `targetGroups`", function () {
267+
assert.equal(targetGroups.length, Object.keys(targetProperties).length);
268+
});
269+
270+
it(`should parse classnames`, function () {
271+
let name, actual, expected;
272+
name = "md:bg-[url('/image-md.jpg')]";
273+
actual = groupUtil.parseClassname(name, targetGroups, mergedConfig, 0);
274+
expected = {
275+
index: 0,
276+
name: name,
277+
variants: "md:",
278+
parentType: "Backgrounds",
279+
body: "bg-[url('/",
280+
value: "'/image-md.jpg'",
281+
shorthand: "",
282+
leading: "",
283+
trailing: "",
284+
important: false,
285+
};
286+
assert.deepEqual(actual, expected);
287+
});
288+
289+
it(`should get correct group index`, function () {
290+
let name, actual, expected;
291+
const groups = groupUtil.getGroups(targetGroups, mergedConfig);
292+
name = "md:bg-[url(some)]";
293+
actual = groupUtil.getGroupIndex(name, groups, mergedConfig.separator);
294+
expected = 0;
295+
assert.equal(actual, expected);
296+
});
297+
});

0 commit comments

Comments
 (0)