-
Notifications
You must be signed in to change notification settings - Fork 14
/
unused_css_checker.go
63 lines (58 loc) · 1.91 KB
/
unused_css_checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"os"
"regexp"
"strings"
)
// ClassNamesSplit cases for spliting classname to parts
func ClassNamesSplit(r rune) bool {
return r == ':' || r == '.' || r == ' ' || r == '>'
}
// JSClassNamesSplit splits js class string to separated parts
func JSClassNamesSplit(r rune) bool {
return r == '`' || r == ' ' || r == '.' || r == '='
}
// UnusedClassesChecker checks classes that never used by any js/jsx/ts/tsx/html/htm files in the project
func UnusedClassesChecker() []StyleSection {
files, err := WalkMatch(params.Path, WalkMatchOptions{patterns: []string{"*.js", "*.jsx", "*.ts", "*.tsx", "*.html", "*.htm"}, ignores: params.Ignores})
notFoundSections := []StyleSection{}
if err != nil {
return notFoundSections
}
referredHashes := map[uint64]bool{}
classReg := regexp.MustCompile(`class=["'\{]{1}[^>]*["'\}]{1}|className=["'\{]{1}[^>]*["'\}]{1}`)
for _, filePath := range files {
dat, err := os.ReadFile(filePath)
if err != nil {
return notFoundSections
}
result := strings.Replace(strings.Replace(string(dat), "\n", "", -1), "\r", "", -1)
matches := classReg.FindAllStringSubmatch(result, -1)
for _, match := range matches {
className := strings.Replace(strings.Replace(match[0], "class", "", -1), "className", "", -1)
className = strings.Replace(className, `"`, "", -1)
className = strings.Replace(className, `{`, "", -1)
className = strings.Replace(className, `}`, "", -1)
for _, name := range strings.FieldsFunc(className, JSClassNamesSplit) {
referredHashes[hash(name)] = true
}
}
}
for _, style := range styleList {
names := strings.FieldsFunc(style.name, ClassNamesSplit)
found := false
fmt.Println(style.name, " ", len(names), names)
for _, name := range names {
_, has := referredHashes[hash(name)]
if has {
found = true
break
}
}
if !found {
notFoundSections = append(notFoundSections, style)
}
}
return notFoundSections
}