forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-custom-event-names.js
188 lines (167 loc) · 6.37 KB
/
static-custom-event-names.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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Enforce event naming convention',
category: 'Possible Errors',
},
fixable: 'code',
messages: {
missingEventName:
'Event is missing a static readonly eventName property containing the event name, or the eventName property was not found as the first node in the class body.',
eventNameNotReadonly: 'The eventName property must be marked as readonly.',
eventNameNotStatic: 'The eventName property must be a static property.',
superEventNameWrong: 'The first argument to super() must be EventClass.eventName.',
noSuperCallFound: 'Could not find a super() call in the constructor.',
noConstructorFound: 'Could not find a constructor for the custom event.',
},
schema: [] // no options
},
create: function(context) {
function findConstructorAndSuperCallAndFirstArgumentToSuper(node) {
const foundNodes = {
constructor: undefined,
superExpression: undefined,
firstArgumentToSuper: undefined,
};
const constructor = node.body.body.find(bodyNode => {
return bodyNode.type === 'MethodDefinition' && bodyNode.key?.name === 'constructor';
});
if (!constructor) {
return foundNodes;
}
foundNodes.constructor = constructor;
const superExpression = constructor.value.body.body.find(bodyNode => {
const isExpression = bodyNode.type === 'ExpressionStatement';
if (!isExpression) {
return false;
}
return isExpression && bodyNode.expression.callee?.type === 'Super';
});
if (!superExpression) {
return foundNodes;
}
foundNodes.superExpression = superExpression;
const firstArgumentToSuper = superExpression.expression.arguments[0];
if (!firstArgumentToSuper) {
// This is invalid so bail.
return foundNodes;
}
foundNodes.firstArgumentToSuper = firstArgumentToSuper;
return foundNodes;
}
function tryToAutoFixIfWeCan(fixer, node) {
// We can autofix nodes when either:
// 1. They do not have the static declaration, and there is a super('fooevent') call
// => in this case, we can define static readonly eventName = 'fooevent' and update the super() call's first argument.
// 2. There is a staticEventName declared, but the super() call is using the literal. In this case, we can update the super() call's first argument.
// Note: if we cannot fix, we return an empty array, which signifies to
// ESLint that there's no fixes we'd like to apply.
const nodeIsMissingEventName = node.body.body[0]?.key?.name !== 'eventName';
const className = node.id.name;
const {firstArgumentToSuper} = findConstructorAndSuperCallAndFirstArgumentToSuper(node);
if (!firstArgumentToSuper || firstArgumentToSuper.type !== 'Literal') {
// Either it's OK, or it's some value that isn't a string literal, so we should bail.
return [];
}
const eventNameFromSuper = firstArgumentToSuper.value;
const fixes = [];
if (nodeIsMissingEventName) {
fixes.push(fixer.insertTextBefore(node.body.body[0], `static readonly eventName = '${eventNameFromSuper}';`));
}
fixes.push(fixer.replaceText(firstArgumentToSuper, `${className}.eventName`));
return fixes;
}
function lintClassNode(node) {
const bodyMembersOfClass = node.body.body;
// Look for the static readonly eventName line.
// We purposefully look at the first body node as it should be defined first
const firstBodyNode = bodyMembersOfClass[0];
if (!firstBodyNode || firstBodyNode.key.name !== 'eventName') {
context.report({
node,
messageId: 'missingEventName',
fix(fixer) {
return tryToAutoFixIfWeCan(fixer, node);
}
});
return;
}
if (!firstBodyNode.readonly) {
context.report({node, messageId: 'eventNameNotReadonly'});
}
if (!firstBodyNode.static) {
context.report({node, messageId: 'eventNameNotStatic'});
return;
}
// Now we know the static readonly eventName is defined, we check for
// the constructor and the super() call.
const {constructor, superExpression, firstArgumentToSuper} =
findConstructorAndSuperCallAndFirstArgumentToSuper(node);
if (!constructor) {
context.report({node, messageId: 'noConstructorFound'});
return;
}
if (!superExpression) {
context.report({node, messageId: 'noSuperCallFound'});
return;
}
if (!firstArgumentToSuper) {
context.report({node, messageId: 'superEventNameWrong'});
return;
}
const customEventClassName = node.id.name;
if (firstArgumentToSuper.type !== 'MemberExpression') {
context.report({
node,
messageId: 'superEventNameWrong',
fix(fixer) {
return tryToAutoFixIfWeCan(fixer, node);
}
});
return;
}
if (firstArgumentToSuper.object.name !== customEventClassName ||
firstArgumentToSuper.property.name !== 'eventName') {
context.report({node, messageId: 'superEventNameWrong'});
return;
}
}
let foundLocalEventClassDeclaration = false;
const classDeclarationsToLint = [];
return {
ClassDeclaration(node) {
// If we find a local class defined called Event, we do not apply this
// check, as we have some instances where a local Event class is used
// which is not the builtin Event class that represents DOM emitted
// events.
if (node.id.name === 'Event') {
foundLocalEventClassDeclaration = true;
return;
}
if (!node.superClass) {
return;
}
if (node.superClass.name !== 'Event') {
return;
}
classDeclarationsToLint.push(node);
},
'Program:exit'() {
if (foundLocalEventClassDeclaration) {
return;
}
classDeclarationsToLint.forEach(node => {
lintClassNode(node);
});
},
};
}
};