Skip to content

Commit 40a0fea

Browse files
revert: back to 3.17.0 equivalent
1 parent ecc0f9f commit 40a0fea

File tree

3 files changed

+24
-60
lines changed

3 files changed

+24
-60
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ You can can the same information on your favorite command line software as well.
4141

4242
## Latest changelog
4343

44-
- perf: [`no-custom-classname` optimization](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/338) (by [XantreDev](https://github.com/XantreDev) 🙏)
4544
- fix: [support `tag.div` and `tag(Component)`](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/302) (by [nihalgonsalves](https://github.com/nihalgonsalves) 🙏)
4645
- feat: [**support flat config and ESLint 9**](https://github.com/francoismassart/eslint-plugin-tailwindcss/pull/330) (by [kazupon](https://github.com/kazupon) 🙏)
4746
- feat: new rule [**`no-unnecessary-arbitrary-value`**](docs/rules/no-unnecessary-arbitrary-value.md) 🎉

lib/util/cssFiles.js

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
const fg = require('fast-glob');
44
const fs = require('fs');
55
const postcss = require('postcss');
6+
const removeDuplicatesFromArray = require('./removeDuplicatesFromArray');
67

7-
const classRegexp = /\.([^\.\,\s\n\:\(\)\[\]\'~\+\>\*\\]*)/gim;
8-
8+
let previousGlobsResults = [];
99
let lastUpdate = null;
1010
let classnamesFromFiles = [];
1111

12-
/**
13-
* @type {Map<string, number}
14-
*/
15-
const prevEditedTimestamp = new Map()
16-
1712
/**
1813
* Read CSS files and extract classnames
1914
* @param {Array} patterns Glob patterns to locate files
@@ -22,56 +17,27 @@ const prevEditedTimestamp = new Map()
2217
*/
2318
const generateClassnamesListSync = (patterns, refreshRate = 5_000) => {
2419
const now = new Date().getTime();
20+
const files = fg.sync(patterns, { suppressErrors: true });
21+
const newGlobs = previousGlobsResults.flat().join(',') != files.flat().join(',');
2522
const expired = lastUpdate === null || now - lastUpdate > refreshRate;
26-
27-
if (!expired) {
28-
return classnamesFromFiles;
29-
}
30-
const files = fg.sync(patterns, { suppressErrors: true, stats: true });
31-
lastUpdate = now;
32-
33-
/**
34-
* @type {Set<string}
35-
*/
36-
const detectedClassnames = new Set();
37-
/**
38-
* @type {Set<string>}
39-
*/
40-
const filesSet = new Set();
41-
for (const { path: file, stats } of files) {
42-
filesSet.add(file);
43-
if (!stats) {}
44-
// file is not changed -> we do need to do extra work
45-
else if (prevEditedTimestamp.get(file) === stats.mtimeMs) {
46-
continue;
47-
} else {
48-
prevEditedTimestamp.set(file, stats.mtimeMs);
23+
if (newGlobs || expired) {
24+
previousGlobsResults = files;
25+
lastUpdate = now;
26+
let detectedClassnames = [];
27+
for (const file of files) {
28+
const data = fs.readFileSync(file, 'utf-8');
29+
const root = postcss.parse(data);
30+
root.walkRules((rule) => {
31+
const regexp = /\.([^\.\,\s\n\:\(\)\[\]\'~\+\>\*\\]*)/gim;
32+
const matches = [...rule.selector.matchAll(regexp)];
33+
const classnames = matches.map((arr) => arr[1]);
34+
detectedClassnames.push(...classnames);
35+
});
36+
detectedClassnames = removeDuplicatesFromArray(detectedClassnames);
4937
}
50-
const data = fs.readFileSync(file, 'utf-8');
51-
const root = postcss.parse(data);
52-
root.walkRules((rule) => {
53-
for (const match of rule.selector.matchAll(classRegexp)) {
54-
detectedClassnames.add(match[1]);
55-
}
56-
});
38+
classnamesFromFiles = detectedClassnames;
5739
}
58-
// avoiding memory leak
59-
{
60-
/**
61-
* @type {string[]}
62-
*/
63-
const keysToDelete = []
64-
for (const cachedFilePath of prevEditedTimestamp.keys()) {
65-
if (!filesSet.has(cachedFilePath)) {
66-
keysToDelete.push(cachedFilePath);
67-
}
68-
}
69-
for (const key of keysToDelete) {
70-
prevEditedTimestamp.delete(key);
71-
}
72-
}
73-
classnamesFromFiles = [...detectedClassnames];
74-
return classnamesFromFiles
40+
return classnamesFromFiles;
7541
};
7642

7743
module.exports = generateClassnamesListSync;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,10 @@ ruleTester.run("no-custom-classname", rule, {
441441
options: [
442442
{
443443
cssFiles: ["./tests/lib/**/*.css"],
444-
cssFilesRefreshRate: 0,
445444
},
446445
],
447446
},
448-
...["myTag", "myTag.subTag", "myTag(SomeComponent)"].map((tag) => ({
447+
...(['myTag', 'myTag.subTag', 'myTag(SomeComponent)'].map(tag => ({
449448
code: `
450449
${tag}\`
451450
sm:w-6
@@ -456,7 +455,7 @@ ruleTester.run("no-custom-classname", rule, {
456455
lg:w-4
457456
\`;`,
458457
options: [{ tags: ["myTag"] }],
459-
})),
458+
}))),
460459
{
461460
code: `
462461
<div class="flex flex-row-reverse space-x-4 space-x-reverse">
@@ -1225,7 +1224,7 @@ ruleTester.run("no-custom-classname", rule, {
12251224
],
12261225
errors: generateErrors("dark"),
12271226
},
1228-
...["myTag", "myTag.subTag", "myTag(SomeComponent)"].flatMap((tag) => [
1227+
...(['myTag', 'myTag.subTag', 'myTag(SomeComponent)'].flatMap(tag => ([
12291228
{
12301229
code: `
12311230
${tag}\`
@@ -1273,7 +1272,7 @@ ruleTester.run("no-custom-classname", rule, {
12731272
options: [{ tags: ["myTag"] }],
12741273
errors: generateErrors("custom-2 custom-1"),
12751274
},
1276-
]),
1275+
]))),
12771276
{
12781277
code: `
12791278
<div class="bg-red-600 p-10">

0 commit comments

Comments
 (0)