Skip to content

Commit

Permalink
fix(no-imperative-action): detect actions.pure as a valid context for…
Browse files Browse the repository at this point in the history
… calling action creators

fix #4
  • Loading branch information
rlaffers committed May 12, 2021
1 parent f27c9c2 commit b9ae2cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/rules/no-imperative-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@

const getDocsUrl = require('../utils/getDocsUrl')

const { isActionCreatorCall } = require('../utils/predicates')
const {
isActionCreatorCall,
isCreateMachineCall,
} = require('../utils/predicates')

function isPureActionCall(node) {
return node.type === 'CallExpression' && node.callee.name === 'pure'
return (
(node.type === 'CallExpression' && node.callee.name === 'pure') ||
(node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'actions' &&
node.callee.property.name === 'pure')
)
}

function isWithinPureActionCreator(node) {
let parent = node.parent
while (parent) {
if (isCreateMachineCall(parent)) {
return false
}
if (isPureActionCall(parent)) {
return true
}
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-imperative-action.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-imperative-action')

// TODO detect actions.* as action creators
const tests = {
valid: [
`
Expand All @@ -20,6 +21,7 @@ const tests = {
log('message'),
escalate('error'),
pure(() => send('EVENT')),
actions.pure(() => send('EVENT')),
],
on: {
TRIGGER1: {
Expand All @@ -31,6 +33,7 @@ const tests = {
send(['EVENT']),
sendParent('EVENT'),
pure(() => send('EVENT')),
actions.pure(() => send('EVENT')),
],
},
},
Expand All @@ -49,6 +52,7 @@ const tests = {
myAction4: respond('EVENT'),
myAction5: raise('EVENT'),
myAction6: pure(() => send('EVENT')),
myAction6: actions.pure(() => send('EVENT')),
},
}
)
Expand Down

0 comments on commit b9ae2cd

Please sign in to comment.