forked from jens-ox/eslint-plugin-material-ui-unused-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.js
143 lines (125 loc) · 3.85 KB
/
rule.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function getBasicIdentifier(node) {
if (node.type === 'Identifier') {
// classes.foo
return node.name;
}
if (node.type === 'Literal') {
// classes['foo']
return node.value;
}
if (node.type === 'TemplateLiteral') {
// classes[`foo`]
if (node.expressions.length) {
// classes[`foo${bar}`]
return null;
}
return node.quasis[0].value.raw;
}
// Might end up here with things like:
// classes['foo' + bar]
return null;
}
module.exports = {
meta: {
type: 'problem',
},
create: function rule(context) {
const usedClasses = {};
const definedClasses = {};
return {
CallExpression(node) {
if (node.callee.name === 'makeStyles') {
const styles = node.arguments[0];
if (styles && styles.type === 'ArrowFunctionExpression') {
const { body } = styles;
let stylesObj;
if (body.type === 'ObjectExpression') {
stylesObj = body;
} else if (body.type === 'BlockStatement') {
body.body.forEach((bodyNode) => {
if (
bodyNode.type === 'ReturnStatement' &&
bodyNode.argument.type === 'ObjectExpression'
) {
stylesObj = bodyNode.argument;
}
});
}
if (stylesObj) {
stylesObj.properties.forEach((property) => {
if (property.computed) {
// Skip over computed properties for now.
// e.g. `{ [foo]: { ... } }`
return;
}
if (
property.type === 'ExperimentalSpreadProperty' ||
property.type === 'SpreadElement'
) {
// Skip over object spread for now.
// e.g. `{ ...foo }`
return;
}
definedClasses[property.key.name] = property;
});
}
}
}
},
MemberExpression(node) {
if (node.object.type === 'Identifier' && node.object.name === 'classes') {
const whichClass = getBasicIdentifier(node.property);
if (whichClass) {
usedClasses[whichClass] = true;
}
return;
}
const classIdentifier = getBasicIdentifier(node.property);
if (!classIdentifier) {
// props['foo' + bar].baz
return;
}
if (classIdentifier !== 'classes') {
// props.foo.bar
return;
}
const { parent } = node;
if (parent.type !== 'MemberExpression') {
// foo.styles
return;
}
if (node.object.object && node.object.object.type !== 'ThisExpression') {
// foo.foo.styles
return;
}
const propsIdentifier = getBasicIdentifier(parent.object);
if (propsIdentifier && propsIdentifier !== 'props') {
return;
}
if (!propsIdentifier && parent.object.type !== 'MemberExpression') {
return;
}
if (parent.parent.type === 'MemberExpression') {
// this.props.props.styles
return;
}
const parentClassIdentifier = getBasicIdentifier(parent.property);
if (parentClassIdentifier) {
usedClasses[parentClassIdentifier] = true;
}
},
'Program:exit': () => {
// Now we know all of the defined classes and used classes, so we can
// see if there are any defined classes that are not used.
Object.keys(definedClasses).forEach((definedClassKey) => {
if (!usedClasses[definedClassKey]) {
context.report(
definedClasses[definedClassKey],
`Class \`${definedClassKey}\` is unused`,
);
}
});
},
};
},
}