Skip to content

Commit 491561c

Browse files
committed
feat(eslint-plugin): stricter checks on missing eventStart calls
BREAKING CHANGE: - Correctness checks; if `event` is passed into an async function, you most certainly want to call `eventStart`.
1 parent ed546c5 commit 491561c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

packages/eslint-plugin/lint-rules/check-event-name.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
consistentEventName:
1414
"Use an event name that can be derived from the function name",
1515
replaceEventName: `Replace value with {{value}}`,
16+
missingEventStart:
17+
"Function accepts 'event' as the first argument, but doesn't call 'eventStart'.",
18+
addEventStart: `Add 'eventStart(event, "")' in your function body.`,
1619
},
1720
},
1821

@@ -23,12 +26,36 @@ module.exports = {
2326
currentFunction = {
2427
parent: currentFunction,
2528
node,
26-
isAsyncEventFunction: node.async && node.params[0]?.name === "event",
29+
isAsyncEventFunction:
30+
node.async && node.id?.name && node.params[0]?.name === "event",
31+
hasEventStart: false,
2732
functionName: node.id?.name,
2833
};
2934
}
3035

3136
function processFunctionEnd() {
37+
if (
38+
currentFunction?.isAsyncEventFunction &&
39+
!currentFunction.hasEventStart
40+
) {
41+
context.report({
42+
node: currentFunction.node.body,
43+
messageId: "missingEventStart",
44+
suggest: [
45+
{
46+
messageId: "addEventStart",
47+
data: {},
48+
fix: function (fixer) {
49+
return fixer.insertTextBefore(
50+
currentFunction.node.body.body[0],
51+
`eventStart(event, "");\n `,
52+
);
53+
},
54+
},
55+
],
56+
});
57+
}
58+
3259
currentFunction = currentFunction.parent;
3360
}
3461

@@ -39,18 +66,16 @@ module.exports = {
3966

4067
// Process `eventStart` calls
4168
"CallExpression[callee.name='eventStart']"(node) {
42-
if (
43-
!currentFunction.isAsyncEventFunction ||
44-
!currentFunction.functionName ||
45-
currentFunction.functionName.length === 0
46-
) {
69+
if (!currentFunction?.isAsyncEventFunction) {
4770
return;
4871
}
4972

5073
if (node.arguments?.length !== 2) {
5174
return;
5275
}
5376

77+
currentFunction.hasEventStart = true;
78+
5479
let value = undefined;
5580
if (node.arguments[1].type === "Literal") {
5681
value = node.arguments[1].value;

packages/eslint-plugin/test/check-event-name.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ ruleTester.run("check-event-name", rule, {
6161
},
6262
],
6363
invalid: [
64+
{
65+
code: `async function missingEventStart(event) { const foo = 5; }`,
66+
errors: [
67+
{
68+
message: rule.meta.messages.missingEventStart,
69+
suggestions: [
70+
{
71+
messageId: "addEventStart",
72+
data: {},
73+
output: `async function missingEventStart(event) { eventStart(event, "");
74+
const foo = 5; }`,
75+
},
76+
],
77+
},
78+
],
79+
},
6480
{
6581
code: `async function fooBar(event) { eventStart(event, "foo"); }`,
6682
errors: [

packages/eslint-plugin/test/node-builtin-module-url-import.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ruleTester.run("node-builtin-module-url-import", rule, {
3333
invalid: [
3434
{
3535
code: `import { join } from "path";`,
36+
output: `import { join } from "node:path";`,
3637
errors: [
3738
{
3839
message: rule.meta.messages.consistentImport,

0 commit comments

Comments
 (0)