Skip to content

Commit

Permalink
fix(invoke-usage): allow multiple services to be invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Jul 28, 2021
1 parent 6f7da29 commit 25c252d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
58 changes: 33 additions & 25 deletions lib/rules/invoke-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,41 @@ module.exports = {
return {
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name="invoke"]':
function (node) {
if (node.value.type !== 'ObjectExpression') {
context.report({
node,
messageId: 'invokeIsNotObject',
})
return
}
if (!hasProperty('src', node.value)) {
context.report({
node,
messageId: 'invokeObjectLacksSrc',
})
return
}
const src = node.value.properties.find(propertyHasName('src'))
if (
!isStringLiteralOrIdentifier(src.value) &&
!isFunctionExpression(src.value) &&
src.value.type !== 'ObjectExpression' &&
!isCreateMachineCall(src.value)
) {
context.report({
node: src,
messageId: 'srcPropertyIsInvalid',
})
if (node.value.type === 'ArrayExpression') {
node.value.elements.forEach((node) => testInvokeDefinition(node))
} else {
testInvokeDefinition(node.value)
}
},
}

function testInvokeDefinition(node) {
if (node.type !== 'ObjectExpression') {
context.report({
node,
messageId: 'invokeIsNotObject',
})
return
}
if (!hasProperty('src', node)) {
context.report({
node,
messageId: 'invokeObjectLacksSrc',
})
return
}
const src = node.properties.find(propertyHasName('src'))
if (
!isStringLiteralOrIdentifier(src.value) &&
!isFunctionExpression(src.value) &&
src.value.type !== 'ObjectExpression' &&
!isCreateMachineCall(src.value)
) {
context.report({
node: src,
messageId: 'srcPropertyIsInvalid',
})
}
}
},
}
47 changes: 47 additions & 0 deletions tests/lib/rules/invoke-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ const tests = {
},
})
`,
`
createMachine({
initial: 'active',
states: {
active: {
invoke: [
{
src: () => [],
},
{
src: 'someService',
},
{
src: someService,
},
],
},
},
})
`,
],
invalid: [
{
Expand Down Expand Up @@ -72,6 +92,33 @@ const tests = {
{ messageId: 'srcPropertyIsInvalid' },
],
},
{
code: `
createMachine({
initial: 'active',
states: {
active: {
invoke: [
() => {},
'someService',
{
onDone: 'stopped',
},
{
src: true,
}
]
},
},
})
`,
errors: [
{ messageId: 'invokeIsNotObject' },
{ messageId: 'invokeIsNotObject' },
{ messageId: 'invokeObjectLacksSrc' },
{ messageId: 'srcPropertyIsInvalid' },
],
},
],
}

Expand Down

0 comments on commit 25c252d

Please sign in to comment.