Skip to content

Commit

Permalink
fix(no-inline-implementation): detect inline implementations inside a…
Browse files Browse the repository at this point in the history
…rray of event transitions
  • Loading branch information
rlaffers committed May 12, 2021
1 parent e0cfe58 commit ed83fcf
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions lib/rules/no-inline-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ function isInlineAction(node, allowKnownActionCreators) {
)
}

const selectorEventTransitionProperty =
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="on"] > ObjectExpression > Property > ObjectExpression > Property'

const selectorArrayOfEventTransitionsProperty =
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="on"] > ObjectExpression > Property > ArrayExpression > ObjectExpression > Property'

const defaultOptions = {
allowKnownActionCreators: false,
}
Expand Down Expand Up @@ -90,44 +96,44 @@ module.exports = {
}
}

// TODO fix detecting activities
// TODO fix docs about activities
// TODO detect inline impls inside onDone, onError transitions
// TODO "on" transitions can have an array syntax (2 variants)
return {
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="on"] > ObjectExpression > Property > ObjectExpression > Property':
function (node) {
if (
node.key.name === 'cond' &&
(isFunctionExpression(node.value) ||
isIdentifier(node.value) ||
isCallExpression(node.value))
) {
context.report({
node,
messageId: 'moveGuardToOptions',
})
return
}
function checkTransitionProperty(node) {
if (
node.key.name === 'cond' &&
(isFunctionExpression(node.value) ||
isIdentifier(node.value) ||
isCallExpression(node.value))
) {
context.report({
node,
messageId: 'moveGuardToOptions',
})
return
}

if (node.key.name === 'actions') {
if (isInlineAction(node.value, options.allowKnownActionCreators)) {
if (node.key.name === 'actions') {
if (isInlineAction(node.value, options.allowKnownActionCreators)) {
context.report({
node,
messageId: 'moveActionToOptions',
})
} else if (isArrayExpression(node.value)) {
node.value.elements.forEach((element) => {
if (isInlineAction(element, options.allowKnownActionCreators)) {
context.report({
node,
node: element,
messageId: 'moveActionToOptions',
})
} else if (isArrayExpression(node.value)) {
node.value.elements.forEach((element) => {
if (isInlineAction(element, options.allowKnownActionCreators)) {
context.report({
node: element,
messageId: 'moveActionToOptions',
})
}
})
}
}
},
})
}
}
}

// TODO detect inline impls inside onDone, onError transitions
// TODO "on" transitions can have an array syntax
return {
[selectorEventTransitionProperty]: checkTransitionProperty,
[selectorArrayOfEventTransitionsProperty]: checkTransitionProperty,

'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="activities"]':
function (node) {
Expand Down

0 comments on commit ed83fcf

Please sign in to comment.