-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-auto-forward): add rule no-auto-forward
- Loading branch information
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Forbid auto-forwarding events | ||
|
||
Prefer sending events explicitly to child actors/services. | ||
|
||
## Rule Details | ||
|
||
Avoid blindly forwarding all events to invoked services or spawned actors - it may lead to unexpected behavior or infinite loops. The official documentation [suggests sending events explicitly](https://xstate.js.org/docs/guides/communication.html#the-invoke-property) with the [`forwardTo`](https://xstate.js.org/docs/guides/actions.html#forward-to-action) or `send` action creators. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
// ❌ auto-forwarding events to an invoked service | ||
createMachine({ | ||
states: { | ||
playing: { | ||
invoke: { | ||
src: 'game', | ||
autoForward: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// ❌ auto-forwarding events to a spawned actor | ||
createMachine({ | ||
states: { | ||
initializing: { | ||
entry: assign({ | ||
gameRef: () => spawn(game, { autoForward: true }), | ||
}), | ||
}, | ||
}, | ||
}) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```javascript | ||
// ✅ no auto-forward | ||
createMachine{{ | ||
states: { | ||
playing: { | ||
invoke: { | ||
src: 'game', | ||
}, | ||
}, | ||
}, | ||
}} | ||
|
||
// ✅ autoForward set to false | ||
createMachine({ | ||
states: { | ||
initializing: { | ||
entry: assign({ | ||
gameRef: () => spawn(game, { autoForward: false }), | ||
}), | ||
}, | ||
}, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const getDocsUrl = require('../utils/getDocsUrl') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Forbid auto-forwarding events to child actors', | ||
category: 'Best Practices', | ||
url: getDocsUrl('no-auto-forward'), | ||
recommended: 'warn', | ||
}, | ||
schema: [], | ||
messages: { | ||
noAutoForward: | ||
'Forwarding all events may lead to unexpected behavior and/or infinite loops. Prefer using `forwardTo` action creator to send events explicitly.', | ||
}, | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="invoke"] > ObjectExpression > Property[key.name="autoForward"]': function ( | ||
node | ||
) { | ||
if (node.value.value === true) { | ||
context.report({ | ||
node, | ||
messageId: 'noAutoForward', | ||
}) | ||
} | ||
}, | ||
|
||
'CallExpression[callee.name=/^createMachine$|^Machine$/] CallExpression[callee.name="spawn"] > ObjectExpression > Property[key.name="autoForward"]': function ( | ||
node | ||
) { | ||
if (node.value.value === true) { | ||
context.report({ | ||
node, | ||
messageId: 'noAutoForward', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-auto-forward') | ||
|
||
const tests = { | ||
valid: [ | ||
` | ||
createMachine({ | ||
states: { | ||
playing: { | ||
invoke: { | ||
src: 'game', | ||
}, | ||
}, | ||
}, | ||
}) | ||
`, | ||
` | ||
createMachine({ | ||
states: { | ||
initializing: { | ||
entry: assign({ | ||
gameRef: () => spawn(game, { autoForward: false }), | ||
}), | ||
}, | ||
}, | ||
}) | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
createMachine({ | ||
states: { | ||
playing: { | ||
invoke: { | ||
src: 'game', | ||
autoForward: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
`, | ||
errors: [{ messageId: 'noAutoForward' }], | ||
}, | ||
{ | ||
code: ` | ||
createMachine({ | ||
states: { | ||
initializing: { | ||
entry: assign({ | ||
gameRef: () => spawn(game, { autoForward: true }), | ||
}), | ||
}, | ||
}, | ||
}) | ||
`, | ||
errors: [{ messageId: 'noAutoForward' }], | ||
}, | ||
], | ||
} | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
}) | ||
ruleTester.run('no-auto-forward', rule, tests) |