Skip to content

Commit ceddc54

Browse files
authored
fix: prefix support for named group/peer syntax (francoismassart#244)
1 parent b3a8ae6 commit ceddc54

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

lib/rules/no-custom-classname.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const parserUtil = require('../util/parser');
1414
const getClassnamesFromCSS = require('../util/cssFiles');
1515
const createContextFallback = require('tailwindcss/lib/lib/setupContextUtils').createContext;
1616
const generated = require('../util/generated');
17+
const escapeRegex = require('../util/regex').escapeRegex;
1718

1819
//------------------------------------------------------------------------------
1920
// Rule Definition
@@ -26,7 +27,8 @@ const CUSTOM_CLASSNAME_DETECTED_MSG = `Classname '{{classname}}' is not a Tailwi
2627
// Group/peer names can be arbitrarily named and are not
2728
// generated by generateRules. Using a custom regexp to
2829
// validate these avoids false reports.
29-
const GROUP_NAME_REGEXP = /^(group|peer)\/[\w\$\#\@\%\^\&\*\_\-]+$/i;
30+
const getGroupNameRegex = (prefix = '') =>
31+
new RegExp(`^${escapeRegex(prefix)}(group|peer)\/[\\w\\$\\#\\@\\%\\^\\&\\*\\_\\-]+$`, 'i');
3032

3133
const contextFallbackCache = new WeakMap();
3234

@@ -110,6 +112,7 @@ module.exports = {
110112
// Init assets before sorting
111113
const groups = groupUtil.getGroups(defaultGroups, mergedConfig);
112114
const classnamesFromFiles = getClassnamesFromCSS(cssFiles, cssFilesRefreshRate);
115+
const groupNameRegex = getGroupNameRegex(mergedConfig.prefix);
113116

114117
/**
115118
* Parse the classnames and report found conflicts
@@ -134,7 +137,7 @@ module.exports = {
134137
if (fromFilesIdx >= 0) {
135138
return; // Lazier is faster... processing next className!
136139
}
137-
if (GROUP_NAME_REGEXP.test(className)) {
140+
if (groupNameRegex.test(className)) {
138141
return; // Lazier is faster... processing next className!
139142
}
140143

lib/util/regex.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Escapes a string to be used in a regular expression
3+
* Copied from https://stackoverflow.com/a/3561711.
4+
* @param {string} input
5+
* @returns {string}
6+
*/
7+
function escapeRegex(input) {
8+
return input.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
9+
}
10+
11+
module.exports = {
12+
escapeRegex,
13+
};

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,42 @@ ruleTester.run("no-custom-classname", rule, {
877877
],
878878
},
879879
{ code: `<div className="group/edit">Custom group name</div>` },
880+
{
881+
code: `<div className="tw-group/edit">Prefix and custom group name</div>`,
882+
options: [
883+
{
884+
config: { prefix: "tw-" },
885+
},
886+
],
887+
},
880888
{ code: `<div className="group-hover/edit:hidden">Custom group name variant</div>` },
889+
{
890+
code: `<div className="group-hover/edit:tw-hidden">Prefix and custom group name variant</div>`,
891+
options: [
892+
{
893+
config: { prefix: "tw-" },
894+
},
895+
],
896+
},
881897
{
882898
code: `
883899
<div class="group/nav123$#@%^&*_-">
884900
<div class="group-hover/nav123$#@%^&*_-:h-0">group/nav123$#@%^&*_-</div>
885901
</div>
886902
`,
887903
},
904+
{
905+
code: `
906+
<div class="tw-group/nav123$#@%^&*_-">
907+
<div class="group-hover/nav123$#@%^&*_-:tw-h-0">group/nav123$#@%^&*_-</div>
908+
</div>
909+
`,
910+
options: [
911+
{
912+
config: { prefix: "tw-" },
913+
},
914+
],
915+
},
888916
{
889917
code: `<div className="group-hover/edit:custom-class">Custom group name variant</div>`,
890918
options: [
@@ -894,7 +922,23 @@ ruleTester.run("no-custom-classname", rule, {
894922
],
895923
},
896924
{ code: `<div className="peer/draft">Custom peer name</div>` },
925+
{
926+
code: `<div className="tw-peer/draft">Prefix and custom peer name</div>`,
927+
options: [
928+
{
929+
config: { prefix: "tw-" },
930+
},
931+
],
932+
},
897933
{ code: `<div className="peer-checked/draft:hidden">Custom peer name variant</div>` },
934+
{
935+
code: `<div className="peer-checked/draft:tw-hidden">Prefix and custom peer name variant</div>`,
936+
options: [
937+
{
938+
config: { prefix: "tw-" },
939+
},
940+
],
941+
},
898942
{
899943
code: `<div className="peer-checked/draft:custom-class">Custom peer name variant</div>`,
900944
options: [

0 commit comments

Comments
 (0)