Skip to content

Commit

Permalink
fix(event-names): fix event name linting in sendTo and forwardTo acti…
Browse files Browse the repository at this point in the history
…on creators

re #22
  • Loading branch information
rlaffers committed Sep 28, 2023
1 parent 10e1e0c commit 334b571
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/event-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function containsWildcardOrDot(name) {
}

const selectorSendEvent =
'CallExpression[callee.name=/^createMachine$|^Machine$/] CallExpression[callee.name=/^send$|^sendParent$|^respond$|^raise$/]'
'CallExpression[callee.name=/^createMachine$|^Machine$/] CallExpression[callee.name=/^send$|^sendParent$|^respond$|^raise$|^sendTo$|^forwardTo$/]'

/**
* Default regular expression for the regex option.
Expand Down Expand Up @@ -157,7 +157,7 @@ module.exports = {
},

[selectorSendEvent]: function (node) {
const eventArg = node.arguments[0]
const eventArg = (['sendTo', 'forwardTo'].includes(node.callee.name)) ? node.arguments[1] : node.arguments[0]
if (!eventArg) {
return
}
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/event-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const tests = {
sendParent('MOUSE.CLICK'),
raise('MY_MOUSE.SCROLL_DOWN'),
send({ type: 'TOGGLE' }),
sendTo('myActor', 'TOGGLE'),
sendTo('myActor', { type: 'TOGGLE' }),
forwardTo('myActor'),
],
})
`,
Expand Down Expand Up @@ -71,6 +74,8 @@ const tests = {
respond('My Event'),
raise('myEvent.click'),
send({ type: 'myEvent' }),
sendTo('myActor', 'myEvent2'),
sendTo('myActor', { type: 'myEvent3' }),
],
})
`,
Expand Down Expand Up @@ -114,6 +119,14 @@ const tests = {
messageId: 'invalidEventName',
data: { eventName: 'myEvent', fixedEventName: 'MY_EVENT' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'myEvent2', fixedEventName: 'MY_EVENT_2' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'myEvent3', fixedEventName: 'MY_EVENT_3' },
},
],
output: `
createMachine({
Expand All @@ -129,6 +142,8 @@ const tests = {
respond('MY_EVENT'),
raise('MY_EVENT.CLICK'),
send({ type: 'MY_EVENT' }),
sendTo('myActor', 'MY_EVENT_2'),
sendTo('myActor', { type: 'MY_EVENT_3' }),
],
})
`,
Expand All @@ -150,6 +165,8 @@ const tests = {
respond('My Event'),
raise('myEvent.click'),
send({ type: 'myEvent' }),
sendTo('myActor', 'myEvent2'),
sendTo('myActor', { type: 'myEvent3' }),
],
})
`,
Expand Down Expand Up @@ -193,6 +210,14 @@ const tests = {
messageId: 'invalidEventName',
data: { eventName: 'myEvent', fixedEventName: 'my_event' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'myEvent2', fixedEventName: 'my_event_2' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'myEvent3', fixedEventName: 'my_event_3' },
},
],
output: `
/* eslint event-names: [ "warn", "snakeCase" ] */
Expand All @@ -209,6 +234,8 @@ const tests = {
respond('my_event'),
raise('my_event.click'),
send({ type: 'my_event' }),
sendTo('myActor', 'my_event_2'),
sendTo('myActor', { type: 'my_event_3' }),
],
})
`,
Expand All @@ -230,6 +257,8 @@ const tests = {
respond('My Event'),
raise('my_event.click'),
send({ type: 'my_event' }),
sendTo('myActor', 'my_event_2'),
sendTo('myActor', { type: 'my_event_3' }),
],
})
`,
Expand Down Expand Up @@ -273,6 +302,14 @@ const tests = {
messageId: 'invalidEventName',
data: { eventName: 'my_event', fixedEventName: 'myEvent' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'my_event_2', fixedEventName: 'myEvent2' },
},
{
messageId: 'invalidEventName',
data: { eventName: 'my_event_3', fixedEventName: 'myEvent3' },
},
],
output: `
/* eslint event-names: [ "warn", "camelCase" ] */
Expand All @@ -289,6 +326,8 @@ const tests = {
respond('myEvent'),
raise('myEvent.click'),
send({ type: 'myEvent' }),
sendTo('myActor', 'myEvent2'),
sendTo('myActor', { type: 'myEvent3' }),
],
})
`,
Expand All @@ -303,6 +342,8 @@ const tests = {
respond('*'),
raise('EVENT.*'),
send({ type: 'EVENT.*' }),
sendTo('myActor', 'EVENT.*'),
sendTo('myActor', { type: 'EVENT.*' }),
],
})
`,
Expand All @@ -322,6 +363,12 @@ const tests = {
{
messageId: 'invalidSendEventName',
},
{
messageId: 'invalidSendEventName',
},
{
messageId: 'invalidSendEventName',
},
],
},
{
Expand Down

0 comments on commit 334b571

Please sign in to comment.