-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
191 lines (182 loc) · 5.78 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const providerRegex = /Provider$/;
function getJSXElementName(jsx) {
if (jsx.openingElement.name.type === 'JSXIdentifier') {
// e.g. List
return jsx.openingElement.name.name;
}
if (jsx.openingElement.name.type === 'JSXMemberExpression') {
// e.g. List.Provider
return `${jsx.openingElement.name.object.name}.${jsx.openingElement.name.property.name}`;
}
}
function handleJSX(context, name, exported, jsx) {
if (
!providerRegex.test(getJSXElementName(jsx)) &&
!jsx.openingElement.attributes.find(
(a) => a.name?.name === 'data-component',
)
) {
context.possibleReports.push({
name,
exported,
report: {
node: jsx.openingElement,
message: `${name} is missing the data-component attribute for the top-level element.`,
fix(fixer) {
return fixer.insertTextAfterRange(
jsx.openingElement.typeParameters
? jsx.openingElement.typeParameters.range
: jsx.openingElement.name.range,
` data-component="${name}"`,
);
},
},
});
}
}
function handleBlockStatement(context, name, exported, block) {
// Find the root return statement. Are there any other types of returns we need to handle?
const ret = block.body.find((c) => c.type === 'ReturnStatement');
if (ret && ret.argument.type === 'JSXElement') {
handleJSX(context, name, exported, ret.argument);
}
}
function isForwardRef(expression) {
const calleeName =
expression.callee.type === 'MemberExpression'
? expression.callee.property.name
: expression.callee.name;
return calleeName === 'forwardRef' && expression.arguments.length == 1;
}
function handleExpression(context, name, exported, expression) {
switch (expression.type) {
case 'FunctionExpression':
handleBlockStatement(context, name, exported, expression.body);
break;
case 'ArrowFunctionExpression':
switch (expression.body.type) {
case 'JSXElement':
handleJSX(context, name, exported, expression.body);
break;
case 'BlockStatement':
handleBlockStatement(context, name, exported, expression.body);
break;
}
break;
case 'ClassExpression':
expression.body.body.forEach((x) => {
if (x.type === 'MethodDefinition' && x.key.name === 'render') {
handleBlockStatement(context, name, exported, x.value.body);
}
});
break;
case 'CallExpression':
if (isForwardRef(expression)) {
handleExpression(context, name, exported, expression.arguments[0]);
}
break;
}
}
const rules = {
'data-component': {
meta: {
type: 'problem',
docs: {
description:
'Missing data-component attribute for top-level element of component',
category: 'Instrumentation',
recommended: true,
},
fixable: 'code',
},
create(context) {
return {
Program(root) {
context = { ...context, possibleReports: [] };
root.body.forEach((node) => {
// We will need to save any non-exported declarations and handle them only if they get exported at the end
let exported = false;
if (
(node.type === 'ExportNamedDeclaration' ||
node.type === 'ExportDefaultDeclaration') &&
node.declaration
) {
exported = true;
node = node.declaration;
}
switch (node.type) {
case 'VariableDeclaration':
node.declarations.forEach((variable) => {
handleExpression(
context,
variable.id.name,
exported,
variable.init,
);
});
break;
case 'FunctionDeclaration':
handleBlockStatement(
context,
node.id.name,
exported,
node.body,
);
break;
case 'ClassDeclaration':
node.body.body.forEach((x) => {
if (
x.type === 'MethodDefinition' &&
x.key.name === 'render'
) {
handleBlockStatement(
context,
node.id.name,
exported,
x.value.body,
);
return;
}
});
break;
case 'CallExpression':
if (
isForwardRef(node) &&
node.arguments[0].type === 'FunctionExpression' &&
node.arguments[0].id
) {
handleExpression(
context,
node.arguments[0].id.name,
exported,
node.arguments[0],
);
}
break;
case 'AssignmentExpression':
handleExpression(context, node.left.name, exported, node.right);
break;
case 'ExportNamedDeclaration':
node.specifiers.forEach((s) => {
const report = context.possibleReports.find(
(r) => r.name === s.local.name,
);
if (report) {
report.exported = true;
}
});
break;
}
});
// Report all issues for exported components
context.possibleReports.forEach((r) => {
if (r.exported) {
context.report(r.report);
}
});
},
};
},
},
};
module.exports = { rules };