Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Created December 10, 2018 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smashercosmo/6d07950ad7177e5dc4371b6cb5bb50e6 to your computer and use it in GitHub Desktop.
Save smashercosmo/6d07950ad7177e5dc4371b6cb5bb50e6 to your computer and use it in GitHub Desktop.
redux action eslint rule
function hasNotOnlyTypePayloadAndMetaProps(node) {
return node.properties.some(
prop =>
prop.key.name !== 'type' &&
prop.key.name !== 'payload' &&
prop.key.name !== 'meta',
)
}
function isReduxActionTypePropertyValue(value) {
return /^([A-Z]+_)+[A-Z]+$/.test(value)
}
function isReduxActionTypeProperty(property) {
return (
property.type === 'Property' &&
property.key.name === 'type' &&
((property.value.type === 'Identifier' &&
isReduxActionTypePropertyValue(property.value.name)) ||
(property.value.type === 'Literal' &&
isReduxActionTypePropertyValue(property.value.value)))
)
}
function isReduxAction(node) {
return (
node &&
Array.isArray(node.properties) &&
node.properties.length >= 1 &&
node.properties.some(isReduxActionTypeProperty)
)
}
module.exports = function reduxActionRule(context) {
return {
ObjectExpression(node) {
if (isReduxAction(node) && hasNotOnlyTypePayloadAndMetaProps(node)) {
context.report({
node,
message: 'Actions should only have "type", "payload" or "meta" props',
})
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment