Skip to content

Commit

Permalink
feat(no-invalid-transition-props): add error for invalid props within…
Browse files Browse the repository at this point in the history
… "always" transitions
  • Loading branch information
rlaffers committed Aug 21, 2023
1 parent 7de8da8 commit 69ff424
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/rules/no-invalid-transition-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const getDocsUrl = require('../utils/getDocsUrl')
const { isObjectExpression, isArrayExpression } = require('../utils/predicates')
const getSettings = require('../utils/getSettings')

const validProperties = {
const validTransitionProperties = {
4: ['target', 'cond', 'actions', 'in', 'internal', 'description'],
5: ['target', 'guard', 'actions', 'reenter', 'description'],
}

function isValidTransitionProperty(property, version) {
return (
validProperties[version] &&
validProperties[version].includes(property.key.name)
validTransitionProperties[version] &&
validTransitionProperties[version].includes(property.key.name)
)
}

Expand All @@ -36,6 +36,9 @@ const globalEventTransitionArrayDeclaration =
const onDoneOrOnErrorTransitionDeclaration =
'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name=/^onDone$|^onError$/]'

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

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -115,6 +118,7 @@ module.exports = {
[globalEventTransitionArrayDeclaration]: checkTransitionArrayDeclaration,

[onDoneOrOnErrorTransitionDeclaration]: checkTransitionDeclaration,
[alwaysTransitionDeclaration]: checkTransitionArrayDeclaration,
}
},
}
106 changes: 106 additions & 0 deletions tests/lib/rules/no-invalid-transition-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,43 @@ const tests = {
})
`
),
// transitions within the "always" block
withVersion(
4,
`
createMachine({
states: {
deciding: {
always: [
{
cond: 'myGuard',
target: 'active',
actions: [],
}
]
}
}
})
`
),
withVersion(
5,
`
createMachine({
states: {
deciding: {
always: [
{
guard: 'myGuard',
target: 'active',
actions: [],
}
]
}
}
})
`
),
],
invalid: [
withVersion(4, {
Expand Down Expand Up @@ -300,6 +337,75 @@ const tests = {
{ messageId: 'invalidTransitionProperty', data: { propName: 'entry' } },
],
}),
// transitions within the "always" block
withVersion(4, {
code: `
createMachine({
states: {
deciding: {
always: [
{
unknown: '???',
cond: 'myGuard',
guard: '???',
invoke: '???',
target: 'active',
actions: [],
}
]
}
}
})
`,
errors: [
{
messageId: 'invalidTransitionProperty',
data: { propName: 'unknown' },
},
{
messageId: 'invalidTransitionProperty',
data: { propName: 'guard' },
},
{
messageId: 'invalidTransitionProperty',
data: { propName: 'invoke' },
},
],
}),
withVersion(5, {
code: `
createMachine({
states: {
deciding: {
always: [
{
unknown: '???',
cond: '???',
guard: 'myGuard',
invoke: '???',
target: 'active',
actions: [],
}
]
}
}
})
`,
errors: [
{
messageId: 'invalidTransitionProperty',
data: { propName: 'unknown' },
},
{
messageId: 'invalidTransitionProperty',
data: { propName: 'cond' },
},
{
messageId: 'invalidTransitionProperty',
data: { propName: 'invoke' },
},
],
}),
],
}

Expand Down

0 comments on commit 69ff424

Please sign in to comment.