forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-bound-component-methods.js
124 lines (111 loc) · 4.23 KB
/
no-bound-component-methods.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
// 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 that no methods that are used as LitHtml events are bound.',
category: 'Possible Errors',
},
fixable: 'code',
schema: [], // no options
messages: {
nonRenderBindFound:
'Found bound method name {{ methodName }} on {{ componentName }} that was not `render`. Lit-Html binds all event handlers for you automatically so this is not required.',
},
},
create: function(context) {
function nodeIsHTMLElementClassDeclaration(node) {
return node.type === 'ClassDeclaration' && node.superClass && node.superClass.name === 'HTMLElement';
}
const classesToCheck = new Set();
// Store any method names that were passed to addEventListener.
// With the following code:
// window.addEventListener('click', this.boundOnClick)
// we would add `boundOnClick` to this set.
const addEventListenerCallPropertyNames = new Set();
function checkPropertyDeclarationForBinding(className, node) {
if (!node.value || node.value.type !== 'CallExpression') {
return;
}
if (node.value.callee.type !== 'MemberExpression') {
return;
}
if (node.value.callee.property.name !== 'bind') {
return;
}
// At this point we know it's a property of the form:
// someBoundThing = this.thing.bind(X)
// and now we want to check that the argument passed to bind is `this`.
// If the argument to bind is not `this`, we leave it be and move on.
if (node.value.arguments[0]?.type !== 'ThisExpression') {
return;
}
// At this point it's definitely of the form:
// someBoundThing = this.thing.bind(this)
// But we know that `render` may be bound for the scheduler, so if it's render we can move on
if (node.value.callee.object.property.name === 'render') {
return;
}
// Now it's an error UNLESS we found a call to
// addEventListener(x, this.#boundFoo),
// in which case it's allowed.
// Get the property name for the bound method
// #boundFoo = this.foo.bind(this);
// node.key.name === 'boundFoo';
const boundPropertyName = node.key.name;
if (addEventListenerCallPropertyNames.has(boundPropertyName)) {
return;
}
context.report({
node,
messageId: 'nonRenderBindFound',
data: {componentName: className, methodName: node.value.callee.object.property.name}
});
}
function checkClassForBoundMethods(classDeclarationNode) {
const classPropertyDeclarations = classDeclarationNode.body.body.filter(node => {
return node.type === 'PropertyDefinition';
});
for (const decl of classPropertyDeclarations) {
checkPropertyDeclarationForBinding(classDeclarationNode.id.name, decl);
}
}
return {
ClassDeclaration(classDeclarationNode) {
if (!nodeIsHTMLElementClassDeclaration(classDeclarationNode)) {
return;
}
classesToCheck.add(classDeclarationNode);
},
'CallExpression[callee.type=\'MemberExpression\'][callee.property.name=\'addEventListener\']'(
callExpressionNode) {
const methodArg = callExpressionNode.arguments[1];
if (!methodArg) {
// User is in the middle of typing, so just early exit.
return;
}
// Confirm that the argument is this.X, otherwise skip it
if (methodArg.type !== 'MemberExpression') {
return;
}
// Get the property from the addEventListener call
// window.addEventListener('click', this.#boundFoo)
// This will be the node representing `#boundFoo`
// and its `.name` property will be `boundFoo`
const propertyArg = methodArg.property;
addEventListenerCallPropertyNames.add(propertyArg.name);
},
'Program:exit'() {
for (const classNode of classesToCheck) {
checkClassForBoundMethods(classNode);
}
}
};
}
};