Skip to content

Commit

Permalink
fix: incorrect conflicting classnames (#186) (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoismassart authored May 31, 2023
1 parent 7fa086b commit 4a1b382
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/config/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ module.exports.groups = [
{
type: 'Backgrounds',
members: [
{
type: 'Background Image URL',
members: 'bg\\-\\[url\\((?<value>${backgroundImageUrl})\\)\\]',
},
{
type: 'Background Attachment',
members: 'bg\\-(fixed|local|scroll)',
Expand Down
8 changes: 8 additions & 0 deletions lib/util/groupMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ function generateOptions(propName, keys, config, isNegative = false) {
// Forbidden prefixes
escapedKeys.push(`\\[length\:.{1,}\\]`);
return '(' + escapedKeys.join('|') + ')';
case 'backgroundImageUrl':
// Forbidden prefixes
escapedKeys.push(`.{1,}`);
return '(' + escapedKeys.join('|') + ')';
case 'backgroundImage':
// Forbidden prefixes
escapedKeys.push(`\\[url\\(.{1,}\\)\\]`);
Expand Down Expand Up @@ -324,6 +328,10 @@ function patchRegex(re, config) {
generateOptions(absoluteProp, [], config, isNegative)
);
return `${patched}(${replaced})`;
} else if (prop === 'backgroundImageUrl') {
// Special case
replaced = replaced.replace(new RegExp('\\$\\{' + prop + '\\}'), generateOptions(prop, [], config, false));
return `${patched}(${replaced})`;
} else if (!config.theme || !config.theme[absoluteProp]) {
// prop not found in config
return;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-tailwindcss",
"version": "3.12.1-beta.0",
"version": "3.12.1",
"description": "Rules enforcing best practices while using Tailwind CSS",
"keywords": [
"eslint",
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/no-contradicting-classname.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ ruleTester.run("no-contradicting-classname", rule, {
\`)}
/>`,
},
{
code: `
<div class="bg-[url('/image.jpg')] bg-center">Issue #186</div>`,
},
],

invalid: [
Expand Down Expand Up @@ -694,6 +698,13 @@ ruleTester.run("no-contradicting-classname", rule, {
</div>`,
errors: generateErrors(["group/name:scale-75 group/name:scale-50"]),
},
{
code: `
<div class="bg-[url('default.jpg')] sm:bg-[url('foo.jpg')] sm:bg-[url('bar.jpg')]">
named group
</div>`,
errors: generateErrors(["sm:bg-[url('foo.jpg')] sm:bg-[url('bar.jpg')]"]),
},
// {
// code: `
// <div class="scale-75 transform-none">
Expand Down
51 changes: 51 additions & 0 deletions tests/lib/util/groupMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,54 @@ describe("parseClassname", function () {
assert.equal(regex1.exec(str1).groups.negPos, "0");
});
});

describe("getGroupIndex", function () {
const targetProperties = {
Backgrounds: [
"Background Image URL",
"Background Attachment",
"Background Clip",
"Background Color",
"Deprecated Background Opacity",
"Background Origin",
"Background Position",
"Background Repeat",
"Background Size",
"Background Image",
"Gradient Color Stops",
],
};
const targetGroups = defaultGroups.filter((g) => Object.keys(targetProperties).includes(g.type));

it("should have filtered `targetGroups`", function () {
assert.equal(targetGroups.length, Object.keys(targetProperties).length);
});

it(`should parse classnames`, function () {
let name, actual, expected;
name = "md:bg-[url('/image-md.jpg')]";
actual = groupUtil.parseClassname(name, targetGroups, mergedConfig, 0);
expected = {
index: 0,
name: name,
variants: "md:",
parentType: "Backgrounds",
body: "bg-[url('/",
value: "'/image-md.jpg'",
shorthand: "",
leading: "",
trailing: "",
important: false,
};
assert.deepEqual(actual, expected);
});

it(`should get correct group index`, function () {
let name, actual, expected;
const groups = groupUtil.getGroups(targetGroups, mergedConfig);
name = "md:bg-[url(some)]";
actual = groupUtil.getGroupIndex(name, groups, mergedConfig.separator);
expected = 0;
assert.equal(actual, expected);
});
});

0 comments on commit 4a1b382

Please sign in to comment.