-
Notifications
You must be signed in to change notification settings - Fork 11
/
rule.js
156 lines (139 loc) · 4.47 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
144
145
146
147
148
149
150
151
152
153
154
155
156
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 definedClasses = {};
const potentialUses = []
let hookName = ''
let instanceName = ''
return {
CallExpression(node) {
if (node.callee.name === 'makeStyles' && node.parent.type === 'VariableDeclarator') {
hookName = node.parent.id.name
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;
}
});
} else if (
body.type === 'CallExpression' &&
body.callee.name === 'createStyles' &&
body.arguments[0].type === 'ObjectExpression') {
stylesObj = body.arguments[0]
}
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;
});
}
}
}
if (hookName !== '' && node.callee.name === hookName) {
instanceName = node.parent.id.name
}
},
MemberExpression(node) {
if (node.object.type === 'Identifier') {
const whichClass = getBasicIdentifier(node.property);
if (whichClass) {
potentialUses.push({
instanceName: node.object.name,
whichClass
})
}
return;
}
const classIdentifier = getBasicIdentifier(node.property);
if (!classIdentifier) {
// props['foo' + bar].baz
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) {
potentialUses.push({
instanceName: node.object.name,
whichClass: parentClassIdentifier
})
}
},
'Program:exit': () => {
const confirmedUses = potentialUses.filter((p) => p.instanceName === instanceName).map((p) => p.whichClass)
// 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 (!confirmedUses.includes(definedClassKey)) {
context.report(
definedClasses[definedClassKey],
`Class \`${definedClassKey}\` is unused`,
);
}
});
},
};
},
}